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

ChPLContradiction.cs 42KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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 Contourpoint
  13. {
  14. public double x;
  15. public double y;
  16. public double z;
  17. public ObjectId objID;
  18. }
  19. /// <summary>
  20. /// 记录闭合等高线高程和距高程点距离
  21. /// 包含高程和距离
  22. /// </summary>
  23. public struct closecontour
  24. {
  25. public double elevation;
  26. public double interval;
  27. }
  28. /// <summary>
  29. /// 判断点线是否有矛盾
  30. /// </summary>
  31. class ChPLContradiction
  32. {
  33. public static int blc;//比例尺
  34. public static string sqx;//首曲线
  35. public static string jqx;//计曲线
  36. public const int looptimes = 5;//循环查找次数
  37. public static string elevationLayer;//高程点图层名
  38. public int leng;//查找距离
  39. int temp;
  40. public int windowwidth;//当前可视范围
  41. public int windowheight;
  42. //存放所有高程点数组
  43. public List<Contourpoint> pointarrlist = new List<Contourpoint>();
  44. //存放有问题的高程点数组
  45. public List<Contourpoint> errorpointarrlist = new List<Contourpoint>();
  46. //存放无法识别点数组
  47. public List<Contourpoint> unrecognize = new List<Contourpoint>();
  48. //存放搜索到的等高线用于判断是否重复
  49. public List<Polyline> plltemp = new List<Polyline>();
  50. public List<Polyline2d> pll2dtemp = new List<Polyline2d>();
  51. /// <summary>
  52. /// 检测点线矛盾,并画圆标记出错误点
  53. /// </summary>
  54. public void contourch()
  55. {
  56. Editor editor = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  57. //清空各数组
  58. pointarrlist.Clear();
  59. errorpointarrlist.Clear();
  60. double flagradius = 4.0 * blc / 1000;//画圆标记半径
  61. leng = Convert.ToInt32(flagradius + 8);//确定搜索距离
  62. temp = leng;
  63. windowwidth = 30 * 4 * blc / 1000;//确定可视范围
  64. windowheight = 30 * 4 * blc / 1000;
  65. selectcontourpoint(elevationLayer);//选出高程点
  66. if (pointarrlist.Count == 0)
  67. {
  68. return;
  69. }
  70. //开始计时
  71. Stopwatch watch = new Stopwatch();
  72. watch.Start();
  73. LayerControl layerscontrol = new LayerControl();
  74. string layname = "点线矛盾标记图层";
  75. if (layerscontrol.haslayername(layname) == false)
  76. {
  77. colorgb col = new colorgb(255, 0, 0);
  78. layerscontrol.creatlayer(layname, col);
  79. }
  80. layname = "点线矛盾高程点图层";
  81. if (layerscontrol.haslayername(layname) == false)
  82. {
  83. colorgb col = new colorgb(255, 255, 0);
  84. layerscontrol.creatlayer(layname, col);
  85. layerscontrol.movelayertofront(layname);
  86. }
  87. layname = "无法识别";
  88. if (layerscontrol.haslayername(layname) == false)
  89. {
  90. colorgb colo = new colorgb(0, 255, 0);
  91. layerscontrol.creatlayer(layname, colo);
  92. }
  93. //遍历各高程点,检测是否高程值有错误
  94. for (int i = 0; i < pointarrlist.Count; i++)
  95. {
  96. Contourpoint p = pointarrlist[i];
  97. check(p);
  98. }
  99. Database database = HostApplicationServices.WorkingDatabase;
  100. //遍历各错误高程点,画圆标记出
  101. for (int i = 0; i < errorpointarrlist.Count; i++)
  102. {
  103. Contourpoint p = errorpointarrlist[i];
  104. double x = p.x;
  105. double y = p.y;
  106. double z = p.z;
  107. ObjectId id = p.objID;
  108. using (Transaction acTrans = database.TransactionManager.StartTransaction())
  109. {
  110. ViewTableRecord currview = editor.GetCurrentView();
  111. currview.CenterPoint = new Point2d(x, y);
  112. currview.ViewDirection = new Vector3d(0, 0, 1);
  113. currview.Width = windowwidth;
  114. currview.Height = windowheight;
  115. editor.SetCurrentView(currview);
  116. acTrans.Commit();
  117. }
  118. Transaction traction = database.TransactionManager.StartTransaction();
  119. DocumentLock documentlock = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  120. BlockTable blocktable = traction.GetObject(database.BlockTableId,
  121. OpenMode.ForWrite) as BlockTable;
  122. makeflag(errorpointarrlist[i].x, errorpointarrlist[i].y, 0, flagradius, 1);
  123. Entity entity = (Entity)traction.GetObject(id, OpenMode.ForWrite);
  124. entity.Layer = "点线矛盾高程点图层";
  125. //ViewTableRecord currview = editor.GetCurrentView();
  126. //currview.CenterPoint = new Point2d(x, y);
  127. //currview.ViewDirection = new Vector3d(0, 0, 1);
  128. //currview.Width = windowwidth;
  129. //currview.Height = windowheight;
  130. //editor.SetCurrentView(currview);
  131. Point3d p1 = new Point3d(p.x - 4 * blc / 2000, p.y - 4 * blc / 2000, 0);
  132. Point3d p2 = new Point3d(p.x + 4 * blc / 2000, p.y + 4 * blc / 2000, 0);
  133. TypedValue[] typedvalue = new TypedValue[2];
  134. typedvalue.SetValue(new TypedValue((int)DxfCode.Start, "Text"), 0);
  135. typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, elevationLayer), 1);
  136. SelectionFilter filter = new SelectionFilter(typedvalue);
  137. PromptSelectionResult psr = editor.SelectCrossingWindow(p1, p2, filter);
  138. if (psr.Status == PromptStatus.OK)
  139. {
  140. SelectionSet set = psr.Value;
  141. ObjectId[] objs = new ObjectId[set.Count];
  142. objs = set.GetObjectIds();
  143. for (int ii = 0; ii < objs.Length; ii++)
  144. {
  145. Entity Text = (Entity)traction.GetObject(objs[ii], OpenMode.ForWrite);
  146. DBText T = (DBText)Text;
  147. double T_Value = Convert.ToDouble(T.TextString);
  148. if (T_Value == z)
  149. {
  150. T.Layer = "点线矛盾高程点图层";
  151. }
  152. }
  153. }
  154. traction.Commit();
  155. documentlock.Dispose();
  156. }
  157. for (int i = 0; i < unrecognize.Count; i++)
  158. {
  159. makeflag(unrecognize[i].x, unrecognize[i].y, 0, flagradius, 2);
  160. }
  161. //耗时
  162. watch.Stop();
  163. long times = watch.ElapsedMilliseconds;
  164. //editor.Regen();
  165. //editor.UpdateScreen();
  166. editor.WriteMessage("\n" + "耗时:" + times.ToString() + "毫秒");
  167. editor.WriteMessage("\n" + "检测到错误高程点" + errorpointarrlist.Count.ToString());
  168. editor.WriteMessage("\n" + "检测到无法识别点" + unrecognize.Count.ToString());
  169. editor.WriteMessage("\n");
  170. }
  171. /// <summary>
  172. /// 在错误高程点处画圆标记
  173. /// </summary>
  174. /// <param name="radius">圆的半径</param>
  175. public void makeflag(double x, double y, double z, double radius, int type)
  176. {
  177. Database database = HostApplicationServices.WorkingDatabase;
  178. DocumentLock doclock = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  179. Transaction traction = database.TransactionManager.StartTransaction();
  180. try
  181. {
  182. BlockTable blocktable;
  183. blocktable = traction.GetObject(database.BlockTableId,
  184. OpenMode.ForWrite) as BlockTable;
  185. BlockTableRecord blocktablerecord;
  186. blocktablerecord = traction.GetObject(blocktable[BlockTableRecord.ModelSpace],
  187. OpenMode.ForWrite) as BlockTableRecord;
  188. Circle circ = new Circle();
  189. if (type == 1)
  190. {
  191. circ.Layer = "点线矛盾标记图层";
  192. }
  193. else if (type == 2)
  194. {
  195. circ.Layer = "无法识别";
  196. }
  197. circ.Center = new Point3d(x, y, z);
  198. circ.Radius = radius;
  199. circ.Normal = new Vector3d(0, 0, 1);
  200. circ.SetDatabaseDefaults();
  201. blocktablerecord.AppendEntity(circ);
  202. traction.AddNewlyCreatedDBObject(circ, true);
  203. traction.Commit();
  204. }
  205. catch (GrxCAD.Runtime.Exception)
  206. {
  207. traction.Abort();
  208. }
  209. finally
  210. {
  211. doclock.Dispose();
  212. traction.Dispose();
  213. }
  214. }
  215. /// <summary>
  216. /// 选出所有高程点,并取出其x,y,z坐标
  217. /// </summary>
  218. /// <param name="layername">高程点图层名</param>
  219. public void selectcontourpoint(string layername)
  220. {
  221. Document document = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  222. Database database = HostApplicationServices.WorkingDatabase;
  223. // 获得当前文档的编辑器
  224. Editor editor = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  225. // 创建一个 TypedValue 数组,用于定义过滤条件
  226. TypedValue[] typedvalue = new TypedValue[1];
  227. typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, layername), 0);
  228. // 赋值过滤条件给 SelectionFilter 对象
  229. SelectionFilter selectionfilter = new SelectionFilter(typedvalue);
  230. // 要求在图形区域中手动选择对象
  231. PromptSelectionResult psr = editor.GetSelection(selectionfilter);
  232. //自动选择图像区域全部对象
  233. //PromptSelectionResult psr = editor.SelectAll(selectionfilter);
  234. if (psr.Status == PromptStatus.OK)
  235. {
  236. SelectionSet selectionset = psr.Value;
  237. ObjectId[] obj = new ObjectId[selectionset.Count];
  238. obj = selectionset.GetObjectIds();
  239. for (int i = 0; i < obj.Length; i++)
  240. {
  241. using (Transaction traction = database.TransactionManager.StartTransaction())
  242. {
  243. ObjectId objid = obj[i];
  244. //找到实体,取出高程点的x,y,z
  245. Entity entity = (Entity)traction.GetObject(objid, OpenMode.ForRead);
  246. if (entity is BlockReference)
  247. {
  248. BlockReference blockreference = (BlockReference)entity;
  249. Contourpoint cp = new Contourpoint();
  250. cp.x = Math.Round(blockreference.Position.X, 4, MidpointRounding.AwayFromZero);
  251. cp.y = Math.Round(blockreference.Position.Y, 4, MidpointRounding.AwayFromZero);
  252. cp.z = Math.Round(blockreference.Position.Z, 4, MidpointRounding.AwayFromZero);
  253. cp.objID = objid;
  254. pointarrlist.Add(cp);
  255. }
  256. traction.Commit();
  257. }
  258. }
  259. editor.WriteMessage("\n" + "选中高程点数量:" + pointarrlist.Count.ToString());
  260. }
  261. }
  262. /// <summary>
  263. /// 检查是否错误
  264. /// </summary>
  265. public void check(Contourpoint p)
  266. {
  267. double x = Math.Round(p.x, 4, MidpointRounding.AwayFromZero);
  268. double y = Math.Round(p.y, 4, MidpointRounding.AwayFromZero);
  269. double z = Math.Round(p.z, 4, MidpointRounding.AwayFromZero);
  270. List<double> toplist = calculatevalue(x, y, z, 1);
  271. List<double> belowlist = calculatevalue(x, y, z, 3);
  272. //点在上下两条等高线中间情况
  273. if ((toplist.Count > 0) && (belowlist.Count > 0))
  274. {
  275. //1.一般情况
  276. #region 一般情况
  277. if (toplist[0] > belowlist[belowlist.Count - 1])
  278. {
  279. //高程值上大小小
  280. if (z >= toplist[0] || z <= belowlist[belowlist.Count - 1])
  281. {
  282. errorpointarrlist.Add(p);
  283. return;
  284. }
  285. else
  286. return;
  287. }
  288. //高程值上小下大
  289. if (toplist[toplist.Count - 1] < belowlist[0])
  290. {
  291. if (z >= belowlist[0] || z <= toplist[toplist.Count - 1])
  292. {
  293. errorpointarrlist.Add(p);
  294. return;
  295. }
  296. else
  297. return;
  298. }
  299. #endregion
  300. //点在左右半包围情况,上下高程相等,要么是最大,要么是最小,进行左右搜索比较
  301. //2.半包围一般情况
  302. #region 半包围一般情况
  303. if ((toplist[0] == belowlist[0]) || (toplist[toplist.Count - 1] == belowlist[belowlist.Count - 1]))
  304. {
  305. List<double> rightlist = calculatevalue(x, y, z, 4);
  306. List<double> leftlist = calculatevalue(x, y, z, 2);
  307. if ((rightlist.Count > 0) && (leftlist.Count > 0))
  308. {
  309. //高程值右大左小
  310. if (rightlist[0] > leftlist[leftlist.Count - 1])
  311. {
  312. if ((z >= rightlist[0]) || (z <= leftlist[leftlist.Count - 1]))
  313. {
  314. errorpointarrlist.Add(p);
  315. return;
  316. }
  317. else
  318. return;
  319. }
  320. //高程值右小左大
  321. if (leftlist[0] > rightlist[rightlist.Count - 1])
  322. {
  323. if ((z >= leftlist[0]) || (z <= rightlist[rightlist.Count - 1]))
  324. {
  325. errorpointarrlist.Add(p);
  326. return;
  327. }
  328. else
  329. return;
  330. }
  331. #endregion
  332. //3.大半包围或闭合情况,即上下左右高程值相等
  333. #region 四面闭合
  334. if ((rightlist[0] == leftlist[0]) || (rightlist[rightlist.Count - 1] == leftlist[leftlist.Count - 1]))
  335. {
  336. //ArrayList closecontourlist = new ArrayList();
  337. List<closecontour> closecontourlist = new List<closecontour>();
  338. closecontourlist = closecontourcheck(p, 1);
  339. if (closecontourlist.Count < 2)
  340. {
  341. closecontourlist = closecontourcheck(p, 3);
  342. if (closecontourlist.Count < 2)
  343. {
  344. closecontourlist = closecontourcheck(p, 2);
  345. if (closecontourlist.Count < 2)
  346. {
  347. closecontourlist = closecontourcheck(p, 4);
  348. if (closecontourlist.Count < 2)
  349. {
  350. unrecognize.Add(p);
  351. return;
  352. }
  353. }
  354. }
  355. }
  356. //存储高程值计算等高距
  357. List<double> ele = new List<double>();
  358. if (closecontourlist.Count >= 2)
  359. {
  360. double minidistance = closecontourlist[0].interval;
  361. double contour = closecontourlist[0].elevation;
  362. ele.Add(contour);
  363. for (int i = 1; i < closecontourlist.Count; i++)
  364. {
  365. if (closecontourlist[i].interval < minidistance)
  366. {
  367. minidistance = closecontourlist[i].interval;
  368. contour = closecontourlist[i].elevation;
  369. }
  370. ele.Add(closecontourlist[i].elevation);
  371. }
  372. //从小到大排序
  373. ele.Sort();
  374. //等高距
  375. double contourinterval = ele[1] - ele[0];
  376. //中间高程最小
  377. if (contour == ele[0])
  378. {
  379. if ((z - ele[0] >= 0) || ((ele[0] - contourinterval) - z >= 0))
  380. //if ((z > ele[0]) || z < (ele[0] - contourinterval))
  381. {
  382. errorpointarrlist.Add(p);
  383. return;
  384. }
  385. else
  386. return;
  387. }
  388. //中间高程最大
  389. if (contour == ele[ele.Count - 1])
  390. {
  391. if ((z - (ele[ele.Count - 1] + contourinterval) >= 0) || ele[ele.Count - 1] - z >= 0)
  392. //if (z > (ele[ele.Count - 1] + contourinterval) || z < ele[ele.Count - 1])
  393. {
  394. errorpointarrlist.Add(p);
  395. return;
  396. }
  397. else
  398. return;
  399. }
  400. }
  401. }
  402. #endregion
  403. }
  404. //4.上下左、上下右三方闭合
  405. #region 上下左、上下右三方闭合
  406. else
  407. {
  408. List<closecontour> closecontourlist = new List<closecontour>();
  409. //找出等高线条数大于2的方向进行搜索
  410. if (toplist.Count >= 2)
  411. {
  412. closecontourlist = closecontourcheck(p, 1);
  413. }
  414. else if (belowlist.Count >= 2)
  415. {
  416. closecontourlist = closecontourcheck(p, 3);
  417. }
  418. else if (leftlist.Count >= 2)
  419. {
  420. closecontourlist = closecontourcheck(p, 4);
  421. }
  422. else
  423. closecontourlist = closecontourcheck(p, 2);
  424. if (closecontourlist.Count < 2)
  425. {
  426. closecontourlist = closecontourcheck(p, 3);
  427. }
  428. if (closecontourlist.Count < 2)
  429. {
  430. closecontourlist = closecontourcheck(p, 1);
  431. }
  432. if (closecontourlist.Count < 2)
  433. {
  434. closecontourlist = closecontourcheck(p, 4);
  435. }
  436. if (closecontourlist.Count < 2)
  437. {
  438. closecontourlist = closecontourcheck(p, 2);
  439. }
  440. if (closecontourlist.Count >= 2)
  441. {
  442. compareelevation(p, closecontourlist);
  443. return;
  444. }
  445. else
  446. {
  447. unrecognize.Add(p);
  448. }
  449. }
  450. #endregion
  451. }
  452. }
  453. //上下方向没有搜索两条等高线,则向左右方向搜索
  454. if (toplist.Count == 0 || belowlist.Count == 0)
  455. {
  456. checkleftright(p);
  457. }
  458. }
  459. /// <summary>
  460. /// 比较高程点值和该高程点左右等高线高程
  461. /// </summary>
  462. public void checkleftright(Contourpoint p)
  463. {
  464. double x = Math.Round(p.x, 4, MidpointRounding.AwayFromZero);
  465. double y = Math.Round(p.y, 4, MidpointRounding.AwayFromZero);
  466. double z = Math.Round(p.z, 4, MidpointRounding.AwayFromZero);
  467. List<double> rightlist = calculatevalue(x, y, z, 4);
  468. List<double> leftlist = calculatevalue(x, y, z, 2);
  469. //
  470. if (rightlist.Count > 0 && leftlist.Count > 0)
  471. {
  472. //右大左小
  473. if (rightlist[0] > leftlist[leftlist.Count - 1])
  474. {
  475. if (z > rightlist[0] || z < leftlist[leftlist.Count - 1])
  476. {
  477. errorpointarrlist.Add(p);
  478. return;
  479. }
  480. }
  481. //右小左大
  482. if (leftlist[0] > rightlist[rightlist.Count - 1])
  483. {
  484. if (z > leftlist[0] || z < rightlist[rightlist.Count - 1])
  485. {
  486. errorpointarrlist.Add(p);
  487. return;
  488. }
  489. }
  490. //左右相等,左右上、或者左右下三方闭合
  491. if ((leftlist[0] == rightlist[0]) || (leftlist[leftlist.Count - 1] == rightlist[rightlist.Count - 1]))
  492. {
  493. List<double> toplist = calculatevalue(x, y, z, 1);
  494. List<double> belowlist = calculatevalue(x, y, z, 3);
  495. #region
  496. //ArrayList closecontourlist = new ArrayList();
  497. List<closecontour> closecontourlist = new List<closecontour>();
  498. //找出等高线条数大于2的方向进行搜索
  499. if (leftlist.Count >= 2)
  500. {
  501. closecontourlist = closecontourcheck(p, 2);
  502. }
  503. else if (rightlist.Count >= 2)
  504. {
  505. closecontourlist = closecontourcheck(p, 4);
  506. }
  507. else if (toplist.Count >= 2)
  508. {
  509. closecontourlist = closecontourcheck(p, 1);
  510. }
  511. else
  512. closecontourlist = closecontourcheck(p, 3);
  513. if (closecontourlist.Count < 2)
  514. {
  515. closecontourlist = closecontourcheck(p, 2);
  516. }
  517. if (closecontourlist.Count < 2)
  518. {
  519. closecontourlist = closecontourcheck(p, 4);
  520. }
  521. if (closecontourlist.Count < 2)
  522. {
  523. closecontourlist = closecontourcheck(p, 1);
  524. }
  525. if (closecontourlist.Count < 2)
  526. {
  527. closecontourlist = closecontourcheck(p, 3);
  528. }
  529. if (closecontourlist.Count >= 2)
  530. {
  531. compareelevation(p, closecontourlist);
  532. return;
  533. }
  534. else
  535. {
  536. unrecognize.Add(p);
  537. return;
  538. }
  539. #endregion
  540. }
  541. }
  542. //边缘情况
  543. else
  544. {
  545. List<double> toplist = calculatevalue(x, y, z, 1);
  546. List<double> belowlist = calculatevalue(x, y, z, 3);
  547. //ArrayList closecontourlist = new ArrayList();
  548. List<closecontour> closecontourlist = new List<closecontour>();
  549. //找出等高线条数大于1的方向进行搜索
  550. if (leftlist.Count >= 1)
  551. {
  552. closecontourlist = closecontourcheck(p, 2);
  553. }
  554. else if (rightlist.Count >= 1)
  555. {
  556. closecontourlist = closecontourcheck(p, 4);
  557. }
  558. else if (toplist.Count >= 1)
  559. {
  560. closecontourlist = closecontourcheck(p, 1);
  561. }
  562. else
  563. closecontourlist = closecontourcheck(p, 3);
  564. if (closecontourlist.Count < 2)
  565. {
  566. closecontourlist = closecontourcheck(p, 2);
  567. }
  568. if (closecontourlist.Count < 2)
  569. {
  570. closecontourlist = closecontourcheck(p, 4);
  571. }
  572. if (closecontourlist.Count < 2)
  573. {
  574. closecontourlist = closecontourcheck(p, 1);
  575. }
  576. if (closecontourlist.Count < 2)
  577. {
  578. closecontourlist = closecontourcheck(p, 3);
  579. }
  580. if (closecontourlist.Count >= 2)
  581. {
  582. compareelevation(p, closecontourlist);
  583. return;
  584. }
  585. else
  586. {
  587. unrecognize.Add(p);
  588. return;
  589. }
  590. }
  591. }
  592. /// <summary>
  593. /// 从高程点向上搜索,找到上的等高线,并取得其高程值
  594. /// </summary>
  595. /// <param name="direction">搜索方向,上、下、左、右分别为1、3、2、4</param>
  596. /// <returns>以数组List(double)形式从小到大排序之后返回搜索到的各等高线高程</returns>
  597. public List<double> calculatevalue(double x, double y, double z, int direction)
  598. {
  599. Database database = HostApplicationServices.WorkingDatabase;
  600. Editor editor = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  601. Point3dCollection ptcoll = new Point3dCollection();
  602. List<double> elevation = new List<double>();
  603. try
  604. {
  605. //ViewTableRecord currview = editor.GetCurrentView();
  606. //currview.CenterPoint = new Point2d(x, y);
  607. //currview.ViewDirection = new Vector3d(0, 0, 1);
  608. //currview.Width = windowwidth;
  609. //currview.Height = windowheight;
  610. //editor.SetCurrentView(currview);
  611. double high = y;
  612. double width = x;
  613. bool flag = true;
  614. int times = 0;
  615. PromptSelectionResult pselectresult;
  616. Point3d p1, p2;
  617. while (flag)
  618. {
  619. times++;
  620. if (times > looptimes)
  621. {
  622. leng = temp;
  623. break;
  624. }
  625. leng = leng * times;
  626. if (direction == 1)
  627. {
  628. high += leng;
  629. p1 = new Point3d(x, y, 0);
  630. p2 = new Point3d(x, high, 0);
  631. }
  632. else if (direction == 3)
  633. {
  634. high -= leng;
  635. p1 = new Point3d(x, y, 0);
  636. p2 = new Point3d(x, high, 0);
  637. }
  638. else if (direction == 2)
  639. {
  640. width -= leng;
  641. p1 = new Point3d(x, y, 0);
  642. p2 = new Point3d(width, y, 0);
  643. }
  644. else
  645. {
  646. width += leng;
  647. p1 = new Point3d(x, y, 0);
  648. p2 = new Point3d(width, y, 0);
  649. }
  650. ptcoll.Add(p1);
  651. ptcoll.Add(p2);
  652. string filttext = sqx + "," + jqx;
  653. TypedValue[] filterlist = new TypedValue[1]
  654. {
  655. new TypedValue((int)DxfCode.LayerName,filttext)
  656. };
  657. //filterelist.SetValue(new TypedValue((int)DxfCode.LayerName, "8110,8120"),0);
  658. // 赋值过滤条件给 SelectionFilter 对象
  659. SelectionFilter filter = new SelectionFilter(filterlist);
  660. dynamic acadApp = Application.AcadApplication;
  661. acadApp.ZoomExtents();
  662. pselectresult = editor.SelectFence(ptcoll, filter);
  663. acadApp.ZoomPrevious();
  664. if (pselectresult.Status == PromptStatus.OK)
  665. {
  666. SelectionSet selectionset = pselectresult.Value;
  667. ObjectId[] obj = new ObjectId[selectionset.Count];
  668. obj = selectionset.GetObjectIds();
  669. DocumentLock doclock = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  670. for (int i = 0; i < obj.Length; i++)
  671. {
  672. Transaction traction = database.TransactionManager.StartTransaction();
  673. BlockTable blocktable = traction.GetObject(database.BlockTableId,
  674. OpenMode.ForWrite) as BlockTable;
  675. BlockTableRecord blocktablerecord = traction.GetObject(blocktable[BlockTableRecord.ModelSpace],
  676. OpenMode.ForWrite) as BlockTableRecord;
  677. ObjectId objid = obj[i];
  678. Entity entity = (Entity)traction.GetObject(objid, OpenMode.ForRead);
  679. if (entity is Polyline)
  680. {
  681. Polyline pline = (Polyline)entity;
  682. double ele = pline.Elevation;
  683. elevation.Add(ele);
  684. plltemp.Add(pline);
  685. }
  686. if (entity is Polyline2d)
  687. {
  688. Polyline2d pline2d = (Polyline2d)entity;
  689. double ele = pline2d.Elevation;
  690. elevation.Add(ele);
  691. pll2dtemp.Add(pline2d);
  692. }
  693. traction.Commit();
  694. traction.Dispose();
  695. }
  696. doclock.Dispose();
  697. }
  698. ptcoll.Clear();
  699. if (elevation.Count > 0)
  700. {
  701. leng = temp;
  702. flag = false;
  703. //Line line = new Line(p1, p2);
  704. //line.SetDatabaseDefaults();
  705. //blocktablerecord.AppendEntity(line);
  706. //traction.AddNewlyCreatedDBObject(line, true);
  707. }
  708. }
  709. elevation.Sort();
  710. return elevation;
  711. }
  712. catch (GrxCAD.Runtime.Exception ex)
  713. {
  714. string str = ex.ToString();
  715. Application.ShowAlertDialog(str);
  716. return elevation;
  717. }
  718. //finally
  719. //{
  720. //}
  721. }
  722. /// <summary>
  723. /// 上下方向搜索距离高程点距离最近的等高线的高程和距离
  724. /// </summary>
  725. /// <param name="direction">搜索方向,1、2、3、4分别代表上、下、左、右</param>
  726. /// <returns>以list返回闭合等高线结构体数组</returns>
  727. public List<closecontour> closecontourcheck(Contourpoint p, int direction)
  728. {
  729. double x = p.x;
  730. double y = p.y;
  731. double z = p.z;
  732. Database database = HostApplicationServices.WorkingDatabase;
  733. Editor editor = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  734. List<closecontour> arrlist = new List<closecontour>();
  735. double high = y;
  736. double width = x;
  737. bool flag = true;
  738. int times = 0;
  739. PromptSelectionResult pselectresult;
  740. Point3d p1, p2;
  741. while (flag)
  742. {
  743. //清空上一次选中的不足两个的对象
  744. arrlist.Clear();
  745. times++;
  746. if (times > looptimes)
  747. {
  748. leng = temp;
  749. break;
  750. }
  751. leng = leng * times;
  752. //向上搜索
  753. if (direction == 1)
  754. {
  755. high += leng;
  756. p1 = new Point3d(x, y, 0);
  757. p2 = new Point3d(x + 0.001, high, 0);
  758. }
  759. //向下搜索
  760. else if (direction == 3)
  761. {
  762. high -= leng;
  763. p1 = new Point3d(x, y, 0);
  764. p2 = new Point3d(x + 0.001, high, 0);
  765. }
  766. //向左搜索
  767. else if (direction == 2)
  768. {
  769. width -= leng;
  770. p1 = new Point3d(x, y, 0);
  771. p2 = new Point3d(width, y + 0.001, 0);
  772. }
  773. //向右搜索
  774. else
  775. {
  776. width += leng;
  777. p1 = new Point3d(x, y, 0);
  778. p2 = new Point3d(width, y + 0.001, 0);
  779. }
  780. string filttext = sqx + "," + jqx;
  781. TypedValue[] filterlist = new TypedValue[1]
  782. {
  783. new TypedValue((int)DxfCode.LayerName,filttext)
  784. };
  785. SelectionFilter filter = new SelectionFilter(filterlist);
  786. dynamic acadApp = Application.AcadApplication;
  787. acadApp.ZoomExtents();
  788. pselectresult = editor.SelectCrossingWindow(p1, p2, filter);
  789. acadApp.ZoomPrevious();
  790. if (pselectresult.Status == PromptStatus.OK)
  791. {
  792. SelectionSet selectionset = pselectresult.Value;
  793. ObjectId[] obj = new ObjectId[selectionset.Count];
  794. obj = selectionset.GetObjectIds();
  795. for (int i = 0; i < obj.Length; i++)
  796. {
  797. if (obj.Length < 2)
  798. break;
  799. DocumentLock doclock = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  800. using (Transaction traction = database.TransactionManager.StartTransaction())
  801. {
  802. BlockTable blocktable = traction.GetObject(database.BlockTableId,OpenMode.ForWrite) as BlockTable;
  803. BlockTableRecord blocktablerecord = traction.GetObject(blocktable[BlockTableRecord.ModelSpace],
  804. OpenMode.ForWrite) as BlockTableRecord;
  805. //else
  806. //{
  807. // Line line = new Line(p1, p2);
  808. // line.SetDatabaseDefaults();
  809. // blocktablerecord.AppendEntity(line);
  810. // traction.AddNewlyCreatedDBObject(line, true);
  811. //}
  812. ObjectId objid = obj[i];
  813. Entity entity = (Entity)traction.GetObject(objid, OpenMode.ForRead);
  814. if (entity is Polyline)
  815. {
  816. Polyline pline = (Polyline)entity;
  817. Point3d pt = pline.GetClosestPointTo(new Point3d(x, y, z), new Vector3d(0, 0, 1), false);
  818. double distance = System.Math.Sqrt((pt.X - x) * (pt.X - x) + (pt.Y - y) * (pt.Y - y));
  819. double elev = pline.Elevation;
  820. closecontour cc = new closecontour();
  821. cc.elevation = elev;
  822. cc.interval = distance;
  823. arrlist.Add(cc);
  824. //Line line = new Line(new Point3d(x, y, z), new Point3d(p.X, p.Y, p.Z));
  825. //line.SetDatabaseDefaults();
  826. //blocktablerecord.AppendEntity(line);
  827. //traction.AddNewlyCreatedDBObject(line, true);
  828. }
  829. if (entity is Polyline2d)
  830. {
  831. Polyline2d pline2d = (Polyline2d)entity;
  832. Point3d pt = pline2d.GetClosestPointTo(new Point3d(x, y, z), new Vector3d(0, 0, 1), false);
  833. double distance = System.Math.Sqrt((pt.X - x) * (pt.X - x) + (pt.Y - y) * (pt.Y - y));
  834. double elev = pline2d.Elevation;
  835. closecontour cc = new closecontour();
  836. cc.elevation = elev;
  837. cc.interval = distance;
  838. arrlist.Add(cc);
  839. //Line line = new Line(new Point3d(x, y, z), new Point3d(p.X, p.Y, p.Z));
  840. //line.SetDatabaseDefaults();
  841. //blocktablerecord.AppendEntity(line);
  842. //traction.AddNewlyCreatedDBObject(line, true);
  843. }
  844. traction.Commit();
  845. }
  846. doclock.Dispose();
  847. }
  848. if (arrlist.Count >= 2)
  849. {
  850. flag = false;
  851. }
  852. }
  853. }
  854. return arrlist;
  855. }
  856. /// <summary>
  857. /// 在闭合等高线处,根据高程点周围等高线高程变化趋势。判断高程点的高程范围
  858. /// </summary>
  859. /// <param name="arrlist">高程点周围等高线高程</param>
  860. public void compareelevation(Contourpoint p, List<closecontour> arrlist)
  861. {
  862. double x = p.x;
  863. double y = p.y;
  864. double z = p.z;
  865. //存储高程值计算等高距
  866. List<double> ele = new List<double>();
  867. if (arrlist.Count >= 2)
  868. {
  869. double minidistance = arrlist[0].interval;
  870. double contour = arrlist[0].elevation;
  871. ele.Add(contour);
  872. for (int i = 1; i < arrlist.Count; i++)
  873. {
  874. if (arrlist[i].interval < minidistance)
  875. {
  876. minidistance = arrlist[i].interval;
  877. contour = arrlist[i].elevation;
  878. }
  879. ele.Add(arrlist[i].elevation);
  880. }
  881. //从小到大排序
  882. ele.Sort();
  883. //等高距
  884. double contourinterval = ele[1] - ele[0];
  885. //中间高程最小
  886. if (contour == ele[0])
  887. {
  888. if (z > ele[0] || z < (ele[0] - contourinterval))
  889. {
  890. errorpointarrlist.Add(p);
  891. return;
  892. }
  893. else
  894. return;
  895. }
  896. //中间高程最大
  897. if (contour == ele[ele.Count - 1])
  898. {
  899. if (z > (ele[ele.Count - 1] + contourinterval) || z < ele[ele.Count - 1])
  900. {
  901. errorpointarrlist.Add(p);
  902. return;
  903. }
  904. else
  905. return;
  906. }
  907. }
  908. }
  909. }
  910. }