工具箱相关
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

FeatureOverlap.cs 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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 FeatureOverlap
  13. {
  14. //比例尺
  15. public static int blc;
  16. public static bool move;
  17. //重叠地物数量
  18. private int number = 0;
  19. public void check()
  20. {
  21. LayerControl lc = new LayerControl();
  22. if (lc.haslayername("地物重叠") == false)
  23. {
  24. lc.creatlayer("地物重叠", new colorgb(255, 0, 255));
  25. lc.movelayertofront("地物重叠");
  26. }
  27. else
  28. {
  29. lc.movelayertofront("地物重叠");
  30. }
  31. //选择块
  32. selecblock();
  33. search(blocklist,entitylist);
  34. }
  35. /// <summary>
  36. /// 定义一个块类,主要包括位置和名字
  37. /// </summary>
  38. public struct Block
  39. {
  40. public double x;
  41. public double y;
  42. public double z;
  43. public string name;
  44. public Block(double x, double y, double z, string name)
  45. {
  46. this.x = x;
  47. this.y = y;
  48. this.z = z;
  49. this.name = name;
  50. }
  51. }
  52. //块列表
  53. private List<Block> blocklist = new List<Block>();
  54. private List<Entity> entitylist = new List<Entity>();
  55. private void selecblock()
  56. {
  57. Document document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  58. Database database = HostApplicationServices.WorkingDatabase;
  59. DocumentLock documentlock = null;
  60. Transaction traction = null;
  61. try
  62. {
  63. traction = database.TransactionManager.StartTransaction();
  64. documentlock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  65. BlockTable blocktable = traction.GetObject(database.BlockTableId,
  66. OpenMode.ForRead) as BlockTable;
  67. Editor editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  68. TypedValue[] typedvalue = new TypedValue[1];
  69. typedvalue.SetValue(new TypedValue((int)DxfCode.Start, "Insert"), 0);
  70. SelectionFilter selectionfilter = new SelectionFilter(typedvalue);
  71. PromptSelectionResult psr = editor.GetSelection(selectionfilter); ///手动选择块参照
  72. //PromptSelectionResult psr = editor.SelectAll();
  73. if (psr.Status == PromptStatus.OK)
  74. {
  75. SelectionSet selectionset = psr.Value;
  76. ObjectId[] obj = new ObjectId[selectionset.Count];
  77. obj = selectionset.GetObjectIds();
  78. for (int i = 0; i < obj.Length; i++)
  79. {
  80. ObjectId objid = obj[i];
  81. Entity entity = (Entity)traction.GetObject(objid, OpenMode.ForWrite);
  82. if (entity is BlockReference)
  83. {
  84. BlockReference block = entity as BlockReference;
  85. ObjectId id = block.BlockTableRecord;
  86. BlockTableRecord blocktablerecord = traction.GetObject(id,
  87. OpenMode.ForRead) as BlockTableRecord;
  88. string blockname = blocktablerecord.Name.ToLower();
  89. Block blc = new Block();
  90. ///块参照的位置
  91. blc.x = block.Position.X;
  92. blc.y = block.Position.Y;
  93. blc.z = block.Position.Z;
  94. blc.name = blockname;
  95. ///块参照的名称
  96. switch (blockname)
  97. {
  98. case "gc028":
  99. case "gc090":
  100. case "gc110":
  101. case "gc111":
  102. case "gc119":
  103. case "gc120":
  104. case "gc121":
  105. case "gc122":
  106. case "gc124":
  107. case "gc125":
  108. case "gc126":
  109. case "gc143":
  110. case "gc144":
  111. case "gc145":
  112. case "gc160":
  113. case "gc163":
  114. case "gc164":
  115. case "gc165":
  116. case "gc200":
  117. case "gc205":
  118. case "gc206":
  119. case "gc207":
  120. case "gc208":
  121. case "gc210":
  122. case "gc211":
  123. case "gc213":
  124. case "gc246":
  125. case "gc251":
  126. case "gc253":
  127. case "gc254":
  128. case "gc319":
  129. case "gc320":
  130. case "gc322":
  131. case "gc323":
  132. case "gc325":
  133. case "gc326":
  134. entitylist.Add(entity);
  135. blocklist.Add(blc);
  136. break;
  137. case "gc170":
  138. if (block.Layer == "9000")
  139. {
  140. entitylist.Add(entity);
  141. blocklist.Add(blc);
  142. }
  143. break;
  144. default:
  145. break;
  146. }
  147. }
  148. }
  149. editor.WriteMessage("\n" + "选中块数量:" + blocklist.Count.ToString());
  150. }
  151. traction.Commit();
  152. }
  153. catch (Autodesk.AutoCAD.Runtime.Exception)
  154. {
  155. traction.Abort();
  156. return;
  157. }
  158. finally
  159. {
  160. traction.Dispose();
  161. documentlock.Dispose();
  162. }
  163. }
  164. /// <summary>
  165. /// 寻找与上面已选实体相交的那部分
  166. /// </summary>
  167. /// <param name="list"></param>
  168. public void search(List<Block> list, List<Entity> list1)
  169. {
  170. Database database = HostApplicationServices.WorkingDatabase;
  171. Transaction traction = null;
  172. DocumentLock documentlock = null;
  173. try
  174. {
  175. Editor editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  176. for (int i = 0; i < list.Count; i++)
  177. {
  178. double x = Math.Round((double)list[i].x, 3, MidpointRounding.AwayFromZero);
  179. double y = Math.Round((double)list[i].y, 3, MidpointRounding.AwayFromZero);
  180. Entity entity1 = list1[i];
  181. string name = list[i].name;
  182. rebound reb = get_boundary(name);
  183. double height = reb.height;
  184. double width = reb.width;
  185. int type = reb.type;
  186. ///按照比例尺决定大小
  187. if (blc == 1000)
  188. {
  189. height = height / 2;
  190. width = width / 2;
  191. }
  192. else if (blc == 500)
  193. {
  194. height = height / 4;
  195. width = width / 4;
  196. }
  197. else if (blc == 2000)
  198. {
  199. height = height * 1;
  200. width = width * 1;
  201. }
  202. ///是为了获得四个角,好框选整个图形
  203. Point3d p1 = new Point3d(x - width, y, 0);
  204. Point3d p2 = new Point3d(x + width, y, 0);
  205. Point3d p3 = new Point3d(x + width, y + height, 0);
  206. Point3d p4 = new Point3d(x - width, y + height, 0);
  207. if (type == 2)
  208. {
  209. p1 = new Point3d(x - width, y - height, 0);
  210. p2 = new Point3d(x + width, y - height, 0);
  211. p3 = new Point3d(x + width, y + height, 0);
  212. p4 = new Point3d(x - width, y + height, 0);
  213. }
  214. else if (type == 3)
  215. {
  216. p1 = new Point3d(x - width, y - height, 0);
  217. p2 = new Point3d(x + width, y - height, 0);
  218. p3 = new Point3d(x + width, y, 0);
  219. p4 = new Point3d(x - width, y, 0);
  220. }
  221. using (Transaction acTrans = database.TransactionManager.StartTransaction())
  222. {
  223. ViewTableRecord currview = editor.GetCurrentView();
  224. currview.CenterPoint = new Point2d(x, y);
  225. currview.ViewDirection = new Vector3d(0, 0, 1);
  226. currview.Width = 40;
  227. currview.Height = 40;
  228. editor.SetCurrentView(currview);
  229. acTrans.Commit();
  230. }
  231. PromptSelectionResult pselectresult = editor.SelectCrossingWindow(p1, p3);///框选范围内相交的实体
  232. ///如果有,按照类型对相交实体进行判断
  233. traction = database.TransactionManager.StartTransaction();
  234. documentlock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  235. BlockTable blocktable = traction.GetObject(database.BlockTableId,
  236. OpenMode.ForRead) as BlockTable;
  237. BlockTableRecord blocktablerecord = traction.GetObject(blocktable[BlockTableRecord.ModelSpace],
  238. OpenMode.ForWrite) as BlockTableRecord;
  239. if (pselectresult.Status == PromptStatus.OK)
  240. {
  241. SelectionSet selectionset = pselectresult.Value;
  242. ObjectId[] obj = new ObjectId[selectionset.Count];
  243. obj = selectionset.GetObjectIds();
  244. int count = 0;
  245. for (int j = 0; j < obj.Length; j++)
  246. {
  247. ObjectId objid = obj[j];
  248. Entity entity = (Entity)traction.GetObject(objid, OpenMode.ForWrite);
  249. ///块参照
  250. if (entity is BlockReference)
  251. {
  252. BlockReference cblock = entity as BlockReference;
  253. ObjectId cid = cblock.BlockTableRecord;
  254. BlockTableRecord crecord = traction.GetObject(cid,
  255. OpenMode.ForRead) as BlockTableRecord;
  256. string blockname = crecord.Name.ToLower();
  257. if (blockname != "gc086")
  258. {
  259. count++;
  260. }
  261. }
  262. ///文字
  263. else if (entity is DBText)
  264. {
  265. string layer = entity.Layer;
  266. if (layer != "0")
  267. {
  268. if (entity1 is BlockReference)
  269. {
  270. BlockReference cblock = entity1 as BlockReference;
  271. DBText txt = entity as DBText;
  272. if (cblock.Position.Z.ToString("N1") != txt.TextString)
  273. count++;
  274. }
  275. }
  276. }
  277. ///线
  278. else if (entity is Polyline || entity is Polyline2d)
  279. {
  280. string layer = entity.Layer;
  281. if (layer == "4400" || layer == "6100" || layer == "4300" || layer == "2000")
  282. {
  283. if (name != "gc200")
  284. {
  285. count++;
  286. }
  287. }
  288. }
  289. }
  290. if (count > 1)
  291. {
  292. double height1 = 2 * height;
  293. Point3d orip = new Point3d(x, y, 0);
  294. Point3d newp = new Point3d(x, y + height1, 0);
  295. if (name == "gc120" || name == "gc119" || name == "gc126" || name == "gc123" || name == "gc325" || name == "gc205"
  296. || name == "gc206" || name == "gc207" || name == "gc125" || name == "gc165" || name == "gc326" || name == "gc170"
  297. || name == "gc160" || name == "gc246" || name == "gc208" || name == "gc121" || name == "gc163" || name == "gc164"
  298. || name == "gc090" || name == "gc124" || name == "gc379")
  299. {
  300. if (entity1.IsWriteEnabled == true&&move)
  301. {
  302. Move(entity1, orip, newp);
  303. double px1 = p1.X;
  304. double py1 = p1.Y;
  305. double px2 = p2.X;
  306. double py2 = p2.Y;
  307. double px3 = p3.X;
  308. double py3 = p3.Y;
  309. double px4 = p4.X;
  310. double py4 = p4.Y;
  311. p1 = new Point3d(px1, py1 + height1, 0);
  312. p2 = new Point3d(px2, py2 + height1, 0);
  313. p3 = new Point3d(px3, py3 + height1, 0);
  314. p4 = new Point3d(px4, py4 + height1, 0);
  315. Point3dCollection coll = new Point3dCollection();
  316. coll.Add(p1);
  317. coll.Add(p2);
  318. coll.Add(p3);
  319. coll.Add(p4);
  320. Polyline2d line2d = new Polyline2d(Poly2dType.SimplePoly, coll, 0, true, 0, 0, null);
  321. line2d.SetDatabaseDefaults();
  322. line2d.Layer = "地物重叠";
  323. blocktablerecord.AppendEntity(line2d);
  324. traction.AddNewlyCreatedDBObject(line2d, true);
  325. }
  326. else
  327. {
  328. double px1 = p1.X;
  329. double py1 = p1.Y;
  330. double px2 = p2.X;
  331. double py2 = p2.Y;
  332. double px3 = p3.X;
  333. double py3 = p3.Y;
  334. double px4 = p4.X;
  335. double py4 = p4.Y;
  336. p1 = new Point3d(px1, py1, 0);
  337. p2 = new Point3d(px2, py2, 0);
  338. p3 = new Point3d(px3, py3, 0);
  339. p4 = new Point3d(px4, py4, 0);
  340. Point3dCollection coll = new Point3dCollection();
  341. coll.Add(p1);
  342. coll.Add(p2);
  343. coll.Add(p3);
  344. coll.Add(p4);
  345. Polyline2d line2d = new Polyline2d(Poly2dType.SimplePoly, coll, 0, true, 0, 0, null);
  346. line2d.SetDatabaseDefaults();
  347. line2d.Layer = "地物重叠";
  348. blocktablerecord.AppendEntity(line2d);
  349. traction.AddNewlyCreatedDBObject(line2d, true);
  350. }
  351. }
  352. number++;
  353. }
  354. }
  355. traction.Commit();
  356. }
  357. editor.WriteMessage("\n");
  358. editor.WriteMessage("检测到重叠地物:" + number);
  359. editor.WriteMessage("\n");
  360. number = 0;
  361. }
  362. catch (Autodesk.AutoCAD.Runtime.Exception)
  363. {
  364. traction.Abort();
  365. return;
  366. }
  367. finally
  368. {
  369. if (documentlock != null)
  370. {
  371. documentlock.Dispose();
  372. }
  373. if (traction != null)
  374. {
  375. traction.Dispose();
  376. }
  377. }
  378. }
  379. /// <summary>
  380. /// type=1表示图形为上半部分,type=2表示图形为上下两部分,type=3表示下半部分(中心点跟图形的关系)
  381. /// </summary>
  382. private struct rebound
  383. {
  384. public double height;
  385. public double width;
  386. public int type;
  387. public rebound(double height, double width, int type)
  388. {
  389. this.height = height;
  390. this.width = width;
  391. this.type = type;
  392. }
  393. }
  394. /// <summary>
  395. /// 对每个地物图标的大小进行绘制
  396. /// </summary>
  397. /// <param name="name"></param>
  398. /// <returns></returns>
  399. private rebound get_boundary(string name)
  400. {
  401. double height = 0;
  402. double width = 0;
  403. int type = 0;
  404. switch (name)
  405. {
  406. case "gc028":
  407. height = 5;
  408. width = 4;
  409. type = 2;
  410. break;
  411. case "gc090":
  412. height = 2.5;
  413. width = 2.5;
  414. type = 2;
  415. break;
  416. //case "gc110":
  417. // height = 4;
  418. // width = 4;
  419. // type = 2;
  420. // break;
  421. //case "gc111":
  422. // height = 4;
  423. // width = 4;
  424. // type = 2;
  425. // break;
  426. case "gc119":
  427. height = 2.6;
  428. width = 2.6;
  429. type = 1;
  430. break;
  431. case "gc120":
  432. height = 5;
  433. width = 1;
  434. type = 1;
  435. break;
  436. case "gc121":
  437. height = 1.1;
  438. width = 1.1;
  439. type = 2;
  440. break;
  441. case "gc122":
  442. height = 2.1;
  443. width = 1.1;
  444. type = 2;
  445. break;
  446. case "gc123":
  447. height = 4.2;
  448. width = 2.1;
  449. type = 2;
  450. break;
  451. case "gc124":
  452. height = 3;
  453. width = 1.5;
  454. type = 1;
  455. break;
  456. case "gc125":
  457. height = 3.8;
  458. width = 1.2;
  459. type = 2;
  460. break;
  461. case "gc126":
  462. height = 2.5;
  463. width = 3;
  464. type = 2;
  465. break;
  466. case "gc143":
  467. height = 6;
  468. width = 2;
  469. type = 1;
  470. break;
  471. case "gc144":
  472. height = 6;
  473. width = 2;
  474. type = 1;
  475. break;
  476. case "gc145":
  477. height = 6;
  478. width = 2;
  479. type = 1;
  480. break;
  481. case "gc160":
  482. height = 4.7;
  483. width = 4.7;
  484. type = 2;
  485. break;
  486. case "gc163":
  487. height = 0.6;
  488. width = 2.5;
  489. type = 2;
  490. break;
  491. case "gc164":
  492. height = 0.7;
  493. width = 0.7;
  494. type = 2;
  495. break;
  496. case "gc165":
  497. height = 5;
  498. width = 1;
  499. type = 2;
  500. break;
  501. case "gc170":
  502. height = 1.1;
  503. width = 1.1;
  504. type = 2;
  505. break;
  506. case "gc200":
  507. height = 1.5;
  508. width = 1.5;
  509. type = 2;
  510. break;
  511. case "gc205":
  512. height = 5;
  513. width = 2;
  514. type = 1;
  515. break;
  516. case "gc206":
  517. height = 5;
  518. width = 2;
  519. type = 1;
  520. break;
  521. case "gc207":
  522. height = 4;
  523. width = 1;
  524. type = 2;
  525. break;
  526. case "gc208":
  527. height = 1.6;
  528. width = 2.4;
  529. type = 2;
  530. break;
  531. case "gc210":
  532. height = 6;
  533. width = 2.2;
  534. type = 1;
  535. break;
  536. case "gc211":
  537. height = 3;
  538. width = 2.8;
  539. type = 3;
  540. break;
  541. case "gc212":
  542. height = 5;
  543. width = 2.6;
  544. type = 1;
  545. break;
  546. case "gc246":
  547. height = 6;
  548. width = 4;
  549. type = 1;
  550. break;
  551. case "gc251":
  552. height = 1;
  553. width = 3.5;
  554. type = 2;
  555. break;
  556. case "gc253":
  557. height = 0.7;
  558. width = 0.7;
  559. type = 2;
  560. break;
  561. case "gc254":
  562. height = 3;
  563. width = 2.5;
  564. type = 2;
  565. break;
  566. case "gc319":
  567. height = 6;
  568. width = 2;
  569. type = 1;
  570. break;
  571. case "gc320":
  572. height = 6;
  573. width = 2;
  574. type = 1;
  575. break;
  576. case "gc322":
  577. height = 6;
  578. width = 2;
  579. type = 1;
  580. break;
  581. case "gc323":
  582. height = 4;
  583. width = 1.1;
  584. type = 1;
  585. break;
  586. case "gc325":
  587. height = 5;
  588. width = 0.7;
  589. type = 1;
  590. break;
  591. case "gc326":
  592. height = 1.6;
  593. width = 1.6;
  594. type = 2;
  595. break;
  596. default:
  597. height = 2.5;
  598. width = 2;
  599. type = 2;
  600. break;
  601. }
  602. rebound rec = new rebound(height, width, type);
  603. return rec;
  604. }
  605. /// <summary>
  606. /// 指定基点与目标点移动实体
  607. /// </summary>
  608. /// <param name="ent">实体对象</param>
  609. /// <param name="BasePt">基点</param>
  610. /// <param name="TargetPt">目标点</param>
  611. public static void Move(Entity ent, Point3d basePt, Point3d targetPt)
  612. {
  613. Vector3d vec = targetPt - basePt;
  614. Matrix3d mt = Matrix3d.Displacement(vec);
  615. ent.TransformBy(mt);
  616. }
  617. }
  618. }