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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using GrxCAD.DatabaseServices;
  7. using GrxCAD.Geometry;
  8. using GrxCAD.ApplicationServices;
  9. using GrxCAD.EditorInput;
  10. using System.Windows.Forms;
  11. namespace HCTools
  12. {
  13. class Connect
  14. {
  15. Point3dCollection joinpoint = new Point3dCollection();//连接点
  16. #region 连接
  17. /// <summary>
  18. /// 连接线
  19. /// </summary>
  20. public void joinplline()
  21. {
  22. ObjectId[] line = BasicFunction.getPll();//获取待连接线
  23. if (line == null)
  24. return;
  25. //遍历比较待连接线,连接高程相同的两等高线
  26. Database db = HostApplicationServices.WorkingDatabase;
  27. DocumentLock doclock = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  28. Editor ed = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  29. using (Transaction trans = db.TransactionManager.StartTransaction())
  30. {
  31. if (line.Length == 1)
  32. {
  33. ed.WriteMessage("请选择另一条连接线");
  34. return;
  35. }
  36. try
  37. {
  38. Entity entity1 = line[0].GetObject(OpenMode.ForWrite)as Entity;
  39. Entity entity2 = line[1].GetObject(OpenMode.ForWrite) as Entity;
  40. if (entity1 is Polyline2d && entity2 is Polyline)
  41. {
  42. Polyline2d pll1 = (Polyline2d)entity1;
  43. Polyline pll2 = (Polyline)trans.GetObject(entity2.Id, OpenMode.ForWrite);
  44. pll2.Elevation = pll1.Elevation;
  45. Calculate(pll1.StartPoint, pll1.EndPoint, pll2.StartPoint, pll2.EndPoint);
  46. Polyline pline = new Polyline();
  47. pline.AddVertexAt(0, new Point2d(joinpoint[0].X, joinpoint[0].Y), 0, 0, 0);
  48. pline.AddVertexAt(1, new Point2d(joinpoint[1].X, joinpoint[1].Y), 0, 0, 0);//创建连接线
  49. if (pll2.Elevation == pll1.Elevation)
  50. {
  51. //pll1.JoinEntity(pline);//浩辰的JoinEntity函数会报enotemplementedyet,不知道原因。
  52. Polyline pll_temp = PllConn(pll2, pline);
  53. //pll1.JoinEntity(pll2);
  54. Polyline pll_final = Pllpll2d(pll_temp, pll1, trans);
  55. pll1.Erase();
  56. pll2.Erase();
  57. BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));
  58. BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  59. btr.AppendEntity(pll_final);
  60. trans.AddNewlyCreatedDBObject(pll_final, true);
  61. }
  62. else
  63. MessageBox.Show("两线高程不一致!", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
  64. }
  65. else if (entity1 is Polyline && entity2 is Polyline2d)
  66. {
  67. Polyline pll1 = (Polyline)entity1;
  68. Polyline2d pll2 = (Polyline2d)entity2;
  69. Calculate(pll1.StartPoint, pll1.EndPoint, pll2.StartPoint, pll2.EndPoint);
  70. Polyline pline = new Polyline();
  71. pline.AddVertexAt(0, new Point2d(joinpoint[0].X, joinpoint[0].Y), 0, 0, 0);
  72. pline.AddVertexAt(1, new Point2d(joinpoint[1].X, joinpoint[1].Y), 0, 0, 0);//创建连接线
  73. if (pll2.Elevation == pll1.Elevation)
  74. {
  75. //pll1.JoinEntity(pline);//浩辰的JoinEntity函数会报enotemplementedyet,不知道原因。
  76. Polyline pll_temp = PllConn(pll1,pline);
  77. //pll1.JoinEntity(pll2);
  78. Polyline pll_final = Pllpll2d(pll_temp, pll2, trans);
  79. pll1.Erase();
  80. pll2.Erase();
  81. BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));
  82. BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  83. btr.AppendEntity(pll_final);
  84. trans.AddNewlyCreatedDBObject(pll_final, true);
  85. }
  86. else
  87. MessageBox.Show("两线高程不一致!", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
  88. }
  89. else if (entity1 is Polyline2d && entity2 is Polyline2d)
  90. {
  91. Polyline2d pll1 = (Polyline2d)entity1;
  92. Polyline2d pll2 = (Polyline2d)entity2;
  93. Calculate(pll1.StartPoint, pll1.EndPoint, pll2.StartPoint, pll2.EndPoint);
  94. Point3dCollection pos = new Point3dCollection{joinpoint[0], joinpoint[1]};
  95. DoubleCollection doubles = new DoubleCollection{0,0,0,0};
  96. Polyline2d pline = new Polyline2d(Poly2dType.SimplePoly, pos, 0, false, 0, 0, doubles);
  97. BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));
  98. BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  99. btr.AppendEntity(pline);
  100. trans.AddNewlyCreatedDBObject(pline, true);
  101. //pline.AddVertexAt(0, new Point2d(joinpoint[0].X, joinpoint[0].Y), 0, 0, 0);
  102. //pline.AddVertexAt(1, new Point2d(joinpoint[1].X, joinpoint[1].Y), 0, 0, 0);//创建连接线
  103. if (pll2.Elevation == pll1.Elevation)
  104. {
  105. Polyline2d pll_temp = Pll2dConn(pll1, pline,trans);
  106. btr.AppendEntity(pll_temp);
  107. trans.AddNewlyCreatedDBObject(pll_temp, true);
  108. //pll1.JoinEntity(pline);
  109. //pll1.JoinEntity(pll2);
  110. Polyline2d pll_final = Pll2dConn(pll_temp, pll2,trans);
  111. pll1.Erase();
  112. pline.Erase();
  113. pll2.Erase();
  114. pll_temp.Erase();
  115. btr.AppendEntity(pll_final);
  116. trans.AddNewlyCreatedDBObject(pll_final, true);
  117. }
  118. else
  119. MessageBox.Show("两线高程不一致!", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
  120. }
  121. else
  122. {
  123. Polyline pll1 = (Polyline)entity1;
  124. Polyline pll2 = (Polyline)entity2;
  125. Calculate(pll1.StartPoint, pll1.EndPoint, pll2.StartPoint, pll2.EndPoint);
  126. Polyline pline = new Polyline();
  127. pline.AddVertexAt(0, new Point2d(joinpoint[0].X, joinpoint[0].Y), 0, 0, 0);
  128. pline.AddVertexAt(1, new Point2d(joinpoint[1].X, joinpoint[1].Y), 0, 0, 0);//创建连接线
  129. if (pll2.Elevation == pll1.Elevation)
  130. {
  131. //pll1.JoinEntity(pline);//浩辰的JoinEntity函数会报enotemplementedyet,不知道原因。
  132. Polyline pll_temp = PllConn(pll1, pline);
  133. //pll1.JoinEntity(pll2);
  134. Polyline pll_final = PllConn(pll_temp, pll2);
  135. pll1.Erase();
  136. pll2.Erase();
  137. BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));
  138. BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  139. btr.AppendEntity(pll_final);
  140. trans.AddNewlyCreatedDBObject(pll_final, true);
  141. }
  142. else
  143. MessageBox.Show("两线高程不一致!", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);
  144. }
  145. trans.Commit();
  146. }
  147. catch (GrxCAD.Runtime.Exception ex)
  148. {
  149. string str = ex.ToString();
  150. GrxCAD.ApplicationServices.Application.ShowAlertDialog(str);
  151. }
  152. }
  153. }
  154. /// <summary>
  155. /// 计算获得连接点
  156. /// </summary>
  157. private Point3dCollection Calculate(Point3d startpt1, Point3d endpt1, Point3d startpt2, Point3d endpt2)
  158. {
  159. Double[] distance=new Double[4];
  160. joinpoint.Clear();
  161. int i = 0;
  162. //两线段四个端点之间的距离
  163. distance[0] = Math.Sqrt(Math.Pow((startpt1.X - startpt2.X), 2) + Math.Pow((startpt1.Y - startpt2.Y), 2));
  164. distance[1] = Math.Sqrt(Math.Pow((startpt1.X - endpt2.X), 2) + Math.Pow((startpt1.Y - endpt2.Y), 2));
  165. distance[2] = Math.Sqrt(Math.Pow((endpt1.X - startpt2.X), 2) + Math.Pow((endpt1.Y - startpt2.Y), 2));
  166. distance[3] = Math.Sqrt(Math.Pow((endpt1.X - endpt2.X), 2) + Math.Pow((endpt1.Y - endpt2.Y), 2));
  167. //比较获取最小的距离,得到连接点
  168. for (i = 0; i < distance.Length; i++)
  169. {
  170. if (distance[i] == distance.Min())
  171. break;
  172. }
  173. switch (i.ToString())
  174. {
  175. case "0":
  176. joinpoint.Add(startpt1);
  177. joinpoint.Add(startpt2);
  178. break;
  179. case "1":
  180. joinpoint.Add(startpt1);
  181. joinpoint.Add(endpt2);
  182. break;
  183. case "2":
  184. joinpoint.Add(endpt1);
  185. joinpoint.Add(startpt2);
  186. break;
  187. case "3":
  188. joinpoint.Add(endpt1);
  189. joinpoint.Add(endpt2);
  190. break;
  191. }
  192. return joinpoint;
  193. }
  194. /// <summary>
  195. /// 连接两条线(两条都是Polyline)
  196. /// </summary>
  197. private Polyline PllConn(Polyline pll1, Polyline pll2)
  198. {
  199. List<Point2d> pllpt = new List<Point2d>();
  200. List<Point2d> pll1pt = new List<Point2d>();
  201. for (int i = 0; i < pll1.NumberOfVertices; i++)
  202. {
  203. pllpt.Add(pll1.GetPoint2dAt(i));
  204. }
  205. for (int i = 0; i < pll2.NumberOfVertices; i++)
  206. {
  207. pll1pt.Add(pll2.GetPoint2dAt(i));
  208. }
  209. //if (pllpt[pllpt.Count - 1] != pll1pt[pll1pt.Count - 1])
  210. // pllpt.Reverse();
  211. //else if (pllpt[0] != pll1pt[0])
  212. // pll1pt.Reverse();
  213. if (pllpt[pllpt.Count - 1] == pll1pt[pll1pt.Count - 1])
  214. pll1pt.Reverse();
  215. else if (pllpt[0] == pll1pt[pll1pt.Count - 1])
  216. {
  217. pllpt.Reverse();
  218. pll1pt.Reverse();
  219. }
  220. else if (pllpt[0] == pll1pt[0])
  221. pllpt.Reverse();
  222. for (int i = 0; i < pll1pt.Count; i++)
  223. {
  224. if (pllpt.Contains(pll1pt[i]))
  225. continue;
  226. else
  227. pllpt.Add(pll1pt[i]);
  228. }
  229. Polyline pline = new Polyline();
  230. for (int i = 0; i < pllpt.Count; i++)
  231. {
  232. pline.AddVertexAt(i, new Point2d(pllpt[i].X, pllpt[i].Y), 0, 0, 0);
  233. }
  234. pline.Elevation = pll1.Elevation;
  235. pline.LinetypeId = pll1.LinetypeId;
  236. pline.Color = pll1.Color;
  237. pline.Layer = pll1.Layer;
  238. return pline;
  239. }
  240. /// <summary>
  241. /// 连接两条线(两条都是Polyline2d)
  242. /// </summary>
  243. private Polyline2d Pll2dConn(Polyline2d pll1, Polyline2d pll2, Transaction tr)
  244. {
  245. List<Point3d> pllpt = new List<Point3d>();
  246. List<Point3d> pll1pt = new List<Point3d>();
  247. List<double> bulgeList = new List<double>();//存凸度
  248. List<double> bulgeList1 = new List<double>();
  249. Point3dCollection ptcoll = new Point3dCollection();
  250. DoubleCollection dbcoll = new DoubleCollection();
  251. foreach(ObjectId id in pll1)
  252. {
  253. Vertex2d vx = (Vertex2d)tr.GetObject(id, OpenMode.ForRead);
  254. if (vx.VertexType != Vertex2dType.SplineControlVertex)
  255. {
  256. pllpt.Add(vx.Position);
  257. bulgeList.Add(vx.Bulge);
  258. }
  259. }
  260. foreach (ObjectId id in pll2)
  261. {
  262. Vertex2d vx = (Vertex2d)tr.GetObject(id, OpenMode.ForRead);
  263. if (vx.VertexType != Vertex2dType.SplineControlVertex)
  264. {
  265. pll1pt.Add(vx.Position);
  266. bulgeList1.Add(vx.Bulge);
  267. }
  268. }
  269. //if (pllpt[pllpt.Count - 1] != pll1pt[pll1pt.Count - 1])
  270. //{
  271. // pllpt.Reverse();
  272. // bulgeList.Reverse();
  273. //}
  274. //else if (pllpt[0] != pll1pt[0])
  275. //{
  276. // pll1pt.Reverse();
  277. // bulgeList1.Reverse();
  278. //}
  279. if (pllpt[pllpt.Count - 1] == pll1pt[pll1pt.Count - 1])
  280. {
  281. pll1pt.Reverse();
  282. bulgeList1.Reverse();
  283. }
  284. else if (pllpt[0] == pll1pt[pll1pt.Count - 1])
  285. {
  286. pllpt.Reverse();
  287. pll1pt.Reverse();
  288. bulgeList.Reverse();
  289. bulgeList1.Reverse();
  290. }
  291. else if (pllpt[0] == pll1pt[0])
  292. {
  293. pllpt.Reverse();
  294. bulgeList1.Reverse();
  295. }
  296. for (int i = 0; i < pll1pt.Count; i++)
  297. {
  298. if (pllpt.Contains(pll1pt[i]))
  299. continue;
  300. else
  301. {
  302. pllpt.Add(pll1pt[i]);
  303. bulgeList.Add(bulgeList1[i]);
  304. }
  305. }
  306. for (int i = 0; i < pllpt.Count; i++)
  307. {
  308. ptcoll.Add(pllpt[i]);
  309. }
  310. for (int i = 0; i < bulgeList.Count; i++)
  311. {
  312. dbcoll.Add(bulgeList[i]);
  313. }
  314. Polyline2d pline = new Polyline2d(pll1.PolyType, ptcoll, 0, false, 0, 0, dbcoll);
  315. pline.Elevation = pll1.Elevation;
  316. pline.LinetypeId = pll1.LinetypeId;
  317. pline.Color = pll1.Color;
  318. pline.Layer = pll1.Layer;
  319. return pline;
  320. }
  321. /// <summary>
  322. /// 连接两条线(一条是Polyline,一条是Polyline2d,连接后的线为Polyline)
  323. /// </summary>
  324. private Polyline Pllpll2d(Polyline pll1, Polyline2d pll2, Transaction tr)
  325. {
  326. List<Point3d> pllpt = new List<Point3d>();
  327. List<Point3d> pll1pt = new List<Point3d>();
  328. for (int i = 0; i < pll1.NumberOfVertices; i++)
  329. {
  330. pllpt.Add(pll1.GetPoint3dAt(i));
  331. }
  332. foreach (ObjectId id in pll2)
  333. {
  334. Vertex2d vx = (Vertex2d)tr.GetObject(id, OpenMode.ForRead);
  335. if (vx.VertexType != Vertex2dType.SplineControlVertex)
  336. {
  337. pll1pt.Add(vx.Position);
  338. }
  339. }
  340. //if (pllpt[pllpt.Count - 1] != pll1pt[pll1pt.Count - 1])
  341. // pllpt.Reverse();
  342. //else if (pllpt[0] != pll1pt[0])
  343. // pll1pt.Reverse();
  344. if (pllpt[pllpt.Count - 1] == pll1pt[pll1pt.Count - 1])
  345. pll1pt.Reverse();
  346. else if (pllpt[0] == pll1pt[pll1pt.Count - 1])
  347. {
  348. pllpt.Reverse();
  349. pll1pt.Reverse();
  350. }
  351. else if (pllpt[0] == pll1pt[0])
  352. pllpt.Reverse();
  353. for (int i = 0; i < pll1pt.Count; i++)
  354. {
  355. if (pllpt.Contains(pll1pt[i]))
  356. continue;
  357. else
  358. pllpt.Add(pll1pt[i]);
  359. }
  360. Polyline pline = new Polyline();
  361. for (int i = 0; i < pllpt.Count; i++)
  362. {
  363. pline.AddVertexAt(i, new Point2d(pllpt[i].X, pllpt[i].Y), 0, 0, 0);
  364. }
  365. pline.Elevation = pll1.Elevation;
  366. pline.LinetypeId = pll1.LinetypeId;
  367. pline.Color = pll1.Color;
  368. pline.Layer = pll1.Layer;
  369. return pline;
  370. }
  371. #endregion
  372. //public void conn()
  373. //{
  374. // ObjectIdCollection ids = getEntity();
  375. // BlockReference br = null;
  376. // Polyline pll = null;
  377. // DBObjectCollection objcoll = new DBObjectCollection();
  378. // string Brname = "";
  379. // Point3d Brposition = new Point3d();
  380. // Database db = HostApplicationServices.WorkingDatabase;
  381. // DocumentLock doclock = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  382. // Editor ed = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  383. // using (Transaction trans = db.TransactionManager.StartTransaction())
  384. // {
  385. // foreach(ObjectId id in ids)
  386. // {
  387. // if (trans.GetObject(id, OpenMode.ForRead) is BlockReference)
  388. // {
  389. // br = trans.GetObject(id, OpenMode.ForRead)as BlockReference;
  390. // Brname = br.Name;
  391. // Brposition = br.Position;
  392. // }
  393. // if (trans.GetObject(id, OpenMode.ForRead) is Polyline)
  394. // {
  395. // pll = trans.GetObject(id, OpenMode.ForRead) as Polyline;
  396. // }
  397. // }
  398. // br.Explode(objcoll);
  399. // trans.Commit();
  400. // }
  401. // foreach (Entity ent in objcoll)
  402. // {
  403. // using (Transaction trans = db.TransactionManager.StartTransaction())
  404. // {
  405. // BlockTable blocktable = trans.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
  406. // BlockTableRecord blocktablerecord = trans.GetObject(blocktable[BlockTableRecord.ModelSpace],
  407. // OpenMode.ForWrite) as BlockTableRecord;
  408. // blocktablerecord.AppendEntity(ent);
  409. // trans.AddNewlyCreatedDBObject(ent, true);
  410. // trans.Commit();
  411. // }
  412. // }
  413. // doclock.Dispose();
  414. //}
  415. //private static ObjectIdCollection getEntity()
  416. //{
  417. // ObjectIdCollection ids = new ObjectIdCollection();
  418. // Document doc = Application.DocumentManager.MdiActiveDocument;
  419. // Database db = doc.Database;
  420. // Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  421. // TypedValue[] value = new TypedValue[]
  422. // {
  423. // new TypedValue((int)DxfCode.Start,"Insert,POLYLINE"),
  424. // };//设置筛选条件
  425. // SelectionFilter filter = new SelectionFilter(value);
  426. // //选择区域中全部对象
  427. // //PromptSelectionResult psr = ed.SelectAll(filter);
  428. // // 要求在图形区域中手动选择对象
  429. // PromptSelectionResult psr = ed.GetSelection(filter);
  430. // if (psr.Status == PromptStatus.OK)
  431. // {
  432. // using (Transaction trans = db.TransactionManager.StartTransaction())
  433. // {
  434. // SelectionSet ss = psr.Value;
  435. // ids = new ObjectIdCollection(ss.GetObjectIds());
  436. // trans.Commit();
  437. // }
  438. // }
  439. // return ids;
  440. //}
  441. }
  442. }