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

CheckPLOverlay.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6. using GrxCAD.Geometry;
  7. using GrxCAD.DatabaseServices;
  8. using GrxCAD.ApplicationServices;
  9. using GrxCAD.EditorInput;
  10. namespace HCTools
  11. {
  12. public struct CPoint
  13. {
  14. public double x;
  15. public double y;
  16. public double z;
  17. public object objID;
  18. }
  19. /// <summary>
  20. /// 判断点和线是否重叠
  21. /// </summary>
  22. class CheckPLOverlay
  23. {
  24. public static int blc;//比例尺
  25. public static string sqx;//首曲线
  26. public static string jqx;//计曲线
  27. public const int looptimes = 15;//循环查找次数
  28. public static string EleLayerName;//高程点图层名
  29. public int windowwidth;//当前可视范围
  30. public int windowheight;
  31. public double radius;//半径
  32. public double length;//搜索半径
  33. //存放所有高程点数组
  34. public List<CPoint> pointarrlist = new List<CPoint>();
  35. //存放有问题的高程点数组
  36. public List<CPoint> errorpointarrlist = new List<CPoint>();
  37. /// <summary>
  38. /// 检测点线矛盾,并画圆标记出错误点
  39. /// </summary>
  40. public void contourch()
  41. {
  42. Editor editor = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  43. //清空各数组
  44. pointarrlist.Clear();
  45. errorpointarrlist.Clear();
  46. double flagradius = 4.0 * blc / 1000;//画圆标记半径
  47. windowwidth = 30 * 4 * blc / 1000;//确定可视范围
  48. windowheight = 30 * 4 * blc / 1000;
  49. if (blc == 500)
  50. {
  51. radius = 0.125;
  52. }
  53. else if (blc == 1000)
  54. {
  55. radius = 0.25;
  56. }
  57. else if (blc == 2000)
  58. {
  59. radius = 0.5;
  60. }
  61. else if (blc == 5000)
  62. {
  63. radius = 1.25;
  64. }
  65. //外切圆半径
  66. length = radius * Math.Pow(2, 0.5);
  67. selectcontourpoint(EleLayerName);//选出高程点
  68. if (pointarrlist.Count == 0)
  69. {
  70. return;
  71. }
  72. //开始计时
  73. Stopwatch watch = new Stopwatch();
  74. watch.Start();
  75. LayerControl layerscontrol = new LayerControl();
  76. string layname = "点线重叠标记符号";
  77. if (!layerscontrol.haslayername(layname))
  78. {
  79. colorgb col = new colorgb(255, 0, 255);
  80. layerscontrol.creatlayer(layname, col);
  81. layerscontrol.movelayertofront(layname);
  82. }
  83. else
  84. layerscontrol.movelayertofront(layname);
  85. layname = "点线重叠高程点";
  86. if (layerscontrol.haslayername(layname) == false)
  87. {
  88. colorgb col = new colorgb(255, 0, 255);
  89. layerscontrol.creatlayer(layname, col);
  90. }
  91. for (int i = 0; i < pointarrlist.Count; i++)
  92. {
  93. CPoint p = pointarrlist[i];
  94. bool flag = check(p.x, p.y, p.z);
  95. if (flag == false)
  96. {
  97. errorpointarrlist.Add(p);
  98. }
  99. }
  100. Database database = HostApplicationServices.WorkingDatabase;
  101. using (Transaction traction = database.TransactionManager.StartTransaction())
  102. {
  103. DocumentLock documentlock = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  104. BlockTable blocktable = traction.GetObject(database.BlockTableId,
  105. OpenMode.ForWrite) as BlockTable;
  106. string decimals = "";
  107. if (blc == 2000 || blc == 5000)
  108. {
  109. decimals = "F1";
  110. }
  111. else
  112. {
  113. decimals = "F2";
  114. }
  115. for (int i = 0; i < errorpointarrlist.Count; i++)
  116. {
  117. CPoint p = errorpointarrlist[i];
  118. BasicFunction functions = new BasicFunction();
  119. functions.makeflag(p.x, p.y, 0, flagradius, "点线重叠标记符号");
  120. ObjectId id = (ObjectId)p.objID;
  121. Entity entity = (Entity)traction.GetObject(id, OpenMode.ForWrite);
  122. entity.Layer = "点线重叠高程点";
  123. double z = p.z;
  124. string ele = z.ToString(decimals);
  125. Point3d p1 = new Point3d(p.x, p.y - 2.5, 0);
  126. Point3d p2 = new Point3d(p.x + 10, p.y + 2.5, 0);
  127. TypedValue[] typedvalue = new TypedValue[2];
  128. typedvalue.SetValue(new TypedValue((int)DxfCode.Start, "Text"), 0);
  129. typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, EleLayerName), 1);
  130. SelectionFilter filter = new SelectionFilter(typedvalue);
  131. PromptSelectionResult psr = editor.SelectCrossingWindow(p1, p2, filter);
  132. if (psr.Status == PromptStatus.OK)
  133. {
  134. SelectionSet set = psr.Value;
  135. ObjectId[] objs = new ObjectId[set.Count];
  136. objs = set.GetObjectIds();
  137. for (int ii = 0; ii < objs.Length; ii++)
  138. {
  139. Entity Text = (Entity)traction.GetObject(objs[ii], OpenMode.ForWrite);
  140. DBText T = (DBText)Text;
  141. if (T.TextString == ele)
  142. {
  143. T.Layer = "点线重叠高程点";
  144. T.Color = GrxCAD.Colors.Color.FromRgb(255, 0, 255);
  145. }
  146. }
  147. }
  148. }
  149. traction.Commit();
  150. documentlock.Dispose();
  151. traction.Dispose();
  152. }
  153. //耗时
  154. watch.Stop();
  155. long times = watch.ElapsedMilliseconds;
  156. //editor.Regen();
  157. //editor.UpdateScreen();
  158. editor.WriteMessage("\n" + "耗时:" + times.ToString() + "毫秒");
  159. editor.WriteMessage("\n" + "检测到压线高程点" + errorpointarrlist.Count.ToString());
  160. editor.WriteMessage("\n");
  161. }
  162. /// <summary>
  163. /// 选出所有高程点,并取出其x,y,z坐标
  164. /// </summary>
  165. /// <param name="layername">高程点图层名</param>
  166. private void selectcontourpoint(string layername)
  167. {
  168. Document document = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  169. Database database = HostApplicationServices.WorkingDatabase;
  170. using (Transaction traction = database.TransactionManager.StartTransaction())
  171. {
  172. // 获得当前文档的编辑器
  173. Editor editor = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  174. // 创建一个 TypedValue 数组,用于定义过滤条件
  175. TypedValue[] typedvalue = new TypedValue[1];
  176. typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, layername), 0);
  177. // 赋值过滤条件给 SelectionFilter 对象
  178. SelectionFilter selectionfilter = new SelectionFilter(typedvalue);
  179. // 要求在图形区域中手动选择对象
  180. PromptSelectionResult psr = editor.GetSelection(selectionfilter);
  181. //自动选择图像区域全部对象
  182. //PromptSelectionResult psr = editor.SelectAll(selectionfilter);
  183. if (psr.Status == PromptStatus.OK)
  184. {
  185. SelectionSet selectionset = psr.Value;
  186. ObjectId[] obj = new ObjectId[selectionset.Count];
  187. obj = selectionset.GetObjectIds();
  188. for (int i = 0; i < obj.Length; i++)
  189. {
  190. ObjectId objid = obj[i];
  191. //找到实体,取出高程点的x,y,z
  192. Entity entity = (Entity)traction.GetObject(objid, OpenMode.ForRead);
  193. if (entity is BlockReference)
  194. {
  195. BlockReference blockreference = (BlockReference)entity;
  196. CPoint cp = new CPoint();
  197. cp.x = blockreference.Position.X;
  198. cp.y = blockreference.Position.Y;
  199. cp.z = blockreference.Position.Z;
  200. cp.objID = objid;
  201. pointarrlist.Add(cp);
  202. }
  203. }
  204. editor.WriteMessage("\n" + "选中高程点数量:" + pointarrlist.Count.ToString());
  205. }
  206. traction.Commit();
  207. traction.Dispose();
  208. }
  209. }
  210. /// <summary>
  211. /// 检查是否压线
  212. /// </summary>
  213. /// <param name="x">高程点x坐标</param>
  214. /// <param name="y">高程点y坐标</param>
  215. /// <param name="z">高程点z坐标</param>
  216. private bool check(double x, double y, double z)
  217. {
  218. Database database = HostApplicationServices.WorkingDatabase;
  219. Editor editor = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  220. DocumentLock doclock = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  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 = windowwidth;
  227. currview.Height = windowheight;
  228. editor.SetCurrentView(currview);
  229. acTrans.Commit();
  230. }
  231. Transaction traction = database.TransactionManager.StartTransaction();
  232. List<double> elevation = new List<double>();
  233. try
  234. {
  235. BlockTable blocktable = traction.GetObject(database.BlockTableId,
  236. OpenMode.ForWrite) as BlockTable;
  237. BlockTableRecord blocktablerecord = traction.GetObject(blocktable[BlockTableRecord.ModelSpace],
  238. OpenMode.ForWrite) as BlockTableRecord;
  239. //ViewTableRecord currview = editor.GetCurrentView();
  240. //currview.CenterPoint = new Point2d(x, y);
  241. //currview.ViewDirection = new Vector3d(0, 0, 1);
  242. //currview.Width = windowwidth;
  243. //currview.Height = windowheight;
  244. //editor.SetCurrentView(currview);
  245. string filttext = sqx + "," + jqx;
  246. TypedValue[] filterlist = new TypedValue[1]
  247. {
  248. new TypedValue((int)DxfCode.LayerName,filttext)
  249. };
  250. //filterelist.SetValue(new TypedValue((int)DxfCode.LayerName, "8110,8120"),0);
  251. // 赋值过滤条件给 SelectionFilter 对象
  252. SelectionFilter filter = new SelectionFilter(filterlist);
  253. Point3d p1 = new Point3d(x - length, y - length, 0);
  254. Point3d p2 = new Point3d(x + length, y + length, 0);
  255. PromptSelectionResult pselectresult;
  256. pselectresult = editor.SelectCrossingWindow(p1, p2, filter);
  257. if (pselectresult.Status == PromptStatus.OK)
  258. {
  259. SelectionSet selectionset = pselectresult.Value;
  260. ObjectId[] obj = new ObjectId[selectionset.Count];
  261. obj = selectionset.GetObjectIds();
  262. for (int i = 0; i < obj.Length; i++)
  263. {
  264. ObjectId objid = obj[i];
  265. Entity entity = (Entity)traction.GetObject(objid, OpenMode.ForRead);
  266. if (entity is Polyline)
  267. {
  268. Polyline pline = (Polyline)entity;
  269. Point3d p = pline.GetClosestPointTo(new Point3d(x, y, z), new Vector3d(0, 0, 1), false);
  270. double distance = System.Math.Sqrt((p.X - x) * (p.X - x) + (p.Y - y) * (p.Y - y));
  271. if (distance < radius)
  272. {
  273. if ((distance == 0 || pline.Elevation != z) == true)
  274. {
  275. return false;
  276. //cpoint cp = new cpoint();
  277. //cp.x = x;
  278. //cp.y = y;
  279. //cp.z = z;
  280. //errorpointarrlist.Add(cp);
  281. }
  282. }
  283. }
  284. else if (entity is Polyline2d)
  285. {
  286. Polyline2d pline2d = (Polyline2d)entity;
  287. Point3d p = pline2d.GetClosestPointTo(new Point3d(x, y, z), new Vector3d(0, 0, 1), false);
  288. double distance = System.Math.Sqrt((p.X - x) * (p.X - x) + (p.Y - y) * (p.Y - y));
  289. if (distance < radius)
  290. {
  291. if ((distance == 0 && pline2d.Elevation == z) == false)
  292. {
  293. return false;
  294. //cpoint cp = new cpoint();
  295. //cp.x = x;
  296. //cp.y = y;
  297. //cp.z = z;
  298. //errorpointarrlist.Add(cp);
  299. }
  300. }
  301. }
  302. }
  303. }
  304. }
  305. catch (GrxCAD.Runtime.Exception ex)
  306. {
  307. string str = ex.ToString();
  308. Application.ShowAlertDialog(str);
  309. }
  310. finally
  311. {
  312. doclock.Dispose();
  313. traction.Dispose();
  314. }
  315. return true;
  316. }
  317. }
  318. }