工具箱相关
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.

Joinedge_Form.cs 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. using GrxCAD.DatabaseServices;
  2. using GrxCAD.EditorInput;
  3. using GrxCAD.Geometry;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace HCTools
  14. {
  15. public partial class Joinedge_Form : Form
  16. {
  17. public Joinedge_Form()
  18. {
  19. InitializeComponent();
  20. }
  21. private void Joinedge_Form_Load(object sender, EventArgs e)
  22. {
  23. this.comboBox_tk.DropDownStyle = ComboBoxStyle.DropDownList;
  24. //自动填充下拉框
  25. Database database = GrxCAD.DatabaseServices.HostApplicationServices.WorkingDatabase;
  26. using (Transaction traction = database.TransactionManager.StartTransaction())
  27. {
  28. //存放所有图层名
  29. List<string> alllayername = new List<string>();
  30. LayerTable layertable = traction.GetObject(database.LayerTableId, OpenMode.ForRead) as LayerTable;
  31. foreach (ObjectId objid in layertable)
  32. {
  33. LayerTableRecord layertablerecord = traction.GetObject(objid, OpenMode.ForRead) as LayerTableRecord;
  34. alllayername.Add(layertablerecord.Name);
  35. }
  36. alllayername.Sort();
  37. for (int i = 0; i < alllayername.Count; i++)
  38. {
  39. this.comboBox_tk.Items.Add(alllayername[i]);
  40. }
  41. if (alllayername.Contains("TK"))
  42. {
  43. int index = alllayername.IndexOf("TK");
  44. this.comboBox_tk.SelectedIndex = index;
  45. }
  46. traction.Commit();
  47. }
  48. }
  49. private void button_ok_Click(object sender, EventArgs e)
  50. {
  51. if (comboBox_tk.Text == "")
  52. {
  53. MessageBox.Show("请选择分幅图框所在图层");
  54. return;
  55. }
  56. if (textBox_sele.Text == "")
  57. {
  58. MessageBox.Show("请输入搜索阈值");
  59. return;
  60. }
  61. string tklyr = comboBox_tk.Text;
  62. joinedge(tklyr);
  63. }
  64. private void joinedge(string tklyr)
  65. {
  66. dynamic acadApp = GrxCAD.ApplicationServices.Application.AcadApplication;
  67. acadApp.ZoomExtents();
  68. Editor ed = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  69. Database db = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
  70. //获取分幅图框
  71. TypedValue[] typedvalue = new TypedValue[2];
  72. typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, tklyr), 0);
  73. typedvalue.SetValue(new TypedValue((int)DxfCode.Start, "Polyline,LWPolyline"), 1);
  74. SelectionFilter selectionfilter = new SelectionFilter(typedvalue);
  75. PromptSelectionResult psr = ed.SelectAll(selectionfilter);
  76. if (psr.Status == PromptStatus.OK)
  77. {
  78. SelectionSet selectionset = psr.Value;
  79. ObjectId[] obj = new ObjectId[selectionset.Count];
  80. obj = selectionset.GetObjectIds();
  81. List<ObjectId> selectedid = new List<ObjectId>();
  82. for (int i = 0; i < obj.Length; i++)
  83. {
  84. //获取图幅框信息和与图幅框相交的实体
  85. using (Transaction tr = db.TransactionManager.StartTransaction())
  86. {
  87. Entity ent0 = tr.GetObject(obj[i], OpenMode.ForRead) as Entity;
  88. Point3dCollection pts = getpt(ent0);
  89. selectedid = selebytk(pts, ed, ent0, tr);
  90. tr.Commit();
  91. }
  92. }
  93. //遍历与图框相交的线,在首尾点做框选,如果有被选中的线则做连接
  94. for (int i = 0; i < selectedid.Count; i++)
  95. {
  96. using (Transaction tr = db.TransactionManager.StartTransaction())
  97. {
  98. Entity ent = tr.GetObject(selectedid[i], OpenMode.ForWrite) as Entity;
  99. conn(ent, tr, ed);
  100. tr.Commit();
  101. }
  102. }
  103. }
  104. }
  105. private void sortpts(Point3dCollection ptcoll)
  106. {
  107. List<double> x = new List<double>();
  108. List<double> y = new List<double>();
  109. for (int i = 0; i < 4; i++)
  110. {
  111. if (!x.Contains(ptcoll[i].X))
  112. x.Add(ptcoll[i].X);
  113. if (!y.Contains(ptcoll[i].Y))
  114. y.Add(ptcoll[i].Y);
  115. }
  116. if (x[0] > x[1])
  117. {
  118. double temp = x[0];
  119. x[0] = x[1];
  120. x[1] = temp;
  121. }
  122. if (y[0] > y[1])
  123. {
  124. double temp = y[0];
  125. y[0] = y[1];
  126. y[1] = temp;
  127. }
  128. ptcoll[0] = new Point3d(Math.Round(x[0], 3, MidpointRounding.AwayFromZero), Math.Round(y[0], 3, MidpointRounding.AwayFromZero), 0); //左下
  129. ptcoll[1] = new Point3d(Math.Round(x[1], 3, MidpointRounding.AwayFromZero), Math.Round(y[0], 3, MidpointRounding.AwayFromZero), 0); //右下
  130. ptcoll[2] = new Point3d(Math.Round(x[1], 3, MidpointRounding.AwayFromZero), Math.Round(y[1], 3, MidpointRounding.AwayFromZero), 0); //右上
  131. ptcoll[3] = new Point3d(Math.Round(x[0], 3, MidpointRounding.AwayFromZero), Math.Round(y[1], 3, MidpointRounding.AwayFromZero), 0); //左上
  132. }
  133. private Point3dCollection getpt(Entity ent)
  134. {
  135. Point3dCollection ptcoll = new Point3dCollection();
  136. if (ent is Polyline)
  137. {
  138. Polyline pll = (Polyline)ent;
  139. //获取图框节点并排序
  140. int numVertices = pll.NumberOfVertices;
  141. for (int j = 0; j < numVertices; j++)
  142. {
  143. ptcoll.Add(pll.GetPoint3dAt(j));
  144. }
  145. sortpts(ptcoll);
  146. }
  147. else if (ent is Polyline2d)
  148. {
  149. Polyline2d pl2d = (Polyline2d)ent;
  150. pl2d.GetStretchPoints(ptcoll);
  151. sortpts(ptcoll);
  152. }
  153. else if (ent is Polyline3d)
  154. {
  155. Polyline3d pl3d = (Polyline3d)ent;
  156. pl3d.GetStretchPoints(ptcoll);
  157. sortpts(ptcoll);
  158. }
  159. return ptcoll;
  160. }
  161. private List<ObjectId> selebytk(Point3dCollection ptcoll, Editor ed,
  162. Entity ent0, Transaction tr)
  163. {
  164. //设置当前视图
  165. setwindow(ptcoll[0], ptcoll[2], ed);
  166. List<ObjectId> result = new List<ObjectId>();
  167. TypedValue[] typedvalue = new TypedValue[1];
  168. typedvalue.SetValue(new TypedValue((int)DxfCode.Start, "Polyline,LWPolyline"),0);
  169. SelectionFilter selectionfilter = new SelectionFilter(typedvalue);
  170. PromptSelectionResult psr = ed.SelectCrossingWindow(ptcoll[0], ptcoll[2]);
  171. if (psr.Status == PromptStatus.OK)
  172. {
  173. SelectionSet selectionset = psr.Value;
  174. ObjectId[] obj = new ObjectId[selectionset.Count];
  175. obj = selectionset.GetObjectIds();
  176. for (int i = 0; i < obj.Length; i++)
  177. {
  178. Entity ent = tr.GetObject(obj[i], OpenMode.ForRead) as Entity;
  179. if (ent.Layer == comboBox_tk.Text)
  180. continue;
  181. var plane = new Plane(Point3d.Origin, Vector3d.ZAxis);
  182. //选择与图框相交的线将objectid添加到list中
  183. ent0.IntersectWith(ent, Intersect.OnBothOperands, plane, ptcoll, IntPtr.Zero, IntPtr.Zero);
  184. if (ptcoll.Count != 0)
  185. result.Add(ent.ObjectId);
  186. }
  187. }
  188. return result;
  189. }
  190. private void conn(Entity ent, Transaction tr, Editor ed)
  191. {
  192. double sele = double.Parse(textBox_sele.Text);
  193. Polyline pll = new Polyline();
  194. Polyline2d pll2d = new Polyline2d();
  195. Polyline3d pll3d = new Polyline3d();
  196. Point3d s_pt = new Point3d();
  197. Point3d e_pt = new Point3d();
  198. if (ent is Polyline)
  199. {
  200. pll = (Polyline)ent;
  201. s_pt = pll.StartPoint;
  202. e_pt = pll.EndPoint;
  203. }
  204. if (ent is Polyline2d)
  205. {
  206. pll2d = (Polyline2d)ent;
  207. s_pt = pll2d.StartPoint;
  208. e_pt = pll2d.EndPoint;
  209. }
  210. if (ent is Polyline3d)
  211. {
  212. pll3d = (Polyline3d)ent;
  213. s_pt = pll3d.StartPoint;
  214. e_pt = pll3d.EndPoint;
  215. }
  216. TypedValue[] typedvalue = new TypedValue[1];
  217. typedvalue.SetValue(new TypedValue((int)DxfCode.Start, "Polyline,LWPolyline"), 0);
  218. SelectionFilter selectionfilter = new SelectionFilter(typedvalue);
  219. Point3d s_pt1 = new Point3d(s_pt.X - sele, s_pt.Y - sele, 0);
  220. Point3d s_pt2 = new Point3d(s_pt.X + sele, s_pt.Y + sele, 0);
  221. Point3d e_pt1 = new Point3d(e_pt.X - sele, e_pt.Y - sele, 0);
  222. Point3d e_pt2 = new Point3d(e_pt.X + sele, e_pt.Y + sele, 0);
  223. setwindow(s_pt1, s_pt2, ed);
  224. PromptSelectionResult psr_start = ed.SelectCrossingWindow(s_pt1, s_pt2);
  225. setwindow(e_pt1, e_pt2, ed);
  226. PromptSelectionResult psr_end = ed.SelectCrossingWindow(e_pt1, e_pt2);
  227. if (psr_start.Status == PromptStatus.OK)
  228. {
  229. SelectionSet selectionset = psr_start.Value;
  230. ObjectId[] obj = new ObjectId[selectionset.Count];
  231. obj = selectionset.GetObjectIds();
  232. for (int i = 0; i < obj.Length; i++)
  233. {
  234. Entity ent1 = tr.GetObject(obj[i], OpenMode.ForWrite) as Entity;
  235. if (ent1.Layer == comboBox_tk.Text)
  236. continue;
  237. if (comp(ent, ent1))
  238. {
  239. joinents(ent, ent1, tr);
  240. }
  241. }
  242. }
  243. }
  244. private bool comp(Entity ent1, Entity ent2)
  245. {
  246. if (ent1.GetType() == ent2.GetType())
  247. {
  248. if (ent1 is Polyline)
  249. {
  250. Polyline pll1 = (Polyline)ent1;
  251. Polyline pll2 = (Polyline)ent2;
  252. if (pll1.Layer == pll2.Layer &&
  253. pll1.Elevation == pll2.Elevation)
  254. return true;
  255. else
  256. return false;
  257. }
  258. else if (ent1 is Polyline2d)
  259. {
  260. Polyline2d pll1 = (Polyline2d)ent1;
  261. Polyline2d pll2 = (Polyline2d)ent2;
  262. if (pll1.Layer == pll2.Layer &&
  263. pll1.Elevation == pll2.Elevation)
  264. return true;
  265. else
  266. return false;
  267. }
  268. else if (ent1 is Polyline3d)
  269. {
  270. Polyline3d pll1 = (Polyline3d)ent1;
  271. Polyline3d pll2 = (Polyline3d)ent2;
  272. if (pll1.Layer == pll2.Layer)
  273. return true;
  274. else
  275. return false;
  276. }
  277. else
  278. return false;
  279. }
  280. else
  281. return false;
  282. }
  283. private void setwindow(Point3d pt1, Point3d pt2, Editor ed)
  284. {
  285. //设置当前视图
  286. ViewTableRecord currview1 = ed.GetCurrentView();
  287. currview1.CenterPoint = new Point2d((pt2.X - pt1.X) / 2 + pt1.X,
  288. (pt2.Y - pt1.Y) / 2 + pt1.Y);
  289. currview1.Width = pt2.X - pt1.X;
  290. currview1.Height = pt2.Y - pt1.Y;
  291. ed.SetCurrentView(currview1);
  292. }
  293. private void joinents(Entity ent1, Entity ent2, Transaction tr)
  294. {
  295. Database Db = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
  296. BlockTable Bt = tr.GetObject(Db.BlockTableId, OpenMode.ForWrite) as BlockTable;
  297. BlockTableRecord Btr = tr.GetObject(Bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  298. if (ent1 is Polyline||ent1 is Polyline2d)
  299. {
  300. Polyline pll1 = new Polyline();
  301. Polyline pll2 = new Polyline();
  302. if (ent1 is Polyline)
  303. {
  304. pll1 = (Polyline)ent1;
  305. pll2 = (Polyline)ent2;
  306. }
  307. else
  308. {
  309. Polyline2d pl2d1 = (Polyline2d)ent1;
  310. Polyline2d pl2d2 = (Polyline2d)ent2;
  311. int ptnum1 = 0;
  312. foreach (ObjectId vertexId in pl2d1)
  313. {
  314. Vertex2d vtx = tr.GetObject(vertexId, OpenMode.ForRead) as Vertex2d;
  315. Point2d point = new Point2d(pl2d1.VertexPosition(vtx).X, pl2d1.VertexPosition(vtx).Y); // 获取顶点坐标
  316. double bulge = vtx.Bulge; // 获取凸度
  317. pll1.AddVertexAt(ptnum1, point, bulge, 0, 0);
  318. ptnum1++;
  319. }
  320. pll1.Layer = pl2d1.Layer;
  321. pll1.Color = pl2d1.Color;
  322. pll1.Linetype = pl2d1.Linetype;
  323. pll1.Elevation = pl2d1.Elevation;
  324. pll1.Plinegen = pl2d1.LinetypeGenerationOn;
  325. pll1.ConstantWidth = pl2d1.ConstantWidth;
  326. pll1.Closed = pl2d1.Closed;
  327. int ptnum2 = 0;
  328. foreach (ObjectId vertexId in pl2d2)
  329. {
  330. Vertex2d vtx = tr.GetObject(vertexId, OpenMode.ForRead) as Vertex2d;
  331. Point2d point = new Point2d(pl2d2.VertexPosition(vtx).X, pl2d2.VertexPosition(vtx).Y); // 获取顶点坐标
  332. double bulge = vtx.Bulge; // 获取凸度
  333. pll2.AddVertexAt(ptnum2, point, bulge, 0, 0);
  334. ptnum2++;
  335. }
  336. pll2.Layer = pl2d2.Layer;
  337. pll2.Color = pl2d2.Color;
  338. pll2.Linetype = pl2d2.Linetype;
  339. pll2.Elevation = pl2d2.Elevation;
  340. pll2.Plinegen = pl2d2.LinetypeGenerationOn;
  341. pll2.ConstantWidth = pl2d2.ConstantWidth;
  342. pll2.Closed = pl2d2.Closed;
  343. }
  344. if (!pll1.Closed)
  345. {
  346. if (pll2.StartPoint.DistanceTo(pll1.EndPoint) > Tolerance.Global.EqualPoint)
  347. {
  348. pll2.ReverseCurve(); // 反转多段线顶点顺序
  349. }
  350. // 获取顶点
  351. var vertices = new List<Point2d>();
  352. for (int i = 0; i < pll1.NumberOfVertices; i++)
  353. vertices.Add(pll1.GetPoint2dAt(i));
  354. for (int i = 0; i < pll2.NumberOfVertices; i++)
  355. vertices.Add(pll2.GetPoint2dAt(i));
  356. // 创建新多段线
  357. var newPline = new Polyline();
  358. for (int i = 0; i < vertices.Count; i++)
  359. newPline.AddVertexAt(i, vertices[i], 0, 0, 0);
  360. newPline.Layer = pll1.Layer;
  361. newPline.Elevation = pll1.Elevation;
  362. newPline.Linetype = pll1.Linetype;
  363. newPline.Color = pll1.Color;
  364. newPline.Plinegen = pll1.Plinegen;
  365. newPline.ConstantWidth = pll1.ConstantWidth;
  366. Btr.AppendEntity(newPline);
  367. tr.AddNewlyCreatedDBObject(newPline, true);
  368. }
  369. else
  370. {
  371. // 将多段线转换为曲线集合
  372. DBObjectCollection curves1 = new DBObjectCollection();
  373. curves1.Add(pll1);
  374. DBObjectCollection curves2 = new DBObjectCollection();
  375. curves2.Add(pll2);
  376. closedjoin(curves1, curves2, Btr, tr,pll1);
  377. }
  378. }
  379. else if(ent1 is Polyline3d)
  380. {
  381. Polyline3d pl3d1 = (Polyline3d)ent1;
  382. Polyline3d pl3d2 = (Polyline3d)ent2;
  383. if(!pl3d1.Closed)
  384. {
  385. Polyline3d pl3d_new = new Polyline3d();
  386. if (pl3d2.StartPoint.DistanceTo(pl3d1.EndPoint) > Tolerance.Global.EqualPoint)
  387. {
  388. pl3d2.ReverseCurve(); // 反转多段线顶点顺序
  389. }
  390. Point3dCollection Verptcoll1 = new Point3dCollection();
  391. pl3d1.GetStretchPoints(Verptcoll1);
  392. Point3dCollection Verptcoll2 = new Point3dCollection();
  393. pl3d2.GetStretchPoints(Verptcoll2);
  394. foreach (Point3d pt in Verptcoll2)
  395. Verptcoll1.Add(pt);
  396. foreach(Point3d pt in Verptcoll1)
  397. {
  398. pl3d_new.AppendVertex(new PolylineVertex3d(pt));
  399. }
  400. Btr.AppendEntity(pl3d_new);
  401. tr.AddNewlyCreatedDBObject(pl3d_new, true);
  402. }
  403. else
  404. {
  405. DBObjectCollection curves1 = new DBObjectCollection();
  406. DBObjectCollection curves2 = new DBObjectCollection();
  407. curves1.Add(pl3d1);
  408. curves2.Add(pl3d2);
  409. closedjoin(curves1, curves2, Btr, tr, pl3d1);
  410. }
  411. }
  412. }
  413. private void closedjoin(DBObjectCollection curves1, DBObjectCollection curves2,
  414. BlockTableRecord Btr, Transaction tr, Entity ent)
  415. {
  416. OutputbyTK_Form ot = new OutputbyTK_Form();
  417. // 创建 Region
  418. DBObjectCollection regions1 = new DBObjectCollection();
  419. DBObjectCollection regions2 = new DBObjectCollection();
  420. try
  421. {
  422. // 将多段线转换为 Region
  423. regions1 = GrxCAD.DatabaseServices.Region.CreateFromCurves(curves1);
  424. regions2 = GrxCAD.DatabaseServices.Region.CreateFromCurves(curves2);
  425. }
  426. catch (Exception ex)
  427. {
  428. GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage($"\n错误: {ex.Message}");
  429. }
  430. // 执行布尔运算(并集)
  431. GrxCAD.DatabaseServices.Region region1 = (GrxCAD.DatabaseServices.Region)regions1[0];
  432. GrxCAD.DatabaseServices.Region region2 = (GrxCAD.DatabaseServices.Region)regions2[0];
  433. region1.BooleanOperation(BooleanOperationType.BoolUnite, region2);
  434. DBObjectCollection explodedCurves = new DBObjectCollection();
  435. region1.Explode(explodedCurves);
  436. if (ent is Polyline)
  437. {
  438. Polyline polyline = ot.createpllfromregion(explodedCurves);
  439. Polyline pll1 = (Polyline)ent;
  440. polyline.Elevation = pll1.Elevation;
  441. polyline.Layer = pll1.Layer;
  442. polyline.Color = pll1.Color;
  443. polyline.Linetype = pll1.Linetype;
  444. polyline.Plinegen = pll1.Plinegen;
  445. polyline.ConstantWidth = polyline.ConstantWidth;
  446. Btr.AppendEntity(polyline);
  447. tr.AddNewlyCreatedDBObject(polyline, true);
  448. }
  449. else if(ent is Polyline3d)
  450. {
  451. Polyline3d ply3d_region = ot.createpl3dfromregion(explodedCurves);
  452. Polyline3d ply3d = (Polyline3d)ent;
  453. ply3d_region.Layer = ply3d.Layer;
  454. ply3d_region.Color = ply3d.Color;
  455. ply3d_region.Linetype = ply3d.Linetype;
  456. ply3d_region.LineWeight = ply3d.LineWeight;
  457. }
  458. }
  459. }
  460. }