public static void FindOverlapingPolylines() { var document = Application.DocumentManager.MdiActiveDocument; var polylines = CadUtils.FindAllPolylines(document); FindOverlapingPolylines(polylines); }
static TopoData BuildTopology() { var polylineIds = CadUtils.FindAllPolylines(Application.DocumentManager.MdiActiveDocument); var datebase = Application.DocumentManager.MdiActiveDocument.Database; using (var tr = datebase.TransactionManager.StartTransaction()) { // 读入多边形数据 var reader = new DwgReader(); var polygons = new Dictionary <ObjectId, IPolygon>(); var quadtree = new Quadtree <IGeometry>(); // 构建拓扑 foreach (ObjectId polylineId in polylineIds) { var polygon = reader.ReadEntityAsPolygon(tr, polylineId) as IPolygon; if (polygon != null) { polygons.Add(polylineId, polygon); quadtree.Insert(polygon.EnvelopeInternal, polygon); } } tr.Commit(); return(new TopoData() { Polygons = polygons, Quadtree = quadtree, Reader = reader, }); } }
public static List <ObjectId> FindHoles() { var polylineIds = CadUtils.FindAllPolylines(Application.DocumentManager.MdiActiveDocument); var datebase = Application.DocumentManager.MdiActiveDocument.Database; using (var tr = datebase.TransactionManager.StartTransaction()) { // 读入多边形数据 var reader = new DwgReader(); var polygons = new List <IPolygon>(); var quadtree = new Quadtree <IGeometry>(); foreach (ObjectId polylineId in polylineIds) { var polygon = reader.ReadEntityAsPolygon(tr, polylineId) as IPolygon; if (polygon != null) { polygons.Add(polygon); quadtree.Insert(polygon.EnvelopeInternal, polygon); } } var possibleHoleIds = new List <ObjectId>(); // 便利多边形,如果有洞,开始计算 foreach (var polygon in polygons) { // 找洞 foreach (var geom in quadtree.Query(polygon.EnvelopeInternal)) { var insidePolygon = geom as IPolygon; if (insidePolygon != null && polygon.Within(insidePolygon) && insidePolygon.UserData != polygon.UserData) // 不要是自己 { possibleHoleIds.Add((ObjectId)insidePolygon.UserData); } } } tr.Commit(); return(possibleHoleIds); } }
public static bool HasHoles(Document document) { var polylineIds = CadUtils.FindAllPolylines(document); var datebase = document.Database; using (var tr = datebase.TransactionManager.StartTransaction()) { foreach (var objectId in polylineIds) { var hole = tr.GetObject(objectId, OpenMode.ForRead) as Entity; if (CadUtils.GetCassFlag(hole) == CassFlagIsland) { return(true); } } // Saves the changes to the database and closes the transaction tr.Commit(); } return(false); }
public static double FindHoleArea(ObjectId parcelId) { var polylineIds = CadUtils.FindAllPolylines(Application.DocumentManager.MdiActiveDocument); var datebase = Application.DocumentManager.MdiActiveDocument.Database; var dictionary = new Dictionary <ObjectId, double>(); using (var tr = datebase.TransactionManager.StartTransaction()) { // 读入多边形数据 var reader = new DwgReader(); var polygons = new List <IPolygon>(); var quadtree = new Quadtree <IGeometry>(); foreach (ObjectId polylineId in polylineIds) { var polygon = reader.ReadEntityAsPolygon(tr, polylineId) as IPolygon; if (polygon != null) { polygons.Add(polygon); quadtree.Insert(polygon.EnvelopeInternal, polygon); } } // 遍历多边形,如果有洞,开始计算 foreach (var polygon in polygons) { // 找洞 var insidePolygons = new List <IPolygon>(); foreach (var geom in quadtree.Query(polygon.EnvelopeInternal)) { var insidePolygon = geom as IPolygon; if (insidePolygon != null && polygon.Contains(insidePolygon) && !insidePolygon.Equals(polygon) && // 不是同一个 insidePolygon.UserData != polygon.UserData) { insidePolygons.Add(insidePolygon); } } // 算面积 var polygonId = (ObjectId)polygon.UserData; var linearRings = new List <ILinearRing>(); if (insidePolygons.Any()) { foreach (var insidePolygon in insidePolygons) { ILinearRing linearRing = reader.GeometryFactory.CreateLinearRing(insidePolygon.ExteriorRing.CoordinateSequence); if (!linearRing.IsCCW) { linearRing.Reverse(); } linearRings.Add(linearRing); } } IPolygon newPolygon = reader.GeometryFactory.CreatePolygon(polygon.Shell, linearRings.ToArray()); dictionary.Add(polygonId, newPolygon.Area); } tr.Commit(); } return(dictionary[parcelId]); }
public static List <ObjectId> FindPotentialHoles(Document document) { var polylineIds = CadUtils.FindAllPolylines(document); var datebase = document.Database; var hashSetObjIds = new HashSet <ObjectId>(); // 避免重复的数据,用hashset using (var tr = datebase.TransactionManager.StartTransaction()) { // 读入多边形数据 var reader = new DwgReader(); var polygons = new List <IPolygon>(); var quadtree = new Quadtree <IGeometry>(); var possibleHoleIds = new List <ObjectId>(); foreach (ObjectId polylineId in polylineIds) { var polygon = reader.ReadEntityAsPolygon(tr, polylineId) as IPolygon; if (polygon != null) { polygons.Add(polygon); quadtree.Insert(polygon.EnvelopeInternal, polygon); } // ObjectIds using (var ent = tr.GetObject(polylineId, OpenMode.ForRead) as Entity) { if (ent is Polyline2d || ent is Polyline) { // 如果是地块,直接跳过 string cassFlag = CadUtils.GetCassFlag(ent); if (CassFlagName.ToLower() != cassFlag.ToLower()) { possibleHoleIds.Add(polylineId); } } } } // 遍历多边形,如果有洞,开始计算 foreach (var polygon in polygons) { //看看是否包含洞 //quadtree.Query() // 找洞 foreach (var geom in quadtree.Query(polygon.EnvelopeInternal)) { var hole = geom as IPolygon; if (hole == null) { continue; } var holeId = (ObjectId)hole.UserData; if (possibleHoleIds.Contains(holeId) && // 不是潜在的地块 !hole.Equals(polygon) && // 不是同一个多边形 hole.UserData != polygon.UserData && // 不是自己 hole.Within(polygon)) // 有洞! { hashSetObjIds.Add(holeId); // } } } tr.Commit(); return(hashSetObjIds.ToList()); } }
public static void FindDanglingLine(Database database) { var polylineIds = CadUtils.FindAllPolylines(Application.DocumentManager.MdiActiveDocument); using (var tr = database.TransactionManager.StartTransaction()) { var pmFixed3 = new PrecisionModel(3); // 读入多边形数据 var lineStringList = new List <IGeometry>(); foreach (ObjectId polylineId in polylineIds) { var curve = tr.GetObject(polylineId, OpenMode.ForRead) as Curve; var reader = new DwgReader(); IGeometry lineString; try { lineString = reader.ReadCurveAsPolygon(tr, curve) as Polygon; } catch (Exception) { lineString = reader.ReadCurveAsLineString(tr, curve) as LineString; } if (lineString != null && lineString.IsEmpty == false) { lineString = SimpleGeometryPrecisionReducer.Reduce(lineString, pmFixed3); lineStringList.Add(lineString); } } // 开始做Union var nodedLineString = UnaryUnionOp.Union(lineStringList); var polygonizer = new Polygonizer(); polygonizer.Add(nodedLineString); var polys = polygonizer.GetPolygons(); var dangles = polygonizer.GetDangles(); var cuts = polygonizer.GetCutEdges(); var writer = new DwgWriter(); var modelSpaceId = SymbolUtilityServices.GetBlockModelSpaceId(database); var blockTableRecord = (BlockTableRecord)tr.GetObject(modelSpaceId, OpenMode.ForWrite, false); // 悬挂线 foreach (ILineString lineString in dangles) { //if (lineString != null) //{ // var polyline = writer.WritePolyline(lineString); // polyline.ColorIndex = 3; // //polyline.Layer = ""; // // 输出到CAD // blockTableRecord.AppendEntity(polyline); // tr.AddNewlyCreatedDBObject(polyline, true); //} } tr.Commit(); } }