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 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(); } }
public static Point3d?GetCentroid(Entity entity, Transaction tr) { Point3d?result = null; var reader = new DwgReader(); var geomerty = reader.ReadGeometry(entity, tr); var coords = NetTopologySuite.Algorithm.Centroid.GetCentroid(geomerty); if (coords != null) { result = new Point3d(coords.X, coords.Y, coords.Z); } return(result); }
public static IList <Point3d> GetMinimumDiameterRectangle(Entity entity, Transaction tr) { var reader = new DwgReader(); var geomerty = reader.ReadGeometry(entity, tr); var diameter = new NetTopologySuite.Algorithm.MinimumDiameter(geomerty); var rectangle = diameter.GetMinimumRectangle(); var coordinates = new List <Point3d>(); foreach (var coordinate in rectangle.Coordinates) { var point = new Point3d(coordinate.X, coordinate.Y, 0); coordinates.Add(point); } return(coordinates); }
public static void LineStringSelfIntersections(ObjectId polylineId) { 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 geometry = LineStringSelfIntersectionsOp(lineString); if (geometry == null || geometry.IsEmpty) { return; } var writer = new DwgWriter(); var modelSpaceId = SymbolUtilityServices.GetBlockModelSpaceId(database); var blockTableRecord = (BlockTableRecord)tr.GetObject(modelSpaceId, OpenMode.ForWrite, false); if (geometry.GeometryType == "Point") { DBPoint dbPt = writer.WriteDbPoint(geometry as Point); var mode = (short)Application.GetSystemVariable("pdmode"); if (mode == 0) { Application.SetSystemVariable("pdmode", 99); } dbPt.ColorIndex = 1; // 输出到CAD blockTableRecord.AppendEntity(dbPt); tr.AddNewlyCreatedDBObject(dbPt, true); } else if (geometry.GeometryType == "MultiPoint") { var multipPoint = geometry as MultiPoint; if (multipPoint == null) { return; } var dbPoints = writer.WriteDbPoints(multipPoint); foreach (DBPoint dbPt in dbPoints) { var mode = (short)Application.GetSystemVariable("pdmode"); if (mode == 0) { Application.SetSystemVariable("pdmode", 99); } dbPt.ColorIndex = 1; // 输出到CAD blockTableRecord.AppendEntity(dbPt); tr.AddNewlyCreatedDBObject(dbPt, true); } } tr.Commit(); } }
public static List <ObjectId> SplitPolygon(ObjectId plineId, ObjectId lineId) { var polyIds = new List <ObjectId>(); if (plineId.IsNull && lineId.IsNull) { return(polyIds); } var database = plineId.Database; using (var tr = database.TransactionManager.StartTransaction()) { var reader = new DwgReader() { PrecisionScale = 100000 }; var pline = tr.GetObject(plineId, OpenMode.ForRead) as Curve; if (pline == null || !pline.Closed) { return(polyIds); } var lineObj = tr.GetObject(lineId, OpenMode.ForRead) as Curve; if (lineObj == null) { return(polyIds); } var splitGeometry = reader.ReadCurveAsPolygon(tr, pline); splitGeometry.Buffer(0); var lineGeometry = reader.ReadGeometry(lineObj, tr); lineGeometry.Buffer(0); // 合并 var resultPolygons = SplitPolygon(splitGeometry, lineGeometry); var writer = new DwgWriter() { PrecisionScale = 100000 }; var multiPolygon = resultPolygons as MultiPolygon; if (multiPolygon != null) { var polylines = writer.WritePolyline(multiPolygon); // 加入数据库 foreach (var polyline in polylines) { var modelspaceId = SymbolUtilityServices.GetBlockModelSpaceId(database); var modelspace = (BlockTableRecord)tr.GetObject(modelspaceId, OpenMode.ForWrite); var polyId = modelspace.AppendEntity(polyline); tr.AddNewlyCreatedDBObject(polyline, true); polyIds.Add(polyId); } } tr.Commit(); } return(polyIds); }
public static List <ObjectId> PolygonizeLineStrings(Database database, IEnumerable <ObjectId> polylineIdCollection, String outLayerName = "0", AcadColor color = null, double precision = 0.00001) { var result = new List <ObjectId>(); using (var tr = database.TransactionManager.StartTransaction()) { // 读入多边形数据 var lineStringList = new List <IGeometry>(); foreach (ObjectId polylineId in polylineIdCollection) { var curve = tr.GetObject(polylineId, OpenMode.ForRead) as Curve; var reader = new DwgReader(); var lineString = reader.ReadGeometry(curve, tr) as LineString; if (lineString != null && lineString.IsEmpty == false) { lineStringList.Add(lineString); } } #if TryManualUnion // 创建节点 var nodedContours = new List <List <IGeometry> >(); var scale = 1.0 / precision; var geomNoder = new NetTopologySuite.Noding.Snapround.GeometryNoder(new PrecisionModel(scale)); var nodedList = geomNoder.Node(lineStringList); // 这里可能可以用nodedList.Count来计算一个gourp的size来限定线程数量 var maxCountInGroup = 200; foreach (var c in nodedList) { //Coordinate[] linePts = CoordinateArrays.RemoveRepeatedPoints(c.Coordinates); //if (linePts.Count() > 1) // nodedContours.Add(c.Factory.CreateLineString(linePts)); //c.Buffer(0.001); // 将所有的线段每maxCountInGroup个分成一组. var groupCount = nodedContours.Count; if (groupCount == 0) { nodedContours.Add(new List <IGeometry>()); } var itemCount = nodedContours[nodedContours.Count - 1].Count; if (itemCount < maxCountInGroup) { nodedContours[nodedContours.Count - 1].Add(c); } else { nodedContours.Add(new List <IGeometry>()); nodedContours[nodedContours.Count - 1].Add(c); } } var workers = new List <Worker>(); var threadList = new List <Thread>(); // 为每组geometry开一个线程做union. foreach (List <IGeometry> nodedContour in nodedContours) { // Start a thread. var worker = new Worker(nodedContour); workers.Add(worker); var thread = new Thread(worker.Execute); threadList.Add(thread); thread.Start(); } // 等待所有线程运行结束 foreach (Thread thread in threadList) { thread.Join(); } // 最后将每组union得到的IGeometry再做一次union var nodedLineString = workers[0].Geometry; for (int i = 1; i < workers.Count; i++) { nodedLineString = nodedLineString.Union(workers[i].Geometry); } #else // 开始做Union var nodedLineString = UnaryUnionOp.Union(lineStringList, new GeometryFactory(new PrecisionModel(0.9d))); //var nodedLineString = lineStringList[0]; //for (int i = 1; i < lineStringList.Count; i++) //{ // nodedLineString = nodedLineString.Union(lineStringList[i]); //} #endif //造区 Polygonizer 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 (IGeometry geometry in polys) { var polygon = geometry as Polygon; if (polygon != null) { var polylines = writer.WritePolyline(polygon); foreach (Polyline polyline in polylines) { if (color != null) { polyline.Color = color; } polyline.Layer = outLayerName; // 输出到CAD var polylineId = blockTableRecord.AppendEntity(polyline); result.Add(polylineId); tr.AddNewlyCreatedDBObject(polyline, true); } } } //// 悬挂线 //foreach (ILineString lineString in dangles) //{ // if (lineString != null) // { // var polyline = writer.WritePolyline(lineString); // if (color != null) // polyline.Color = color; // polyline.Layer = outLayerName; // // 输出到CAD // blockTableRecord.AppendEntity(polyline); // tr.AddNewlyCreatedDBObject(polyline, true); // } //} //// 裁剪线 //foreach (ILineString lineString in cuts) //{ // if (lineString != null) // { // var polyline = writer.WritePolyline(lineString); // if (color != null) // polyline.Color = color; // polyline.Layer = outLayerName; // // 输出到CAD // blockTableRecord.AppendEntity(polyline); // tr.AddNewlyCreatedDBObject(polyline, true); // } //} tr.Commit(); } return(result); }