using GrxCAD.DatabaseServices; using GrxCAD.EditorInput; using GrxCAD.Geometry; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HCTools { class Dgxthrorvrd { public static List dgxlist; public static List rvrdlist; public static List kzlist; public static int blc; public void DgxCut() { string dgxname= ""; Database db = GrxCAD.DatabaseServices.HostApplicationServices.WorkingDatabase; using (Transaction trans = db.TransactionManager.StartTransaction()) { Editor ed = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; LayerTable layerTable = trans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable; for (int i = 0; i < dgxlist.Count; i++) { if (i == 0) dgxname = dgxlist[i]; else dgxname = dgxname + "," + dgxlist[i]; } TypedValue[] value = new TypedValue[] { new TypedValue((int)DxfCode.LayerName,dgxname) }; SelectionFilter filter = new SelectionFilter(value); PromptSelectionResult psr = ed.SelectAll(filter); if (psr.Status == PromptStatus.OK) { SelectionSet ss = psr.Value; ObjectIdCollection idcoll = new ObjectIdCollection(ss.GetObjectIds()); for (int ii = 0; ii < idcoll.Count; ii++) { Polyline pll = trans.GetObject(idcoll[ii], OpenMode.ForWrite) as Polyline; Intersectcheck(pll, ed, trans, db); } } trans.Commit(); } dgxlist.Clear(); rvrdlist.Clear(); kzlist.Clear(); } private void Intersectcheck(Polyline pll, Editor ed, Transaction trans,Database db) { var plane = new Plane(Point3d.Origin, Vector3d.ZAxis); Point3dCollection pts = new Point3dCollection(); Point3dCollection pts1 = new Point3dCollection(); Point3dCollection kz_pts = new Point3dCollection(); string rvrdname = ""; string kzname = ""; //为坎子让出的长度 double cut_dist = 0.5; if (blc == 1000) cut_dist = cut_dist * 2; if (blc == 2000) cut_dist = cut_dist * 4; if (blc == 5000) cut_dist = cut_dist * 10; if (blc == 10000) cut_dist = cut_dist * 20; //添加道路河流房屋的图层名并搜索相应图层上的实体 if (rvrdlist != null) { if (kzlist != null) { rvrdlist.AddRange(kzlist); } List lyrname = rvrdlist; for (int i = 0; i < lyrname.Count; i++) { if (i == 0) rvrdname = lyrname[i]; else rvrdname = rvrdname + "," + lyrname[i]; } TypedValue[] value = new TypedValue[] { new TypedValue((int)DxfCode.LayerName,rvrdname) }; SelectionFilter filter = new SelectionFilter(value); PromptSelectionResult psr = ed.SelectAll(filter); if (psr.Status == PromptStatus.OK) { //创建新图层 LayerControl layerscontrol = new LayerControl(); string layname = "待删除等高线"; if (!layerscontrol.haslayername(layname)) { colorgb col = new colorgb(255, 0, 255); layerscontrol.creatlayer(layname, col); layerscontrol.movelayertofront(layname); } else layerscontrol.movelayertofront(layname); SelectionSet ss = psr.Value; ObjectIdCollection idcoll = new ObjectIdCollection(ss.GetObjectIds()); for (int i = 0; i < idcoll.Count; i++) { Polyline pll_rvrd = trans.GetObject(idcoll[i], OpenMode.ForWrite) as Polyline; Point3dCollection intersectionPoints = new Point3dCollection(); pll.IntersectWith(pll_rvrd, Intersect.OnBothOperands, plane, intersectionPoints, IntPtr.Zero, IntPtr.Zero); if (intersectionPoints.Count > 0) { foreach (Point3d pt in intersectionPoints) { pts.Add(pt); } } } } } //添加坎子图层名并搜索相应图层上的实体 if (kzlist != null) { for (int i = 0; i < kzlist.Count; i++) { if (i == 0) kzname = kzlist[i]; else kzname = kzname + "," + kzlist[i]; } TypedValue[] value_kz = new TypedValue[] { new TypedValue((int)DxfCode.LayerName,kzname) }; SelectionFilter filter_kz = new SelectionFilter(value_kz); PromptSelectionResult psr_kz = ed.SelectAll(filter_kz); if (psr_kz.Status == PromptStatus.OK) { SelectionSet ss = psr_kz.Value; ObjectIdCollection idcoll = new ObjectIdCollection(ss.GetObjectIds()); for (int i = 0; i < idcoll.Count; i++) { Polyline pll_rvrd = trans.GetObject(idcoll[i], OpenMode.ForWrite) as Polyline; Point3dCollection intersectionPoints = new Point3dCollection(); pll.IntersectWith(pll_rvrd, Intersect.OnBothOperands, plane, intersectionPoints, IntPtr.Zero, IntPtr.Zero); if (intersectionPoints.Count > 0) { foreach (Point3d pt in intersectionPoints) { pts1.Add(pt); } } } #region //if (pts.Count > 0) //{ // //按参数值排序交点 // List paramList = new List(); // foreach (Point3d breakPoint in pts) // { // double param = pll.GetParameterAtPoint(breakPoint); // paramList.Add(param); // } // paramList.Sort(); // //存储分割后的多段线 // List newPlls = new List(); // // 初始化第一条多段线 // Polyline currentPline = new Polyline(); // currentPline.SetDatabaseDefaults(); // currentPline.Layer = pll.Layer; // currentPline.Color = pll.Color; // // 遍历多段线的节点 // for (int i = 0; i < pll.NumberOfVertices; i++) // { // Point2d point = pll.GetPoint2dAt(i); // double bulge = pll.GetBulgeAt(i); // // 添加节点到当前多段线 // currentPline.AddVertexAt(currentPline.NumberOfVertices, point, bulge, 0, 0); // // 检查是否需要分割 // if (paramList.Count > 0 && i == (int)paramList[0]) // { // // 检查当前节点和下一个节点之间是否有交点 // while (paramList.Count > 0 && paramList[0] >= i && paramList[0] < i + 1) // { // // 计算交点处的精确位置 // double param = paramList[0]; // double dist = pll.GetDistanceAtParameter(param)- cut_dist; // Point3d splitPoint3d = pll.GetPointAtDist(dist); // Point2d splitPoint = new Point2d(splitPoint3d.X, splitPoint3d.Y); // kz_pts.Add(splitPoint3d); // // 添加当前多段线到结果列表 // currentPline.AddVertexAt(currentPline.NumberOfVertices, splitPoint, 0, 0, 0); // BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead)); // BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); // btr.AppendEntity(currentPline); // trans.AddNewlyCreatedDBObject(currentPline, true); // newPlls.Add(currentPline); // // 初始化下一条多段线 // currentPline = new Polyline(); // currentPline.SetDatabaseDefaults(); // currentPline.Layer = pll.Layer; // currentPline.Color = pll.Color; // // 添加分割点作为下一条多段线的起点 // double dist1 = pll.GetDistanceAtParameter(param) + cut_dist; // Point3d splitPoint3d1 = pll.GetPointAtDist(dist1); // Point2d splitPoint1 = new Point2d(splitPoint3d1.X, splitPoint3d1.Y); // kz_pts.Add(splitPoint3d1); // currentPline.AddVertexAt(0, splitPoint1, 0, 0, 0); // // 移除已处理的交点 // paramList.RemoveAt(0); // } // } // } // if (currentPline.NumberOfVertices > 0) // { // BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead)); // BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); // btr.AppendEntity(currentPline); // trans.AddNewlyCreatedDBObject(currentPline, true); // newPlls.Add(currentPline); // } // pll.Erase(); // for (int i = 0; i < newPlls.Count; i++) // { // if (kz_pts.Contains(newPlls[i].StartPoint) && // kz_pts.Contains(newPlls[i].EndPoint) && // i % 2 == 1) // newPlls[i].Layer = "待删除等高线"; // else if (newPlls.Count == 2) // { // if (newPlls[0].Length < newPlls[1].Length) // newPlls[0].Layer = "待删除等高线"; // else // newPlls[1].Layer = "待删除等高线"; // } // } //} #endregion } } if (pts.Count > 0) { //创建新图层 LayerControl layerscontrol = new LayerControl(); string layname = "待删除等高线"; if (!layerscontrol.haslayername(layname)) { colorgb col = new colorgb(255, 0, 255); layerscontrol.creatlayer(layname, col); layerscontrol.movelayertofront(layname); } else layerscontrol.movelayertofront(layname); //按参数值排序交点 List paramList = new List(); List kzparam = new List(); foreach (Point3d breakPoint in pts) { double param = pll.GetParameterAtPoint(breakPoint); if (pts1.Contains(breakPoint)) kzparam.Add(param); paramList.Add(param); } paramList.Sort(); //存储分割后的多段线 List newPlls = new List(); // 初始化第一条多段线 Polyline currentPline = new Polyline(); currentPline.SetDatabaseDefaults(); currentPline.Layer = pll.Layer; currentPline.Color = pll.Color; // 遍历多段线的节点 for (int i = 0; i < pll.NumberOfVertices; i++) { Point2d point = pll.GetPoint2dAt(i); double bulge = pll.GetBulgeAt(i); // 添加节点到当前多段线 currentPline.AddVertexAt(currentPline.NumberOfVertices, point, bulge, 0, 0); // 检查是否需要分割 if (paramList.Count > 0 && i == (int)paramList[0]) { // 检查当前节点和下一个节点之间是否有交点 while (paramList.Count > 0 && paramList[0] >= i && paramList[0] < i + 1) { // 计算交点处的精确位置 double param = paramList[0]; double dist; if (kzparam.Contains(param)) { dist = pll.GetDistanceAtParameter(param) - cut_dist; kz_pts.Add(pll.GetPointAtDist(dist)); } else dist = pll.GetDistanceAtParameter(param); Point3d splitPoint3d = pll.GetPointAtDist(dist); Point2d splitPoint = new Point2d(splitPoint3d.X, splitPoint3d.Y); // 添加当前多段线到结果列表 currentPline.AddVertexAt(currentPline.NumberOfVertices, splitPoint, 0, 0, 0); BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead)); BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); btr.AppendEntity(currentPline); trans.AddNewlyCreatedDBObject(currentPline, true); newPlls.Add(currentPline); // 初始化下一条多段线 currentPline = new Polyline(); currentPline.SetDatabaseDefaults(); currentPline.Layer = pll.Layer; currentPline.Color = pll.Color; // 添加分割点作为下一条多段线的起点 if (kzparam.Contains(param)) { double dist1 = pll.GetDistanceAtParameter(param) + cut_dist; Point3d splitPoint3d1 = pll.GetPointAtDist(dist1); Point2d splitPoint1 = new Point2d(splitPoint3d1.X, splitPoint3d1.Y); kz_pts.Add(splitPoint3d1); currentPline.AddVertexAt(0, splitPoint1, 0, 0, 0); } else currentPline.AddVertexAt(0, splitPoint, 0, 0, 0); // 移除已处理的交点 paramList.RemoveAt(0); } } } if (currentPline.NumberOfVertices > 0) { BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead)); BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); btr.AppendEntity(currentPline); trans.AddNewlyCreatedDBObject(currentPline, true); newPlls.Add(currentPline); } pll.Erase(); for (int i = 0; i < newPlls.Count; i++) { if (newPlls.Count == 2) { if (newPlls[0].Length < newPlls[1].Length) newPlls[0].Layer = "待删除等高线"; else newPlls[1].Layer = "待删除等高线"; } else if (pts.Contains(newPlls[i].StartPoint) && pts.Contains(newPlls[i].EndPoint) && i % 2 == 1) newPlls[i].Layer = "待删除等高线"; else if (kz_pts.Contains(newPlls[i].StartPoint) && kz_pts.Contains(newPlls[i].EndPoint) && i % 2 == 1) newPlls[i].Layer = "待删除等高线"; } } } } }