├── .vs
└── DraftingDemo
│ ├── v15
│ ├── Server
│ │ └── sqlite3
│ │ │ ├── db.lock
│ │ │ ├── storage.ide
│ │ │ ├── storage.ide-shm
│ │ │ └── storage.ide-wal
│ └── .suo
│ └── v14
│ └── .suo
├── NestingLibPort
├── obj
│ └── Debug
│ │ ├── NestingLibPort.csproj.CopyComplete
│ │ ├── TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
│ │ ├── TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
│ │ ├── TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
│ │ ├── NestingLibPort.csproj.CoreCompileInputs.cache
│ │ ├── NestingLibPort.dll
│ │ ├── NestingLibPort.pdb
│ │ ├── DesignTimeResolveAssemblyReferences.cache
│ │ ├── DesignTimeResolveAssemblyReferencesInput.cache
│ │ ├── NestingLibPort.csprojAssemblyReference.cache
│ │ ├── NestingLibPort.csprojResolveAssemblyReference.cache
│ │ └── NestingLibPort.csproj.FileListAbsolute.txt
├── bin
│ └── Debug
│ │ ├── Clipper.dll
│ │ ├── Clipper.pdb
│ │ ├── NestingLibPort.dll
│ │ ├── NestingLibPort.pdb
│ │ ├── clipper_library.dll
│ │ └── clipper_library.pdb
├── ToDo.txt
├── Data
│ ├── SegmentRelation.cs
│ ├── Placement.cs
│ ├── Result.cs
│ ├── NfpPair.cs
│ ├── ParallelData.cs
│ ├── Vector.cs
│ ├── Bound.cs
│ ├── NfpKey.cs
│ ├── Segment.cs
│ └── NestPath.cs
├── Util
│ ├── PositionUtil.cs
│ ├── CollectionUtils.cs
│ ├── Coor
│ │ ├── ClipperCoor.cs
│ │ └── NestCoor.cs
│ ├── Config.cs
│ ├── NfpUtil.cs
│ ├── SvgUtil.cs
│ ├── CommonUtil.cs
│ └── PlacementWorker.cs
├── Properties
│ └── AssemblyInfo.cs
├── NestingLibPort.csproj
├── Algorithm
│ ├── Individual.cs
│ └── GeneticAlgorithm.cs
└── Nest.cs
├── NestingConsole
├── obj
│ └── Debug
│ │ ├── TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
│ │ ├── TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
│ │ ├── TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
│ │ ├── NestingConsole.exe
│ │ ├── NestingConsole.pdb
│ │ ├── DesignTimeResolveAssemblyReferencesInput.cache
│ │ ├── NestingConsole.csprojResolveAssemblyReference.cache
│ │ └── NestingConsole.csproj.FileListAbsolute.txt
├── bin
│ └── Debug
│ │ ├── NestingConsole.exe
│ │ ├── NestingConsole.pdb
│ │ ├── NestingLibPort.dll
│ │ ├── NestingLibPort.pdb
│ │ ├── clipper_library.dll
│ │ ├── clipper_library.pdb
│ │ ├── NestingConsole.vshost.exe
│ │ ├── NestingConsole.exe.config
│ │ ├── NestingConsole.vshost.exe.config
│ │ ├── NestingConsole.vshost.exe.manifest
│ │ ├── test.xml
│ │ └── output.svg
├── App.config
├── Properties
│ └── AssemblyInfo.cs
├── Program.cs
└── NestingConsole.csproj
├── clipper
├── obj
│ └── Debug
│ │ ├── clipper_library.csproj.CoreCompileInputs.cache
│ │ ├── clipper_library.dll
│ │ ├── clipper_library.pdb
│ │ ├── DesignTimeResolveAssemblyReferences.cache
│ │ ├── DesignTimeResolveAssemblyReferencesInput.cache
│ │ └── clipper_library.csproj.FileListAbsolute.txt
├── bin
│ └── Debug
│ │ ├── clipper_library.dll
│ │ └── clipper_library.pdb
├── Properties
│ └── AssemblyInfo.cs
└── clipper_library.csproj
├── packages
└── Newtonsoft.Json.11.0.2
│ ├── .signature.p7s
│ ├── Newtonsoft.Json.11.0.2.nupkg
│ ├── lib
│ ├── net20
│ │ └── Newtonsoft.Json.dll
│ ├── net35
│ │ └── Newtonsoft.Json.dll
│ ├── net40
│ │ └── Newtonsoft.Json.dll
│ ├── net45
│ │ └── Newtonsoft.Json.dll
│ ├── netstandard1.0
│ │ └── Newtonsoft.Json.dll
│ ├── netstandard1.3
│ │ └── Newtonsoft.Json.dll
│ ├── netstandard2.0
│ │ └── Newtonsoft.Json.dll
│ ├── portable-net45+win8+wp8+wpa81
│ │ └── Newtonsoft.Json.dll
│ └── portable-net40+sl5+win8+wp8+wpa81
│ │ └── Newtonsoft.Json.dll
│ └── LICENSE.md
├── LICEENSE
├── ReadMe.md
└── DraftingDemo.sln
/.vs/DraftingDemo/v15/Server/sqlite3/db.lock:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/NestingLibPort/obj/Debug/NestingLibPort.csproj.CopyComplete:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/NestingConsole/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/NestingConsole/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/NestingConsole/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/NestingLibPort/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/NestingLibPort/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/NestingLibPort/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.vs/DraftingDemo/v14/.suo:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/.vs/DraftingDemo/v14/.suo
--------------------------------------------------------------------------------
/.vs/DraftingDemo/v15/.suo:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/.vs/DraftingDemo/v15/.suo
--------------------------------------------------------------------------------
/clipper/obj/Debug/clipper_library.csproj.CoreCompileInputs.cache:
--------------------------------------------------------------------------------
1 | ab056e2cdc72e88f7713f2810d2bef2d152ee201
2 |
--------------------------------------------------------------------------------
/NestingLibPort/obj/Debug/NestingLibPort.csproj.CoreCompileInputs.cache:
--------------------------------------------------------------------------------
1 | 8f29a34684c408fed824432612d68d7c4d3b86c4
2 |
--------------------------------------------------------------------------------
/NestingLibPort/bin/Debug/Clipper.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingLibPort/bin/Debug/Clipper.dll
--------------------------------------------------------------------------------
/NestingLibPort/bin/Debug/Clipper.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingLibPort/bin/Debug/Clipper.pdb
--------------------------------------------------------------------------------
/clipper/bin/Debug/clipper_library.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/clipper/bin/Debug/clipper_library.dll
--------------------------------------------------------------------------------
/clipper/bin/Debug/clipper_library.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/clipper/bin/Debug/clipper_library.pdb
--------------------------------------------------------------------------------
/clipper/obj/Debug/clipper_library.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/clipper/obj/Debug/clipper_library.dll
--------------------------------------------------------------------------------
/clipper/obj/Debug/clipper_library.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/clipper/obj/Debug/clipper_library.pdb
--------------------------------------------------------------------------------
/NestingConsole/bin/Debug/NestingConsole.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingConsole/bin/Debug/NestingConsole.exe
--------------------------------------------------------------------------------
/NestingConsole/bin/Debug/NestingConsole.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingConsole/bin/Debug/NestingConsole.pdb
--------------------------------------------------------------------------------
/NestingConsole/bin/Debug/NestingLibPort.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingConsole/bin/Debug/NestingLibPort.dll
--------------------------------------------------------------------------------
/NestingConsole/bin/Debug/NestingLibPort.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingConsole/bin/Debug/NestingLibPort.pdb
--------------------------------------------------------------------------------
/NestingConsole/obj/Debug/NestingConsole.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingConsole/obj/Debug/NestingConsole.exe
--------------------------------------------------------------------------------
/NestingConsole/obj/Debug/NestingConsole.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingConsole/obj/Debug/NestingConsole.pdb
--------------------------------------------------------------------------------
/NestingLibPort/bin/Debug/NestingLibPort.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingLibPort/bin/Debug/NestingLibPort.dll
--------------------------------------------------------------------------------
/NestingLibPort/bin/Debug/NestingLibPort.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingLibPort/bin/Debug/NestingLibPort.pdb
--------------------------------------------------------------------------------
/NestingLibPort/obj/Debug/NestingLibPort.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingLibPort/obj/Debug/NestingLibPort.dll
--------------------------------------------------------------------------------
/NestingLibPort/obj/Debug/NestingLibPort.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingLibPort/obj/Debug/NestingLibPort.pdb
--------------------------------------------------------------------------------
/NestingConsole/bin/Debug/clipper_library.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingConsole/bin/Debug/clipper_library.dll
--------------------------------------------------------------------------------
/NestingConsole/bin/Debug/clipper_library.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingConsole/bin/Debug/clipper_library.pdb
--------------------------------------------------------------------------------
/NestingLibPort/bin/Debug/clipper_library.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingLibPort/bin/Debug/clipper_library.dll
--------------------------------------------------------------------------------
/NestingLibPort/bin/Debug/clipper_library.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingLibPort/bin/Debug/clipper_library.pdb
--------------------------------------------------------------------------------
/packages/Newtonsoft.Json.11.0.2/.signature.p7s:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/packages/Newtonsoft.Json.11.0.2/.signature.p7s
--------------------------------------------------------------------------------
/.vs/DraftingDemo/v15/Server/sqlite3/storage.ide:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/.vs/DraftingDemo/v15/Server/sqlite3/storage.ide
--------------------------------------------------------------------------------
/.vs/DraftingDemo/v15/Server/sqlite3/storage.ide-shm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/.vs/DraftingDemo/v15/Server/sqlite3/storage.ide-shm
--------------------------------------------------------------------------------
/.vs/DraftingDemo/v15/Server/sqlite3/storage.ide-wal:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/.vs/DraftingDemo/v15/Server/sqlite3/storage.ide-wal
--------------------------------------------------------------------------------
/NestingConsole/bin/Debug/NestingConsole.vshost.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingConsole/bin/Debug/NestingConsole.vshost.exe
--------------------------------------------------------------------------------
/clipper/obj/Debug/DesignTimeResolveAssemblyReferences.cache:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/clipper/obj/Debug/DesignTimeResolveAssemblyReferences.cache
--------------------------------------------------------------------------------
/packages/Newtonsoft.Json.11.0.2/Newtonsoft.Json.11.0.2.nupkg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/packages/Newtonsoft.Json.11.0.2/Newtonsoft.Json.11.0.2.nupkg
--------------------------------------------------------------------------------
/packages/Newtonsoft.Json.11.0.2/lib/net20/Newtonsoft.Json.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/packages/Newtonsoft.Json.11.0.2/lib/net20/Newtonsoft.Json.dll
--------------------------------------------------------------------------------
/packages/Newtonsoft.Json.11.0.2/lib/net35/Newtonsoft.Json.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/packages/Newtonsoft.Json.11.0.2/lib/net35/Newtonsoft.Json.dll
--------------------------------------------------------------------------------
/packages/Newtonsoft.Json.11.0.2/lib/net40/Newtonsoft.Json.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/packages/Newtonsoft.Json.11.0.2/lib/net40/Newtonsoft.Json.dll
--------------------------------------------------------------------------------
/packages/Newtonsoft.Json.11.0.2/lib/net45/Newtonsoft.Json.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/packages/Newtonsoft.Json.11.0.2/lib/net45/Newtonsoft.Json.dll
--------------------------------------------------------------------------------
/NestingLibPort/obj/Debug/DesignTimeResolveAssemblyReferences.cache:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingLibPort/obj/Debug/DesignTimeResolveAssemblyReferences.cache
--------------------------------------------------------------------------------
/clipper/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/clipper/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache
--------------------------------------------------------------------------------
/NestingConsole/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingConsole/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache
--------------------------------------------------------------------------------
/NestingLibPort/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingLibPort/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache
--------------------------------------------------------------------------------
/NestingLibPort/obj/Debug/NestingLibPort.csprojAssemblyReference.cache:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingLibPort/obj/Debug/NestingLibPort.csprojAssemblyReference.cache
--------------------------------------------------------------------------------
/packages/Newtonsoft.Json.11.0.2/lib/netstandard1.0/Newtonsoft.Json.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/packages/Newtonsoft.Json.11.0.2/lib/netstandard1.0/Newtonsoft.Json.dll
--------------------------------------------------------------------------------
/packages/Newtonsoft.Json.11.0.2/lib/netstandard1.3/Newtonsoft.Json.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/packages/Newtonsoft.Json.11.0.2/lib/netstandard1.3/Newtonsoft.Json.dll
--------------------------------------------------------------------------------
/packages/Newtonsoft.Json.11.0.2/lib/netstandard2.0/Newtonsoft.Json.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/packages/Newtonsoft.Json.11.0.2/lib/netstandard2.0/Newtonsoft.Json.dll
--------------------------------------------------------------------------------
/NestingConsole/obj/Debug/NestingConsole.csprojResolveAssemblyReference.cache:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingConsole/obj/Debug/NestingConsole.csprojResolveAssemblyReference.cache
--------------------------------------------------------------------------------
/NestingLibPort/obj/Debug/NestingLibPort.csprojResolveAssemblyReference.cache:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/NestingLibPort/obj/Debug/NestingLibPort.csprojResolveAssemblyReference.cache
--------------------------------------------------------------------------------
/NestingConsole/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/packages/Newtonsoft.Json.11.0.2/lib/portable-net45+win8+wp8+wpa81/Newtonsoft.Json.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/packages/Newtonsoft.Json.11.0.2/lib/portable-net45+win8+wp8+wpa81/Newtonsoft.Json.dll
--------------------------------------------------------------------------------
/packages/Newtonsoft.Json.11.0.2/lib/portable-net40+sl5+win8+wp8+wpa81/Newtonsoft.Json.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dharani811/NestingCShaprt/HEAD/packages/Newtonsoft.Json.11.0.2/lib/portable-net40+sl5+win8+wp8+wpa81/Newtonsoft.Json.dll
--------------------------------------------------------------------------------
/NestingConsole/bin/Debug/NestingConsole.exe.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/NestingConsole/bin/Debug/NestingConsole.vshost.exe.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/NestingLibPort/ToDo.txt:
--------------------------------------------------------------------------------
1 | 1.ClassName & Variable Name fixing
2 | 2.GeometryUtil.NoFitPolygon first two parameters should be readonly ( NestPath A, NestPath B)
3 | 3.GeometryUtil Method Signature public static Double polygonSlideDistance(final NestPath TA , final NestPath TB , Segment direction, boolean ignoreNegative)
4 | 4.GeometryUtil Method Signature public static boolean intersect(final NestPath CA, final NestPath CB)
5 | 5.GeometryUtil.Linerialze " list.AddToBack(arc); " was list.Add(arc).. caution needed here
6 | "
--------------------------------------------------------------------------------
/NestingConsole/bin/Debug/NestingConsole.vshost.exe.manifest:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/NestingLibPort/Data/SegmentRelation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace NestingLibPort.Data
8 | {
9 |
10 | public class SegmentRelation
11 | {
12 | public int type;
13 | public int A;
14 | public int B;
15 |
16 | public SegmentRelation(int type, int a, int b)
17 | {
18 | this.type = type;
19 | A = a;
20 | B = b;
21 | }
22 |
23 | public SegmentRelation()
24 | {
25 | }
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/NestingLibPort/Data/Placement.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace NestingLibPort.Data
8 | {
9 |
10 | public class Placement
11 | {
12 | public int bid;
13 | public Segment translate;
14 | public double rotate;
15 |
16 |
17 | public Placement(int bid, Segment translate, double rotate)
18 | {
19 | this.bid = bid;
20 | this.translate = translate;
21 | this.rotate = rotate;
22 | }
23 |
24 | public Placement()
25 | {
26 | }
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/NestingLibPort/Util/PositionUtil.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using NestingLibPort.Data;
7 |
8 |
9 | //package com.qunhe.util.nest.util;
10 |
11 | //import com.qunhe.util.nest.data.NestPath;
12 |
13 | //import java.util.List;
14 | namespace NestingLibPort.Util
15 | {
16 | public class PositionUtil
17 | {
18 |
19 | public static List positionTranslate4Path(double x, double y, List paths)
20 | {
21 | foreach (NestPath path in paths)
22 | {
23 | path.translate(x, y);
24 | y = path.getMaxY() + 10;
25 | }
26 | return paths;
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/NestingLibPort/Data/Result.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace NestingLibPort.Data
8 | {
9 |
10 | public class Result
11 | {
12 | public List> placements;
13 | public double fitness;
14 | public List paths;
15 | public double area;
16 |
17 | public Result(List> placements, double fitness, List paths, double area)
18 | {
19 | this.placements = placements;
20 | this.fitness = fitness;
21 | this.paths = paths;
22 | this.area = area;
23 | }
24 |
25 | public Result()
26 | {
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/NestingLibPort/Util/CollectionUtils.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace NestingLibPort.Util
5 | {
6 | public static class CollectionUtils
7 | {
8 |
9 | private static Random rng = new Random();
10 | public static void Swap(this IList list, int indexA, int indexB)
11 | {
12 | T tmp = list[indexA];
13 | list[indexA] = list[indexB];
14 | list[indexB] = tmp;
15 | }
16 |
17 |
18 | public static void Shuffle(this IList list)
19 | {
20 | int n = list.Count;
21 | while (n > 1)
22 | {
23 | n--;
24 | int k = rng.Next(n + 1);
25 | T value = list[k];
26 | list[k] = list[n];
27 | list[n] = value;
28 | }
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/NestingLibPort/Util/Coor/ClipperCoor.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace NestingLibPort.Util.Coor
8 | {
9 | public class ClipperCoor
10 | {
11 | long x;
12 | long y;
13 |
14 | public ClipperCoor()
15 | {
16 | }
17 |
18 | public ClipperCoor(long x, long y)
19 | {
20 | this.x = x;
21 | this.y = y;
22 | }
23 |
24 | public long getX()
25 | {
26 | return x;
27 | }
28 |
29 | public void setX(long x)
30 | {
31 | this.x = x;
32 | }
33 |
34 | public long getY()
35 | {
36 | return y;
37 | }
38 |
39 | public void setY(long y)
40 | {
41 | this.y = y;
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/NestingLibPort/Util/Coor/NestCoor.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace NestingLibPort.Util.Coor
8 | {
9 | public class NestCoor
10 | {
11 | public double x;
12 | public double y;
13 |
14 | public NestCoor()
15 | {
16 | }
17 |
18 | public NestCoor(double x, double y)
19 | {
20 | this.x = x;
21 | this.y = y;
22 | }
23 |
24 | public double getX()
25 | {
26 | return x;
27 | }
28 |
29 | public void setX(double x)
30 | {
31 | this.x = x;
32 | }
33 |
34 | public double getY()
35 | {
36 | return y;
37 | }
38 |
39 | public void setY(double y)
40 | {
41 | this.y = y;
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/NestingLibPort/Util/Config.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | //package com.qunhe.util.nest.util;
8 | namespace NestingLibPort.Util
9 | {
10 | public class Config
11 | {
12 | public static int CLIIPER_SCALE = 10000;
13 | public static double CURVE_TOLERANCE = 0.02;
14 | public double SPACING;
15 | public int POPULATION_SIZE;
16 | public int MUTATION_RATE;
17 | private bool CONCAVE;
18 | public bool USE_HOLE;
19 |
20 |
21 | public Config()
22 | {
23 | CLIIPER_SCALE = 10000;
24 | CURVE_TOLERANCE = 0.3;
25 | SPACING = 10;
26 | POPULATION_SIZE = 10;
27 | MUTATION_RATE = 10;
28 | CONCAVE = false;
29 | USE_HOLE = false;
30 | }
31 |
32 | public bool isCONCAVE()
33 | {
34 | return CONCAVE;
35 | }
36 |
37 | public bool isUSE_HOLE()
38 | {
39 | return USE_HOLE;
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/NestingLibPort/Data/NfpPair.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace NestingLibPort.Data
8 | {
9 |
10 | public class NfpPair
11 | {
12 | NestPath A;
13 | NestPath B;
14 | NfpKey key;
15 |
16 |
17 | public NfpPair(NestPath a, NestPath b, NfpKey key)
18 | {
19 | A = a;
20 | B = b;
21 | this.key = key;
22 | }
23 |
24 | public NestPath getA()
25 | {
26 | return A;
27 | }
28 |
29 | public void setA(NestPath a)
30 | {
31 | A = a;
32 | }
33 |
34 | public NestPath getB()
35 | {
36 | return B;
37 | }
38 |
39 | public void setB(NestPath b)
40 | {
41 | B = b;
42 | }
43 |
44 | public NfpKey getKey()
45 | {
46 | return key;
47 | }
48 |
49 | public void setKey(NfpKey key)
50 | {
51 | this.key = key;
52 | }
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/LICEENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Song Gao
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/NestingConsole/obj/Debug/NestingConsole.csproj.FileListAbsolute.txt:
--------------------------------------------------------------------------------
1 | D:\Code\Office\NestingPlugin\Nesting With UI\Nesting With UI\NestingConsole\bin\Debug\NestingConsole.exe.config
2 | D:\Code\Office\NestingPlugin\Nesting With UI\Nesting With UI\NestingConsole\bin\Debug\NestingConsole.exe
3 | D:\Code\Office\NestingPlugin\Nesting With UI\Nesting With UI\NestingConsole\bin\Debug\NestingConsole.pdb
4 | D:\Code\Office\NestingPlugin\Nesting With UI\Nesting With UI\NestingConsole\bin\Debug\NestingLibPort.dll
5 | D:\Code\Office\NestingPlugin\Nesting With UI\Nesting With UI\NestingConsole\bin\Debug\clipper_library.dll
6 | D:\Code\Office\NestingPlugin\Nesting With UI\Nesting With UI\NestingConsole\bin\Debug\NestingLibPort.pdb
7 | D:\Code\Office\NestingPlugin\Nesting With UI\Nesting With UI\NestingConsole\bin\Debug\clipper_library.pdb
8 | D:\Code\Office\NestingPlugin\Nesting With UI\Nesting With UI\NestingConsole\obj\Debug\NestingConsole.csprojResolveAssemblyReference.cache
9 | D:\Code\Office\NestingPlugin\Nesting With UI\Nesting With UI\NestingConsole\obj\Debug\NestingConsole.exe
10 | D:\Code\Office\NestingPlugin\Nesting With UI\Nesting With UI\NestingConsole\obj\Debug\NestingConsole.pdb
11 |
--------------------------------------------------------------------------------
/packages/Newtonsoft.Json.11.0.2/LICENSE.md:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2007 James Newton-King
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of
6 | this software and associated documentation files (the "Software"), to deal in
7 | the Software without restriction, including without limitation the rights to
8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 | the Software, and to permit persons to whom the Software is furnished to do so,
10 | subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 |
--------------------------------------------------------------------------------
/NestingLibPort/Data/ParallelData.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 |
8 | //package com.qunhe.util.nest.data;
9 |
10 | //import java.util.ArrayList;
11 | //import java.util.List;
12 | namespace NestingLibPort.Data
13 | {
14 | public class ParallelData
15 | {
16 | public NfpKey key;
17 | public List value;
18 |
19 | public ParallelData()
20 | {
21 | value = new List();
22 | }
23 |
24 | public ParallelData(NfpKey key, List value)
25 | {
26 | this.key = key;
27 | this.value = value;
28 | }
29 |
30 | public NfpKey getKey()
31 | {
32 | return key;
33 | }
34 |
35 | public void setKey(NfpKey key)
36 | {
37 | this.key = key;
38 | }
39 |
40 | public List getValue()
41 | {
42 | return value;
43 | }
44 |
45 | public void setValue(List value)
46 | {
47 | this.value = value;
48 | }
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/NestingLibPort/Data/Vector.cs:
--------------------------------------------------------------------------------
1 |
2 | using ClipperLib;
3 | using System;
4 | using System.Collections.Generic;
5 |
6 | namespace NestingLibPort.Data
7 | {
8 | using Paths = List>;
9 |
10 | public class Vector
11 | {
12 | public double x;
13 | public double y;
14 | public int id;
15 | public double rotation;
16 | public Paths nfp;
17 |
18 | public Vector(double x, double y, int id, double rotation)
19 | {
20 | this.x = x;
21 | this.y = y;
22 | this.id = id;
23 | this.rotation = rotation;
24 | this.nfp = new Paths();
25 | }
26 |
27 | public Vector(double x, double y, int id, double rotation, Paths nfp)
28 | {
29 | this.x = x;
30 | this.y = y;
31 | this.id = id;
32 | this.rotation = rotation;
33 | this.nfp = nfp;
34 | }
35 |
36 | public Vector()
37 | {
38 | nfp = new Paths();
39 | }
40 |
41 |
42 | public override String ToString()
43 | {
44 | return "x = " + x + " , y = " + y;
45 | }
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/ReadMe.md:
--------------------------------------------------------------------------------
1 | This a port of Nesting library written in Java at https://github.com/exacloud/Nest4J
2 |
3 | Not complete yet, but basics work.Working on it, will keep you posted.
4 |
5 | i/p is XML format
6 |
7 | o/p will be generated in SVG format
8 |
9 | sample input file is available in bin folder of NestingConsole project.
10 |
11 | Sample use case:
12 |
13 |
14 |
15 |
16 | //create bin
17 | NestPath bin = new NestPath();
18 | double binWidth =1000;
19 | double binHeight = 1000;
20 | bin.add(0, 0);
21 | bin.add(binWidth, 0);
22 | bin.add(binWidth, binHeight);
23 | bin.add(0, binHeight);
24 |
25 | //read polygons from xml file
26 | var nestPaths = SvgUtil.transferSvgIntoPolygons("test.xml");
27 |
28 | //default Config, you can customize(please look into Config class)
29 | Config config = new Config();
30 |
31 | //Init Nesting
32 | Nest nest = new Nest(bin, nestPaths, config, 2);
33 |
34 | //perform nesting
35 | List> appliedPlacement = nest.startNest();
36 |
37 | //convert to svg polygons for viewing in browser
38 | var svgPolygons = SvgUtil.svgGenerator(nestPaths, appliedPlacement, binWidth, binHeight);
39 |
40 | //save file onto disk
41 | SvgUtil.saveSvgFile(svgPolygons, "output.svg");
42 |
43 |
44 |
45 | UseFul Links:
46 | https://github.com/exacloud/Nest4J
47 |
48 | https://github.com/Jack000/Deepnest
49 |
50 | https://github.com/Jack000/SVGnest
51 |
--------------------------------------------------------------------------------
/NestingConsole/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("NestingConsole")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("HP Inc.")]
12 | [assembly: AssemblyProduct("NestingConsole")]
13 | [assembly: AssemblyCopyright("Copyright © HP Inc. 2018")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("a479f9c1-d348-450f-82f4-53806bbd2ea1")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/NestingLibPort/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("NestingLibPort")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("HP Inc.")]
12 | [assembly: AssemblyProduct("NestingLibPort")]
13 | [assembly: AssemblyCopyright("Copyright © HP Inc. 2018")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("b396f5bf-95f6-48dd-adce-2af4ffcb7350")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/clipper/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("clipper_library")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("Angus Johnson")]
12 | [assembly: AssemblyProduct("clipper_library")]
13 | [assembly: AssemblyCopyright("Copyright © Angus Johnson 2010-14")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("51a6bdca-bc4e-4b2c-ae69-36e2497204f2")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/NestingConsole/Program.cs:
--------------------------------------------------------------------------------
1 | using NestingLibPort;
2 | using NestingLibPort.Data;
3 | using NestingLibPort.Util;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Diagnostics;
7 | using System.Linq;
8 | using System.Text;
9 | using System.Threading.Tasks;
10 |
11 | namespace NestingConsole
12 | {
13 | class Program
14 | {
15 | static void Main(string[] args)
16 | {
17 | NestPath bin = new NestPath();
18 | double binWidth =1000;
19 | double binHeight = 1000;
20 | bin.add(0, 0);
21 | bin.add(binWidth, 0);
22 | bin.add(binWidth, binHeight);
23 | bin.add(0, binHeight);
24 | Console.WriteLine("Bin Size : Width = " + binWidth + " Height=" + binHeight);
25 | var nestPaths = SvgUtil.transferSvgIntoPolygons("test.xml");
26 | Console.WriteLine("Reading File = test.xml");
27 | Console.WriteLine("No of parts = " + nestPaths.Count);
28 | Config config = new Config();
29 | Console.WriteLine("Configuring Nest");
30 | Nest nest = new Nest(bin, nestPaths, config, 2);
31 | Console.WriteLine("Performing Nest");
32 | List> appliedPlacement = nest.startNest();
33 | Console.WriteLine("Nesting Completed");
34 | var svgPolygons = SvgUtil.svgGenerator(nestPaths, appliedPlacement, binWidth, binHeight);
35 | Console.WriteLine("Converted to SVG format");
36 | SvgUtil.saveSvgFile(svgPolygons, "output.svg");
37 | Console.WriteLine("Saved svg file..Opening File");
38 | Process.Start("output.svg");
39 | Console.ReadLine();
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/NestingLibPort/Data/Bound.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace NestingLibPort.Data
8 | {
9 |
10 | public class Bound
11 | {
12 | public double xmin;
13 | public double ymin;
14 | public double width;
15 | public double height;
16 |
17 | public Bound(double xmin, double ymin, double width, double height)
18 | {
19 | this.xmin = xmin;
20 | this.ymin = ymin;
21 | this.width = width;
22 | this.height = height;
23 | }
24 |
25 | public Bound()
26 | {
27 | }
28 |
29 | public double getXmin()
30 | {
31 | return xmin;
32 | }
33 |
34 | public void setXmin(double xmin)
35 | {
36 | this.xmin = xmin;
37 | }
38 |
39 | public double getYmin()
40 | {
41 | return ymin;
42 | }
43 |
44 | public void setYmin(double ymin)
45 | {
46 | this.ymin = ymin;
47 | }
48 |
49 | public double getWidth()
50 | {
51 | return width;
52 | }
53 |
54 | public void setWidth(double width)
55 | {
56 | this.width = width;
57 | }
58 |
59 | public double getHeight()
60 | {
61 | return height;
62 | }
63 |
64 | public void setHeight(double height)
65 | {
66 | this.height = height;
67 | }
68 |
69 |
70 | public override String ToString()
71 | {
72 | return "xmin = " + xmin + " , ymin = " + ymin + " , width = " + width + ", height = " + height;
73 | }
74 | }
75 |
76 | }
77 |
--------------------------------------------------------------------------------
/NestingLibPort/Data/NfpKey.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace NestingLibPort.Data
8 | {
9 | public class NfpKey
10 | {
11 |
12 | public int A;
13 | public int B;
14 | public bool inside;
15 | public double Arotation;
16 | public double Brotation;
17 |
18 | public NfpKey(int a, int b, bool inside, double arotation, double brotation)
19 | {
20 | A = a;
21 | B = b;
22 | this.inside = inside;
23 | Arotation = arotation;
24 | Brotation = brotation;
25 | }
26 |
27 | public NfpKey()
28 | {
29 | }
30 |
31 | public int getA()
32 | {
33 | return A;
34 | }
35 |
36 | public void setA(int a)
37 | {
38 | A = a;
39 | }
40 |
41 | public int getB()
42 | {
43 | return B;
44 | }
45 |
46 | public void setB(int b)
47 | {
48 | B = b;
49 | }
50 |
51 | public bool isInside()
52 | {
53 | return inside;
54 | }
55 |
56 | public void setInside(bool inside)
57 | {
58 | this.inside = inside;
59 | }
60 |
61 | public double getArotation()
62 | {
63 | return Arotation;
64 | }
65 |
66 | public void setArotation(double arotation)
67 | {
68 | Arotation = arotation;
69 | }
70 |
71 | public double getBrotation()
72 | {
73 | return Brotation;
74 | }
75 |
76 | public void setBrotation(double brotation)
77 | {
78 | Brotation = brotation;
79 | }
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/DraftingDemo.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 14
4 | VisualStudioVersion = 14.0.25420.1
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "clipper_library", "clipper\clipper_library.csproj", "{9B062971-A88E-4A3D-B3C9-12B78D15FA66}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NestingLibPort", "NestingLibPort\NestingLibPort.csproj", "{B396F5BF-95F6-48DD-ADCE-2AF4FFCB7350}"
9 | EndProject
10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NestingConsole", "NestingConsole\NestingConsole.csproj", "{A479F9C1-D348-450F-82F4-53806BBD2EA1}"
11 | EndProject
12 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{359D6B51-FC29-4E04-9758-BB121948F59B}"
13 | ProjectSection(SolutionItems) = preProject
14 | ReadMe.md = ReadMe.md
15 | EndProjectSection
16 | EndProject
17 | Global
18 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
19 | Debug|Any CPU = Debug|Any CPU
20 | Release|Any CPU = Release|Any CPU
21 | EndGlobalSection
22 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
23 | {9B062971-A88E-4A3D-B3C9-12B78D15FA66}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24 | {9B062971-A88E-4A3D-B3C9-12B78D15FA66}.Debug|Any CPU.Build.0 = Debug|Any CPU
25 | {9B062971-A88E-4A3D-B3C9-12B78D15FA66}.Release|Any CPU.ActiveCfg = Release|Any CPU
26 | {9B062971-A88E-4A3D-B3C9-12B78D15FA66}.Release|Any CPU.Build.0 = Release|Any CPU
27 | {B396F5BF-95F6-48DD-ADCE-2AF4FFCB7350}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
28 | {B396F5BF-95F6-48DD-ADCE-2AF4FFCB7350}.Debug|Any CPU.Build.0 = Debug|Any CPU
29 | {B396F5BF-95F6-48DD-ADCE-2AF4FFCB7350}.Release|Any CPU.ActiveCfg = Release|Any CPU
30 | {B396F5BF-95F6-48DD-ADCE-2AF4FFCB7350}.Release|Any CPU.Build.0 = Release|Any CPU
31 | {A479F9C1-D348-450F-82F4-53806BBD2EA1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
32 | {A479F9C1-D348-450F-82F4-53806BBD2EA1}.Debug|Any CPU.Build.0 = Debug|Any CPU
33 | {A479F9C1-D348-450F-82F4-53806BBD2EA1}.Release|Any CPU.ActiveCfg = Release|Any CPU
34 | {A479F9C1-D348-450F-82F4-53806BBD2EA1}.Release|Any CPU.Build.0 = Release|Any CPU
35 | EndGlobalSection
36 | GlobalSection(SolutionProperties) = preSolution
37 | HideSolutionNode = FALSE
38 | EndGlobalSection
39 | GlobalSection(ExtensibilityGlobals) = postSolution
40 | SolutionGuid = {69487260-F8A5-4F6C-A05B-767F050580DE}
41 | EndGlobalSection
42 | EndGlobal
43 |
--------------------------------------------------------------------------------
/clipper/clipper_library.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | AnyCPU
6 | 8.0.30703
7 | 2.0
8 | {9B062971-A88E-4A3D-B3C9-12B78D15FA66}
9 | Library
10 | Properties
11 | ClipperLib
12 | clipper_library
13 | v4.0
14 | 512
15 |
16 |
17 | true
18 | full
19 | false
20 | bin\Debug\
21 | DEBUG;TRACE
22 | prompt
23 | 4
24 |
25 |
26 | pdbonly
27 | true
28 | bin\Release\
29 | TRACE
30 | prompt
31 | 4
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
54 |
--------------------------------------------------------------------------------
/NestingLibPort/Data/Segment.cs:
--------------------------------------------------------------------------------
1 | using NestingLibPort.Util;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace NestingLibPort.Data
9 | {
10 |
11 | public class Segment
12 | {
13 | public double x;
14 | public double y;
15 |
16 | public bool marked = false;
17 | public Segment start;
18 | public Segment end;
19 |
20 |
21 |
22 | public override bool Equals(Object obj)
23 | {
24 | Segment s = (Segment)obj;
25 | if (x == s.x && y == s.y)
26 | {
27 | return true;
28 | }
29 | return false;
30 | }
31 |
32 | public Segment()
33 | {
34 | }
35 |
36 |
37 | public Segment(Segment srcSeg)
38 | {
39 | this.x = srcSeg.x;
40 | this.y = srcSeg.y;
41 | }
42 |
43 | public Segment(int x, int y)
44 | {
45 | this.x = (double)x;
46 | this.y = (double)y;
47 | }
48 |
49 | public Segment(double x, double y)
50 | {
51 | int Ix = (int)(x * Config.CLIIPER_SCALE);
52 | int Iy = (int)(y * Config.CLIIPER_SCALE);
53 |
54 | this.x = (double)Ix * 1.0 / Config.CLIIPER_SCALE;
55 | this.y = (double)Iy * 1.0 / Config.CLIIPER_SCALE;
56 | }
57 |
58 |
59 | public override String ToString()
60 | {
61 | return "x = " + x + ", y = " + y;
62 | }
63 |
64 | public bool isMarked()
65 | {
66 | return marked;
67 | }
68 |
69 | public void setMarked(bool marked)
70 | {
71 | this.marked = marked;
72 | }
73 |
74 | public Segment getStart()
75 | {
76 | return start;
77 | }
78 |
79 | public void setStart(Segment start)
80 | {
81 | this.start = start;
82 | }
83 |
84 | public Segment getEnd()
85 | {
86 | return end;
87 | }
88 |
89 | public void setEnd(Segment end)
90 | {
91 | this.end = end;
92 | }
93 |
94 | public double getX()
95 | {
96 | return x;
97 | }
98 |
99 | public void setX(double x)
100 | {
101 | int lx = (int)(x * Config.CLIIPER_SCALE);
102 | this.x = lx * 1.0 / Config.CLIIPER_SCALE;
103 | }
104 |
105 | public double getY()
106 | {
107 | return y;
108 | }
109 |
110 | public void setY(double y)
111 | {
112 | int ly = (int)(y * Config.CLIIPER_SCALE);
113 | this.y = ly * 1.0 / Config.CLIIPER_SCALE;
114 | }
115 | }
116 |
117 | }
118 |
--------------------------------------------------------------------------------
/clipper/obj/Debug/clipper_library.csproj.FileListAbsolute.txt:
--------------------------------------------------------------------------------
1 | E:\Nesting Plugin Backup\Nesting\Nesting Clipper(Angusj)\Nesting Scribber\clipper\bin\Debug\clipper_library.dll
2 | E:\Nesting Plugin Backup\Nesting\Nesting Clipper(Angusj)\Nesting Scribber\clipper\bin\Debug\clipper_library.pdb
3 | E:\Nesting Plugin Backup\Nesting\Nesting Clipper(Angusj)\Nesting Scribber\clipper\obj\Debug\clipper_library.csproj.CoreCompileInputs.cache
4 | E:\Nesting Plugin Backup\Nesting\Nesting Clipper(Angusj)\Nesting Scribber\clipper\obj\Debug\clipper_library.dll
5 | E:\Nesting Plugin Backup\Nesting\Nesting Clipper(Angusj)\Nesting Scribber\clipper\obj\Debug\clipper_library.pdb
6 | C:\Users\Macrowire\Desktop\nesting 4J\Nesting Scribber\clipper\bin\Debug\clipper_library.dll
7 | C:\Users\Macrowire\Desktop\nesting 4J\Nesting Scribber\clipper\bin\Debug\clipper_library.pdb
8 | C:\Users\Macrowire\Desktop\nesting 4J\Nesting Scribber\clipper\obj\Debug\clipper_library.csprojResolveAssemblyReference.cache
9 | C:\Users\Macrowire\Desktop\nesting 4J\Nesting Scribber\clipper\obj\Debug\clipper_library.dll
10 | C:\Users\Macrowire\Desktop\nesting 4J\Nesting Scribber\clipper\obj\Debug\clipper_library.pdb
11 | E:\Nesting Plugin Backup\Nesting\Nesting (27-October 06-47pm)\Nesting Scribber\clipper\bin\Debug\clipper_library.dll
12 | E:\Nesting Plugin Backup\Nesting\Nesting (27-October 06-47pm)\Nesting Scribber\clipper\bin\Debug\clipper_library.pdb
13 | E:\Nesting Plugin Backup\Nesting\Nesting (27-October 06-47pm)\Nesting Scribber\clipper\obj\Debug\clipper_library.csprojAssemblyReference.cache
14 | E:\Nesting Plugin Backup\Nesting\Nesting (27-October 06-47pm)\Nesting Scribber\clipper\obj\Debug\clipper_library.csproj.CoreCompileInputs.cache
15 | E:\Nesting Plugin Backup\Nesting\Nesting (27-October 06-47pm)\Nesting Scribber\clipper\obj\Debug\clipper_library.dll
16 | E:\Nesting Plugin Backup\Nesting\Nesting (27-October 06-47pm)\Nesting Scribber\clipper\obj\Debug\clipper_library.pdb
17 | E:\Nesting Plugin Backup\Nesting\Nesting (Output)\Initial Output & verified\Nesting Final UI\Nesting With UI\clipper\bin\Debug\clipper_library.dll
18 | E:\Nesting Plugin Backup\Nesting\Nesting (Output)\Initial Output & verified\Nesting Final UI\Nesting With UI\clipper\bin\Debug\clipper_library.pdb
19 | E:\Nesting Plugin Backup\Nesting\Nesting (Output)\Initial Output & verified\Nesting Final UI\Nesting With UI\clipper\obj\Debug\clipper_library.csproj.CoreCompileInputs.cache
20 | E:\Nesting Plugin Backup\Nesting\Nesting (Output)\Initial Output & verified\Nesting Final UI\Nesting With UI\clipper\obj\Debug\clipper_library.dll
21 | E:\Nesting Plugin Backup\Nesting\Nesting (Output)\Initial Output & verified\Nesting Final UI\Nesting With UI\clipper\obj\Debug\clipper_library.pdb
22 | D:\Code\Office\NestingPlugin\Nesting With UI\Nesting With UI\clipper\bin\Debug\clipper_library.dll
23 | D:\Code\Office\NestingPlugin\Nesting With UI\Nesting With UI\clipper\bin\Debug\clipper_library.pdb
24 | D:\Code\Office\NestingPlugin\Nesting With UI\Nesting With UI\clipper\obj\Debug\clipper_library.dll
25 | D:\Code\Office\NestingPlugin\Nesting With UI\Nesting With UI\clipper\obj\Debug\clipper_library.pdb
26 |
--------------------------------------------------------------------------------
/NestingConsole/NestingConsole.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {A479F9C1-D348-450F-82F4-53806BBD2EA1}
8 | Exe
9 | Properties
10 | NestingConsole
11 | NestingConsole
12 | v4.5
13 | 512
14 | true
15 |
16 |
17 |
18 | AnyCPU
19 | true
20 | full
21 | false
22 | bin\Debug\
23 | DEBUG;TRACE
24 | prompt
25 | 4
26 |
27 |
28 | AnyCPU
29 | pdbonly
30 | true
31 | bin\Release\
32 | TRACE
33 | prompt
34 | 4
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 | {B396F5BF-95F6-48DD-ADCE-2AF4FFCB7350}
56 | NestingLibPort
57 |
58 |
59 |
60 |
67 |
--------------------------------------------------------------------------------
/NestingLibPort/NestingLibPort.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {B396F5BF-95F6-48DD-ADCE-2AF4FFCB7350}
8 | Library
9 | Properties
10 | NestingLibPort
11 | NestingLibPort
12 | v4.5
13 | 512
14 |
15 |
16 |
17 | true
18 | full
19 | false
20 | bin\Debug\
21 | DEBUG;TRACE
22 | prompt
23 | 4
24 |
25 |
26 | pdbonly
27 | true
28 | bin\Release\
29 | TRACE
30 | prompt
31 | 4
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 | {9b062971-a88e-4a3d-b3c9-12b78d15fa66}
77 | clipper_library
78 |
79 |
80 |
81 |
88 |
--------------------------------------------------------------------------------
/NestingLibPort/Algorithm/Individual.cs:
--------------------------------------------------------------------------------
1 | using NestingLibPort.Data;
2 | using System;
3 | using System.Collections.Generic;
4 |
5 | //package com.qunhe.util.nest.algorithm;
6 |
7 | //import com.qunhe.util.nest.data.NestPath;
8 |
9 | //import java.util.ArrayList;
10 | //import java.util.List;
11 |
12 |
13 | namespace NestingLibPort.Algorithm
14 | {
15 | public class Individual:IComparable
16 | {
17 | public List placement;
18 | public List rotation;
19 | public double fitness;
20 |
21 |
22 | public Individual(Individual individual)
23 | {
24 | fitness = individual.fitness;
25 | placement = new List();
26 | rotation = new List();
27 | for (int i = 0; i < individual.placement.Count; i++)
28 | {
29 | NestPath cloneNestPath = new NestPath(individual.placement[i]);
30 | placement.Add(cloneNestPath);
31 | }
32 | for (int i = 0; i < individual.rotation.Count; i++)
33 | {
34 | double rotationAngle = individual.getRotation()[i];
35 | rotation.Add(rotationAngle);
36 | }
37 | }
38 |
39 |
40 | public Individual()
41 | {
42 | fitness = -1;
43 | placement = new List();
44 | rotation = new List();
45 | }
46 |
47 | public Individual(List placement, List rotation)
48 | {
49 | fitness = -1;
50 | this.placement = placement;
51 | this.rotation = rotation;
52 | }
53 |
54 | public int size()
55 | {
56 | return placement.Count;
57 | }
58 |
59 | public List getPlacement()
60 | {
61 | return placement;
62 | }
63 |
64 | public void setPlacement(List placement)
65 | {
66 | this.placement = placement;
67 | }
68 |
69 | public List getRotation()
70 | {
71 | return rotation;
72 | }
73 |
74 | public void setRotation(List rotation)
75 | {
76 | this.rotation = rotation;
77 | }
78 |
79 |
80 | public int CompareTo(Individual o)
81 | {
82 | if (fitness > o.fitness)
83 | {
84 | return 1;
85 | }
86 | else if (fitness == o.fitness)
87 | {
88 | return 0;
89 | }
90 | return -1;
91 | }
92 |
93 |
94 |
95 | public bool Equals(Object obj)
96 | {
97 | Individual individual = (Individual)obj;
98 | if (placement.Count != individual.size())
99 | {
100 | return false;
101 | }
102 | for (int i = 0; i < placement.Count; i++)
103 | {
104 | if (!placement[i].Equals(individual.getPlacement()[i]))
105 | {
106 | return false;
107 | }
108 | }
109 | if (rotation.Count != individual.getRotation().Count)
110 | {
111 | return false;
112 | }
113 | for (int i = 0; i < rotation.Count; i++)
114 | {
115 | if (rotation[i] != individual.getRotation()[i])
116 | {
117 | return false;
118 | }
119 | }
120 | return true;
121 | }
122 |
123 |
124 |
125 | public override String ToString()
126 | {
127 | String res = "";
128 | int count = 0;
129 | for (int i = 0; i < placement.Count; i++)
130 | {
131 | res += "NestPath " + count + "\n";
132 | count++;
133 | res += placement[i].ToString() + "\n";
134 | }
135 | res += "rotation \n";
136 | foreach (int r in rotation)
137 | {
138 | res += r + " ";
139 | }
140 | res += "\n";
141 |
142 | return res;
143 | }
144 |
145 | public double getFitness()
146 | {
147 | return fitness;
148 | }
149 |
150 | public void setFitness(double fitness)
151 | {
152 | this.fitness = fitness;
153 | }
154 |
155 |
156 | }
157 | }
158 |
--------------------------------------------------------------------------------
/NestingLibPort/Util/NfpUtil.cs:
--------------------------------------------------------------------------------
1 | using NestingLibPort.Data;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 |
9 | //package com.qunhe.util.nest.util;
10 |
11 | //import com.qunhe.util.nest.data.*;
12 | //import java.util.List;
13 | namespace NestingLibPort.Util
14 | {
15 | public class NfpUtil
16 | {
17 |
18 | /**
19 | * 获取一对多边形,并生成nfp
20 | * @param pair
21 | * @param config
22 | * @return
23 | */
24 | public static ParallelData nfpGenerator(NfpPair pair, Config config)
25 | {
26 | bool searchEdges = config.isCONCAVE();
27 | bool useHoles = config.isUSE_HOLE();
28 |
29 | NestPath A = GeometryUtil.rotatePolygon2Polygon(pair.getA(), pair.getKey().getArotation());
30 | NestPath B = GeometryUtil.rotatePolygon2Polygon(pair.getB(), pair.getKey().getBrotation());
31 |
32 | List nfp;
33 | if (pair.getKey().isInside())
34 | {
35 | if (GeometryUtil.isRectangle(A, 0.001))
36 | {
37 | nfp = GeometryUtil.noFitPolygonRectangle(A, B);
38 | if (nfp == null)
39 | {
40 |
41 | }
42 | }
43 | else
44 | {
45 | nfp = GeometryUtil.noFitPolygon(A, B, true, searchEdges);
46 | }
47 | if (nfp != null && nfp.Count > 0)
48 | {
49 | for (int i = 0; i < nfp.Count; i++)
50 | {
51 | if (GeometryUtil.polygonArea(nfp[i]) > 0)
52 | {
53 | nfp[i].reverse();
54 | }
55 | }
56 | }
57 | else
58 | {
59 | //Warning on null inner NFP
60 | }
61 | }
62 | else
63 | {
64 | int count = 0;
65 | if (searchEdges)
66 | {
67 |
68 | // NFP Generator TODO double scale contorl
69 | nfp = GeometryUtil.noFitPolygon(A, B, false, searchEdges);
70 | if (nfp == null)
71 | {
72 |
73 | }
74 | }
75 | else
76 | {
77 |
78 | nfp = GeometryUtil.minkowskiDifference(A, B);
79 | }
80 | // sanity check
81 | if (nfp == null || nfp.Count == 0)
82 | {
83 |
84 | return null;
85 | }
86 | for (int i = 0; i < nfp.Count; i++)
87 | {
88 | if (!searchEdges || i == 0)
89 | {
90 | if (Math.Abs(GeometryUtil.polygonArea(nfp[i])) < Math.Abs(GeometryUtil.polygonArea(A)))
91 | {
92 | nfp.RemoveAt(i);
93 |
94 | return null;
95 | }
96 | }
97 | }
98 | if (nfp.Count == 0)
99 | {
100 |
101 | return null;
102 | }
103 |
104 | for (int i = 0; i < nfp.Count; i++)
105 | {
106 | if (GeometryUtil.polygonArea(nfp[i]) > 0)
107 | {
108 | nfp[i].reverse();
109 | }
110 |
111 | if (i > 0)
112 | {
113 | if ((bool)GeometryUtil.pointInPolygon(nfp[i].get(0), nfp[0]))
114 | {
115 | if (GeometryUtil.polygonArea(nfp[i]) < 0)
116 | {
117 | nfp[i].reverse();
118 | }
119 | }
120 | }
121 | }
122 |
123 | if (useHoles && A.getChildren().Count > 0)
124 | {
125 | Bound Bbounds = GeometryUtil.getPolygonBounds(B);
126 | for (int i = 0; i < A.getChildren().Count; i++)
127 | {
128 | Bound Abounds = GeometryUtil.getPolygonBounds(A.getChildren()[i]);
129 |
130 | if (Abounds.width > Bbounds.width && Abounds.height > Bbounds.height)
131 | {
132 |
133 | List cnfp = GeometryUtil.noFitPolygon(A.getChildren()[i], B, true, searchEdges);
134 | // ensure all interior NFPs have the same winding direction
135 |
136 | if (cnfp != null && cnfp.Count > 0)
137 | {
138 |
139 | for (int j = 0; j < cnfp.Count; j++)
140 | {
141 | if (GeometryUtil.polygonArea(cnfp[j]) < 0)
142 | {
143 | cnfp[j].reverse();
144 | }
145 | nfp.Add(cnfp[j]);
146 | }
147 | }
148 |
149 | }
150 | }
151 | }
152 | }
153 | if (nfp == null)
154 | {
155 |
156 | }
157 | return new ParallelData(pair.getKey(), nfp);
158 | }
159 | }
160 |
161 | }
162 |
--------------------------------------------------------------------------------
/NestingLibPort/Util/SvgUtil.cs:
--------------------------------------------------------------------------------
1 | using NestingLibPort.Data;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.IO;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 | using System.Xml;
9 | using System.Xml.Linq;
10 |
11 |
12 | //package com.qunhe.util.nest.util;
13 |
14 | //import com.qunhe.util.nest.data.NestPath;
15 | //import com.qunhe.util.nest.data.Placement;
16 | //import com.qunhe.util.nest.data.Segment;
17 |
18 | //import java.io.File;
19 | //import java.util.ArrayList;
20 | //import java.util.List;
21 | namespace NestingLibPort.Util
22 | {
23 | public static class SvgUtil
24 | {
25 | public static void saveSvgFile(List strings,string path)
26 | {
27 | StreamWriter f=null;
28 | if (!File.Exists(path) )
29 |
30 | {
31 | f = File.CreateText(path);
32 | }
33 | else
34 | {
35 | File.Delete(path);
36 | f = File.CreateText(path);
37 |
38 | }
39 |
40 | f.Write("\n" +
41 | "\n" +
42 | "\n" +
44 | " \n" +
45 | "");
51 | f.Close();
52 | }
53 | public static List svgGenerator(List list, List> applied, double binwidth, double binHeight)
54 | {
55 | List strings = new List();
56 | int x = 10;
57 | int y = 0;
58 | foreach (List binlist in applied) {
59 | String s = " " + "\n";
60 | s += " \n";
61 | foreach (Placement placement in binlist) {
62 | int bid = placement.bid;
63 | NestPath nestPath = getNestPathByBid(bid, list);
64 | double ox = placement.translate.x;
65 | double oy = placement.translate.y;
66 | double rotate = placement.rotate;
67 | s += " \n";
68 | s += "" + " \n";
79 | s += " \n";
80 | }
81 | s += " \n";
82 | y +=(int) (binHeight + 50);
83 | strings.Add(s);
84 | }
85 | return strings;
86 | }
87 |
88 | public static NestPath getNestPathByBid(int bid, List list)
89 | {
90 | foreach (NestPath nestPath in list)
91 | {
92 | if (nestPath.bid == bid)
93 | {
94 | return nestPath;
95 | }
96 | }
97 | return null;
98 | }
99 |
100 |
101 | public static List transferSvgIntoPolygons(string xmlFilePath)
102 | {
103 | List nestPaths = new List();
104 |
105 | XDocument document = XDocument.Load(xmlFilePath);
106 | List elementList = document.Root.DescendantNodes().OfType().ToList();
107 | int count = 0;
108 | foreach (XElement element in elementList) {
109 | count++;
110 | if ("polygon"==(element.Name)) {
111 | String datalist = element.Attributes((XName)"points").ToList()[0].Value.ToString();
112 | NestPath polygon = new NestPath();
113 | foreach (String s in datalist.Split(' ')) {
114 | var temp = s.Trim();
115 | if (temp.IndexOf(",") == -1) {
116 | continue;
117 | }
118 | String[] value = s.Split(',');
119 | double x = Double.Parse(value[0]);
120 | double y = Double.Parse(value[1]);
121 | polygon.add(x, y);
122 | }
123 | polygon.bid = count;
124 | polygon.setRotation(4);
125 | nestPaths.Add(polygon);
126 | } else if ("rect"==element.Name) {
127 | double width = Double.Parse(element.Attributes((XName)"width").ToList()[0].Value.ToString());
128 | double height = Double.Parse(element.Attributes((XName)"height").ToList()[0].Value.ToString());
129 | double x = Double.Parse(element.Attributes((XName)"x").ToList()[0].Value.ToString());
130 | double y = Double.Parse(element.Attributes((XName)"y").ToList()[0].Value.ToString());
131 | NestPath rect = new NestPath();
132 | rect.add(x, y);
133 | rect.add(x + width, y);
134 | rect.add(x + width, y + height);
135 | rect.add(x, y + height);
136 | rect.bid = count;
137 | rect.setRotation(4);
138 | nestPaths.Add(rect);
139 | }
140 | }
141 | return nestPaths;
142 | }
143 |
144 |
145 |
146 | }
147 | }
148 |
--------------------------------------------------------------------------------
/NestingLibPort/obj/Debug/NestingLibPort.csproj.FileListAbsolute.txt:
--------------------------------------------------------------------------------
1 | D:\Code\Office\NestingPlugin\Nesting Scribber\NestingLibPort\bin\Debug\NestingLibPort.dll
2 | D:\Code\Office\NestingPlugin\Nesting Scribber\NestingLibPort\bin\Debug\NestingLibPort.pdb
3 | D:\Code\Office\NestingPlugin\Nesting Scribber\NestingLibPort\bin\Debug\Clipper.dll
4 | D:\Code\Office\NestingPlugin\Nesting Scribber\NestingLibPort\bin\Debug\Clipper.pdb
5 | D:\Code\Office\NestingPlugin\Nesting Scribber\NestingLibPort\obj\Debug\NestingLibPort.csprojResolveAssemblyReference.cache
6 | D:\Code\Office\NestingPlugin\Nesting Scribber\NestingLibPort\obj\Debug\NestingLibPort.dll
7 | D:\Code\Office\NestingPlugin\Nesting Scribber\NestingLibPort\obj\Debug\NestingLibPort.pdb
8 | C:\Users\Admin\Downloads\12.58\Nesting Scribber\NestingLibPort\bin\Debug\NestingLibPort.dll
9 | C:\Users\Admin\Downloads\12.58\Nesting Scribber\NestingLibPort\bin\Debug\NestingLibPort.pdb
10 | C:\Users\Admin\Downloads\12.58\Nesting Scribber\NestingLibPort\bin\Debug\Clipper.dll
11 | C:\Users\Admin\Downloads\12.58\Nesting Scribber\NestingLibPort\bin\Debug\Clipper.pdb
12 | C:\Users\Admin\Downloads\12.58\Nesting Scribber\NestingLibPort\obj\Debug\NestingLibPort.csprojAssemblyReference.cache
13 | C:\Users\Admin\Downloads\12.58\Nesting Scribber\NestingLibPort\obj\Debug\NestingLibPort.csproj.CoreCompileInputs.cache
14 | C:\Users\Admin\Downloads\12.58\Nesting Scribber\NestingLibPort\obj\Debug\NestingLibPort.csproj.CopyComplete
15 | C:\Users\Admin\Downloads\12.58\Nesting Scribber\NestingLibPort\obj\Debug\NestingLibPort.dll
16 | C:\Users\Admin\Downloads\12.58\Nesting Scribber\NestingLibPort\obj\Debug\NestingLibPort.pdb
17 | E:\Nesting Plugin Backup\Nesting\Nesting Clipper(Angusj)\Nesting Scribber\NestingLibPort\bin\Debug\NestingLibPort.dll
18 | E:\Nesting Plugin Backup\Nesting\Nesting Clipper(Angusj)\Nesting Scribber\NestingLibPort\bin\Debug\NestingLibPort.pdb
19 | E:\Nesting Plugin Backup\Nesting\Nesting Clipper(Angusj)\Nesting Scribber\NestingLibPort\bin\Debug\clipper_library.dll
20 | E:\Nesting Plugin Backup\Nesting\Nesting Clipper(Angusj)\Nesting Scribber\NestingLibPort\bin\Debug\clipper_library.pdb
21 | E:\Nesting Plugin Backup\Nesting\Nesting Clipper(Angusj)\Nesting Scribber\NestingLibPort\obj\Debug\NestingLibPort.csprojAssemblyReference.cache
22 | E:\Nesting Plugin Backup\Nesting\Nesting Clipper(Angusj)\Nesting Scribber\NestingLibPort\obj\Debug\NestingLibPort.csproj.CoreCompileInputs.cache
23 | E:\Nesting Plugin Backup\Nesting\Nesting Clipper(Angusj)\Nesting Scribber\NestingLibPort\obj\Debug\NestingLibPort.csproj.CopyComplete
24 | E:\Nesting Plugin Backup\Nesting\Nesting Clipper(Angusj)\Nesting Scribber\NestingLibPort\obj\Debug\NestingLibPort.dll
25 | E:\Nesting Plugin Backup\Nesting\Nesting Clipper(Angusj)\Nesting Scribber\NestingLibPort\obj\Debug\NestingLibPort.pdb
26 | C:\Users\Macrowire\Desktop\nesting 4J\Nesting Scribber\NestingLibPort\bin\Debug\NestingLibPort.dll
27 | C:\Users\Macrowire\Desktop\nesting 4J\Nesting Scribber\NestingLibPort\bin\Debug\NestingLibPort.pdb
28 | C:\Users\Macrowire\Desktop\nesting 4J\Nesting Scribber\NestingLibPort\bin\Debug\clipper_library.dll
29 | C:\Users\Macrowire\Desktop\nesting 4J\Nesting Scribber\NestingLibPort\bin\Debug\clipper_library.pdb
30 | C:\Users\Macrowire\Desktop\nesting 4J\Nesting Scribber\NestingLibPort\obj\Debug\NestingLibPort.csprojResolveAssemblyReference.cache
31 | C:\Users\Macrowire\Desktop\nesting 4J\Nesting Scribber\NestingLibPort\obj\Debug\NestingLibPort.dll
32 | C:\Users\Macrowire\Desktop\nesting 4J\Nesting Scribber\NestingLibPort\obj\Debug\NestingLibPort.pdb
33 | E:\Nesting Plugin Backup\Nesting\Nesting (27-October 06-47pm)\Nesting Scribber\NestingLibPort\bin\Debug\NestingLibPort.dll
34 | E:\Nesting Plugin Backup\Nesting\Nesting (27-October 06-47pm)\Nesting Scribber\NestingLibPort\bin\Debug\NestingLibPort.pdb
35 | E:\Nesting Plugin Backup\Nesting\Nesting (27-October 06-47pm)\Nesting Scribber\NestingLibPort\bin\Debug\clipper_library.dll
36 | E:\Nesting Plugin Backup\Nesting\Nesting (27-October 06-47pm)\Nesting Scribber\NestingLibPort\bin\Debug\clipper_library.pdb
37 | E:\Nesting Plugin Backup\Nesting\Nesting (27-October 06-47pm)\Nesting Scribber\NestingLibPort\obj\Debug\NestingLibPort.csprojAssemblyReference.cache
38 | E:\Nesting Plugin Backup\Nesting\Nesting (27-October 06-47pm)\Nesting Scribber\NestingLibPort\obj\Debug\NestingLibPort.csproj.CoreCompileInputs.cache
39 | E:\Nesting Plugin Backup\Nesting\Nesting (27-October 06-47pm)\Nesting Scribber\NestingLibPort\obj\Debug\NestingLibPort.csproj.CopyComplete
40 | E:\Nesting Plugin Backup\Nesting\Nesting (27-October 06-47pm)\Nesting Scribber\NestingLibPort\obj\Debug\NestingLibPort.dll
41 | E:\Nesting Plugin Backup\Nesting\Nesting (27-October 06-47pm)\Nesting Scribber\NestingLibPort\obj\Debug\NestingLibPort.pdb
42 | E:\Nesting Plugin Backup\Nesting\Nesting (Output)\Initial Output & verified\Nesting Final UI\Nesting With UI\NestingLibPort\bin\Debug\NestingLibPort.dll
43 | E:\Nesting Plugin Backup\Nesting\Nesting (Output)\Initial Output & verified\Nesting Final UI\Nesting With UI\NestingLibPort\bin\Debug\NestingLibPort.pdb
44 | E:\Nesting Plugin Backup\Nesting\Nesting (Output)\Initial Output & verified\Nesting Final UI\Nesting With UI\NestingLibPort\bin\Debug\clipper_library.dll
45 | E:\Nesting Plugin Backup\Nesting\Nesting (Output)\Initial Output & verified\Nesting Final UI\Nesting With UI\NestingLibPort\bin\Debug\clipper_library.pdb
46 | E:\Nesting Plugin Backup\Nesting\Nesting (Output)\Initial Output & verified\Nesting Final UI\Nesting With UI\NestingLibPort\obj\Debug\NestingLibPort.csprojAssemblyReference.cache
47 | E:\Nesting Plugin Backup\Nesting\Nesting (Output)\Initial Output & verified\Nesting Final UI\Nesting With UI\NestingLibPort\obj\Debug\NestingLibPort.csproj.CoreCompileInputs.cache
48 | E:\Nesting Plugin Backup\Nesting\Nesting (Output)\Initial Output & verified\Nesting Final UI\Nesting With UI\NestingLibPort\obj\Debug\NestingLibPort.csproj.CopyComplete
49 | E:\Nesting Plugin Backup\Nesting\Nesting (Output)\Initial Output & verified\Nesting Final UI\Nesting With UI\NestingLibPort\obj\Debug\NestingLibPort.dll
50 | E:\Nesting Plugin Backup\Nesting\Nesting (Output)\Initial Output & verified\Nesting Final UI\Nesting With UI\NestingLibPort\obj\Debug\NestingLibPort.pdb
51 | D:\Code\Office\NestingPlugin\Nesting With UI\Nesting With UI\NestingLibPort\bin\Debug\NestingLibPort.dll
52 | D:\Code\Office\NestingPlugin\Nesting With UI\Nesting With UI\NestingLibPort\bin\Debug\NestingLibPort.pdb
53 | D:\Code\Office\NestingPlugin\Nesting With UI\Nesting With UI\NestingLibPort\bin\Debug\clipper_library.dll
54 | D:\Code\Office\NestingPlugin\Nesting With UI\Nesting With UI\NestingLibPort\bin\Debug\clipper_library.pdb
55 | D:\Code\Office\NestingPlugin\Nesting With UI\Nesting With UI\NestingLibPort\obj\Debug\NestingLibPort.csprojResolveAssemblyReference.cache
56 | D:\Code\Office\NestingPlugin\Nesting With UI\Nesting With UI\NestingLibPort\obj\Debug\NestingLibPort.dll
57 | D:\Code\Office\NestingPlugin\Nesting With UI\Nesting With UI\NestingLibPort\obj\Debug\NestingLibPort.pdb
58 |
--------------------------------------------------------------------------------
/NestingLibPort/Util/CommonUtil.cs:
--------------------------------------------------------------------------------
1 | using NestingLibPort.Data;
2 | using NestingLibPort.Util.Coor;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 | using ClipperLib;
7 |
8 |
9 | namespace NestingLibPort.Util
10 | {
11 | using Path = List;
12 | using Paths = List>;
13 |
14 | public class CommonUtil
15 | {
16 |
17 |
18 | public static NestPath Path2NestPath(Path path)
19 | {
20 | NestPath nestPath = new NestPath();
21 | for (int i = 0; i < path.Count; i++)
22 | {
23 | IntPoint lp = path[i];
24 | NestCoor coor = CommonUtil.toNestCoor(lp.X, lp.Y);
25 | nestPath.add(new Segment(coor.getX(), coor.getY()));
26 | }
27 | return nestPath;
28 | }
29 |
30 | public static Path NestPath2Path(NestPath nestPath)
31 | {
32 | Path path = new Path();
33 | foreach (Segment s in nestPath.getSegments())
34 | {
35 | ClipperCoor coor = CommonUtil.toClipperCoor(s.getX(), s.getY());
36 | var lp = new IntPoint(coor.getX(), coor.getY());
37 | path.Add(lp);
38 | }
39 | return path;
40 | }
41 |
42 | /**
43 | * 坐标转换
44 | * @param x
45 | * @param y
46 | * @return
47 | */
48 | public static ClipperCoor toClipperCoor(double x, double y)
49 | {
50 | return new ClipperCoor((long)(x * Config.CLIIPER_SCALE), (long)(y * Config.CLIIPER_SCALE));
51 | }
52 |
53 | /**
54 | * 坐标转换
55 | * @param x
56 | * @param y
57 | * @return
58 | */
59 | public static NestCoor toNestCoor(long x, long y)
60 | {
61 | return new NestCoor(((double)x / Config.CLIIPER_SCALE), ((double)y / Config.CLIIPER_SCALE));
62 | }
63 |
64 |
65 | /**
66 | * 为Clipper下的Path添加点
67 | * @param x
68 | * @param y
69 | * @param path
70 | */
71 | private static void addPoint(long x, long y, Path path)
72 | {
73 | IntPoint ip = new IntPoint(x, y);
74 | path.Add(ip);
75 | }
76 |
77 |
78 | /**
79 | * binPath是作为底板的NestPath , polys则为板件的Path列表
80 | * 这个方法是为了将binPath和polys在不改变自身形状,角度的情况下放置在一个坐标系内,保证两两之间不交叉
81 | * @param binPath
82 | * @param polys
83 | */
84 | public static void ChangePosition(NestPath binPath, List polys)
85 | {
86 |
87 | }
88 |
89 | /**
90 | * 将NestPath列表转换成父子关系的树
91 | * @param list
92 | * @param idstart
93 | * @return
94 | */
95 | public static int toTree(List list, int idstart)
96 | {
97 | List parents = new List();
98 | int id = idstart;
99 | /**
100 | * 找出所有的内回环
101 | */
102 | for (int i = 0; i < list.Count; i++)
103 | {
104 | NestPath p = list[i];
105 | bool isChild = false;
106 | for (int j = 0; j < list.Count; j++)
107 | {
108 | if (j == i)
109 | {
110 | continue;
111 | }
112 |
113 | if (GeometryUtil.pointInPolygon(p.getSegments()[0], list[j]) == true)
114 | {
115 | list[j].getChildren().Add(p);
116 | p.setParent(list[j]);
117 | isChild = true;
118 | break;
119 | }
120 | }
121 | if (!isChild)
122 | {
123 | parents.Add(p);
124 | }
125 | }
126 | /**
127 | * 将内环从list列表中去除
128 | */
129 | for (int i = 0; i < list.Count; i++)
130 | {
131 | if (parents.IndexOf(list[i]) < 0)
132 | {
133 | list.RemoveAt(i);
134 | i--;
135 | }
136 | }
137 |
138 | for (int i = 0; i < parents.Count; i++)
139 | {
140 | parents[i].setId(id);
141 | id++;
142 | }
143 |
144 | for (int i = 0; i < parents.Count; i++)
145 | {
146 | if (parents[i].getChildren().Count > 0)
147 | {
148 | id = toTree(parents[i].getChildren(), id);
149 | }
150 | }
151 | return id;
152 | }
153 |
154 | public static NestPath clipperToNestPath(Path polygon)
155 | {
156 |
157 | NestPath normal = new NestPath();
158 | for (int i = 0; i < polygon.Count; i++)
159 | {
160 | NestCoor nestCoor = toNestCoor(polygon[i].X, polygon[i].Y);
161 | normal.add(new Segment(nestCoor.getX(), nestCoor.getY()));
162 | }
163 | return normal;
164 | }
165 |
166 | public static void offsetTree(List t, double offset)
167 | {
168 |
169 | for (int i = 0; i < t.Count; i++)
170 | {
171 | List offsetPaths = polygonOffset(t[i], offset);
172 | if (offsetPaths.Count == 1)
173 | {
174 | t[i].clear();
175 | NestPath from = offsetPaths[0];
176 |
177 | foreach (Segment s in from.getSegments())
178 | {
179 | t[i].add(s);
180 | }
181 | }
182 | if (t[i].getChildren().Count > 0)
183 | {
184 |
185 | offsetTree(t[i].getChildren(), -offset);
186 | }
187 | }
188 | }
189 |
190 | public static List polygonOffset(NestPath polygon, double offset)
191 | {
192 | List result = new List();
193 | if (offset == 0 || GeometryUtil.almostEqual(offset, 0))
194 | {
195 | /**
196 | * return EmptyResult
197 | */
198 | return result;
199 | }
200 | Path p = new Path();
201 | foreach (Segment s in polygon.getSegments())
202 | {
203 | ClipperCoor cc = toClipperCoor(s.getX(), s.getY());
204 | p.Add(new IntPoint(cc.getX(), cc.getY()));
205 | }
206 |
207 | int miterLimit = 2;
208 | ClipperOffset co = new ClipperOffset(miterLimit, Config.CURVE_TOLERANCE * Config.CLIIPER_SCALE);
209 | co.AddPath(p, JoinType.jtRound, EndType.etClosedPolygon);
210 |
211 | Paths newpaths = new Paths();
212 | co.Execute(ref newpaths, offset * Config.CLIIPER_SCALE);
213 |
214 |
215 | /**
216 | * 这里的length是1的话就是我们想要的
217 | */
218 | for (int i = 0; i < newpaths.Count; i++)
219 | {
220 | result.Add(CommonUtil.clipperToNestPath(newpaths[i]));
221 | }
222 |
223 | if (offset > 0)
224 | {
225 | NestPath from = result[0];
226 | if (GeometryUtil.polygonArea(from) > 0)
227 | {
228 | from.reverse();
229 | }
230 | from.add(from.get(0)); from.getSegments().RemoveAt(0);
231 | }
232 |
233 |
234 | return result;
235 | }
236 |
237 |
238 | /**
239 | * 对应于JS项目中的getParts
240 | */
241 | public static List BuildTree(List parts, double curve_tolerance)
242 | {
243 | List polygons = new List();
244 | for (int i = 0; i < parts.Count; i++)
245 | {
246 | NestPath cleanPoly = NestPath.cleanNestPath(parts[i]);
247 | cleanPoly.bid = parts[i].bid;
248 | if (cleanPoly.size() > 2 && Math.Abs(GeometryUtil.polygonArea(cleanPoly)) > curve_tolerance * curve_tolerance)
249 | {
250 | cleanPoly.setSource(i);
251 |
252 | polygons.Add(cleanPoly);
253 | }
254 | }
255 |
256 | CommonUtil.toTree(polygons, 0);
257 | return polygons;
258 | }
259 | }
260 | }
261 |
--------------------------------------------------------------------------------
/NestingLibPort/Algorithm/GeneticAlgorithm.cs:
--------------------------------------------------------------------------------
1 | using NestingLibPort.Data;
2 | using NestingLibPort.Util;
3 | using System;
4 | using System.Collections.Generic;
5 |
6 |
7 | namespace NestingLibPort.Algorithm
8 | {
9 | public class GeneticAlgorithm
10 | {
11 | public List adam;
12 | public NestPath bin;
13 | public Bound binBounds;
14 | public List angles;
15 | public List population;
16 | public Config config;
17 |
18 | public GeneticAlgorithm(List adam, NestPath bin, Config config)
19 | {
20 | this.adam = adam;
21 | this.bin = bin;
22 | this.config = config;
23 | this.binBounds = GeometryUtil.getPolygonBounds(bin);
24 | population = new List();
25 | init();
26 | }
27 |
28 |
29 |
30 | public void generation()
31 | {
32 | List newpopulation = new List();
33 | population.Sort();
34 |
35 | newpopulation.Add(population[0]);
36 | while (newpopulation.Count < config.POPULATION_SIZE)
37 | {
38 | Individual male = randomWeightedIndividual(null);
39 | Individual female = randomWeightedIndividual(male);
40 | List children = mate(male, female);
41 | newpopulation.Add(mutate(children[0]));
42 | if (newpopulation.Count < population.Count)
43 | {
44 | newpopulation.Add(mutate(children[1]));
45 | }
46 | }
47 | population = newpopulation;
48 | }
49 |
50 | public List mate(Individual male, Individual female)
51 | {
52 | List children = new List();
53 |
54 | long cutpoint =(long) Math.Round(Math.Min(Math.Max(new Random().NextDouble(), 0.1), 0.9) * (male.placement.Count - 1));
55 |
56 | List gene1 = new List();
57 | List rot1 = new List();
58 | List gene2 = new List();
59 | List rot2 = new List();
60 |
61 | for (int i = 0; i < cutpoint; i++)
62 | {
63 | gene1.Add(new NestPath(male.placement[i]));
64 | rot1.Add(male.getRotation()[i]);
65 | gene2.Add(new NestPath(female.placement[i]));
66 | rot2.Add(female.getRotation()[i]);
67 | }
68 |
69 | for (int i = 0; i < female.placement.Count; i++)
70 | {
71 | if (!contains(gene1, female.placement[i].getId()))
72 | {
73 | gene1.Add(female.placement[i]);
74 | rot1.Add(female.rotation[i]);
75 | }
76 | }
77 |
78 | for (int i = 0; i < male.placement.Count; i++)
79 | {
80 | if (!contains(gene2, male.placement[i].getId()))
81 | {
82 | gene2.Add(male.placement[i]);
83 | rot2.Add(male.rotation[i]);
84 | }
85 | }
86 | Individual individual1 = new Individual(gene1, rot1);
87 | Individual individual2 = new Individual(gene2, rot2);
88 |
89 | checkAndUpdate(individual1); checkAndUpdate(individual2);
90 |
91 |
92 | children.Add(individual1); children.Add(individual2);
93 | return children;
94 | }
95 |
96 |
97 | private bool contains(List gene, int id)
98 | {
99 | for (int i = 0; i < gene.Count; i++)
100 | {
101 | if (gene[i].getId() == id)
102 | {
103 | return true;
104 | }
105 | }
106 | return false;
107 | }
108 |
109 | private Individual randomWeightedIndividual(Individual exclude)
110 | {
111 | List pop = new List();
112 | for (int i = 0; i < population.Count; i++)
113 | {
114 | Individual individual = population[i];
115 | Individual clone = new Individual(individual);
116 | pop.Add(clone);
117 | }
118 | if (exclude != null)
119 | {
120 | int index = pop.IndexOf(exclude);
121 | if (index >= 0)
122 | {
123 | pop.RemoveAt(index);
124 | }
125 | }
126 | double rand = new Random().NextDouble();
127 | double lower = 0;
128 | double weight = 1 / pop.Count;
129 | double upper = weight;
130 |
131 | for (int i = 0; i < pop.Count; i++)
132 | {
133 | if (rand > lower && rand < upper)
134 | {
135 | return pop[i];
136 | }
137 | lower = upper;
138 | upper += 2 * weight * ((pop.Count - i) / pop.Count);
139 | }
140 | return pop[0];
141 | }
142 |
143 | private void init()
144 | {
145 | angles = new List();
146 | for (int i = 0; i < adam.Count; i++)
147 | {
148 | double angle = randomAngle(adam[i]);
149 | angles.Add(angle);
150 | }
151 | population.Add(new Individual(adam, angles));
152 | while (population.Count < config.POPULATION_SIZE)
153 | {
154 | Individual mutant = mutate(population[0]);
155 | population.Add(mutant);
156 | }
157 | }
158 |
159 | private Individual mutate(Individual individual)
160 | {
161 |
162 | Individual clone = new Individual(individual);
163 | for (int i = 0; i < clone.placement.Count; i++)
164 | {
165 | double random = new Random().NextDouble();
166 | if (random < 0.01 * config.MUTATION_RATE)
167 | {
168 | int j = i + 1;
169 | if (j < clone.placement.Count)
170 | {
171 | var placement = clone.getPlacement();
172 | placement.Swap(i, j);
173 | //Collections.swap(clone.getPlacement(), i, j);
174 | }
175 | }
176 | random = new Random().NextDouble();
177 | if (random < 0.01 * config.MUTATION_RATE)
178 | {
179 | clone.getRotation()[i] = randomAngle(clone.placement[i]);
180 | // clone.getRotation().set(i, randomAngle(clone.placement.get(i)));
181 | }
182 | }
183 | checkAndUpdate(clone);
184 | return clone;
185 | }
186 |
187 | /**
188 | * 为一个polygon 返回一个角度
189 | * @param part
190 | * @return
191 | */
192 | private double randomAngle(NestPath part)
193 | {
194 | List angleList = new List();
195 | double rotate = Math.Max(1, part.getRotation());
196 | if (rotate == 0)
197 | {
198 | angleList.Add(0);
199 | }
200 | else
201 | {
202 | for (int i = 0; i < rotate; i++)
203 | {
204 | angleList.Add((360 / rotate) * i);
205 | }
206 | }
207 | angleList.Shuffle();
208 | //Collections.shuffle(angleList);
209 | for (int i = 0; i < angleList.Count; i++)
210 | {
211 | Bound rotatedPart = GeometryUtil.rotatePolygon(part, angleList[i]);
212 | if (rotatedPart.getWidth() < binBounds.getWidth() && rotatedPart.getHeight() < binBounds.getHeight())
213 | {
214 | return angleList[i];
215 | }
216 | }
217 | /**
218 | * 没有找到合法的角度
219 | */
220 | return -1;
221 | }
222 |
223 | public List getAdam()
224 | {
225 | return adam;
226 | }
227 |
228 | public void setAdam(List adam)
229 | {
230 | this.adam = adam;
231 | }
232 |
233 | public NestPath getBin()
234 | {
235 | return bin;
236 | }
237 |
238 | public void setBin(NestPath bin)
239 | {
240 | this.bin = bin;
241 | }
242 |
243 | public void checkAndUpdate(Individual individual)
244 | {
245 | for (int i = 0; i < individual.placement.Count; i++)
246 | {
247 | double angle = individual.getRotation()[i];
248 | NestPath nestPath = individual.getPlacement()[i];
249 | Bound rotateBound = GeometryUtil.rotatePolygon(nestPath, angle);
250 | if (rotateBound.width < binBounds.width && rotateBound.height < binBounds.height)
251 | {
252 | continue;
253 | }
254 | else
255 | {
256 | double safeAngle = randomAngle(nestPath);
257 | individual.getRotation()[i]= safeAngle;
258 | }
259 | }
260 | }
261 | }
262 | }
263 |
264 |
265 |
266 |
267 |
--------------------------------------------------------------------------------
/NestingLibPort/Data/NestPath.cs:
--------------------------------------------------------------------------------
1 | using ClipperLib;
2 | using NestingLibPort.Util;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Linq;
6 |
7 |
8 | namespace NestingLibPort.Data
9 | {
10 |
11 | using Path = List;
12 | using Paths = List>;
13 |
14 | public class NestPath: IComparable
15 | {
16 |
17 | private List segments;
18 | private List children;
19 | private NestPath parent;
20 | public double offsetX;
21 | public double offsetY;
22 |
23 | private int id;
24 | private int source;
25 | private double rotation;
26 | public Config config;
27 | public double area;
28 |
29 | public int bid;
30 |
31 |
32 | public void add(double x, double y)
33 | {
34 | this.add(new Segment(x, y));
35 | }
36 |
37 |
38 | public override bool Equals(Object obj)
39 | {
40 | NestPath nestPath = (NestPath)obj;
41 | if (segments.Count != nestPath.size())
42 | {
43 | return false;
44 | }
45 | for (int i = 0; i < segments.Count; i++)
46 | {
47 | if (!segments[i].Equals(nestPath.get(i)))
48 | {
49 | return false;
50 | }
51 | }
52 | if (children.Count != nestPath.getChildren().Count)
53 | {
54 | return false;
55 | }
56 | for (int i = 0; i < children.Count; i++)
57 | {
58 | if (!children[i].Equals(nestPath.getChildren()[i]))
59 | {
60 | return false;
61 | }
62 | }
63 | return true;
64 | }
65 |
66 | public Config getConfig()
67 | {
68 | return config;
69 | }
70 |
71 | public void setConfig(Config config)
72 | {
73 | this.config = config;
74 | }
75 |
76 | /**
77 | * 丢弃最后一个segment
78 | */
79 | public void pop()
80 | {
81 | segments.RemoveAt(segments.Count - 1);
82 | }
83 |
84 | public void reverse()
85 | {
86 | List rever = new List();
87 | for (int i = segments.Count - 1; i >= 0; i--)
88 | {
89 | rever.Add(segments[i]);
90 | }
91 | segments.Clear();
92 | foreach (Segment s in rever)
93 | {
94 | segments.Add(s);
95 | }
96 | }
97 |
98 | public Segment get(int i)
99 | {
100 | return segments[i];
101 | }
102 |
103 | public NestPath getParent()
104 | {
105 | return parent;
106 | }
107 |
108 | public void setParent(NestPath parent)
109 | {
110 | this.parent = parent;
111 | }
112 |
113 | public void addChildren(NestPath nestPath)
114 | {
115 | children.Add(nestPath);
116 | nestPath.setParent(this);
117 | }
118 |
119 |
120 | public override String ToString()
121 | {
122 | String res = "";
123 | res += "id = " + id + " , source = " + source + " , rotation = " + rotation + "\n";
124 | int count = 0;
125 | foreach (Segment s in segments)
126 | {
127 | res += "Segment " + count + "\n";
128 | count++;
129 | res += s.ToString() + "\n";
130 | }
131 | count = 0;
132 | foreach (NestPath nestPath in children)
133 | {
134 | res += "children " + count + "\n";
135 | count++;
136 | res += nestPath.ToString();
137 | }
138 | return res;
139 | }
140 |
141 | public List getChildren()
142 | {
143 | return children;
144 | }
145 |
146 | public void setChildren(List children)
147 | {
148 | this.children = children;
149 | }
150 |
151 | public double getRotation()
152 | {
153 | return rotation;
154 | }
155 |
156 | public void setRotation(double rotation)
157 | {
158 | this.rotation = rotation;
159 | }
160 |
161 | public void setSegments(List segments)
162 | {
163 | this.segments = segments;
164 | }
165 |
166 | public int getSource()
167 | {
168 | return source;
169 | }
170 |
171 | public void setSource(int source)
172 | {
173 | this.source = source;
174 | }
175 |
176 | public NestPath()
177 | {
178 | offsetX = 0;
179 | offsetY = 0;
180 | children = new List();
181 | segments = new List();
182 | config = new Config();
183 | area = 0;
184 | }
185 |
186 |
187 | public NestPath(Config config)
188 | {
189 | offsetX = 0;
190 | offsetY = 0;
191 | children = new List();
192 | segments = new List();
193 | area = 0;
194 | this.config = config;
195 | }
196 |
197 | public NestPath(NestPath srcNestPath)
198 | {
199 | segments = new List();
200 | foreach (Segment segment in srcNestPath.getSegments())
201 | {
202 | segments.Add(new Segment(segment));
203 | }
204 |
205 | this.id = srcNestPath.id;
206 | this.rotation = srcNestPath.rotation;
207 | this.source = srcNestPath.source;
208 | this.offsetX = srcNestPath.offsetX;
209 | this.offsetY = srcNestPath.offsetY;
210 | this.bid = srcNestPath.bid;
211 | this.area = srcNestPath.area;
212 | children = new List();
213 |
214 | foreach (NestPath nestPath in srcNestPath.getChildren())
215 | {
216 | NestPath child = new NestPath(nestPath);
217 | child.setParent(this);
218 | children.Add(child);
219 | }
220 | }
221 |
222 | public static NestPath cleanNestPath(NestPath srcPath)
223 | {
224 | /**
225 | * Convert NestPath 2 Clipper
226 | */
227 | Path path = CommonUtil.NestPath2Path(srcPath);
228 | Paths simple = Clipper.SimplifyPolygon(path, PolyFillType.pftEvenOdd);
229 | if (simple.Count == 0)
230 | {
231 | return null;
232 | }
233 | Path biggest = simple[0];
234 | double biggestArea = Math.Abs(Clipper.Area(biggest));
235 | for (int i = 0; i < simple.Count; i++)
236 | {
237 | double area = Math.Abs(Clipper.Area(simple[i]));
238 | if (area > biggestArea)
239 | {
240 | biggest = simple[i];
241 | biggestArea = area;
242 | }
243 | }
244 | //Path clean = biggest.Cleaned(Config.CURVE_TOLERANCE * Config.CLIIPER_SCALE);
245 | Path clean = Clipper.CleanPolygon(biggest, Config.CURVE_TOLERANCE * Config.CLIIPER_SCALE);
246 |
247 | if (clean.Count == 0)
248 | {
249 | return null;
250 | }
251 |
252 | /**
253 | * Convert Clipper 2 NestPath
254 | */
255 | NestPath cleanPath = CommonUtil.Path2NestPath(clean);
256 | cleanPath.bid = srcPath.bid;
257 | cleanPath.setRotation(srcPath.rotation);
258 | return cleanPath;
259 | }
260 |
261 | /**
262 | * 通过平移将NestPath的最低x坐标,y坐标的值必定都是0,
263 | */
264 | public void Zerolize()
265 | {
266 | ZeroX(); ZeroY();
267 | }
268 |
269 | private void ZeroX()
270 | {
271 | double xMin = Double.MaxValue;
272 | foreach (Segment s in segments)
273 | {
274 | if (xMin > s.getX())
275 | {
276 | xMin = s.getX();
277 | }
278 | }
279 | foreach (Segment s in segments)
280 | {
281 | s.setX(s.getX() - xMin);
282 | }
283 | }
284 |
285 | private void ZeroY()
286 | {
287 | double yMin = Double.MaxValue;
288 | foreach (Segment s in segments)
289 | {
290 | if (yMin > s.getY())
291 | {
292 | yMin = s.getY();
293 | }
294 | }
295 | foreach (Segment s in segments)
296 | {
297 | s.setY(s.getY() - yMin);
298 | }
299 | }
300 |
301 | public void clear()
302 | {
303 | segments.Clear();
304 | }
305 |
306 | public int size()
307 | {
308 | return segments.Count;
309 | }
310 |
311 | public void add(Segment s)
312 | {
313 | segments.Add(s);
314 | }
315 |
316 | public List getSegments()
317 | {
318 | return segments;
319 | }
320 |
321 |
322 | public int getId()
323 | {
324 | return id;
325 | }
326 |
327 | public void setId(int id)
328 | {
329 | this.id = id;
330 | }
331 |
332 | public double getOffsetX()
333 | {
334 | return offsetX;
335 | }
336 |
337 | public void setOffsetX(double offsetX)
338 | {
339 | this.offsetX = offsetX;
340 | }
341 |
342 | public double getOffsetY()
343 | {
344 | return offsetY;
345 | }
346 |
347 | public void setOffsetY(double offsetY)
348 | {
349 | this.offsetY = offsetY;
350 | }
351 |
352 | public int CompareTo(NestPath o)
353 | {
354 | double area0 = this.area;
355 | double area1 = o.area;
356 | if (area0 > area1)
357 | {
358 | return 1;
359 | }
360 | else if (area0 == area1)
361 | {
362 | return 0;
363 | }
364 | return -1;
365 | }
366 |
367 | public double getMaxY()
368 | {
369 | double MaxY = Double.MinValue;
370 | foreach (Segment s in segments)
371 | {
372 | if (MaxY < s.getY())
373 | {
374 | MaxY = s.getY();
375 | }
376 | }
377 | return MaxY;
378 | }
379 |
380 | public void translate(double x, double y)
381 | {
382 | foreach (Segment s in segments)
383 | {
384 | s.setX(s.getX() + x);
385 | s.setY(s.getY() + y);
386 | }
387 | }
388 |
389 | }
390 | }
391 |
--------------------------------------------------------------------------------
/NestingLibPort/Util/PlacementWorker.cs:
--------------------------------------------------------------------------------
1 | using ClipperLib;
2 | using NestingLibPort.Data;
3 | using NestingLibPort.Util.Coor;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Web.Script.Serialization;
8 |
9 |
10 | namespace NestingLibPort.Util
11 | {
12 | using Path = List;
13 | using Paths = List>;
14 |
15 | public class PlacementWorker
16 | {
17 |
18 | public NestPath binPolygon;
19 | public Config config;
20 | public Dictionary> nfpCache;
21 | //private static Gson gson = new GsonBuilder().create();
22 | /**
23 | *
24 | * @param binPolygon 底板参数
25 | * @param config 设置
26 | * @param nfpCache nfp列表
27 | */
28 | public PlacementWorker(NestPath binPolygon, Config config, Dictionary> nfpCache)
29 | {
30 | this.binPolygon = binPolygon;
31 | this.config = config;
32 | this.nfpCache = nfpCache;
33 | }
34 |
35 | /**
36 | * 根据板件列表与旋转角列表,通过nfp,计算板件在底板上的位置,并返回这个种群的fitness
37 | * @param paths
38 | * @return
39 | */
40 | public Result placePaths(List paths)
41 | {
42 | List rotated = new List();
43 | for (int i = 0; i < paths.Count; i++)
44 | {
45 | NestPath r = GeometryUtil.rotatePolygon2Polygon(paths[i], paths[i].getRotation());
46 | r.setRotation(paths[i].getRotation());
47 | r.setSource(paths[i].getSource());
48 | r.setId(paths[i].getId());
49 | rotated.Add(r);
50 | }
51 | paths = rotated;
52 |
53 | List> allplacements = new List>();
54 | double fitness = 0;
55 | double binarea = Math.Abs(GeometryUtil.polygonArea(this.binPolygon));
56 | String key = null;
57 | List nfp = null;
58 |
59 | while (paths.Count > 0)
60 | {
61 |
62 | List placed = new List();
63 | List placements = new List();
64 |
65 | fitness += 1;
66 | double minwidth = Double.MaxValue;
67 | for (int i = 0; i < paths.Count; i++)
68 | {
69 |
70 | NestPath path = paths[i];
71 |
72 | //inner NFP
73 | key = new JavaScriptSerializer().Serialize(new NfpKey(-1, path.getId(), true, 0, path.getRotation()));
74 |
75 | //key = gson.toJson(new NfpKey(-1, path.getId(), true, 0, path.getRotation()));
76 |
77 | if (!nfpCache.ContainsKey(key))
78 | {
79 | continue;
80 | }
81 |
82 | List binNfp = nfpCache[key];
83 |
84 |
85 |
86 | // ensure exists
87 | bool error = false;
88 | for (int j = 0; j < placed.Count; j++)
89 | {
90 | key = new JavaScriptSerializer().Serialize(new NfpKey(placed[j].getId(), path.getId(), false, placed[j].getRotation(), path.getRotation()));
91 | // key = gson.toJson(new NfpKey(placed[j].getId(), path.getId(), false, placed[j].getRotation(), path.getRotation()));
92 | if (nfpCache.ContainsKey(key)) nfp = nfpCache[key];
93 | else
94 | {
95 | error = true;
96 | break;
97 | }
98 | }
99 | if (error)
100 | {
101 | continue;
102 | }
103 |
104 |
105 | Vector position = null;
106 | if (placed.Count == 0)
107 | {
108 | // first placement , put it on the lefth
109 | for (int j = 0; j < binNfp.Count; j++)
110 | {
111 | for (int k = 0; k < binNfp[j].size(); k++)
112 | {
113 | if (position == null || binNfp[j].get(k).x - path.get(0).x < position.x)
114 | {
115 | position = new Vector(
116 | binNfp[j].get(k).x - path.get(0).x,
117 | binNfp[j].get(k).y - path.get(0).y,
118 | path.getId(),
119 | path.getRotation()
120 | );
121 | }
122 | }
123 | }
124 | placements.Add(position);
125 | placed.Add(path);
126 | continue;
127 | }
128 |
129 | Paths clipperBinNfp = new Paths();
130 |
131 | for (int j = 0; j < binNfp.Count; j++)
132 | {
133 | NestPath binNfpj = binNfp[j];
134 | clipperBinNfp.Add(scaleUp2ClipperCoordinates(binNfpj));
135 | }
136 | Clipper clipper = new Clipper();
137 | Paths combinedNfp = new Paths();
138 |
139 |
140 | for (int j = 0; j < placed.Count; j++)
141 | {
142 |
143 | key = new JavaScriptSerializer().Serialize(new NfpKey(placed[j].getId(), path.getId(), false, placed[j].getRotation(), path.getRotation()));
144 | //key = gson.toJson(new NfpKey(placed[j].getId(), path.getId(), false, placed[j].getRotation(), path.getRotation()));
145 | nfp = nfpCache[key];
146 | if (nfp == null)
147 | {
148 | continue;
149 | }
150 |
151 | for (int k = 0; k < nfp.Count; k++)
152 | {
153 | Path clone =PlacementWorker.scaleUp2ClipperCoordinates(nfp[k]);
154 | for (int m = 0; m < clone.Count; m++)
155 | {
156 | long clx = (long)clone[m].X;
157 | long cly = (long)clone[m].Y;
158 | IntPoint intPoint=clone[m];
159 | intPoint.X = (clx + (long)(placements[j].x * Config.CLIIPER_SCALE));
160 | intPoint.Y=(cly + (long)(placements[j].y * Config.CLIIPER_SCALE));
161 | clone[m] = intPoint;
162 | }
163 | //clone = clone.Cleaned(0.0001 * Config.CLIIPER_SCALE);
164 | clone = Clipper.CleanPolygon(clone, 0.0001 * Config.CLIIPER_SCALE);
165 | double areaPoly = Math.Abs(Clipper.Area(clone));
166 | if (clone.Count > 2 && areaPoly > 0.1 * Config.CLIIPER_SCALE * Config.CLIIPER_SCALE)
167 | {
168 | clipper.AddPath(clone, PolyType.ptSubject,true);
169 | }
170 | }
171 | }
172 | if (!clipper.Execute(ClipType.ctUnion, combinedNfp, PolyFillType.pftNonZero, PolyFillType.pftNonZero))
173 | {
174 | continue;
175 | }
176 |
177 | //difference with bin polygon
178 | Paths finalNfp = new Paths();
179 | clipper = new Clipper();
180 |
181 | clipper.AddPaths(combinedNfp, PolyType.ptClip,true);
182 | clipper.AddPaths(clipperBinNfp, PolyType.ptSubject,true);
183 | if (!clipper.Execute(ClipType.ctDifference, finalNfp, PolyFillType.pftNonZero, PolyFillType.pftNonZero))
184 | {
185 | continue;
186 | }
187 |
188 | // finalNfp = finalNfp.Cleaned(0.0001 * Config.CLIIPER_SCALE);
189 | finalNfp = Clipper.CleanPolygons(finalNfp, 0.0001 * Config.CLIIPER_SCALE);
190 | for (int j = 0; j < finalNfp.Count(); j++)
191 | {
192 | //double areaPoly = Math.Abs(finalNfp[j].Area);
193 | double areaPoly = Math.Abs(Clipper.Area(finalNfp[j]));
194 | if (finalNfp[j].Count < 3 || areaPoly < 0.1 * Config.CLIIPER_SCALE * Config.CLIIPER_SCALE)
195 | {
196 | finalNfp.RemoveAt(j);
197 | j--;
198 | }
199 | }
200 |
201 | if (finalNfp == null || finalNfp.Count == 0)
202 | {
203 | continue;
204 | }
205 |
206 | List f = new List();
207 | for (int j = 0; j < finalNfp.Count; j++)
208 | {
209 | f.Add(toNestCoordinates(finalNfp[j]));
210 | }
211 |
212 | List finalNfpf = f;
213 | double minarea = Double.MinValue;
214 | double minX = Double.MaxValue;
215 | NestPath nf = null;
216 | double area = Double.MinValue;
217 | Vector shifvector = null;
218 | for (int j = 0; j < finalNfpf.Count; j++)
219 | {
220 | nf = finalNfpf[j];
221 | if (Math.Abs(GeometryUtil.polygonArea(nf)) < 2)
222 | {
223 | continue;
224 | }
225 | for (int k = 0; k < nf.size(); k++)
226 | {
227 | NestPath allpoints = new NestPath();
228 | for (int m = 0; m < placed.Count; m++)
229 | {
230 | for (int n = 0; n < placed[m].size(); n++)
231 | {
232 | allpoints.add(new Segment(placed[m].get(n).x + placements[m].x,
233 | placed[m].get(n).y + placements[m].y));
234 | }
235 | }
236 | shifvector = new Vector(nf.get(k).x - path.get(0).x,nf.get(k).y - path.get(0).y,path.getId(),path.getRotation(),combinedNfp);
237 | for (int m = 0; m < path.size(); m++)
238 | {
239 | allpoints.add(new Segment(path.get(m).x + shifvector.x, path.get(m).y + shifvector.y));
240 | }
241 | Bound rectBounds = GeometryUtil.getPolygonBounds(allpoints);
242 |
243 | area = rectBounds.getWidth() * 2 + rectBounds.getHeight();
244 | if (minarea == Double.MinValue
245 | || area < minarea
246 | || (GeometryUtil.almostEqual(minarea, area)
247 | && (minX == Double.MinValue || shifvector.x < minX)))
248 | {
249 | minarea = area;
250 | minwidth = rectBounds.getWidth();
251 | position = shifvector;
252 | minX = shifvector.x;
253 | }
254 | }
255 | }
256 | if (position != null)
257 | {
258 |
259 | placed.Add(path);
260 | placements.Add(position);
261 | }
262 | }
263 | if (minwidth != Double.MinValue)
264 | {
265 | fitness += minwidth / binarea;
266 | }
267 |
268 |
269 |
270 | for (int i = 0; i < placed.Count; i++)
271 | {
272 | int index = paths.IndexOf(placed[i]);
273 | if (index >= 0)
274 | {
275 | paths.RemoveAt(index);
276 | }
277 | }
278 |
279 | if (placements != null && placements.Count > 0)
280 | {
281 | allplacements.Add(placements);
282 | }
283 | else
284 | {
285 | break; // something went wrong
286 | }
287 |
288 | }
289 | // there were paths that couldn't be placed
290 | fitness += 2 * paths.Count;
291 | return new Result(allplacements, fitness, paths, binarea);
292 | }
293 |
294 |
295 | /**
296 | * 坐标转换,与clipper库交互必须坐标转换
297 | * @param polygon
298 | * @return
299 | */
300 | public static Path scaleUp2ClipperCoordinates(NestPath polygon)
301 | {
302 | Path p = new Path();
303 | foreach (Segment s in polygon.getSegments())
304 | {
305 | ClipperCoor cc = CommonUtil.toClipperCoor(s.x, s.y);
306 | p.Add(new IntPoint(cc.getX(), cc.getY()));
307 | }
308 | return p;
309 | }
310 |
311 | public static NestPath toNestCoordinates(Path polygon)
312 | {
313 | NestPath clone = new NestPath();
314 | for (int i = 0; i < polygon.Count; i++)
315 | {
316 | Segment s = new Segment((double)polygon[i].X / Config.CLIIPER_SCALE, (double)polygon[i].Y / Config.CLIIPER_SCALE);
317 | clone.add(s);
318 | }
319 | return clone;
320 | }
321 |
322 | }
323 | }
324 |
--------------------------------------------------------------------------------
/NestingLibPort/Nest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using NestingLibPort.Data;
7 | using NestingLibPort.Util;
8 | using NestingLibPort.Util.Coor;
9 | using NestingLibPort.Algorithm;
10 | using System.Web.Script.Serialization;
11 |
12 | //package com.qunhe.util.nest;
13 |
14 | //import com.google.gson.Gson;
15 | //import com.google.gson.GsonBuilder;
16 | //import com.qunhe.util.nest.algorithm.GeneticAlgorithm;
17 | //import com.qunhe.util.nest.algorithm.Individual;
18 | //import com.qunhe.util.nest.data.*;
19 | //import com.qunhe.util.nest.data.Vector;
20 | //import com.qunhe.util.nest.util.*;
21 |
22 | //import java.util.*;
23 | namespace NestingLibPort
24 | {
25 |
26 | public class Nest
27 | {
28 | private NestPath binPath;
29 | private List parts;
30 | private Config config;
31 | int loopCount;
32 | private GeneticAlgorithm GA = null;
33 | private Dictionary> nfpCache;
34 | // private static Gson gson = new GsonBuilder().create();
35 | private JavaScriptSerializer serialize = new JavaScriptSerializer();
36 | private int launchcount = 0;
37 |
38 | /**
39 | * 创建一个新的Nest对象
40 | * @param binPath 底板多边形
41 | * @param parts 板件多边形列表
42 | * @param config 参数设置
43 | * @param count 迭代计算次数
44 | */
45 | public Nest(NestPath binPath, List parts, Config config, int count)
46 | {
47 | this.binPath = binPath;
48 | this.parts = parts;
49 | this.config = config;
50 | this.loopCount = count;
51 | nfpCache = new Dictionary>();
52 | }
53 |
54 | /**
55 | * 开始进行Nest计算
56 | * @return
57 | */
58 | public List> startNest()
59 | {
60 | List tree = CommonUtil.BuildTree(parts, Config.CURVE_TOLERANCE);
61 |
62 | CommonUtil.offsetTree(tree, 0.5 * config.SPACING);
63 | binPath.config = config;
64 | foreach (NestPath nestPath in parts)
65 | {
66 | nestPath.config = config;
67 | }
68 | NestPath binPolygon = NestPath.cleanNestPath(binPath);
69 | Bound binBound = GeometryUtil.getPolygonBounds(binPolygon);
70 | if (config.SPACING > 0)
71 | {
72 | List offsetBin = CommonUtil.polygonOffset(binPolygon, -0.5 * config.SPACING);
73 | if (offsetBin.Count == 1)
74 | {
75 | binPolygon = offsetBin[0];
76 | }
77 | }
78 | binPolygon.setId(-1);
79 |
80 | List integers = checkIfCanBePlaced(binPolygon, tree);
81 | List safeTree = new List();
82 | foreach (int i in integers)
83 | {
84 | safeTree.Add(tree[i]);
85 | }
86 | tree = safeTree;
87 |
88 | double xbinmax = binPolygon.get(0).x;
89 | double xbinmin = binPolygon.get(0).x;
90 | double ybinmax = binPolygon.get(0).y;
91 | double ybinmin = binPolygon.get(0).y;
92 |
93 | for (int i = 1; i < binPolygon.size(); i++)
94 | {
95 | if (binPolygon.get(i).x > xbinmax)
96 | {
97 | xbinmax = binPolygon.get(i).x;
98 | }
99 | else if (binPolygon.get(i).x < xbinmin)
100 | {
101 | xbinmin = binPolygon.get(i).x;
102 | }
103 |
104 | if (binPolygon.get(i).y > ybinmax)
105 | {
106 | ybinmax = binPolygon.get(i).y;
107 | }
108 | else if (binPolygon.get(i).y < ybinmin)
109 | {
110 | ybinmin = binPolygon.get(i).y;
111 | }
112 | }
113 | for (int i = 0; i < binPolygon.size(); i++)
114 | {
115 | binPolygon.get(i).x -= xbinmin;
116 | binPolygon.get(i).y -= ybinmin;
117 | }
118 |
119 |
120 | double binPolygonWidth = xbinmax - xbinmin;
121 | double binPolygonHeight = ybinmax - ybinmin;
122 |
123 | if (GeometryUtil.polygonArea(binPolygon) > 0)
124 | {
125 | binPolygon.reverse();
126 | }
127 | /**
128 | * 确保为逆时针
129 | */
130 | for (int i = 0; i < tree.Count; i++)
131 | {
132 | Segment start = tree[i].get(0);
133 | Segment end = tree[i].get(tree[i].size() - 1);
134 | if (start == end || GeometryUtil.almostEqual(start.x, end.x) && GeometryUtil.almostEqual(start.y, end.y))
135 | {
136 | tree[i].pop();
137 | }
138 | if (GeometryUtil.polygonArea(tree[i]) > 0)
139 | {
140 | tree[i].reverse();
141 | }
142 | }
143 |
144 | launchcount = 0;
145 | Result best = null;
146 |
147 |
148 | // Tree Modification based on nest4J
149 | //List modifiedTree = new List();
150 |
151 | //for (int i = 0; i < tree.Count; i++)
152 | //{
153 | // List modifiedSegment = new List();
154 | // NestPath currentTree = tree[i];
155 | // List currentTreeSegments = currentTree.getSegments();
156 | // modifiedSegment.Add(currentTreeSegments[currentTreeSegments.Count-1]);
157 | // for (int j = 0; j < currentTreeSegments.Count-1; j++)
158 | // {
159 | // modifiedSegment.Add(currentTreeSegments[j]);
160 | // }
161 | // currentTree.setSegments(modifiedSegment);
162 | // modifiedTree.Add(currentTree);
163 | //}
164 | //tree = modifiedTree;
165 |
166 |
167 |
168 |
169 | for (int i = 0; i < loopCount; i++)
170 | {
171 |
172 | Result result = launchWorkers(tree, binPolygon, config);
173 |
174 | if (i == 0)
175 | {
176 | best = result;
177 | }
178 | else
179 | {
180 | if (best.fitness > result.fitness)
181 | {
182 | best = result;
183 | }
184 | }
185 | }
186 | double sumarea = 0;
187 | double totalarea = 0;
188 | for (int i = 0; i < best.placements.Count; i++)
189 | {
190 | totalarea += Math.Abs(GeometryUtil.polygonArea(binPolygon));
191 | for (int j = 0; j < best.placements[i].Count; j++)
192 | {
193 | try
194 | {
195 | sumarea += Math.Abs(GeometryUtil.polygonArea(tree[best.placements[i][j].id]));
196 |
197 | }
198 | catch (Exception ex)
199 | {
200 |
201 |
202 | }
203 | }
204 | }
205 | double rate = (sumarea / totalarea) * 100;
206 | List> appliedPlacement = applyPlacement(best, tree);
207 | return appliedPlacement;
208 | }
209 |
210 | /**
211 | * 一次迭代计算
212 | * @param tree 底板
213 | * @param binPolygon 板件列表
214 | * @param config 设置
215 | * @return
216 | */
217 | public Result launchWorkers(List tree, NestPath binPolygon, Config config)
218 | {
219 |
220 | launchcount++;
221 | if (GA == null)
222 | {
223 |
224 | List adam = new List();
225 | foreach (NestPath nestPath in tree)
226 | {
227 | NestPath clone = new NestPath(nestPath);
228 | adam.Add(clone);
229 | }
230 | foreach (NestPath nestPath in adam)
231 | {
232 | nestPath.area = GeometryUtil.polygonArea(nestPath);
233 | }
234 | adam.Sort((x,y)=>x.area.CompareTo(y.area));
235 | //Collections.sort(adam);
236 | GA = new GeneticAlgorithm(adam, binPolygon, config);
237 | }
238 |
239 | Individual individual = null;
240 | for (int i = 0; i < GA.population.Count; i++)
241 | {
242 | if (GA.population[i].getFitness() < 0)
243 | {
244 | individual = GA.population[i];
245 | break;
246 | }
247 | }
248 | // if(individual == null ){
249 | // GA.generation();
250 | // individual = GA.population.get(1);
251 | // }
252 | if (launchcount > 1 && individual == null)
253 | {
254 | GA.generation();
255 | individual = GA.population[1];
256 | }
257 |
258 | // 以上为GA
259 |
260 | List placelist = individual.getPlacement();
261 | List rotations = individual.getRotation();
262 |
263 | List ids = new List();
264 | for (int i = 0; i < placelist.Count; i++)
265 | {
266 | ids.Add(placelist[i].getId());
267 | placelist[i].setRotation(rotations[i]);
268 | }
269 | List nfpPairs = new List();
270 | NfpKey key = null;
271 | /**
272 | * 如果在nfpCache里没找到nfpKey 则添加进nfpPairs
273 | */
274 | for (int i = 0; i < placelist.Count; i++)
275 | {
276 | NestPath part = placelist[i];
277 | key = new NfpKey(binPolygon.getId(), part.getId(), true, 0, part.getRotation());
278 | if (!nfpCache.ContainsKey(serialize.Serialize(key)))
279 | nfpPairs.Add(new NfpPair(binPolygon, part, key));
280 | else
281 | {
282 | }
283 | for (int j = 0; j < i; j++)
284 | {
285 | NestPath placed = placelist[j];
286 | NfpKey keyed = new NfpKey(placed.getId(), part.getId(), false, rotations[j], rotations[i]);
287 | nfpPairs.Add(new NfpPair(placed, part, keyed));
288 | }
289 | }
290 |
291 |
292 | /**
293 | * 第一次nfpCache为空 ,nfpCache存的是nfpKey所对应的两个polygon所形成的Nfp( List )
294 | */
295 | List generatedNfp = new List();
296 | foreach (NfpPair nfpPair in nfpPairs)
297 | {
298 | ParallelData dataTemp = NfpUtil.nfpGenerator(nfpPair, config);
299 | generatedNfp.Add(dataTemp);
300 | }
301 | for (int i = 0; i < generatedNfp.Count; i++)
302 | {
303 | ParallelData Nfp = generatedNfp[i];
304 | //TODO remove gson & generate a new key algorithm
305 | String tkey = serialize.Serialize(Nfp.getKey()); //gson.toJson(Nfp.getKey());
306 | if (!nfpCache.ContainsKey(tkey))
307 | {
308 | nfpCache.Add(tkey, Nfp.value);
309 | }
310 | else
311 | {
312 |
313 | }
314 |
315 | }
316 |
317 | PlacementWorker worker = new PlacementWorker(binPolygon, config, nfpCache);
318 | List placeListSlice = new List();
319 |
320 |
321 |
322 | for (int i = 0; i < placelist.Count; i++)
323 | {
324 | placeListSlice.Add(new NestPath(placelist[i]));
325 | }
326 | List> data = new List>();
327 | data.Add(placeListSlice);
328 | List placements = new List();
329 | for (int i = 0; i < data.Count; i++)
330 | {
331 | Result result = worker.placePaths(data[i]);
332 | placements.Add(result);
333 | }
334 | if (placements.Count == 0)
335 | {
336 | return null;
337 | }
338 | individual.fitness = placements[0].fitness;
339 | Result bestResult = placements[0];
340 | for (int i = 1; i < placements.Count; i++)
341 | {
342 | if (placements[i].fitness < bestResult.fitness)
343 | {
344 | bestResult = placements[i];
345 | }
346 | }
347 | return bestResult;
348 | }
349 |
350 | /**
351 | * 通过id与bid将translate和rotate绑定到对应板件上
352 | * @param best
353 | * @param tree
354 | * @return
355 | */
356 | public static List> applyPlacement(Result best, List tree)
357 | {
358 | List> applyPlacement = new List>();
359 | for (int i = 0; i < best.placements.Count; i++)
360 | {
361 | List binTranslate = new List();
362 | for (int j = 0; j < best.placements[i].Count; j++)
363 | {
364 | Vector v = best.placements[i][j];
365 | NestPath nestPath = tree[v.id];
366 | foreach (NestPath child in nestPath.getChildren())
367 | {
368 | Placement chPlacement = new Placement(child.bid, new Segment(v.x, v.y), v.rotation);
369 | binTranslate.Add(chPlacement);
370 | }
371 | Placement placement = new Placement(nestPath.bid, new Segment(v.x, v.y), v.rotation);
372 | binTranslate.Add(placement);
373 | }
374 | applyPlacement.Add(binTranslate);
375 | }
376 | return applyPlacement;
377 | }
378 |
379 |
380 | /**
381 | * 在遗传算法中每次突变或者是交配产生出新的种群时,可能会出现板件与旋转角度不适配的结果,需要重新检查并适配。
382 | * @param binPolygon
383 | * @param tree
384 | * @return
385 | */
386 | private static List checkIfCanBePlaced(NestPath binPolygon, List tree)
387 | {
388 | List CanBePlacdPolygonIndex = new List();
389 | Bound binBound = GeometryUtil.getPolygonBounds(binPolygon);
390 | for (int i = 0; i < tree.Count; i++)
391 | {
392 | NestPath nestPath = tree[i];
393 | if (nestPath.getRotation() == 0)
394 | {
395 | Bound bound = GeometryUtil.getPolygonBounds(nestPath);
396 | if (bound.width < binBound.width && bound.height < binBound.height)
397 | {
398 | CanBePlacdPolygonIndex.Add(i);
399 | continue;
400 | }
401 | }
402 | else
403 | {
404 | for (int j = 0; j < nestPath.getRotation(); j++)
405 | {
406 | Bound rotatedBound = GeometryUtil.rotatePolygon(nestPath, (360 / nestPath.getRotation()) * j);
407 | if (rotatedBound.width < binBound.width && rotatedBound.height < binBound.height)
408 | {
409 | CanBePlacdPolygonIndex.Add(i);
410 | break;
411 | }
412 | }
413 | }
414 | }
415 | return CanBePlacdPolygonIndex;
416 | }
417 |
418 |
419 |
420 |
421 | public void add(NestPath np)
422 | {
423 | parts.Add(np);
424 | }
425 |
426 | public NestPath getBinPath()
427 | {
428 | return binPath;
429 | }
430 |
431 | public List getParts()
432 | {
433 | return parts;
434 | }
435 |
436 | public void setBinPath(NestPath binPath)
437 | {
438 | this.binPath = binPath;
439 | }
440 |
441 | public void setParts(List parts)
442 | {
443 | this.parts = parts;
444 | }
445 |
446 | public Config getConfig()
447 | {
448 | return config;
449 | }
450 |
451 | public void setConfig(Config config)
452 | {
453 | this.config = config;
454 | }
455 |
456 | }
457 |
458 | }
459 |
--------------------------------------------------------------------------------
/NestingConsole/bin/Debug/test.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
6 |
8 |
10 |
12 |
14 |
16 |
18 |
20 |
22 |
24 |
26 |
28 |
30 |
32 |
34 |
36 |
38 |
40 |
42 |
44 |
46 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
58 |
60 |
62 |
64 |
66 |
68 |
70 |
72 |
74 |
76 |
78 |
80 |
82 |
84 |
86 |
88 |
90 |
92 |
94 |
96 |
98 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
110 |
112 |
114 |
116 |
118 |
120 |
122 |
124 |
126 |
128 |
130 |
132 |
134 |
136 |
138 |
140 |
142 |
144 |
146 |
148 |
150 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
162 |
164 |
166 |
168 |
170 |
172 |
174 |
176 |
178 |
180 |
182 |
184 |
186 |
188 |
190 |
192 |
194 |
196 |
198 |
200 |
202 |
204 |
206 |
208 |
210 |
212 |
214 |
216 |
218 |
220 |
222 |
224 |
226 |
228 |
230 |
232 |
234 |
236 |
238 |
241 |
243 |
245 |
247 |
249 |
251 |
253 |
255 |
257 |
259 |
261 |
263 |
265 |
--------------------------------------------------------------------------------
/NestingConsole/bin/Debug/output.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 |
6 |
--------------------------------------------------------------------------------