基于C# NetTopologySuite从相交线提取目标多边形的技术问题
提取边界多边形问题(NetTopologySuite + C#)
我需要提取示例中的边界多边形,目前在C#中使用NetTopologySuite(与JTS库功能类似),但无法得到预期的红色目标多边形,求解决方法。
初始实现代码
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; PromptEntityOptions pEntOpt1 = new PromptEntityOptions("Outter Boundary"); pEntOpt1.SetRejectMessage("is Not Polyline"); pEntOpt1.AddAllowedClass(typeof(Polyline), true); PromptEntityResult pEntRes1 = ed.GetEntity(pEntOpt1); string typeName = RXObject.GetClass(typeof(Polyline)).DxfName; TypedValue[] tv = new TypedValue[1] { new TypedValue((int)DxfCode.Start, typeName) }; SelectionFilter sf = new SelectionFilter(tv); PromptSelectionResult pSelectRes = ed.GetSelection(sf); if (pSelectRes.Status != PromptStatus.OK) return; ObjectId boudaryPlineId = pEntRes1.ObjectId; ObjectId[] innerPlineids = pSelectRes.Value.GetObjectIds(); ObjectIdCollection innerBoundaries = new ObjectIdCollection(innerPlineids); try { using (doc.LockDocument()) { using (Transaction tr = db.TransactionManager.StartTransaction()) { Polygonizer polygonizer = new Polygonizer(); GeometryFactory gf = new GeometryFactory(); Polyline boundaryPline = tr.GetObject(boudaryPlineId, OpenMode.ForWrite) as Polyline; Coordinate[] coords = new Coordinate[boundaryPline.NumberOfVertices + 1]; for (int j = 0; j < boundaryPline.NumberOfVertices; j++) { Point3d pos = boundaryPline.GetPoint3dAt(j); coords[j] = new Coordinate(pos.X, pos.Y); } coords[boundaryPline.NumberOfVertices] = new Coordinate(boundaryPline.StartPoint.X, boundaryPline.StartPoint.Y); Polygon pg = gf.CreatePolygon(coords); List<Geometry> plines = new List<Geometry>(); foreach (ObjectId indexid in innerBoundaries) { Polyline indexPline = tr.GetObject(indexid, OpenMode.ForWrite) as Polyline; Coordinate[] coords2 = new Coordinate[indexPline.NumberOfVertices]; for (int i = 0; i < indexPline.NumberOfVertices; i++) { Point3d pos = indexPline.GetPoint3dAt(i); coords2[i] = new Coordinate(pos.X, pos.Y); } LineString ls1 = new LineString(coords2); polygonizer.Add(ls1); } ICollection<Geometry> test = polygonizer.GetPolygons(); foreach (Geometry polygon in test) { // to do } tr.Commit(); } } } catch { }
相关图示
应用修改方案后的代码
private List<NetGeometry> GetPolygons(Transaction tr, List<LineString> lineStrings) { List<NetGeometry> polygons = new List<NetGeometry>(); Polygonizer polygonizer = new Polygonizer(false); GeometryFactory gf = new GeometryFactory(new PrecisionModel(10000)); var noder = new SnapRoundingNoder(new PrecisionModel(10000)); // 按需调整PrecisionModel,必须为固定精度 noder.ComputeNodes(lineStrings.Select(s => (ISegmentString)new NodedSegmentString(s.Coordinates, s)).ToList()); var noded = noder.GetNodedSubstrings(); polygonizer.Add(noded.Select(n => (Geometry)gf.CreateLineString(n.Coordinates)).ToList()); //for ( int i = 0; i < lineStrings.Count; i++) //{ // polygonizer.Add(lineStrings[i]); //} ICollection<NetGeometry> result = polygonizer.GetPolygons(); foreach (NetGeometry index in result) { if ( index is Polygon pg) { polygons.Add(pg); } } return polygons; }
修改后结果图示

内容的提问来源于stack exchange,提问作者Hyeseong
相关产品推荐
相关产品推荐




