工具箱相关
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

joinedge.cs 53KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. using Autodesk.AutoCAD.DatabaseServices;
  2. using Autodesk.AutoCAD.DatabaseServices.Filters;
  3. using Autodesk.AutoCAD.EditorInput;
  4. using Autodesk.AutoCAD.Geometry;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace CutDGX
  15. {
  16. public partial class joinedge : Form
  17. {
  18. public joinedge()
  19. {
  20. InitializeComponent();
  21. }
  22. private void joinedge_Load(object sender, EventArgs e)
  23. {
  24. this.comboBox_tk.DropDownStyle = ComboBoxStyle.DropDownList;
  25. //自动填充下拉框
  26. Database database = Autodesk.AutoCAD.DatabaseServices.HostApplicationServices.WorkingDatabase;
  27. using (Transaction traction = database.TransactionManager.StartTransaction())
  28. {
  29. //存放所有图层名
  30. List<string> alllayername = new List<string>();
  31. LayerTable layertable = traction.GetObject(database.LayerTableId, OpenMode.ForRead) as LayerTable;
  32. foreach (ObjectId objid in layertable)
  33. {
  34. LayerTableRecord layertablerecord = traction.GetObject(objid, OpenMode.ForRead) as LayerTableRecord;
  35. alllayername.Add(layertablerecord.Name);
  36. }
  37. alllayername.Sort();
  38. for (int i = 0; i < alllayername.Count; i++)
  39. {
  40. this.comboBox_tk.Items.Add(alllayername[i]);
  41. }
  42. if (alllayername.Contains("TK"))
  43. {
  44. int index = alllayername.IndexOf("TK");
  45. this.comboBox_tk.SelectedIndex = index;
  46. }
  47. traction.Commit();
  48. }
  49. }
  50. private void button_ok_Click(object sender, EventArgs e)
  51. {
  52. if (comboBox_tk.Text == "")
  53. {
  54. MessageBox.Show("请选择分幅图框所在图层");
  55. return;
  56. }
  57. if (textBox_sele.Text == "")
  58. {
  59. MessageBox.Show("请输入搜索阈值");
  60. return;
  61. }
  62. string tklyr = comboBox_tk.Text;
  63. joineg(tklyr);
  64. this.Close();
  65. MessageBox.Show("接边完成。");
  66. }
  67. private void joineg(string tklyr)
  68. {
  69. dynamic acadApp = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
  70. acadApp.ZoomExtents();
  71. Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  72. Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
  73. //获取分幅图框
  74. TypedValue[] typedvalue = new TypedValue[2];
  75. typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, tklyr), 0);
  76. typedvalue.SetValue(new TypedValue((int)DxfCode.Start, "Polyline,LWPolyline"), 1);
  77. SelectionFilter selectionfilter = new SelectionFilter(typedvalue);
  78. PromptSelectionResult psr = ed.SelectAll(selectionfilter);
  79. if (psr.Status == PromptStatus.OK)
  80. {
  81. SelectionSet selectionset = psr.Value;
  82. ObjectId[] obj = new ObjectId[selectionset.Count];
  83. obj = selectionset.GetObjectIds();
  84. List<ObjectId> selectedid = new List<ObjectId>();
  85. for (int i = 0; i < obj.Length; i++)
  86. {
  87. Polyline pll = new Polyline();
  88. //获取图幅框信息和与图幅框相交的实体
  89. using (Transaction tr = db.TransactionManager.StartTransaction())
  90. {
  91. Entity ent0 = tr.GetObject(obj[i], OpenMode.ForRead) as Entity;
  92. //判断图框线类型,如果是polyline3d则转换为polyline进行后续操作
  93. if (ent0 is Polyline3d)
  94. {
  95. Polyline3d pl3d = (Polyline3d)ent0;
  96. foreach (ObjectId vertexId in pl3d)
  97. {
  98. PolylineVertex3d vertex = vertexId.GetObject(OpenMode.ForRead) as PolylineVertex3d;
  99. if (vertex != null)
  100. {
  101. pll.AddVertexAt(pll.NumberOfVertices, new Point2d(vertex.Position.X, vertex.Position.Y), 0, 0, 0);
  102. }
  103. }
  104. }
  105. else
  106. pll = (Polyline)ent0;
  107. Point3dCollection pts = getpt(ent0);
  108. selectedid = selebytk(pts, ed, ent0, tr);
  109. tr.Commit();
  110. }
  111. //遍历与图框相交的线,以实体与图框交点为中心做框选,如果有被选中的线则判断是否需要连接
  112. for (int j = 0; j < selectedid.Count; j++)
  113. {
  114. using (Transaction tr = db.TransactionManager.StartTransaction())
  115. {
  116. if (selectedid[j].IsErased)
  117. continue;
  118. Entity ent = tr.GetObject(selectedid[j], OpenMode.ForWrite) as Entity;
  119. conn(pll, ent, tr, ed);
  120. tr.Commit();
  121. }
  122. }
  123. }
  124. }
  125. }
  126. private Point3dCollection getpt(Entity ent)
  127. {
  128. Point3dCollection ptcoll = new Point3dCollection();
  129. if (ent is Polyline)
  130. {
  131. Polyline pll = (Polyline)ent;
  132. //获取图框节点并排序
  133. int numVertices = pll.NumberOfVertices;
  134. for (int j = 0; j < numVertices; j++)
  135. {
  136. ptcoll.Add(pll.GetPoint3dAt(j));
  137. }
  138. sortpts(ptcoll);
  139. }
  140. else if (ent is Polyline2d)
  141. {
  142. Polyline2d pl2d = (Polyline2d)ent;
  143. pl2d.GetStretchPoints(ptcoll);
  144. sortpts(ptcoll);
  145. }
  146. else if (ent is Polyline3d)
  147. {
  148. Polyline3d pl3d = (Polyline3d)ent;
  149. pl3d.GetStretchPoints(ptcoll);
  150. sortpts(ptcoll);
  151. }
  152. return ptcoll;
  153. }
  154. private List<ObjectId> selebytk(Point3dCollection ptcoll, Editor ed,
  155. Entity ent0, Transaction tr)
  156. {
  157. //设置当前视图
  158. setwindow(ptcoll[0], ptcoll[2], ed);
  159. List<ObjectId> result = new List<ObjectId>();
  160. TypedValue[] typedvalue = new TypedValue[1];
  161. typedvalue.SetValue(new TypedValue((int)DxfCode.Start, "Polyline,LWPolyline,Hatch,Line"), 0);
  162. SelectionFilter selectionfilter = new SelectionFilter(typedvalue);
  163. PromptSelectionResult psr = ed.SelectCrossingWindow(ptcoll[0], ptcoll[2], selectionfilter);
  164. if (psr.Status == PromptStatus.OK)
  165. {
  166. SelectionSet selectionset = psr.Value;
  167. ObjectId[] obj = new ObjectId[selectionset.Count];
  168. obj = selectionset.GetObjectIds();
  169. for (int i = 0; i < obj.Length; i++)
  170. {
  171. Entity ent = tr.GetObject(obj[i], OpenMode.ForRead) as Entity;
  172. //如果和图框在同一个图层就跳过
  173. if (ent.Layer == comboBox_tk.Text)
  174. continue;
  175. var plane = new Plane(Point3d.Origin, Vector3d.ZAxis);
  176. //选择与图框相交的线将objectid添加到list中
  177. var pts = new Point3dCollection();
  178. ent0.IntersectWith(ent, Intersect.OnBothOperands, plane, pts, IntPtr.Zero, IntPtr.Zero);
  179. if (pts.Count != 0)
  180. result.Add(ent.ObjectId);
  181. }
  182. }
  183. return result;
  184. }
  185. //连接分别位于两图框内的部分
  186. private void conn(Polyline pll,Entity ent, Transaction tr,Editor ed)
  187. {
  188. //搜索阈值
  189. double sele = double.Parse(textBox_sele.Text);
  190. //获取实体与图框交点,遍历每个点,以点为中心进行框选搜索是否存在与本实体相交的其他实体
  191. List<Point3d> pts = GetIntersections(ent, pll);
  192. List<ObjectId> idlist = new List<ObjectId>();
  193. foreach (var item in pts)
  194. {
  195. Point3d s_pt1 = new Point3d(item.X - sele, item.Y - sele, 0);
  196. Point3d s_pt2 = new Point3d(item.X + sele, item.Y + sele, 0);
  197. TypedValue[] typedvalue = new TypedValue[1];
  198. typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, ent.Layer), 0);
  199. SelectionFilter selectionfilter = new SelectionFilter(typedvalue);
  200. PromptSelectionResult psr = ed.SelectCrossingWindow(s_pt1, s_pt2,selectionfilter);
  201. if (psr.Status == PromptStatus.OK)
  202. {
  203. SelectionSet selectionset = psr.Value;
  204. ObjectId[] obj = new ObjectId[selectionset.Count];
  205. obj = selectionset.GetObjectIds();
  206. foreach (var id in obj)
  207. {
  208. if (!idlist.Contains(id)&&id!=ent.ObjectId)
  209. idlist.Add(id);
  210. }
  211. }
  212. }
  213. //如果搜索得到的结果不为0,则判断实体是否符合连接条件,如果符合则连接
  214. if (idlist.Count!=0)
  215. {
  216. Tolerance tl = new Tolerance(sele, sele);
  217. for (int i = 0; i < idlist.Count; i++)
  218. {
  219. Entity ent1 = tr.GetObject(idlist[i], OpenMode.ForWrite) as Entity;
  220. //如果实体图层与图框图层相同,实体objectid与待连接实体相同,两实体类型不同,则不进行连接
  221. if (ent1.Layer == comboBox_tk.Text || ent.ObjectId == ent1.ObjectId ||
  222. ent.GetType().Name != ent1.GetType().Name|| IsEntityClosed(ent1)!=IsEntityClosed(ent))
  223. continue;
  224. //闭合实体与非闭合实体采用不同的方法连接
  225. else if (IsEntityClosed(ent1) && IsEntityClosed(ent))
  226. {
  227. pllmerge(ent, ent1, tr);
  228. }
  229. else
  230. {
  231. if (ent is Polyline)
  232. {
  233. Polyline pl1 = (Polyline)ent;
  234. Polyline pl2 = (Polyline)ent1;
  235. if (pl1.Length < sele)
  236. return;
  237. else if (pl2.Length < sele)
  238. continue;
  239. //判断两实体是否在同一图框
  240. var overlay = IsOverlay(ent, ent1, pll, tl);
  241. //如果两线高程不同,两线首尾点没有重合点,两线有重合则不相连
  242. if (pl1.Elevation != pl2.Elevation || (!pl1.StartPoint.IsEqualTo(pl2.StartPoint,tl) &&
  243. !pl1.StartPoint.IsEqualTo(pl2.EndPoint, tl) && !pl1.EndPoint.IsEqualTo(pl2.StartPoint, tl) &&
  244. !pl1.EndPoint.IsEqualTo(pl2.EndPoint, tl)) || overlay)
  245. continue;
  246. }
  247. else if (ent is Polyline2d)
  248. {
  249. Polyline2d pl1 = (Polyline2d)ent;
  250. Polyline2d pl2 = (Polyline2d)ent1;
  251. if (pl1.Length < sele)
  252. return;
  253. else if (pl2.Length < sele)
  254. continue;
  255. //判断两实体是否在同一图框
  256. var overlay = IsOverlay(ent, ent1, pll,tl);
  257. //如果两线高程不同,两线首尾点没有重合点,两线有重合则不相连
  258. if (pl1.Elevation != pl2.Elevation || (!pl1.StartPoint.IsEqualTo(pl2.StartPoint, tl) &&
  259. !pl1.StartPoint.IsEqualTo(pl2.EndPoint, tl) && !pl1.EndPoint.IsEqualTo(pl2.StartPoint, tl) &&
  260. !pl1.EndPoint.IsEqualTo(pl2.EndPoint, tl)) || overlay)
  261. continue;
  262. }
  263. else if (ent is Polyline3d)
  264. {
  265. Polyline3d pl1 = (Polyline3d)ent;
  266. Polyline3d pl2 = (Polyline3d)ent1;
  267. if (pl1.Length < sele)
  268. return;
  269. else if (pl2.Length < sele)
  270. continue;
  271. //判断两实体是否在同一图框
  272. var overlay = IsOverlay(ent, ent1, pll,tl);
  273. //如果两线高程不同,两线首尾点没有重合点,两线有重合则不相连
  274. if ((!pl1.StartPoint.IsEqualTo(pl2.StartPoint, tl) &&!pl1.StartPoint.IsEqualTo(pl2.EndPoint, tl) &&
  275. !pl1.EndPoint.IsEqualTo(pl2.StartPoint, tl) &&!pl1.EndPoint.IsEqualTo(pl2.EndPoint, tl)) ||
  276. overlay)
  277. continue;
  278. }
  279. else if (ent is Line)
  280. {
  281. Line pl1 = (Line)ent;
  282. Line pl2 = (Line)ent1;
  283. if (pl1.Length < sele)
  284. return;
  285. else if (pl2.Length < sele)
  286. continue;
  287. //判断两实体是否在同一图框
  288. bool isoverlay = IsOverlay(pl1, pl2, pll,tl);
  289. //如果两线高程不同,两线首尾点没有重合点,两线有重合则不相连
  290. if ((!pl1.StartPoint.IsEqualTo(pl2.StartPoint, tl) &&!pl1.StartPoint.IsEqualTo(pl2.EndPoint, tl) &&
  291. !pl1.EndPoint.IsEqualTo(pl2.StartPoint, tl) &&!pl1.EndPoint.IsEqualTo(pl2.EndPoint, tl)) ||
  292. isoverlay)
  293. continue;
  294. }
  295. Entity ent_con = CreateConnOrNot(ent, ent1);
  296. ent_con.JoinEntity(ent1);
  297. ent1.Erase();
  298. }
  299. }
  300. }
  301. }
  302. //将闭合实体转换为region,使用布尔运算进行合并后再将合并后的region转换为原类型
  303. private void pllmerge(Entity ent1, Entity ent2, Transaction tr)
  304. {
  305. try
  306. {
  307. Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
  308. BlockTable newBt = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
  309. BlockTableRecord newBtr = tr.GetObject(newBt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  310. // 创建Region集合
  311. DBObjectCollection plines = new DBObjectCollection();
  312. if (ent1 is Polyline)
  313. {
  314. Polyline pl1 = (Polyline)ent1;
  315. Polyline pl2 = (Polyline)ent2;
  316. if (pl1.Elevation != pl2.Elevation)
  317. return;
  318. plines.Add(pl1);
  319. plines.Add(pl2);
  320. }
  321. else if (ent1 is Polyline2d)
  322. {
  323. Polyline2d pl1 = (Polyline2d)ent1;
  324. Polyline2d pl2 = (Polyline2d)ent2;
  325. if (pl1.Elevation != pl2.Elevation)
  326. return;
  327. plines.Add(pl1);
  328. plines.Add(pl2);
  329. }
  330. else if (ent1 is Polyline3d)
  331. {
  332. Polyline3d pl1 = (Polyline3d)ent1;
  333. Polyline3d pl2 = (Polyline3d)ent2;
  334. plines.Add(pl1);
  335. plines.Add(pl2);
  336. }
  337. else if (ent1 is Hatch)
  338. {
  339. Hatch hc1 = (Hatch)ent1;
  340. Hatch hc2 = (Hatch)ent2;
  341. if (hc1.PatternName != hc2.PatternName)
  342. return;
  343. for (int i = 0; i < hc1.NumberOfLoops; i++)
  344. {
  345. HatchLoop loop = hc1.GetLoopAt(i);
  346. if (loop.Curves == null)
  347. {
  348. Polyline polyline = new Polyline();
  349. // 遍历顶点并添加
  350. for (int ii = 0; ii < loop.Polyline.Count; ii++)
  351. {
  352. BulgeVertex vertex = loop.Polyline[ii];
  353. polyline.AddVertexAt(i, vertex.Vertex, vertex.Bulge, 0, 0);
  354. }
  355. polyline.Closed = true; // 闭合多段线
  356. plines.Add(polyline);
  357. }
  358. else
  359. {
  360. foreach (Curve curve in loop.Curves)
  361. {
  362. plines.Add(curve);
  363. }
  364. }
  365. }
  366. for (int i = 0; i < hc2.NumberOfLoops; i++)
  367. {
  368. HatchLoop loop = hc2.GetLoopAt(i);
  369. if (loop.Curves == null)
  370. {
  371. Polyline polyline = new Polyline();
  372. // 遍历顶点并添加
  373. for (int ii = 0; ii < loop.Polyline.Count; ii++)
  374. {
  375. BulgeVertex vertex = loop.Polyline[ii];
  376. polyline.AddVertexAt(i, vertex.Vertex, vertex.Bulge, 0, 0);
  377. }
  378. polyline.Closed = true; // 闭合多段线
  379. plines.Add(polyline);
  380. }
  381. else
  382. {
  383. foreach (Curve curve in loop.Curves)
  384. {
  385. plines.Add(curve);
  386. }
  387. }
  388. }
  389. }
  390. // 从多段线创建Region
  391. DBObjectCollection regions = Autodesk.AutoCAD.DatabaseServices.Region.CreateFromCurves(plines);
  392. // 合并Regions
  393. Autodesk.AutoCAD.DatabaseServices.Region mergedRegion = regions[0] as Autodesk.AutoCAD.DatabaseServices.Region;
  394. for (int i = 1; i < regions.Count; i++)
  395. {
  396. mergedRegion.BooleanOperation(BooleanOperationType.BoolUnite, regions[i] as Autodesk.AutoCAD.DatabaseServices.Region);
  397. }
  398. //炸开region以便转换回原本的类型
  399. DBObjectCollection explodedCurves = new DBObjectCollection();
  400. mergedRegion.Explode(explodedCurves);
  401. if (ent1 is Polyline)
  402. {
  403. Polyline polyline = createpllfromregion(explodedCurves);
  404. Polyline pl = (Polyline)ent1;
  405. polyline.Elevation = pl.Elevation;
  406. polyline.Layer = pl.Layer;
  407. polyline.Color = pl.Color;
  408. polyline.Linetype = pl.Linetype;
  409. polyline.Plinegen = pl.Plinegen;
  410. polyline.ConstantWidth = pl.ConstantWidth;
  411. newBtr.AppendEntity(polyline);
  412. tr.AddNewlyCreatedDBObject(polyline, true);
  413. }
  414. else if (ent1 is Polyline2d)
  415. {
  416. Polyline2d polyline2d = createpl2dfromregion(explodedCurves);
  417. Polyline2d pl2d = (Polyline2d)ent1;
  418. polyline2d.Elevation = pl2d.Elevation;
  419. polyline2d.Layer = pl2d.Layer;
  420. polyline2d.Color = pl2d.Color;
  421. polyline2d.Linetype = pl2d.Linetype;
  422. polyline2d.ConstantWidth = pl2d.ConstantWidth;
  423. newBtr.AppendEntity(polyline2d);
  424. tr.AddNewlyCreatedDBObject(polyline2d, true);
  425. }
  426. else if (ent1 is Polyline3d)
  427. {
  428. Polyline3d pline3d = createpl3dfromregion(explodedCurves);
  429. Polyline3d pl3d = (Polyline3d)ent1;
  430. pline3d.Layer = pl3d.Layer;
  431. pline3d.Color = pl3d.Color;
  432. pline3d.Linetype = pl3d.Linetype;
  433. pline3d.LineWeight = pl3d.LineWeight;
  434. newBtr.AppendEntity(pline3d);
  435. tr.AddNewlyCreatedDBObject(pline3d, true);
  436. }
  437. else if (ent1 is Hatch)
  438. {
  439. Hatch hc = (Hatch)ent1;
  440. ObjectIdCollection boundaryIds = new ObjectIdCollection();
  441. //hatch边缘
  442. Polyline regionpll = createpllfromregion(explodedCurves);
  443. newBtr.AppendEntity(regionpll);
  444. tr.AddNewlyCreatedDBObject(regionpll, true);
  445. boundaryIds.Add(regionpll.ObjectId);
  446. Hatch newHatch = new Hatch();
  447. newHatch.AppendLoop(HatchLoopTypes.Default, boundaryIds);
  448. newBtr.AppendEntity(newHatch);
  449. tr.AddNewlyCreatedDBObject(newHatch, true);
  450. string pname = hc.PatternName;//hatch填充图案名
  451. newHatch.SetHatchPattern(hc.PatternType, pname);
  452. newHatch.EvaluateHatch(true);
  453. regionpll.Erase();
  454. }
  455. ent1.Erase();
  456. ent2.Erase();
  457. }
  458. catch (Exception ex)
  459. {
  460. MessageBox.Show($"\n错误: {ex.Message}");
  461. }
  462. }
  463. //点排序
  464. private void sortpts(Point3dCollection ptcoll)
  465. {
  466. List<double> x = new List<double>();
  467. List<double> y = new List<double>();
  468. for (int i = 0; i < 4; i++)
  469. {
  470. if (!x.Contains(ptcoll[i].X))
  471. x.Add(ptcoll[i].X);
  472. if (!y.Contains(ptcoll[i].Y))
  473. y.Add(ptcoll[i].Y);
  474. }
  475. if (x[0] > x[1])
  476. {
  477. double temp = x[0];
  478. x[0] = x[1];
  479. x[1] = temp;
  480. }
  481. if (y[0] > y[1])
  482. {
  483. double temp = y[0];
  484. y[0] = y[1];
  485. y[1] = temp;
  486. }
  487. ptcoll[0] = new Point3d(Math.Round(x[0], 3, MidpointRounding.AwayFromZero), Math.Round(y[0], 3, MidpointRounding.AwayFromZero), 0); //左下
  488. ptcoll[1] = new Point3d(Math.Round(x[1], 3, MidpointRounding.AwayFromZero), Math.Round(y[0], 3, MidpointRounding.AwayFromZero), 0); //右下
  489. ptcoll[2] = new Point3d(Math.Round(x[1], 3, MidpointRounding.AwayFromZero), Math.Round(y[1], 3, MidpointRounding.AwayFromZero), 0); //右上
  490. ptcoll[3] = new Point3d(Math.Round(x[0], 3, MidpointRounding.AwayFromZero), Math.Round(y[1], 3, MidpointRounding.AwayFromZero), 0); //左上
  491. }
  492. //设置当前视图位置和大小
  493. private void setwindow(Point3d pt1, Point3d pt2, Editor ed)
  494. {
  495. //设置当前视图
  496. ViewTableRecord currview1 = ed.GetCurrentView();
  497. currview1.CenterPoint = new Point2d((pt2.X - pt1.X) / 2 + pt1.X,
  498. (pt2.Y - pt1.Y) / 2 + pt1.Y);
  499. currview1.Width = (pt2.X - pt1.X)*2;
  500. currview1.Height = (pt2.Y - pt1.Y)*2;
  501. ed.SetCurrentView(currview1);
  502. }
  503. //判断是否是闭合图形
  504. private bool IsEntityClosed(Entity ent)
  505. {
  506. switch (ent)
  507. {
  508. case Polyline pline:
  509. return pline.StartPoint== pline.EndPoint;
  510. case Polyline2d pline2d:
  511. return pline2d.StartPoint == pline2d.EndPoint;
  512. case Polyline3d pline3d:
  513. return pline3d.StartPoint == pline3d.EndPoint;
  514. case Spline spline:
  515. return spline.Closed;
  516. case Circle _:
  517. return true;
  518. case Ellipse ellipse:
  519. return Math.Abs(ellipse.EndAngle - ellipse.StartAngle) >= 2 * Math.PI - 1e-6;
  520. case Autodesk.AutoCAD.DatabaseServices.Region _:
  521. case Hatch _:
  522. return true; // 区域和填充图案总是闭合的
  523. case Line _:
  524. case Arc _:
  525. case Ray _:
  526. case Xline _:
  527. return false; // 这些实体总是开放的
  528. default:
  529. // 对于其他实体类型,检查起点和终点是否重合
  530. if (ent is Curve curve)
  531. {
  532. return curve.StartPoint.DistanceTo(curve.EndPoint) < Tolerance.Global.EqualPoint;
  533. }
  534. return false;
  535. }
  536. }
  537. //分别获取两实体交点旁边一点,判断该点在图框内还是外,如果两点位置关系相同,说明两实体在同侧,
  538. //不需要连接,返回true,否则返回false
  539. private bool IsOverlay(Entity ent1,Entity ent2,Polyline pl,Tolerance tl)
  540. {
  541. Point3d region_max = pl.Bounds.Value.MaxPoint;
  542. Point3d region_min = pl.Bounds.Value.MinPoint;
  543. bool ent1_inside;
  544. bool ent2_inside;
  545. Point3d pt1 = new Point3d();
  546. Point3d pt2 = new Point3d();
  547. Point3d compare = pt1;
  548. List<Point3d> pl1_pts = new List<Point3d>();
  549. List<Point3d> pl2_pts = new List<Point3d>();
  550. if (ent1 is Polyline)
  551. {
  552. Polyline pl1 = (Polyline)ent1;
  553. Polyline pl2 = (Polyline)ent2;
  554. pl1_pts.Add(pl1.StartPoint);
  555. pl1_pts.Add(pl1.EndPoint);
  556. pl2_pts.Add(pl2.StartPoint);
  557. pl2_pts.Add(pl2.EndPoint);
  558. var result = Comparept(pl1_pts, pl2_pts, pl1, pl2, pt1, pt2, tl);
  559. pt1 = result[0];
  560. pt2 = result[1];
  561. }
  562. else if (ent1 is Polyline2d)
  563. {
  564. Polyline2d pl1 = (Polyline2d)ent1;
  565. Polyline2d pl2 = (Polyline2d)ent2;
  566. pl1_pts.Add(pl1.StartPoint);
  567. pl1_pts.Add(pl1.EndPoint);
  568. pl2_pts.Add(pl2.StartPoint);
  569. pl2_pts.Add(pl2.EndPoint);
  570. var result = Comparept(pl1_pts, pl2_pts, pl1, pl2, pt1, pt2, tl);
  571. pt1 = result[0];
  572. pt2 = result[1];
  573. }
  574. else if (ent1 is Polyline3d)
  575. {
  576. Polyline3d pl1 = (Polyline3d)ent1;
  577. Polyline3d pl2 = (Polyline3d)ent2;
  578. pl1_pts.Add(pl1.StartPoint);
  579. pl1_pts.Add(pl1.EndPoint);
  580. pl2_pts.Add(pl2.StartPoint);
  581. pl2_pts.Add(pl2.EndPoint);
  582. var result = Comparept(pl1_pts, pl2_pts, pl1, pl2, pt1, pt2, tl);
  583. pt1 = result[0];
  584. pt2 = result[1];
  585. }
  586. else
  587. {
  588. Line pl1 = (Line)ent1;
  589. Line pl2 = (Line)ent2;
  590. pl1_pts.Add(pl1.StartPoint);
  591. pl1_pts.Add(pl1.EndPoint);
  592. pl2_pts.Add(pl2.StartPoint);
  593. pl2_pts.Add(pl2.EndPoint);
  594. for (int i = 0; i < pl1_pts.Count; i++)
  595. {
  596. for (int j = 0; j < pl2_pts.Count; j++)
  597. {
  598. if (pl1_pts[i].IsEqualTo(pl2_pts[j]))
  599. {
  600. if (i == 0)
  601. pt1 = pl1.GetPointAtParameter(pl1.StartParam + 1);
  602. else
  603. pt1 = pl1.GetPointAtParameter(pl1.EndParam - 1);
  604. if (j == 0)
  605. pt2 = pl2.GetPointAtParameter(pl2.StartParam + 1);
  606. else
  607. pt2 = pl2.GetPointAtParameter(pl2.EndParam - 1);
  608. }
  609. }
  610. }
  611. }
  612. //如果pt1还是初始值,说明在同侧,返回true
  613. if (pt1 == compare)
  614. return true;
  615. if (region_max.X >= pt1.X && region_max.Y >= pt1.Y &&
  616. region_min.X <= pt1.X && region_min.Y <= pt1.Y)
  617. ent1_inside = true;
  618. else
  619. ent1_inside = false;
  620. if (region_max.X >= pt2.X && region_max.Y >= pt2.Y &&
  621. region_min.X <= pt2.X && region_min.Y <= pt2.Y)
  622. ent2_inside = true;
  623. else
  624. ent2_inside = false;
  625. if (ent1_inside == ent2_inside)
  626. return true;
  627. else
  628. return false;
  629. }
  630. //若两实体最近的两点不重合但小于阈值,则做一条Line将两点连起来,再用jionentity连接三条线
  631. private Entity CreateConnOrNot(Entity ent1, Entity ent2)
  632. {
  633. Entity result;
  634. Point3d s1 = new Point3d();
  635. Point3d s2 = new Point3d();
  636. Point3d e1 = new Point3d();
  637. Point3d e2 = new Point3d();
  638. Line line = new Line();
  639. var comp = line;
  640. if (ent1 is Polyline)
  641. {
  642. Polyline pl1 = (Polyline)ent1;
  643. Polyline pl2 = (Polyline)ent2;
  644. s1 = pl1.StartPoint;
  645. s2 = pl2.StartPoint;
  646. e1 = pl1.EndPoint;
  647. e2 = pl2.EndPoint;
  648. }
  649. else if (ent1 is Polyline2d)
  650. {
  651. Polyline2d pl1 = (Polyline2d)ent1;
  652. Polyline2d pl2 = (Polyline2d)ent2;
  653. s1 = pl1.StartPoint;
  654. s2 = pl2.StartPoint;
  655. e1 = pl1.EndPoint;
  656. e2 = pl2.EndPoint;
  657. }
  658. else if (ent1 is Polyline3d)
  659. {
  660. Polyline3d pl1 = (Polyline3d)ent1;
  661. Polyline3d pl2 = (Polyline3d)ent2;
  662. s1 = pl1.StartPoint;
  663. s2 = pl2.StartPoint;
  664. e1 = pl1.EndPoint;
  665. e2 = pl2.EndPoint;
  666. }
  667. else
  668. {
  669. Line pl1 = (Line)ent1;
  670. Line pl2 = (Line)ent2;
  671. s1 = pl1.StartPoint;
  672. s2 = pl2.StartPoint;
  673. e1 = pl1.EndPoint;
  674. e2 = pl2.EndPoint;
  675. }
  676. List<Point3d> pts = new List<Point3d>() { s1, s2, e1, e2 };
  677. var groups = pts.GroupBy(p => p).Where(g => g.Count() >= 2).OrderByDescending(g => g.Count()).ToList();
  678. if (groups.Count == 0)
  679. {
  680. Line line1 = new Line(s1, s2);
  681. Line line2 = new Line(s1, e2);
  682. Line line3 = new Line(e1, s2);
  683. Line line4 = new Line(e1, e2);
  684. List<Line> lines = new List<Line>() { line1, line2, line3, line4 };
  685. var sortedlines = lines.OrderBy(x => x.Length).ToList();
  686. line = sortedlines[0];
  687. if (ent1 is Polyline)
  688. {
  689. Polyline pl1 = (Polyline)ent1;
  690. pl1.JoinEntity(line);
  691. result = pl1;
  692. return result;
  693. }
  694. else if (ent1 is Polyline2d)
  695. {
  696. Polyline2d pl1 = (Polyline2d)ent1;
  697. pl1.JoinEntity(line);
  698. result = pl1;
  699. return result;
  700. }
  701. else if (ent1 is Polyline3d)
  702. {
  703. Polyline3d pl1 = (Polyline3d)ent1;
  704. pl1.JoinEntity(line);
  705. result = pl1;
  706. return result;
  707. }
  708. else
  709. {
  710. Line l1 = (Line)ent1;
  711. l1.JoinEntity(line);
  712. result = l1;
  713. return result;
  714. }
  715. }
  716. else
  717. return ent1;
  718. }
  719. //将polyline、polyline2d、polyline3d转换为region
  720. public Polyline createpllfromregion(DBObjectCollection explodedCurves)
  721. {
  722. // 存储提取的线段
  723. List<Curve> curves = new List<Curve>();
  724. foreach (DBObject obj in explodedCurves)
  725. {
  726. if (obj is Curve curve)
  727. {
  728. curves.Add(curve);
  729. }
  730. }
  731. Polyline polyline = new Polyline();
  732. polyline.SetDatabaseDefaults();
  733. while (curves.Count > 0)
  734. {
  735. // 从第一条线段开始
  736. Curve currentCurve = curves[0];
  737. curves.RemoveAt(0);
  738. // 添加当前线段的起点和终点
  739. polyline.AddVertexAt(0, new Point2d(currentCurve.StartPoint.X, currentCurve.StartPoint.Y), 0, 0, 0);
  740. polyline.AddVertexAt(1, new Point2d(currentCurve.EndPoint.X, currentCurve.EndPoint.Y), 0, 0, 0);
  741. // 查找与当前线段相连的线段
  742. bool foundConnectedCurve;
  743. do
  744. {
  745. foundConnectedCurve = false;
  746. for (int i = 0; i < curves.Count; i++)
  747. {
  748. Curve nextCurve = curves[i];
  749. // 检查是否与当前线段的起点或终点相连
  750. if (nextCurve.StartPoint.IsEqualTo(polyline.GetPoint3dAt(polyline.NumberOfVertices - 1)))
  751. {
  752. // 添加到多段线的末尾
  753. polyline.AddVertexAt(polyline.NumberOfVertices, new Point2d(nextCurve.EndPoint.X, nextCurve.EndPoint.Y), 0, 0, 0);
  754. curves.RemoveAt(i);
  755. foundConnectedCurve = true;
  756. break;
  757. }
  758. else if (nextCurve.EndPoint.IsEqualTo(polyline.GetPoint3dAt(0)))
  759. {
  760. // 添加到多段线的开头
  761. polyline.AddVertexAt(0, new Point2d(nextCurve.StartPoint.X, nextCurve.StartPoint.Y), 0, 0, 0);
  762. curves.RemoveAt(i);
  763. foundConnectedCurve = true;
  764. break;
  765. }
  766. else if (nextCurve.EndPoint.IsEqualTo(polyline.GetPoint3dAt(polyline.NumberOfVertices - 1)))
  767. {
  768. // 添加到多段线的末尾
  769. polyline.AddVertexAt(polyline.NumberOfVertices, new Point2d(nextCurve.StartPoint.X, nextCurve.StartPoint.Y), 0, 0, 0);
  770. curves.RemoveAt(i);
  771. foundConnectedCurve = true;
  772. break;
  773. }
  774. else if (nextCurve.StartPoint.IsEqualTo(polyline.GetPoint3dAt(0)))
  775. {
  776. // 添加到多段线的开头
  777. polyline.AddVertexAt(0, new Point2d(nextCurve.EndPoint.X, nextCurve.EndPoint.Y), 0, 0, 0);
  778. curves.RemoveAt(i);
  779. foundConnectedCurve = true;
  780. break;
  781. }
  782. }
  783. } while (foundConnectedCurve);
  784. // 关闭多段线
  785. polyline.Closed = true;
  786. }
  787. return polyline;
  788. }
  789. public Polyline2d createpl2dfromregion(DBObjectCollection explodedCurves)
  790. {
  791. // 存储提取的线段
  792. List<Curve> curves = new List<Curve>();
  793. foreach (DBObject obj in explodedCurves)
  794. {
  795. if (obj is Curve curve)
  796. {
  797. curves.Add(curve);
  798. }
  799. }
  800. Polyline2d pl2d = new Polyline2d();
  801. pl2d.SetDatabaseDefaults();
  802. while (curves.Count > 0)
  803. {
  804. // 从第一条线段开始
  805. Curve currentCurve = curves[0];
  806. curves.RemoveAt(0);
  807. // 添加当前线段的起点和终点
  808. Vertex2d firstVertex = new Vertex2d(currentCurve.StartPoint, 0, 0, 0, 0);
  809. Vertex2d secondVertex = new Vertex2d(currentCurve.EndPoint, 0, 0, 0, 0);
  810. pl2d.AppendVertex(firstVertex);
  811. pl2d.AppendVertex(secondVertex);
  812. // 查找与当前线段相连的线段
  813. bool foundConnectedCurve;
  814. do
  815. {
  816. foundConnectedCurve = false;
  817. for (int i = 0; i < curves.Count; i++)
  818. {
  819. Curve nextCurve = curves[i];
  820. // 检查是否与当前线段的起点或终点相连
  821. if (nextCurve.StartPoint.IsEqualTo(pl2d.EndPoint))
  822. {
  823. // 添加到多段线的末尾
  824. Vertex2d nextVertex = new Vertex2d(nextCurve.EndPoint, 0, 0, 0, 0);
  825. pl2d.AppendVertex(nextVertex);
  826. curves.RemoveAt(i);
  827. foundConnectedCurve = true;
  828. break;
  829. }
  830. else if (nextCurve.EndPoint.IsEqualTo(pl2d.StartPoint))
  831. {
  832. // 添加到多段线的开头
  833. Vertex2d nextVertex = new Vertex2d(nextCurve.StartPoint, 0, 0, 0, 0);
  834. pl2d.AppendVertex(nextVertex);
  835. curves.RemoveAt(i);
  836. foundConnectedCurve = true;
  837. break;
  838. }
  839. else if (nextCurve.EndPoint.IsEqualTo(pl2d.EndPoint))
  840. {
  841. // 添加到多段线的末尾StartPoint
  842. Vertex2d nextVertex = new Vertex2d(nextCurve.StartPoint, 0, 0, 0, 0);
  843. pl2d.AppendVertex(nextVertex);
  844. curves.RemoveAt(i);
  845. foundConnectedCurve = true;
  846. break;
  847. }
  848. else if (nextCurve.StartPoint.IsEqualTo(pl2d.StartPoint))
  849. {
  850. // 添加到多段线的开头
  851. Vertex2d nextVertex = new Vertex2d(nextCurve.EndPoint, 0, 0, 0, 0);
  852. pl2d.AppendVertex(nextVertex);
  853. curves.RemoveAt(i);
  854. foundConnectedCurve = true;
  855. break;
  856. }
  857. }
  858. } while (foundConnectedCurve);
  859. // 关闭多段线
  860. pl2d.Closed = true;
  861. }
  862. return pl2d;
  863. }
  864. public Polyline3d createpl3dfromregion(DBObjectCollection explodedCurves)
  865. {
  866. // 存储提取的线段
  867. List<Curve> curves = new List<Curve>();
  868. foreach (DBObject obj in explodedCurves)
  869. {
  870. if (obj is Curve curve)
  871. {
  872. curves.Add(curve);
  873. }
  874. }
  875. Polyline3d pl3d = new Polyline3d();
  876. pl3d.SetDatabaseDefaults();
  877. while (curves.Count > 0)
  878. {
  879. // 从第一条线段开始
  880. Curve currentCurve = curves[0];
  881. curves.RemoveAt(0);
  882. // 添加当前线段的起点和终点
  883. pl3d.AppendVertex(new PolylineVertex3d(currentCurve.StartPoint));
  884. pl3d.AppendVertex(new PolylineVertex3d(currentCurve.EndPoint));
  885. // 查找与当前线段相连的线段
  886. bool foundConnectedCurve;
  887. do
  888. {
  889. foundConnectedCurve = false;
  890. for (int i = 0; i < curves.Count; i++)
  891. {
  892. Curve nextCurve = curves[i];
  893. // 检查是否与当前线段的起点或终点相连
  894. if (nextCurve.StartPoint.IsEqualTo(pl3d.EndPoint))
  895. {
  896. // 添加到多段线的末尾
  897. pl3d.AppendVertex(new PolylineVertex3d(nextCurve.EndPoint));
  898. curves.RemoveAt(i);
  899. foundConnectedCurve = true;
  900. break;
  901. }
  902. else if (nextCurve.EndPoint.IsEqualTo(pl3d.StartPoint))
  903. {
  904. // 添加到多段线的开头
  905. pl3d.AppendVertex(new PolylineVertex3d(nextCurve.StartPoint));
  906. curves.RemoveAt(i);
  907. foundConnectedCurve = true;
  908. break;
  909. }
  910. else if (nextCurve.EndPoint.IsEqualTo(pl3d.EndPoint))
  911. {
  912. // 添加到多段线的末尾
  913. pl3d.AppendVertex(new PolylineVertex3d(nextCurve.StartPoint));
  914. curves.RemoveAt(i);
  915. foundConnectedCurve = true;
  916. break;
  917. }
  918. else if (nextCurve.StartPoint.IsEqualTo(pl3d.StartPoint))
  919. {
  920. // 添加到多段线的开头
  921. pl3d.AppendVertex(new PolylineVertex3d(nextCurve.EndPoint));
  922. curves.RemoveAt(i);
  923. foundConnectedCurve = true;
  924. break;
  925. }
  926. }
  927. } while (foundConnectedCurve);
  928. // 关闭多段线
  929. pl3d.Closed = true;
  930. }
  931. return pl3d;
  932. }
  933. //为避免误差,将实体移动到原点附近进行相交检查,获取到的交点坐标再转换回原位置
  934. private List<Point3d> GetIntersections(Entity ent, Polyline pll)
  935. {
  936. List<Point3d> sort = new List<Point3d>();
  937. // 创建临时副本
  938. Entity ent1 = ent.Clone()as Entity;
  939. Polyline tempLine2 = (Polyline)pll.Clone();
  940. // 计算平移向量(以线1起点为基准,移动至原点)
  941. Vector3d translation = -tempLine2.StartPoint.GetAsVector();
  942. Matrix3d translateMatrix = Matrix3d.Displacement(translation);
  943. // 应用平移变换
  944. ent1.TransformBy(translateMatrix);
  945. tempLine2.TransformBy(translateMatrix);
  946. Point3dCollection translatedPoints = new Point3dCollection();
  947. var plane = new Plane(Point3d.Origin, Vector3d.ZAxis);
  948. if(ent1 is Polyline)
  949. {
  950. Polyline tempLine1 = (Polyline)ent1;
  951. tempLine1.IntersectWith(tempLine2, Intersect.OnBothOperands, plane, translatedPoints, IntPtr.Zero, IntPtr.Zero);
  952. }
  953. else if (ent1 is Polyline2d)
  954. {
  955. Polyline2d tempLine1 = (Polyline2d)ent1;
  956. tempLine1.IntersectWith(tempLine2, Intersect.OnBothOperands, plane, translatedPoints, IntPtr.Zero, IntPtr.Zero);
  957. }
  958. else if(ent1 is Polyline3d)
  959. {
  960. Polyline3d tempLine1 = (Polyline3d)ent1;
  961. tempLine1.IntersectWith(tempLine2, Intersect.OnBothOperands, plane, translatedPoints, IntPtr.Zero, IntPtr.Zero);
  962. }
  963. else if(ent1 is Line)
  964. {
  965. Line tempLine1 = (Line)ent1;
  966. tempLine1.IntersectWith(tempLine2, Intersect.OnBothOperands, plane, translatedPoints, IntPtr.Zero, IntPtr.Zero);
  967. }
  968. else if(ent1 is Hatch)
  969. {
  970. Hatch hc = (Hatch)ent1;
  971. hc.IntersectWith(tempLine2, Intersect.OnBothOperands, plane, translatedPoints, IntPtr.Zero, IntPtr.Zero);
  972. }
  973. // 计算逆平移矩阵
  974. Matrix3d inverseMatrix = Matrix3d.Displacement(-translation);
  975. if (translatedPoints.Count > 0)
  976. {
  977. // 获取平移后的交点
  978. foreach (Point3d item in translatedPoints)
  979. {
  980. // 恢复交点坐标
  981. var pt = item.TransformBy(inverseMatrix);
  982. sort.Add(pt);
  983. }
  984. var sorted = sort.OrderBy(x => pll.GetParameterAtPoint(pll.GetClosestPointTo(x, false)));
  985. return sorted.ToList();
  986. }
  987. else
  988. return sort;
  989. }
  990. private List<Point3d> Comparept(List<Point3d> pl1_pts, List<Point3d> pl2_pts, Polyline pl1, Polyline pl2,
  991. Point3d pt1, Point3d pt2,Tolerance tl)
  992. {
  993. List<Point3d> result;
  994. for (int i = 0; i < pl1_pts.Count; i++)
  995. {
  996. for (int j = 0; j < pl2_pts.Count; j++)
  997. {
  998. if (pl1_pts[i].IsEqualTo(pl2_pts[j],tl))
  999. {
  1000. if (i == 0)
  1001. {
  1002. int num = 1;
  1003. do
  1004. {
  1005. pt1 = pl1.GetPointAtParameter(pl1.StartParam + num);
  1006. num++;
  1007. }
  1008. while (pl1.GetDistAtPoint(pt1) <= tl.EqualPoint && num < pl1.NumberOfVertices);
  1009. }
  1010. else
  1011. {
  1012. int num = 1;
  1013. do
  1014. {
  1015. pt1 = pl1.GetPointAtParameter(pl1.EndParam - num);
  1016. num++;
  1017. var t = pl1.Length - pl1.GetDistAtPoint(pt1);
  1018. }
  1019. while ((pl1.Length - pl1.GetDistAtPoint(pt1)) <= tl.EqualPoint && num < pl1.NumberOfVertices);
  1020. }
  1021. if (j == 0)
  1022. {
  1023. int num = 1;
  1024. do
  1025. {
  1026. pt2 = pl2.GetPointAtParameter(pl2.StartParam + num);
  1027. num++;
  1028. }
  1029. while (pl2.GetDistAtPoint(pt2) <= tl.EqualPoint && num < pl2.NumberOfVertices);
  1030. }
  1031. else
  1032. {
  1033. int num = 1;
  1034. do
  1035. {
  1036. pt2 = pl2.GetPointAtParameter(pl2.EndParam - num);
  1037. num++;
  1038. }
  1039. while ((pl2.Length - pl2.GetDistAtPoint(pt2)) <= tl.EqualPoint && num < pl2.NumberOfVertices);
  1040. }
  1041. }
  1042. }
  1043. }
  1044. result = new List<Point3d>() { pt1, pt2 };
  1045. return result;
  1046. }
  1047. private List<Point3d> Comparept(List<Point3d> pl1_pts, List<Point3d> pl2_pts, Polyline2d pl1, Polyline2d pl2,
  1048. Point3d pt1, Point3d pt2, Tolerance tl)
  1049. {
  1050. List<Point3d> result;
  1051. int pt_num1 = 0;
  1052. int pt_num2 = 0;
  1053. foreach (ObjectId id in pl1)
  1054. pt_num1++;
  1055. foreach (ObjectId id in pl2)
  1056. pt_num2++;
  1057. for (int i = 0; i < pl1_pts.Count; i++)
  1058. {
  1059. for (int j = 0; j < pl2_pts.Count; j++)
  1060. {
  1061. if (pl1_pts[i].IsEqualTo(pl2_pts[j], tl))
  1062. {
  1063. if (i == 0)
  1064. {
  1065. int num = 1;
  1066. do
  1067. {
  1068. pt1 = pl1.GetPointAtParameter(pl1.StartParam + num);
  1069. num++;
  1070. }
  1071. while (pl1.GetDistAtPoint(pt1) <= tl.EqualPoint && num < pt_num1);
  1072. }
  1073. else
  1074. {
  1075. int num = 1;
  1076. do
  1077. {
  1078. pt1 = pl1.GetPointAtParameter(pl1.EndParam - num);
  1079. num++;
  1080. var t = pl1.Length - pl1.GetDistAtPoint(pt1);
  1081. }
  1082. while ((pl1.Length - pl1.GetDistAtPoint(pt1)) <= tl.EqualPoint && num < pt_num1);
  1083. }
  1084. if (j == 0)
  1085. {
  1086. int num = 1;
  1087. do
  1088. {
  1089. pt2 = pl2.GetPointAtParameter(pl2.StartParam + num);
  1090. num++;
  1091. }
  1092. while (pl2.GetDistAtPoint(pt2) <= tl.EqualPoint && num < pt_num2);
  1093. }
  1094. else
  1095. {
  1096. int num = 1;
  1097. do
  1098. {
  1099. pt2 = pl2.GetPointAtParameter(pl2.EndParam - num);
  1100. num++;
  1101. }
  1102. while ((pl2.Length - pl2.GetDistAtPoint(pt2)) <= tl.EqualPoint && num < pt_num2);
  1103. }
  1104. }
  1105. }
  1106. }
  1107. result = new List<Point3d>() { pt1, pt2 };
  1108. return result;
  1109. }
  1110. private List<Point3d> Comparept(List<Point3d> pl1_pts, List<Point3d> pl2_pts, Polyline3d pl1, Polyline3d pl2,
  1111. Point3d pt1, Point3d pt2, Tolerance tl)
  1112. {
  1113. List<Point3d> result;
  1114. int pt_num1 = 0;
  1115. int pt_num2 = 0;
  1116. foreach (ObjectId id in pl1)
  1117. pt_num1++;
  1118. foreach (ObjectId id in pl2)
  1119. pt_num2++;
  1120. for (int i = 0; i < pl1_pts.Count; i++)
  1121. {
  1122. for (int j = 0; j < pl2_pts.Count; j++)
  1123. {
  1124. if (pl1_pts[i].IsEqualTo(pl2_pts[j], tl))
  1125. {
  1126. if (i == 0)
  1127. {
  1128. int num = 1;
  1129. do
  1130. {
  1131. pt1 = pl1.GetPointAtParameter(pl1.StartParam + num);
  1132. num++;
  1133. }
  1134. while (pl1.GetDistAtPoint(pt1) <= tl.EqualPoint && num < pt_num1);
  1135. }
  1136. else
  1137. {
  1138. int num = 1;
  1139. do
  1140. {
  1141. pt1 = pl1.GetPointAtParameter(pl1.EndParam - num);
  1142. num++;
  1143. var t = pl1.Length - pl1.GetDistAtPoint(pt1);
  1144. }
  1145. while ((pl1.Length - pl1.GetDistAtPoint(pt1)) <= tl.EqualPoint && num < pt_num1);
  1146. }
  1147. if (j == 0)
  1148. {
  1149. int num = 1;
  1150. do
  1151. {
  1152. pt2 = pl2.GetPointAtParameter(pl2.StartParam + num);
  1153. num++;
  1154. }
  1155. while (pl2.GetDistAtPoint(pt2) <= tl.EqualPoint && num < pt_num2);
  1156. }
  1157. else
  1158. {
  1159. int num = 1;
  1160. do
  1161. {
  1162. pt2 = pl2.GetPointAtParameter(pl2.EndParam - num);
  1163. num++;
  1164. }
  1165. while ((pl2.Length - pl2.GetDistAtPoint(pt2)) <= tl.EqualPoint && num < pt_num2);
  1166. }
  1167. }
  1168. }
  1169. }
  1170. result = new List<Point3d>() { pt1, pt2 };
  1171. return result;
  1172. }
  1173. }
  1174. }