123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- 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<string> dgxlist;
- public static List<string> rvrdlist;
- public static List<string> 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<string> 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<double> paramList = new List<double>();
- // foreach (Point3d breakPoint in pts)
- // {
- // double param = pll.GetParameterAtPoint(breakPoint);
- // paramList.Add(param);
- // }
- // paramList.Sort();
-
- // //存储分割后的多段线
- // List<Polyline> newPlls = new List<Polyline>();
-
- // // 初始化第一条多段线
- // 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<double> paramList = new List<double>();
- List<double> kzparam = new List<double>();
- foreach (Point3d breakPoint in pts)
- {
- double param = pll.GetParameterAtPoint(breakPoint);
- if (pts1.Contains(breakPoint))
- kzparam.Add(param);
- paramList.Add(param);
- }
- paramList.Sort();
-
- //存储分割后的多段线
- List<Polyline> newPlls = new List<Polyline>();
-
- // 初始化第一条多段线
- 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 = "待删除等高线";
- }
- }
- }
-
-
- }
- }
|