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

FeatureOverlap.cs 24KB

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