What is the simplest way to join a collection of polygons and draw the resulting contour using Eyeshot? So far I was able only to get filled regions but I am interested in the merging of the contours.
Asked
Active
Viewed 585 times
2 Answers
1
Since you have the region it is very easy to get the contour from it.
// if you know the region is a simple region (not containing full circles) get the curves
List<ICurve> curves = (region.ContourList.FirstOrDefault() as CompositeCurve).CurveList;
ICurve are line and arc only as far as i know. So you can test:
bool isLine = curves[0] is Line
bool isArc = curves[0] is Arc
All curves in the list are ordered so you can reform the region easily. Also, if the region.ContourList
contain more than 1 contour then it mean you have holes in your region. First element will always be the main contour and all the following elements are also contour but of the holes.
The contour curve is given counter clockwise and the inner are clockwise.

Franck
- 4,438
- 1
- 28
- 55
-
Thank you @Franck. Do you know if there is the need of a code similar to the one in my answer to join all the regions or there is something already implemented in Eyeshot? – stenio May 22 '18 at 16:19
-
@stenio personally use `Region.Union` alot and i use the same line describe above to get the final contour. I create `Region` using the `ICurve` constructor specifying all vertese of the polygon. – Franck May 22 '18 at 16:26
-
but if you have say 10 polygons to join, how do you handle the fact that `Region.Union` returns an array instead of a single region? – stenio May 22 '18 at 16:36
-
@stenio if region 1 intersect region 2, the result of `Union` will return an array with 1 region. If region 1 and region 2 does not intersect the array will return empty or contain 2 or more items. The logic on how to merge regions properly is a question on it's own that i have the answer too. – Franck May 22 '18 at 17:11
0
I came up to this solution. To display the polygons simply iterate through the joined regions, iterate through the ContourList
and create the LinearPaths
.
private List<PolyRegion2D> Joiner(IEnumerable<Polygon2D> polygons) {
// The resulting polygons are unconnected
List<PolyRegion2D> res = new List<PolyRegion2D>();
// Put every polygon in a region to do the unions.
LinkedList<PolyRegion2D> polygonRegions = new LinkedList<PolyRegion2D>();
foreach (Polygon2D polygon in polygons) {
polygonRegions.AddLast(new PolyRegion2D(new Polygon2D[]{polygon}));
}
while (polygonRegions.Count > 0) {
PolyRegion2D first = polygonRegions.First.Value;
polygonRegions.RemoveFirst();
PolyRegion2D union;
LinkedListNode<PolyRegion2D> connected = FindConnected(first, polygonRegions, out union);
if (connected == null) {
// Unconnected polygon
res.Add(first);
} else {
// Intersection found
polygonRegions.Remove(connected);
polygonRegions.AddFirst(union);
}
}
return res;
}
private LinkedListNode<PolyRegion2D> FindConnected(PolyRegion2D poly, LinkedList<PolyRegion2D> polys, out PolyRegion2D union) {
LinkedListNode<PolyRegion2D> node = polys.First;
while(node != null){
PolyRegion2D[] union_ = PolyRegion2D.Union(poly, node.Value);
if (union_.Length == 1) {
union = union_[0];
return node;
}
node = node.Next;
}
union = null;
return null;
}

stenio
- 297
- 1
- 10
-
I have just found out the `PolyRegion2D.Union` doesn't join a C shaped polygon with a I to form a square with a hole, but the code above assumed this behaviour. – stenio May 22 '18 at 07:09
-
-
The bug was fixed in build 11.0.642. Now the code above correctly joins all the polygons into the smallest set of unconnected polygons. – stenio May 28 '18 at 07:55