public static ObjectId FindParcelOfHole(Transaction tr, Entity hole) { var entityIds = GroupUtils.GetGroupedObjects(tr, hole); ObjectId entityId = ObjectId.Null; foreach (var objectId in entityIds) { if (objectId == hole.Id) { continue; } using (var ent = tr.GetObject(objectId, OpenMode.ForRead)) { if (ent is Polyline2d || ent is Polyline) { string cassFlag = CadUtils.GetCassFlag(ent); if (CassFlagName.ToLower() == cassFlag.ToLower()) { entityId = objectId; break; } } } } return(entityId); }
public static void CreateBuffer(ObjectId polylineId, double bufferDist) { var database = polylineId.Database; using (var tr = database.TransactionManager.StartTransaction()) { var curve = tr.GetObject(polylineId, OpenMode.ForRead) as Curve; // 读取CAD图元 var reader = new DwgReader(); var lineString = reader.ReadGeometry(curve, tr) as LineString; // 做出缓冲区 var buffer = lineString.Buffer(bufferDist) as Geometry; if (buffer.GeometryType == "Polygon") { var writer = new DwgWriter(); Polyline[] polylines = writer.WritePolyline(buffer as IPolygon); // 输出到CAD foreach (var polyline in polylines) { CadUtils.AddToCurrentDb(tr, database, polyline); } } tr.Commit(); } }
//[CommandMethod("SDKCTX")] public static void FindRemoveAreaPolyline() { Document document = Application.DocumentManager.MdiActiveDocument; var holes = FindPotentialHoles(document); if (!holes.Any()) { document.Editor.WriteMessage("不存在孔洞多边形"); return; } document.Editor.WriteMessage("查找到{0}个孔洞多边形", holes.Count); var holeIds = holes.ToArray(); Extents3d?extents = CadUtils.SafeGetGeometricExtents(holeIds); if (extents.HasValue) { CadUtils.ZoomToWin1(document.Editor, extents.Value, 1.2); } Autodesk.AutoCAD.Internal.Utils.SelectObjects(holeIds); //var ptopts = new PromptKeywordOptions("\n是否将这些多边形设定为扣除面积的图形(Y)?[是(Y)/否(N)]:", "Yes No") { AllowNone = true }; //var ptRes = document.Editor.DoPrompt(ptopts); //if (ptRes.Status == PromptStatus.OK && ptRes.StringResult == "Yes") //{ // SetHolePolygons(document.Database, holeIds); //} }
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, }); } }
static bool IsPolygonHasHole(Transaction tr, Entity entity) { ObjectId[] entityIds = GroupUtils.GetGroupedObjects(tr, entity); foreach (ObjectId entityId in entityIds) { // 如果是本地块,直接skip if (entity.ObjectId == entityId) { continue; } using (var ent = tr.GetObject(entityId, OpenMode.ForRead)) { if (ent is Polyline2d || ent is Polyline) { string cassFlag = CadUtils.GetCassFlag(ent); if (CassFlagIsland.ToLower() == cassFlag.ToLower()) { return(true); } } } } return(false); }
public static void Touch(ObjectId[] polylineIds) { var database = polylineIds[0].Database; using (var tr = database.TransactionManager.StartTransaction()) { foreach (var polylineId in polylineIds) { var curve = tr.GetObject(polylineId, OpenMode.ForRead) as Curve; // 读取CAD图元 var reader = new DwgReader(); var lineString = reader.ReadGeometry(curve, tr) as LineString; //lineString.Touches() // 做出缓冲区 var geometry = LineStringSelfIntersectionsOp(lineString); if (geometry.GeometryType == "Point") { var writer = new DwgWriter(); DBPoint dbPt = writer.WriteDbPoint(geometry as Point); CadUtils.DrawPoint(tr, database, dbPt); } } tr.Commit(); } }
public static void FindOverlapingPolylines() { var document = Application.DocumentManager.MdiActiveDocument; var polylines = CadUtils.FindAllPolylines(document); FindOverlapingPolylines(polylines); }
public static bool IsHoleReferenced(Entity ent, Transaction tr) { if (ent is Polyline2d || ent is Polyline) { // 如果不是flag,没有被引用 string cassFlag = CadUtils.GetCassFlag(ent); if (CassFlagIsland.ToLower() != cassFlag.ToLower()) { return(false); } var groupId = GroupUtils.FindEntityGroupId(tr, ent); if (!groupId.IsValid) { return(false); } // 如果不是flag,没有被引用 //ObjectId[] entityIds = GroupUtils.GetGroupedObjects(ent); //foreach (ObjectId entityId in entityIds) //{ //} var realId = ParcelPolygon.FindParcelOfHole(tr, ent); if (realId.IsNull) { return(false); } return(true); } return(false); }
static Dictionary <ObjectId, IList <ObjectId> > GetPolygonHasHole(Database database, Quadtree <IGeometry> quadtree, IEnumerable <IPolygon> polygons) { var dictionary = new Dictionary <ObjectId, IList <ObjectId> >(); var possibleHoleIds = new List <ObjectId>(); using (var tr = database.TransactionManager.StartTransaction()) { // 便利多边形,如果有洞,开始计算 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) { var objectId = (ObjectId)insidePolygon.UserData; var dbObj = tr.GetObject(objectId, OpenMode.ForRead); // 必须是孔洞 if (CadUtils.GetCassFlag(dbObj) == CassFlagIsland) { if (polygon.Contains(insidePolygon) && !insidePolygon.Equals(polygon) && insidePolygon.UserData != polygon.UserData) { insidePolygons.Add(insidePolygon); possibleHoleIds.Add(objectId); } } } } // 算面积 var polygonId = (ObjectId)polygon.UserData; if (insidePolygons.Any()) { var objectIds = new List <ObjectId>(); foreach (var insidePolygon in insidePolygons) { objectIds.Add((ObjectId)insidePolygon.UserData); } dictionary.Add(polygonId, objectIds); } } } return(dictionary); }
public static void FindTouchedEdge(ObjectId[] polylineIds) { if (polylineIds.Length != 2) { return; } //var ids = CadUtils.FindAllPolylines(Application.DocumentManager.MdiActiveDocument); var database = Application.DocumentManager.MdiActiveDocument.Database; using (var tr = database.TransactionManager.StartTransaction()) { var reader = new DwgReader(); var entity1 = tr.GetObject(polylineIds[0], OpenMode.ForRead) as Curve; var lineString1 = reader.ReadCurveAsLineString(tr, entity1); lineString1.UserData = polylineIds[0]; var entity2 = tr.GetObject(polylineIds[1], OpenMode.ForRead) as Curve; var lineString2 = reader.ReadCurveAsLineString(tr, entity2); lineString2.UserData = polylineIds[1]; var isTouch = lineString1.Touches(lineString2); System.Diagnostics.Trace.WriteLine(isTouch); IGeometry intersections = lineString1.Intersection(lineString2); //IGeometry intersections = CommonUtils.GetIntersectionPoints(lineString, polygon1); if (intersections.Coordinates.Any()) { foreach (var Coordinate in intersections.Coordinates) { var dbpoint = new DBPoint(new Point3d(Coordinate.X, Coordinate.Y, 0)); CadUtils.DrawPoint(tr, database, dbpoint); } // Draw all var d = new DBPoint(new Point3d(intersections.Coordinates[0].X, intersections.Coordinates[0].Y, 0)); CadUtils.DrawPoint(tr, database, d, 2); var lastIndex = intersections.Coordinates[intersections.Coordinates.Length - 1]; var d1 = new DBPoint(new Point3d(lastIndex.X, lastIndex.Y, 0)); CadUtils.DrawPoint(tr, database, d1, 2); } tr.Commit(); } }
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 int SetHolePolygons(Database database, ObjectId[] holeIds) { int count = 0; using (var tr = database.TransactionManager.StartTransaction()) { foreach (var objectId in holeIds) { var hole = tr.GetObject(objectId, OpenMode.ForWrite) as Polyline; if (hole != null) { CadUtils.SetCassFlag(hole, CassFlagIsland); count++; } } // Saves the changes to the database and closes the transaction tr.Commit(); } return(count); }
public static ObjectId[] FindHolesInEntity(Transaction tr, Entity entity) { ObjectId[] entityIds = GroupUtils.GetGroupedObjects(tr, entity); var holeIds = new List <ObjectId>(); foreach (ObjectId entityId in entityIds) { using (var ent = tr.GetObject(entityId, OpenMode.ForRead)) { if (ent is Polyline2d || ent is Polyline) { string cassFlag = CadUtils.GetCassFlag(ent); if (CassFlagIsland.ToLower() == cassFlag.ToLower()) { holeIds.Add(entityId); } } } } return(holeIds.ToArray()); }
static void AddHolesToParcel(ObjectId parcelId, ObjectId groupId, ObjectId[] holeIds) { Database database = parcelId.Database; // 将扣除的孔洞等图形放到孔洞图层,以绿色醒目显示 var layerId = LayerUtils.GetLayerByName(database, "孔洞"); if (!layerId.IsValid) { layerId = LayerUtils.AddNewLayer(database, "孔洞", "Continuous", /*GREEN*/ 3); } // 看看选择的地块是不是真的在地块里面了 var availableHoles = new List <ObjectId>(); using (var tr = database.TransactionManager.StartTransaction()) { var reader = new DwgReader(); var curve = tr.GetObject(parcelId, OpenMode.ForRead) as Curve; if (curve != null && curve.Closed) { var polygon1 = reader.ReadCurveAsPolygon(tr, curve); foreach (var holeId in holeIds) { var hole = tr.GetObject(holeId, OpenMode.ForRead) as Curve; // 只添加地块为封闭的 if (hole == null || !hole.Closed) { continue; } // 继续,看看是不是在地块中间,不在就跳过 var polygon2 = reader.ReadCurveAsPolygon(tr, hole); if (!polygon1.Contains(polygon2)) { continue; } // 如果是,添加地块 hole.UpgradeOpen(); CadUtils.SetCassFlag(hole, CassFlagIsland); hole.LayerId = layerId; availableHoles.Add(holeId); // 如果组不是空,直接加入组 if (!groupId.IsNull) { GroupUtils.AppendEntityIntoGroup(database, groupId, holeId); } } } tr.Commit(); } // 如果没有group,创建group if (groupId.IsNull) { var ids = new ObjectIdCollection() { parcelId }; foreach (var availableHole in availableHoles) { ids.Add(availableHole); } GroupUtils.CreateNewGroup(database, "*A", ids); } }
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]); }
private static PolygonOverlaps FindOverlappingGeometries(FindOverlap findOverlap, TopologyData topoData, ObjectId[] objectIds) { var overlap = new PolygonOverlaps(); var handledPairs = new HashSet <string>(); var count = 0; foreach (var geom in topoData.Geometries) { // 先用NTS建立拓扑,初查,哪些是重叠的,非常快速。 var nearGeoms = GetNearGeometries(geom, topoData.Quadtree); var thisObjId = (ObjectId)geom.UserData; var ids = new ObjectIdCollection(); foreach (var geometry in nearGeoms) { var nearObjId = (ObjectId)geometry.UserData; if (nearObjId != thisObjId) { // 记录当前处理的object对,这样避免重复计算一对相邻的多边形 var handle = thisObjId + "_" + nearObjId; var handle2 = nearObjId + "_" + thisObjId; //var thisGeometry = topoData.GeometryDictionary[thisObjId]; if (!handledPairs.Contains(handle) && !handledPairs.Contains(handle2)) { // 先记录下来,处理过的,后面就不再处理了 handledPairs.Add(handle); // var nearGeometry = topoData.GeometryDictionary[nearObjId]; try { //var intersect = thisGeometry.Overlaps(nearGeometry); //if (intersect) { count++; // 在NTS里面也许是接边的也算重叠的 // 再通过ACAD面域的计算,相交,如果有交集,则认为是有重叠的,而且画出交集。 var regionIntersection = CadUtils.GetIntersectionPart(thisObjId, nearObjId); //var region = findOverlap.GetIntersectionPart(thisObjId, nearObjId); if (regionIntersection != null) { // 相交了,有相交的Region if (regionIntersection.ObjectId1.IsNull) { ids.Add(nearObjId); overlap.GeometryOverlaps.Add(new GeometryOverlap() { ThisGeometry = thisObjId, ThatGeometry = nearObjId, IntersectRegion = regionIntersection.Region }); } // 没有相交,可能是造区错误 else if (regionIntersection.ObjectId1.IsNull) { overlap.CannotCreateRegions.Add(regionIntersection.ObjectId1); } // Object1/Object2 // 造区直接boolean运算发生错误 // 面域上的布尔运算失败 不同体顶点的重合 face_face_ints else if (!regionIntersection.ObjectId1.IsNull && !regionIntersection.ObjectId2.IsNull) { overlap.CannotBooleanRegions.Add( new KeyValuePair <ObjectId, ObjectId>(regionIntersection.ObjectId1, regionIntersection.ObjectId2)); } } } } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex.Message); } } } } } System.Diagnostics.Trace.WriteLine(count); return(overlap); }
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(); } }