工具箱相关
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Dgxthrorvrd.cs 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. using GrxCAD.DatabaseServices;
  2. using GrxCAD.EditorInput;
  3. using GrxCAD.Geometry;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace HCTools
  10. {
  11. class Dgxthrorvrd
  12. {
  13. public static List<string> dgxlist;
  14. public static List<string> rvrdlist;
  15. public static List<string> kzlist;
  16. public static int blc;
  17. public void DgxCut()
  18. {
  19. string dgxname= "";
  20. Database db = GrxCAD.DatabaseServices.HostApplicationServices.WorkingDatabase;
  21. using (Transaction trans = db.TransactionManager.StartTransaction())
  22. {
  23. Editor ed = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  24. LayerTable layerTable = trans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
  25. for (int i = 0; i < dgxlist.Count; i++)
  26. {
  27. if (i == 0)
  28. dgxname = dgxlist[i];
  29. else
  30. dgxname = dgxname + "," + dgxlist[i];
  31. }
  32. TypedValue[] value = new TypedValue[]
  33. {
  34. new TypedValue((int)DxfCode.LayerName,dgxname)
  35. };
  36. SelectionFilter filter = new SelectionFilter(value);
  37. PromptSelectionResult psr = ed.SelectAll(filter);
  38. if (psr.Status == PromptStatus.OK)
  39. {
  40. SelectionSet ss = psr.Value;
  41. ObjectIdCollection idcoll = new ObjectIdCollection(ss.GetObjectIds());
  42. for (int ii = 0; ii < idcoll.Count; ii++)
  43. {
  44. Polyline pll = trans.GetObject(idcoll[ii], OpenMode.ForWrite) as Polyline;
  45. Intersectcheck(pll, ed, trans, db);
  46. }
  47. }
  48. trans.Commit();
  49. }
  50. dgxlist.Clear();
  51. rvrdlist.Clear();
  52. kzlist.Clear();
  53. }
  54. private void Intersectcheck(Polyline pll, Editor ed, Transaction trans,Database db)
  55. {
  56. var plane = new Plane(Point3d.Origin, Vector3d.ZAxis);
  57. Point3dCollection pts = new Point3dCollection();
  58. Point3dCollection pts1 = new Point3dCollection();
  59. Point3dCollection kz_pts = new Point3dCollection();
  60. string rvrdname = "";
  61. string kzname = "";
  62. //为坎子让出的长度
  63. double cut_dist = 0.5;
  64. if (blc == 1000)
  65. cut_dist = cut_dist * 2;
  66. if (blc == 2000)
  67. cut_dist = cut_dist * 4;
  68. if (blc == 5000)
  69. cut_dist = cut_dist * 10;
  70. if (blc == 10000)
  71. cut_dist = cut_dist * 20;
  72. //添加道路河流房屋的图层名并搜索相应图层上的实体
  73. if (rvrdlist != null)
  74. {
  75. if (kzlist != null)
  76. {
  77. rvrdlist.AddRange(kzlist);
  78. }
  79. List<string> lyrname = rvrdlist;
  80. for (int i = 0; i < lyrname.Count; i++)
  81. {
  82. if (i == 0)
  83. rvrdname = lyrname[i];
  84. else
  85. rvrdname = rvrdname + "," + lyrname[i];
  86. }
  87. TypedValue[] value = new TypedValue[]
  88. {
  89. new TypedValue((int)DxfCode.LayerName,rvrdname)
  90. };
  91. SelectionFilter filter = new SelectionFilter(value);
  92. PromptSelectionResult psr = ed.SelectAll(filter);
  93. if (psr.Status == PromptStatus.OK)
  94. {
  95. //创建新图层
  96. LayerControl layerscontrol = new LayerControl();
  97. string layname = "待删除等高线";
  98. if (!layerscontrol.haslayername(layname))
  99. {
  100. colorgb col = new colorgb(255, 0, 255);
  101. layerscontrol.creatlayer(layname, col);
  102. layerscontrol.movelayertofront(layname);
  103. }
  104. else
  105. layerscontrol.movelayertofront(layname);
  106. SelectionSet ss = psr.Value;
  107. ObjectIdCollection idcoll = new ObjectIdCollection(ss.GetObjectIds());
  108. for (int i = 0; i < idcoll.Count; i++)
  109. {
  110. Polyline pll_rvrd = trans.GetObject(idcoll[i], OpenMode.ForWrite) as Polyline;
  111. Point3dCollection intersectionPoints = new Point3dCollection();
  112. pll.IntersectWith(pll_rvrd, Intersect.OnBothOperands, plane, intersectionPoints, IntPtr.Zero, IntPtr.Zero);
  113. if (intersectionPoints.Count > 0)
  114. {
  115. foreach (Point3d pt in intersectionPoints)
  116. {
  117. pts.Add(pt);
  118. }
  119. }
  120. }
  121. }
  122. }
  123. //添加坎子图层名并搜索相应图层上的实体
  124. if (kzlist != null)
  125. {
  126. for (int i = 0; i < kzlist.Count; i++)
  127. {
  128. if (i == 0)
  129. kzname = kzlist[i];
  130. else
  131. kzname = kzname + "," + kzlist[i];
  132. }
  133. TypedValue[] value_kz = new TypedValue[]
  134. {
  135. new TypedValue((int)DxfCode.LayerName,kzname)
  136. };
  137. SelectionFilter filter_kz = new SelectionFilter(value_kz);
  138. PromptSelectionResult psr_kz = ed.SelectAll(filter_kz);
  139. if (psr_kz.Status == PromptStatus.OK)
  140. {
  141. SelectionSet ss = psr_kz.Value;
  142. ObjectIdCollection idcoll = new ObjectIdCollection(ss.GetObjectIds());
  143. for (int i = 0; i < idcoll.Count; i++)
  144. {
  145. Polyline pll_rvrd = trans.GetObject(idcoll[i], OpenMode.ForWrite) as Polyline;
  146. Point3dCollection intersectionPoints = new Point3dCollection();
  147. pll.IntersectWith(pll_rvrd, Intersect.OnBothOperands, plane, intersectionPoints, IntPtr.Zero, IntPtr.Zero);
  148. if (intersectionPoints.Count > 0)
  149. {
  150. foreach (Point3d pt in intersectionPoints)
  151. {
  152. pts1.Add(pt);
  153. }
  154. }
  155. }
  156. #region
  157. //if (pts.Count > 0)
  158. //{
  159. // //按参数值排序交点
  160. // List<double> paramList = new List<double>();
  161. // foreach (Point3d breakPoint in pts)
  162. // {
  163. // double param = pll.GetParameterAtPoint(breakPoint);
  164. // paramList.Add(param);
  165. // }
  166. // paramList.Sort();
  167. // //存储分割后的多段线
  168. // List<Polyline> newPlls = new List<Polyline>();
  169. // // 初始化第一条多段线
  170. // Polyline currentPline = new Polyline();
  171. // currentPline.SetDatabaseDefaults();
  172. // currentPline.Layer = pll.Layer;
  173. // currentPline.Color = pll.Color;
  174. // // 遍历多段线的节点
  175. // for (int i = 0; i < pll.NumberOfVertices; i++)
  176. // {
  177. // Point2d point = pll.GetPoint2dAt(i);
  178. // double bulge = pll.GetBulgeAt(i);
  179. // // 添加节点到当前多段线
  180. // currentPline.AddVertexAt(currentPline.NumberOfVertices, point, bulge, 0, 0);
  181. // // 检查是否需要分割
  182. // if (paramList.Count > 0 && i == (int)paramList[0])
  183. // {
  184. // // 检查当前节点和下一个节点之间是否有交点
  185. // while (paramList.Count > 0 && paramList[0] >= i && paramList[0] < i + 1)
  186. // {
  187. // // 计算交点处的精确位置
  188. // double param = paramList[0];
  189. // double dist = pll.GetDistanceAtParameter(param)- cut_dist;
  190. // Point3d splitPoint3d = pll.GetPointAtDist(dist);
  191. // Point2d splitPoint = new Point2d(splitPoint3d.X, splitPoint3d.Y);
  192. // kz_pts.Add(splitPoint3d);
  193. // // 添加当前多段线到结果列表
  194. // currentPline.AddVertexAt(currentPline.NumberOfVertices, splitPoint, 0, 0, 0);
  195. // BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));
  196. // BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  197. // btr.AppendEntity(currentPline);
  198. // trans.AddNewlyCreatedDBObject(currentPline, true);
  199. // newPlls.Add(currentPline);
  200. // // 初始化下一条多段线
  201. // currentPline = new Polyline();
  202. // currentPline.SetDatabaseDefaults();
  203. // currentPline.Layer = pll.Layer;
  204. // currentPline.Color = pll.Color;
  205. // // 添加分割点作为下一条多段线的起点
  206. // double dist1 = pll.GetDistanceAtParameter(param) + cut_dist;
  207. // Point3d splitPoint3d1 = pll.GetPointAtDist(dist1);
  208. // Point2d splitPoint1 = new Point2d(splitPoint3d1.X, splitPoint3d1.Y);
  209. // kz_pts.Add(splitPoint3d1);
  210. // currentPline.AddVertexAt(0, splitPoint1, 0, 0, 0);
  211. // // 移除已处理的交点
  212. // paramList.RemoveAt(0);
  213. // }
  214. // }
  215. // }
  216. // if (currentPline.NumberOfVertices > 0)
  217. // {
  218. // BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));
  219. // BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  220. // btr.AppendEntity(currentPline);
  221. // trans.AddNewlyCreatedDBObject(currentPline, true);
  222. // newPlls.Add(currentPline);
  223. // }
  224. // pll.Erase();
  225. // for (int i = 0; i < newPlls.Count; i++)
  226. // {
  227. // if (kz_pts.Contains(newPlls[i].StartPoint) &&
  228. // kz_pts.Contains(newPlls[i].EndPoint) &&
  229. // i % 2 == 1)
  230. // newPlls[i].Layer = "待删除等高线";
  231. // else if (newPlls.Count == 2)
  232. // {
  233. // if (newPlls[0].Length < newPlls[1].Length)
  234. // newPlls[0].Layer = "待删除等高线";
  235. // else
  236. // newPlls[1].Layer = "待删除等高线";
  237. // }
  238. // }
  239. //}
  240. #endregion
  241. }
  242. }
  243. if (pts.Count > 0)
  244. {
  245. //创建新图层
  246. LayerControl layerscontrol = new LayerControl();
  247. string layname = "待删除等高线";
  248. if (!layerscontrol.haslayername(layname))
  249. {
  250. colorgb col = new colorgb(255, 0, 255);
  251. layerscontrol.creatlayer(layname, col);
  252. layerscontrol.movelayertofront(layname);
  253. }
  254. else
  255. layerscontrol.movelayertofront(layname);
  256. //按参数值排序交点
  257. List<double> paramList = new List<double>();
  258. List<double> kzparam = new List<double>();
  259. foreach (Point3d breakPoint in pts)
  260. {
  261. double param = pll.GetParameterAtPoint(breakPoint);
  262. if (pts1.Contains(breakPoint))
  263. kzparam.Add(param);
  264. paramList.Add(param);
  265. }
  266. paramList.Sort();
  267. //存储分割后的多段线
  268. List<Polyline> newPlls = new List<Polyline>();
  269. // 初始化第一条多段线
  270. Polyline currentPline = new Polyline();
  271. currentPline.SetDatabaseDefaults();
  272. currentPline.Layer = pll.Layer;
  273. currentPline.Color = pll.Color;
  274. // 遍历多段线的节点
  275. for (int i = 0; i < pll.NumberOfVertices; i++)
  276. {
  277. Point2d point = pll.GetPoint2dAt(i);
  278. double bulge = pll.GetBulgeAt(i);
  279. // 添加节点到当前多段线
  280. currentPline.AddVertexAt(currentPline.NumberOfVertices, point, bulge, 0, 0);
  281. // 检查是否需要分割
  282. if (paramList.Count > 0 && i == (int)paramList[0])
  283. {
  284. // 检查当前节点和下一个节点之间是否有交点
  285. while (paramList.Count > 0 && paramList[0] >= i && paramList[0] < i + 1)
  286. {
  287. // 计算交点处的精确位置
  288. double param = paramList[0];
  289. double dist;
  290. if (kzparam.Contains(param))
  291. {
  292. dist = pll.GetDistanceAtParameter(param) - cut_dist;
  293. kz_pts.Add(pll.GetPointAtDist(dist));
  294. }
  295. else
  296. dist = pll.GetDistanceAtParameter(param);
  297. Point3d splitPoint3d = pll.GetPointAtDist(dist);
  298. Point2d splitPoint = new Point2d(splitPoint3d.X, splitPoint3d.Y);
  299. // 添加当前多段线到结果列表
  300. currentPline.AddVertexAt(currentPline.NumberOfVertices, splitPoint, 0, 0, 0);
  301. BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));
  302. BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  303. btr.AppendEntity(currentPline);
  304. trans.AddNewlyCreatedDBObject(currentPline, true);
  305. newPlls.Add(currentPline);
  306. // 初始化下一条多段线
  307. currentPline = new Polyline();
  308. currentPline.SetDatabaseDefaults();
  309. currentPline.Layer = pll.Layer;
  310. currentPline.Color = pll.Color;
  311. // 添加分割点作为下一条多段线的起点
  312. if (kzparam.Contains(param))
  313. {
  314. double dist1 = pll.GetDistanceAtParameter(param) + cut_dist;
  315. Point3d splitPoint3d1 = pll.GetPointAtDist(dist1);
  316. Point2d splitPoint1 = new Point2d(splitPoint3d1.X, splitPoint3d1.Y);
  317. kz_pts.Add(splitPoint3d1);
  318. currentPline.AddVertexAt(0, splitPoint1, 0, 0, 0);
  319. }
  320. else
  321. currentPline.AddVertexAt(0, splitPoint, 0, 0, 0);
  322. // 移除已处理的交点
  323. paramList.RemoveAt(0);
  324. }
  325. }
  326. }
  327. if (currentPline.NumberOfVertices > 0)
  328. {
  329. BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));
  330. BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  331. btr.AppendEntity(currentPline);
  332. trans.AddNewlyCreatedDBObject(currentPline, true);
  333. newPlls.Add(currentPline);
  334. }
  335. pll.Erase();
  336. for (int i = 0; i < newPlls.Count; i++)
  337. {
  338. if (newPlls.Count == 2)
  339. {
  340. if (newPlls[0].Length < newPlls[1].Length)
  341. newPlls[0].Layer = "待删除等高线";
  342. else
  343. newPlls[1].Layer = "待删除等高线";
  344. }
  345. else if (pts.Contains(newPlls[i].StartPoint) &&
  346. pts.Contains(newPlls[i].EndPoint) &&
  347. i % 2 == 1)
  348. newPlls[i].Layer = "待删除等高线";
  349. else if (kz_pts.Contains(newPlls[i].StartPoint) &&
  350. kz_pts.Contains(newPlls[i].EndPoint) &&
  351. i % 2 == 1)
  352. newPlls[i].Layer = "待删除等高线";
  353. }
  354. }
  355. }
  356. }
  357. }