工具箱相关
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Dgxthrorvrd.cs 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.DatabaseServices;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Geometry;
  5. using Autodesk.AutoCAD.Runtime;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace CutDGX
  12. {
  13. public struct colorgb
  14. {
  15. public byte red;
  16. public byte green;
  17. public byte blue;
  18. public colorgb(byte r, byte g, byte b)
  19. {
  20. this.red = r;
  21. this.green = g;
  22. this.blue = b;
  23. }
  24. }
  25. class Dgxthrorvrd
  26. {
  27. public static List<string> dgxlist;
  28. public static List<string> rvrdlist;
  29. public static List<string> kzlist;
  30. public static int blc;
  31. public void Dgxbreak()
  32. {
  33. string dgxname = "";
  34. Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  35. for (int i = 0; i < dgxlist.Count; i++)
  36. {
  37. if (i == 0)
  38. dgxname = dgxlist[i];
  39. else
  40. dgxname = dgxname + "," + dgxlist[i];
  41. }
  42. TypedValue[] value = new TypedValue[]
  43. {
  44. new TypedValue((int)DxfCode.LayerName,dgxname)
  45. };
  46. SelectionFilter filter = new SelectionFilter(value);
  47. //dynamic acadApp = AutoCAD.ApplicationServices.Application.AcadApplication;
  48. //acadApp.ZoomExtents();
  49. PromptSelectionResult psr = ed.SelectAll(filter);
  50. if (psr.Status == PromptStatus.OK)
  51. {
  52. SelectionSet ss = psr.Value;
  53. ObjectIdCollection idcoll = new ObjectIdCollection(ss.GetObjectIds());
  54. for (int ii = 0; ii < idcoll.Count; ii++)
  55. {
  56. //using (Transaction trans = db.TransactionManager.StartTransaction())
  57. //{
  58. Intersectcheck(idcoll[ii], ed);
  59. //trans.Commit();
  60. //}
  61. }
  62. }
  63. dgxlist.Clear();
  64. if (rvrdlist != null)
  65. rvrdlist.Clear();
  66. if (kzlist != null)
  67. kzlist.Clear();
  68. }
  69. private void Intersectcheck(ObjectId id, Editor ed)
  70. {
  71. var plane = new Plane(Point3d.Origin, Vector3d.ZAxis);
  72. Point3dCollection pts = new Point3dCollection();
  73. Point3dCollection pts1 = new Point3dCollection();
  74. Point3dCollection kz_pts = new Point3dCollection();
  75. string rvrdname = "";
  76. string kzname = "";
  77. //为坎子让出的长度
  78. double cut_dist = 0.5;
  79. if (blc == 1000)
  80. cut_dist = cut_dist * 2;
  81. if (blc == 2000)
  82. cut_dist = cut_dist * 4;
  83. if (blc == 5000)
  84. cut_dist = cut_dist * 10;
  85. if (blc == 10000)
  86. cut_dist = cut_dist * 20;
  87. Database db = Autodesk.AutoCAD.DatabaseServices.HostApplicationServices.WorkingDatabase;
  88. DocumentLock doclock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  89. using (Transaction trans = db.TransactionManager.StartTransaction())
  90. {
  91. Entity ent = trans.GetObject(id, OpenMode.ForWrite) as Entity;
  92. Polyline pll = new Polyline();
  93. if (ent is Polyline)
  94. pll = (Polyline)ent;
  95. else
  96. return;
  97. //添加道路河流房屋的图层名并搜索相应图层上的实体
  98. if (rvrdlist != null)
  99. {
  100. //if (kzlist != null)
  101. //{
  102. // rvrdlist.AddRange(kzlist);
  103. //}
  104. Point3dCollection ptcoll = new Point3dCollection();
  105. for (int i = 0; i < pll.NumberOfVertices; i++)
  106. {
  107. ptcoll.Add(pll.GetPoint3dAt(i));
  108. }
  109. List<string> lyrname = rvrdlist;
  110. for (int i = 0; i < lyrname.Count; i++)
  111. {
  112. if (i == 0)
  113. rvrdname = lyrname[i];
  114. else
  115. rvrdname = rvrdname + "," + lyrname[i];
  116. }
  117. TypedValue[] value = new TypedValue[]
  118. {
  119. new TypedValue((int)DxfCode.LayerName,rvrdname)
  120. };
  121. SelectionFilter filter = new SelectionFilter(value);
  122. ZoomView(ed, pll.Bounds.Value.MinPoint, pll.Bounds.Value.MaxPoint);
  123. PromptSelectionResult psr = ed.SelectFence(ptcoll,filter);
  124. if (psr.Status == PromptStatus.OK)
  125. {
  126. SelectionSet ss = psr.Value;
  127. ObjectIdCollection idcoll = new ObjectIdCollection(ss.GetObjectIds());
  128. for (int i = 0; i < idcoll.Count; i++)
  129. {
  130. Entity ent1 = trans.GetObject(idcoll[i], OpenMode.ForWrite) as Entity;
  131. Polyline pll_rvrd = new Polyline();
  132. if (ent1 is Polyline)
  133. pll_rvrd = (Polyline)ent1;
  134. else
  135. continue;
  136. Point3dCollection intersectionPoints = new Point3dCollection();
  137. intersectionPoints = GetIntersections(pll, pll_rvrd);
  138. //pll.IntersectWith(pll_rvrd, Intersect.OnBothOperands, plane, intersectionPoints, IntPtr.Zero, IntPtr.Zero);
  139. if (intersectionPoints.Count > 0)
  140. {
  141. foreach (Point3d pt in intersectionPoints)
  142. {
  143. if (pts.Contains(pll.GetClosestPointTo(pt, false)))
  144. continue;
  145. else
  146. pts.Add(pll.GetClosestPointTo(pt, false));
  147. }
  148. }
  149. }
  150. }
  151. }
  152. //添加坎子图层名并搜索相应图层上的实体
  153. if (kzlist != null)
  154. {
  155. for (int i = 0; i < kzlist.Count; i++)
  156. {
  157. if (i == 0)
  158. kzname = kzlist[i];
  159. else
  160. kzname = kzname + "," + kzlist[i];
  161. }
  162. TypedValue[] value_kz = new TypedValue[]
  163. {
  164. new TypedValue((int)DxfCode.LayerName,kzname)
  165. };
  166. SelectionFilter filter_kz = new SelectionFilter(value_kz);
  167. PromptSelectionResult psr_kz = ed.SelectAll(filter_kz);
  168. if (psr_kz.Status == PromptStatus.OK)
  169. {
  170. SelectionSet ss = psr_kz.Value;
  171. ObjectIdCollection idcoll = new ObjectIdCollection(ss.GetObjectIds());
  172. List<Point3d> ptlist = new List<Point3d>();
  173. for (int i = 0; i < idcoll.Count; i++)
  174. {
  175. Entity ent1 = trans.GetObject(idcoll[i], OpenMode.ForWrite) as Entity;
  176. Polyline pll_rvrd = new Polyline();
  177. if (ent1 is Polyline)
  178. pll_rvrd = (Polyline)ent1;
  179. else
  180. continue;
  181. Point3dCollection intersectionPoints = new Point3dCollection();
  182. intersectionPoints = GetIntersections(pll, pll_rvrd);
  183. //pll.IntersectWith(pll_rvrd, Intersect.OnBothOperands, plane, intersectionPoints, IntPtr.Zero, IntPtr.Zero);
  184. foreach (var item in intersectionPoints)
  185. {
  186. ptlist.Add((Point3d)item);
  187. }
  188. }
  189. if (ptlist.Count > 0)
  190. {
  191. var sortedlist = ptlist.OrderBy(x => pll.GetParameterAtPoint(x)).ToList();
  192. for (int j = 0; j < sortedlist.Count; j++)
  193. {
  194. Point3d pt;
  195. Point3d bkpt = pll.GetClosestPointTo(sortedlist[j], false);
  196. if (j % 2 == 0 || j == 0)
  197. {
  198. pt = pll.GetPointAtDist(pll.GetDistAtPoint(bkpt) - cut_dist);
  199. }
  200. else
  201. pt = pll.GetPointAtDist(pll.GetDistAtPoint(bkpt) + cut_dist);
  202. //foreach (Point3d pt in intersectionPoints)
  203. //{
  204. // pts1.Add(pt);
  205. //}
  206. pts.Add(pt);
  207. }
  208. }
  209. #region
  210. //if (pts.Count > 0)
  211. //{
  212. // //按参数值排序交点
  213. // List<double> paramList = new List<double>();
  214. // foreach (Point3d breakPoint in pts)
  215. // {
  216. // double param = pll.GetParameterAtPoint(breakPoint);
  217. // paramList.Add(param);
  218. // }
  219. // paramList.Sort();
  220. // //存储分割后的多段线
  221. // List<Polyline> newPlls = new List<Polyline>();
  222. // // 初始化第一条多段线
  223. // Polyline currentPline = new Polyline();
  224. // currentPline.SetDatabaseDefaults();
  225. // currentPline.Layer = pll.Layer;
  226. // currentPline.Color = pll.Color;
  227. // // 遍历多段线的节点
  228. // for (int i = 0; i < pll.NumberOfVertices; i++)
  229. // {
  230. // Point2d point = pll.GetPoint2dAt(i);
  231. // double bulge = pll.GetBulgeAt(i);
  232. // // 添加节点到当前多段线
  233. // currentPline.AddVertexAt(currentPline.NumberOfVertices, point, bulge, 0, 0);
  234. // // 检查是否需要分割
  235. // if (paramList.Count > 0 && i == (int)paramList[0])
  236. // {
  237. // // 检查当前节点和下一个节点之间是否有交点
  238. // while (paramList.Count > 0 && paramList[0] >= i && paramList[0] < i + 1)
  239. // {
  240. // // 计算交点处的精确位置
  241. // double param = paramList[0];
  242. // double dist = pll.GetDistanceAtParameter(param)- cut_dist;
  243. // Point3d splitPoint3d = pll.GetPointAtDist(dist);
  244. // Point2d splitPoint = new Point2d(splitPoint3d.X, splitPoint3d.Y);
  245. // kz_pts.Add(splitPoint3d);
  246. // // 添加当前多段线到结果列表
  247. // currentPline.AddVertexAt(currentPline.NumberOfVertices, splitPoint, 0, 0, 0);
  248. // BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));
  249. // BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  250. // btr.AppendEntity(currentPline);
  251. // trans.AddNewlyCreatedDBObject(currentPline, true);
  252. // newPlls.Add(currentPline);
  253. // // 初始化下一条多段线
  254. // currentPline = new Polyline();
  255. // currentPline.SetDatabaseDefaults();
  256. // currentPline.Layer = pll.Layer;
  257. // currentPline.Color = pll.Color;
  258. // // 添加分割点作为下一条多段线的起点
  259. // double dist1 = pll.GetDistanceAtParameter(param) + cut_dist;
  260. // Point3d splitPoint3d1 = pll.GetPointAtDist(dist1);
  261. // Point2d splitPoint1 = new Point2d(splitPoint3d1.X, splitPoint3d1.Y);
  262. // kz_pts.Add(splitPoint3d1);
  263. // currentPline.AddVertexAt(0, splitPoint1, 0, 0, 0);
  264. // // 移除已处理的交点
  265. // paramList.RemoveAt(0);
  266. // }
  267. // }
  268. // }
  269. // if (currentPline.NumberOfVertices > 0)
  270. // {
  271. // BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));
  272. // BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  273. // btr.AppendEntity(currentPline);
  274. // trans.AddNewlyCreatedDBObject(currentPline, true);
  275. // newPlls.Add(currentPline);
  276. // }
  277. // pll.Erase();
  278. // for (int i = 0; i < newPlls.Count; i++)
  279. // {
  280. // if (kz_pts.Contains(newPlls[i].StartPoint) &&
  281. // kz_pts.Contains(newPlls[i].EndPoint) &&
  282. // i % 2 == 1)
  283. // newPlls[i].Layer = "待删除等高线";
  284. // else if (newPlls.Count == 2)
  285. // {
  286. // if (newPlls[0].Length < newPlls[1].Length)
  287. // newPlls[0].Layer = "待删除等高线";
  288. // else
  289. // newPlls[1].Layer = "待删除等高线";
  290. // }
  291. // }
  292. //}
  293. #endregion
  294. }
  295. }
  296. if (pts.Count > 0)
  297. {
  298. //创建新图层
  299. string layname = "待删除等高线";
  300. if (!haslayername(layname))
  301. {
  302. colorgb col = new colorgb(255, 0, 255);
  303. creatlayer(layname, col);
  304. movelayertofront(layname);
  305. }
  306. else
  307. movelayertofront(layname);
  308. //按参数值排序交点
  309. List<double> paramList = new List<double>();
  310. List<double> kzparam = new List<double>();
  311. foreach (Point3d breakPoint in pts)
  312. {
  313. double param = pll.GetParameterAtPoint(breakPoint);
  314. if (pts1.Contains(breakPoint))
  315. kzparam.Add(param);
  316. paramList.Add(param);
  317. }
  318. paramList.Sort();
  319. //存储分割后的多段线
  320. List<Polyline> newPlls = new List<Polyline>();
  321. // 初始化第一条多段线
  322. Polyline currentPline = new Polyline();
  323. currentPline.SetDatabaseDefaults();
  324. currentPline.Layer = pll.Layer;
  325. currentPline.Color = pll.Color;
  326. // 遍历多段线的节点
  327. //for (int i = 0; i < pll.NumberOfVertices; i++)
  328. //{
  329. // Point2d point = pll.GetPoint2dAt(i);
  330. // double bulge = pll.GetBulgeAt(i);
  331. // // 添加节点到当前多段线
  332. // currentPline.AddVertexAt(currentPline.NumberOfVertices, point, bulge, 0, 0);
  333. // // 检查是否需要分割
  334. // if (paramList.Count > 0 && i == (int)paramList[0])
  335. // {
  336. // // 检查当前节点和下一个节点之间是否有交点
  337. // while (paramList.Count > 0 && paramList[0] >= i && paramList[0] < i + 1)
  338. // {
  339. // // 计算交点处的精确位置
  340. // double param = paramList[0];
  341. // double dist;
  342. // if (kzparam.Contains(param))
  343. // {
  344. // dist = pll.GetDistanceAtParameter(param) - cut_dist;
  345. // kz_pts.Add(pll.GetPointAtDist(dist));
  346. // }
  347. // else
  348. // dist = pll.GetDistanceAtParameter(param);
  349. // Point3d splitPoint3d = pll.GetPointAtDist(dist);
  350. // Point2d splitPoint = new Point2d(splitPoint3d.X, splitPoint3d.Y);
  351. // // 添加当前多段线到结果列表
  352. // currentPline.AddVertexAt(currentPline.NumberOfVertices, splitPoint, 0, 0, 0);
  353. // BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));
  354. // BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  355. // btr.AppendEntity(currentPline);
  356. // trans.AddNewlyCreatedDBObject(currentPline, true);
  357. // newPlls.Add(currentPline);
  358. // // 初始化下一条多段线
  359. // currentPline = new Polyline();
  360. // currentPline.SetDatabaseDefaults();
  361. // currentPline.Layer = pll.Layer;
  362. // currentPline.Color = pll.Color;
  363. // // 添加分割点作为下一条多段线的起点
  364. // if (kzparam.Contains(param))
  365. // {
  366. // double dist1 = pll.GetDistanceAtParameter(param) + cut_dist;
  367. // Point3d splitPoint3d1 = pll.GetPointAtDist(dist1);
  368. // Point2d splitPoint1 = new Point2d(splitPoint3d1.X, splitPoint3d1.Y);
  369. // kz_pts.Add(splitPoint3d1);
  370. // currentPline.AddVertexAt(0, splitPoint1, 0, 0, 0);
  371. // }
  372. // else
  373. // currentPline.AddVertexAt(0, splitPoint, 0, 0, 0);
  374. // // 移除已处理的交点
  375. // paramList.RemoveAt(0);
  376. // }
  377. // }
  378. //}
  379. //if (currentPline.NumberOfVertices > 0)
  380. //{
  381. // BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));
  382. // BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  383. // btr.AppendEntity(currentPline);
  384. // trans.AddNewlyCreatedDBObject(currentPline, true);
  385. // newPlls.Add(currentPline);
  386. //}
  387. List<Point3d> pts_list = new List<Point3d>();
  388. foreach (var item in pts)
  389. {
  390. pts_list.Add((Point3d)item);
  391. }
  392. var pts_sorted = pts_list.OrderBy(x => pll.GetParameterAtPoint(x));
  393. pts.Clear();
  394. foreach (var item in pts_sorted)
  395. {
  396. pts.Add(item);
  397. }
  398. foreach (var item in pll.GetSplitCurves(pts))
  399. {
  400. BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));
  401. BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  402. btr.AppendEntity((Polyline)item);
  403. trans.AddNewlyCreatedDBObject((Polyline)item, true);
  404. newPlls.Add((Polyline)item);
  405. }
  406. pll.Erase();
  407. for (int i = 0; i < newPlls.Count; i++)
  408. {
  409. if (newPlls.Count == 2)
  410. {
  411. if (newPlls[0].Length < newPlls[1].Length)
  412. newPlls[0].Layer = "待删除等高线";
  413. else
  414. newPlls[1].Layer = "待删除等高线";
  415. }
  416. else if (pts.Contains(newPlls[i].StartPoint) &&
  417. pts.Contains(newPlls[i].EndPoint) &&
  418. i % 2 == 1)
  419. newPlls[i].Layer = "待删除等高线";
  420. else if (kz_pts.Contains(newPlls[i].StartPoint) &&
  421. kz_pts.Contains(newPlls[i].EndPoint) &&
  422. i % 2 == 1)
  423. newPlls[i].Layer = "待删除等高线";
  424. }
  425. }
  426. trans.Commit();
  427. }
  428. doclock.Dispose();
  429. }
  430. /// <summary>
  431. /// 缩放视图以便搜索
  432. /// </summary>
  433. private void ZoomView(Editor edit, Point3d pt1, Point3d pt2)
  434. {
  435. var x_min = Math.Min(pt1.X, pt2.X);
  436. var y_min = Math.Min(pt1.Y, pt2.Y);
  437. using (ViewTableRecord currview1 = edit.GetCurrentView())
  438. {
  439. currview1.CenterPoint = new Point2d(Math.Abs((pt2.X - pt1.X) / 2) + x_min,
  440. Math.Abs((pt2.Y - pt1.Y) / 2) + y_min);
  441. currview1.Width = Math.Abs(pt2.X - pt1.X + 1);
  442. currview1.Height = Math.Abs(pt2.Y - pt1.Y + 1);
  443. edit.SetCurrentView(currview1);
  444. }
  445. }
  446. /// <summary>
  447. /// 创建高程点错误标记层
  448. /// </summary>
  449. /// <param name="layername">要创建的图层的名字</param>
  450. /// <param name="colo">RGB三原色</param>
  451. public void creatlayer(string layername, colorgb colo)
  452. {
  453. Database database = Autodesk.AutoCAD.DatabaseServices.HostApplicationServices.WorkingDatabase;
  454. DocumentLock doclock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  455. Transaction traction = database.TransactionManager.StartTransaction();
  456. LayerTable lt = traction.GetObject(database.LayerTableId, OpenMode.ForWrite) as LayerTable;
  457. try
  458. {
  459. LayerTableRecord layertablerecord = new LayerTableRecord();
  460. layertablerecord.Name = layername;//设置层表记录的名字
  461. layertablerecord.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(colo.red, colo.green, colo.blue);//为层表记录赋颜色紫色
  462. //layertablerecord.Color = GrxCAD.Colors.Color.FromColor(System.Drawing.Color.Magenta);
  463. lt.Add(layertablerecord);
  464. traction.AddNewlyCreatedDBObject(layertablerecord, true);
  465. traction.Commit();
  466. }
  467. catch (Autodesk.AutoCAD.Runtime.Exception)
  468. {
  469. traction.Abort();
  470. }
  471. finally
  472. {
  473. traction.Dispose();
  474. doclock.Dispose();
  475. }
  476. }
  477. /// <summary>
  478. /// 将指定图层设置为当前图层
  479. /// </summary>
  480. /// <param name="layername">要设置为当前图层的图层名字</param>
  481. public void movelayertofront(string layername)
  482. {
  483. Database database = Autodesk.AutoCAD.DatabaseServices.HostApplicationServices.WorkingDatabase;
  484. DocumentLock doclock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  485. Transaction traction = database.TransactionManager.StartTransaction();
  486. try
  487. {
  488. LayerTable layertable = traction.GetObject(database.LayerTableId, OpenMode.ForRead) as LayerTable;
  489. database.Clayer = layertable[layername];//把图层选为当前图层
  490. traction.Commit();
  491. }
  492. catch (Autodesk.AutoCAD.Runtime.Exception)
  493. {
  494. traction.Abort();
  495. }
  496. finally
  497. {
  498. traction.Dispose();
  499. doclock.Dispose();
  500. }
  501. }
  502. /// <summary>
  503. /// 判断所命图层名是否已经存在
  504. /// </summary>
  505. /// <param name="layername">要判断的图层名字</param>
  506. /// <returns>返回true表示在层表中存在要判断的图层名,
  507. /// 返回false表示层表中不存在判断的图层名</returns>
  508. public bool haslayername(string layername)
  509. {
  510. Database database = Autodesk.AutoCAD.DatabaseServices.HostApplicationServices.WorkingDatabase;
  511. using (Transaction traction = database.TransactionManager.StartTransaction())
  512. {
  513. LayerTable lt = traction.GetObject(database.LayerTableId, OpenMode.ForRead) as LayerTable;
  514. if (lt.Has(layername))
  515. {
  516. traction.Commit();
  517. return true;
  518. }
  519. else
  520. {
  521. traction.Commit();
  522. return false;
  523. }
  524. }
  525. }
  526. private static Point3dCollection GetIntersections(Polyline pll1, Polyline pll2)
  527. {
  528. List<Point3d> sort = new List<Point3d>();
  529. Point3dCollection result = new Point3dCollection();
  530. // 创建临时副本
  531. Polyline tempLine1 = (Polyline)pll1.Clone();
  532. Polyline tempLine2 = (Polyline)pll2.Clone();
  533. // 计算平移向量(以线1起点为基准,移动至原点)
  534. Vector3d translation = -tempLine1.StartPoint.GetAsVector();
  535. Matrix3d translateMatrix = Matrix3d.Displacement(translation);
  536. // 应用平移变换
  537. tempLine1.TransformBy(translateMatrix);
  538. tempLine2.TransformBy(translateMatrix);
  539. Point3dCollection translatedPoints = new Point3dCollection();
  540. var plane = new Plane(Point3d.Origin, Vector3d.ZAxis);
  541. tempLine1.IntersectWith(tempLine2, Intersect.OnBothOperands, plane, translatedPoints, IntPtr.Zero, IntPtr.Zero);
  542. // 计算逆平移矩阵
  543. Matrix3d inverseMatrix = Matrix3d.Displacement(-translation);
  544. if (translatedPoints.Count > 0)
  545. {
  546. // 获取平移后的交点
  547. foreach (Point3d item in translatedPoints)
  548. {
  549. // 恢复交点坐标
  550. var pt = item.TransformBy(inverseMatrix);
  551. sort.Add(pt);
  552. }
  553. }
  554. var sorted = sort.OrderBy(x => pll1.GetParameterAtPoint(x));
  555. foreach (var item in sorted)
  556. {
  557. result.Add(item);
  558. }
  559. return result;
  560. }
  561. }
  562. }