工具箱相关
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Dgxthrorvrd.cs 30KB

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