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

ChPLConstradiction1.cs 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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.Diagnostics;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace CADTools
  12. {
  13. // 存储交点和对应等高线的数据结构
  14. class IntersectionResult
  15. {
  16. public List<Point3d> IntersectionPoint { get; set; }
  17. public Polyline Contour { get; set; }
  18. }
  19. class ChPLContradiction1
  20. {
  21. public static int blc;//比例尺
  22. public static string sqx;//首曲线
  23. public static string jqx;//计曲线
  24. public static double dgj;//计曲线
  25. public static string elevationLayer;//高程点图层名
  26. public static int num = 0;//点线矛盾点数量
  27. int looptimes = 5;//循环查找次数
  28. public void Check()
  29. {
  30. Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  31. Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
  32. num = 0;
  33. var ptlist = GetPt(ed, db);
  34. for (int i = 0; i < ptlist.Count; i++)
  35. {
  36. BlockReference bf = ptlist[i];
  37. //为方便搜索操作,将待检查高程点设置为当前视图的中心点,视图高设置为600
  38. SetWindow(ed, db, bf.Position.X, bf.Position.Y, 600, 600);
  39. bool contradition = true;
  40. for (int j = 0; j < looptimes; j++)
  41. {
  42. double searchlength = 100.0 * (j + 1);
  43. var searchpl_horizontal = SearchDGX(bf.Position, searchlength, ed, db, 0);
  44. var searchpl_vertical = SearchDGX(bf.Position, searchlength, ed, db, 1);
  45. List<Polyline> all_pl = new List<Polyline>();
  46. all_pl.AddRange(searchpl_horizontal);
  47. foreach (var item in searchpl_vertical)
  48. {
  49. if (all_pl.Contains(item))
  50. continue;
  51. else
  52. all_pl.Add(item);
  53. }
  54. if (all_pl.Count < 2)
  55. continue;
  56. contradition = IfContradition(all_pl, bf.Position, searchlength);
  57. if (!contradition)
  58. break;
  59. }
  60. if (contradition)
  61. {
  62. num++;
  63. MakeFlag(bf.Position, blc);
  64. Point3d p1 = new Point3d(bf.Position.X - 4 * blc / 2000, bf.Position.Y - 4 * blc / 2000, 0);
  65. Point3d p2 = new Point3d(bf.Position.X + 4 * blc / 2000, bf.Position.Y + 4 * blc / 2000, 0);
  66. TypedValue[] typedvalue = new TypedValue[2];
  67. typedvalue.SetValue(new TypedValue((int)DxfCode.Start, "Text"), 0);
  68. typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, elevationLayer), 1);
  69. SelectionFilter filter = new SelectionFilter(typedvalue);
  70. PromptSelectionResult psr = ed.SelectCrossingWindow(p1, p2, filter);
  71. List<ObjectId> objs = new List<ObjectId>();
  72. if (psr.Status == PromptStatus.OK)
  73. {
  74. SelectionSet set = psr.Value;
  75. objs = set.GetObjectIds().ToList();
  76. }
  77. DocumentLock doclock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  78. using (Transaction tr = db.TransactionManager.StartTransaction())
  79. {
  80. if (objs.Count > 0)
  81. {
  82. for (int ii = 0; ii < objs.Count; ii++)
  83. {
  84. Entity ent = (Entity)tr.GetObject(objs[ii], OpenMode.ForWrite);
  85. if (ent is DBText)
  86. {
  87. DBText t = (DBText)ent;
  88. double T_Value = Convert.ToDouble(t.TextString);
  89. int ws = 0;
  90. if (t.TextString.Contains('.'))
  91. {
  92. ws = t.TextString.Split('.')[1].Length;
  93. if (t.TextString.Split('.')[1].All(c => c == '0'))
  94. ws = 0;
  95. }
  96. double ele = Math.Round(t.Position.Z, ws, MidpointRounding.AwayFromZero);
  97. if (T_Value == ele)
  98. {
  99. t.Layer = "点线矛盾高程点图层";
  100. t.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByLayer, 256);
  101. }
  102. }
  103. }
  104. }
  105. BlockReference blcref = (BlockReference)tr.GetObject(bf.ObjectId, OpenMode.ForWrite);
  106. blcref.Layer = "点线矛盾高程点图层";
  107. tr.Commit();
  108. }
  109. doclock.Dispose();
  110. }
  111. }
  112. }
  113. //获取所有高程点
  114. private List<BlockReference> GetPt(Editor ed,Database db)
  115. {
  116. List<BlockReference> result = new List<BlockReference>();
  117. TypedValue[] typedvalue = new TypedValue[1];
  118. typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, elevationLayer), 0);
  119. SelectionFilter selectionfilter = new SelectionFilter(typedvalue);
  120. PromptSelectionResult psr = ed.SelectAll(selectionfilter);
  121. if (psr.Status == PromptStatus.OK)
  122. {
  123. SelectionSet selectionset = psr.Value;
  124. ObjectId[] obj = new ObjectId[selectionset.Count];
  125. obj = selectionset.GetObjectIds();
  126. for (int i = 0; i < obj.Length; i++)
  127. {
  128. DocumentLock doclock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  129. using (Transaction tr = db.TransactionManager.StartTransaction())
  130. {
  131. ObjectId objid = obj[i];
  132. //找到实体,取高程点的位置
  133. Entity entity = (Entity)tr.GetObject(objid, OpenMode.ForWrite);
  134. if (entity is BlockReference)
  135. {
  136. BlockReference bf = (BlockReference)entity;
  137. result.Add(bf);
  138. }
  139. tr.Commit();
  140. }
  141. doclock.Dispose();
  142. }
  143. }
  144. return result;
  145. }
  146. //设置当前视图
  147. private void SetWindow(Editor ed, Database db,double x,double y,double width,double height)
  148. {
  149. using (Transaction tr = db.TransactionManager.StartTransaction())
  150. {
  151. ViewTableRecord currview = ed.GetCurrentView();
  152. currview.CenterPoint = new Point2d(x, y);
  153. currview.ViewDirection = new Vector3d(0, 0, 1);
  154. currview.Width = width;
  155. currview.Height = height;
  156. ed.SetCurrentView(currview);
  157. tr.Commit();
  158. }
  159. }
  160. //按垂直或水平方向搜索高程点周围等高线
  161. private List<Polyline> SearchDGX(Point3d pt,double length,Editor ed, Database db, int direction)
  162. {
  163. List<Polyline> result = new List<Polyline>();
  164. Point3d startpt;
  165. Point3d endpt;
  166. if (direction == 0)
  167. {
  168. startpt = new Point3d(pt.X - length / 2, pt.Y, pt.Z);
  169. endpt = new Point3d(pt.X + length / 2, pt.Y, pt.Z);
  170. }
  171. else
  172. {
  173. startpt = new Point3d(pt.X, pt.Y - length / 2, pt.Z);
  174. endpt = new Point3d(pt.X, pt.Y + length / 2, pt.Z);
  175. }
  176. Point3dCollection ptcoll = new Point3dCollection();
  177. ptcoll.Add(startpt);
  178. ptcoll.Add(endpt);
  179. TypedValue[] typedvalue = new TypedValue[1];
  180. typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, sqx + "," + jqx), 0);
  181. SelectionFilter selectionfilter = new SelectionFilter(typedvalue);
  182. PromptSelectionResult psr = ed.SelectFence(ptcoll, 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. DocumentLock doclock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  192. using (Transaction tr = db.TransactionManager.StartTransaction())
  193. {
  194. Entity ent = (Entity)tr.GetObject(objid, OpenMode.ForRead);
  195. if(ent is Polyline)
  196. {
  197. Polyline pl = (Polyline)ent;
  198. result.Add(pl);
  199. tr.Commit();
  200. }
  201. }
  202. doclock.Dispose();
  203. }
  204. }
  205. return result;
  206. }
  207. //检查是否有点线矛盾
  208. private bool IfContradition(List<Polyline> pllist, Point3d pt, double length)
  209. {
  210. var result = new List<List<IntersectionResult>>();
  211. // 计算平移向量(以pt为基准,移动至原点)
  212. Vector3d translation = -pt.GetAsVector();
  213. Matrix3d translateMatrix = Matrix3d.Displacement(translation);
  214. var pt_moved = pt.TransformBy(translateMatrix);
  215. Line searchline1 = new Line(new Point3d(pt_moved.X - length / 2, pt_moved.Y, pt_moved.Z), new Point3d(pt_moved.X + length / 2, pt_moved.Y, pt_moved.Z));
  216. Line searchline2 = new Line(new Point3d(pt_moved.X, pt_moved.Y - length / 2, pt_moved.Z), new Point3d(pt_moved.X, pt_moved.Y + length / 2, pt_moved.Z));
  217. var horizontal = new List<IntersectionResult>();
  218. var vertical = new List<IntersectionResult>();
  219. foreach (var item in pllist)
  220. {
  221. //创建临时副本用于移动等高线,以便进行无误差的相交判断
  222. Polyline temppl = (Polyline)item.Clone();
  223. //应用平移变换
  224. temppl.TransformBy(translateMatrix);
  225. Point3dCollection horizontal_pt = new Point3dCollection();
  226. Point3dCollection vertical_pt = new Point3dCollection();
  227. var plane = new Plane(Point3d.Origin, Vector3d.ZAxis);
  228. temppl.IntersectWith(searchline1, Intersect.OnBothOperands, plane, horizontal_pt, IntPtr.Zero, IntPtr.Zero);
  229. temppl.IntersectWith(searchline2, Intersect.OnBothOperands, plane, vertical_pt, IntPtr.Zero, IntPtr.Zero);
  230. Matrix3d inverseMatrix = Matrix3d.Displacement(-translation);
  231. var horizontal_pt_transformed = new List<Point3d>();
  232. var vertical_pt_transformed = new List<Point3d>();
  233. if (horizontal_pt.Count != 0)
  234. {
  235. foreach (Point3d ptitem in horizontal_pt)
  236. horizontal_pt_transformed.Add(ptitem.TransformBy(inverseMatrix));
  237. horizontal.Add(new IntersectionResult { IntersectionPoint = horizontal_pt_transformed, Contour = item });
  238. }
  239. if (vertical_pt.Count != 0)
  240. {
  241. foreach (Point3d ptitem in vertical_pt)
  242. vertical_pt_transformed.Add(ptitem.TransformBy(inverseMatrix));
  243. vertical.Add(new IntersectionResult { IntersectionPoint = vertical_pt_transformed, Contour = item });
  244. }
  245. }
  246. //根据高程点值判断高程点两边应有的等高线高程值
  247. var ele1 = Math.Floor(pt.Z / dgj) * dgj;
  248. var ele2 = ele1 + dgj;
  249. if (ele1 == pt.Z)
  250. return true;
  251. Database db = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database;
  252. //搜索是否有相应高程的等高线
  253. bool hastarget_horizontal = horizontal.Any(item => Math.Round(item.Contour.Elevation - ele1, db.Luprec) == 0) &&
  254. horizontal.Any(item => Math.Round(item.Contour.Elevation - ele2, db.Luprec) == 0);
  255. bool hastarget_vertical = vertical.Any(item => Math.Round(item.Contour.Elevation - ele1, db.Luprec) == 0) &&
  256. vertical.Any(item => Math.Round(item.Contour.Elevation - ele2, db.Luprec) == 0);
  257. //如果有,则判断高程点是否在两线之间,
  258. //没有的话再判断高程点是否是根据等高线变化趋势变化
  259. //if (hastarget_horizontal)
  260. //{
  261. // var pl1 = horizontal.FindAll(item => Math.Round(item.Contour.Elevation - ele1, db.Luprec) == 0).ToList();
  262. // var pl2 = horizontal.FindAll(item => Math.Round(item.Contour.Elevation - ele2, db.Luprec) == 0).ToList();
  263. // for (int i = 0; i < pl1.Count; i++)
  264. // {
  265. // Vector3d vec1;
  266. // foreach (Point3d item1 in pl1[i].IntersectionPoint)
  267. // {
  268. // vec1 = (pt - item1).GetNormal();
  269. // for (int ii = 0; ii < pl2.Count; ii++)
  270. // {
  271. // foreach (Point3d item2 in pl2[ii].IntersectionPoint)
  272. // {
  273. // var dot = (pt - item2).GetNormal().DotProduct(vec1);
  274. // if (dot <= 0)
  275. // return false;
  276. // }
  277. // }
  278. // }
  279. // }
  280. //}
  281. //if (hastarget_vertical)
  282. //{
  283. // var pl1 = vertical.FindAll(item => Math.Round(item.Contour.Elevation - ele1, db.Luprec) == 0).ToList();
  284. // var pl2 = vertical.FindAll(item => Math.Round(item.Contour.Elevation - ele2, db.Luprec) == 0).ToList();
  285. // for (int i = 0; i < pl1.Count; i++)
  286. // {
  287. // Vector3d vec1;
  288. // foreach (Point3d item1 in pl1[i].IntersectionPoint)
  289. // {
  290. // vec1 = (pt - item1).GetNormal();
  291. // for (int ii = 0; ii < pl2.Count; ii++)
  292. // {
  293. // foreach (Point3d item2 in pl2[ii].IntersectionPoint)
  294. // {
  295. // var dot = (pt - item2).GetNormal().DotProduct(vec1);
  296. // if (dot <= 0)
  297. // return false;
  298. // }
  299. // }
  300. // }
  301. // }
  302. //}
  303. //else
  304. //{
  305. return CheckPl(horizontal, vertical, pt);
  306. //只有对应方向上的多段线数量足以判断方向才进行下一步
  307. //if (horizontal.Count >= 2)
  308. //{
  309. // //将高程点左右的线分开,分别放到两个list
  310. // var side1 = new List<Point3d>();
  311. // var side2 = new List<Point3d>();
  312. // foreach (var item in horizontal)
  313. // {
  314. // foreach (var p in item.IntersectionPoint)
  315. // {
  316. // if (Math.Sign(pt.X - p.X) > 0 & !side1.Contains(p) && !side2.Contains(p))
  317. // side1.Add(p);
  318. // else if (Math.Sign(pt.X - p.X) < 0 & !side1.Contains(p) && !side2.Contains(p))
  319. // side2.Add(p);
  320. // else
  321. // continue;
  322. // }
  323. // }
  324. // //如果左右各有一条等高线,由于左右的等高线高程不是需要的高程所以为异常点
  325. // if (side1.Count == 1 && side2.Count == 1)
  326. // return true;
  327. // var largerList = side1.Count >= side2.Count ? side1 : side2;
  328. // var pts_sorted = largerList.OrderBy(p => p.DistanceTo(pt));
  329. // Point3d point = new Point3d();
  330. // Polyline pl = new Polyline();
  331. // foreach (var item in pts_sorted)
  332. // {
  333. // //if (point == new Point3d())
  334. // //{
  335. // // point = item;
  336. // // pl = horizontal.Find(p => p.IntersectionPoint.Contains(item)).Contour;
  337. // // if (Math.Abs(pt.Z - pl.Elevation) > dgj)
  338. // // return true;
  339. // //}
  340. // //else
  341. // //{
  342. // point = item;
  343. // //如果下一个点所在多段线不是这个点所在的多段线且高程不同,判断高程点变化方向是否一致
  344. // if (horizontal.Find(p => p.IntersectionPoint.Contains(item)).Contour != pl &&
  345. // horizontal.Find(p => p.IntersectionPoint.Contains(item)).Contour.Elevation != pl.Elevation)
  346. // {
  347. // var direction = point.Z - item.Z;
  348. // if (pt.Z - point.Z < 0 && direction < 0 ||
  349. // pt.Z - point.Z > 0 && direction > 0)
  350. // return false;
  351. // }
  352. // //else
  353. // //point = item;
  354. // //}
  355. // }
  356. //}
  357. //if (vertical.Count >= 2)
  358. //{
  359. // List<Point3d> pts = new List<Point3d>();
  360. // var side1 = new List<Point3d>();
  361. // var side2 = new List<Point3d>();
  362. // foreach (var item in vertical)
  363. // {
  364. // foreach (var p in item.IntersectionPoint)
  365. // {
  366. // if (Math.Sign(pt.Y - p.Y) > 0 & !side1.Contains(p) && !side2.Contains(p))
  367. // side1.Add(p);
  368. // else if (Math.Sign(pt.Y - p.Y) < 0 & !side1.Contains(p) && !side2.Contains(p))
  369. // side2.Add(p);
  370. // else
  371. // continue;
  372. // }
  373. // }
  374. // if (side1.Count == 1 && side2.Count == 1)
  375. // return true;
  376. // var largerList = side1.Count >= side2.Count ? side1 : side2;
  377. // var pts_sorted = largerList.OrderBy(p => p.DistanceTo(pt));
  378. // Point3d point = new Point3d();
  379. // Polyline pl = new Polyline();
  380. // foreach (var item in pts_sorted)
  381. // {
  382. // //if (point == new Point3d())
  383. // //{
  384. // // point = item;
  385. // // pl = vertical.Find(p => p.IntersectionPoint.Contains(item)).Contour;
  386. // // if (Math.Abs(pt.Z - pl.Elevation) > dgj)
  387. // // return true;
  388. // //}
  389. // //else
  390. // //{
  391. // point = item;
  392. // if (vertical.Find(p => p.IntersectionPoint.Contains(item)).Contour != pl)
  393. // {
  394. // var direction = point.Z - item.Z;
  395. // if (pt.Z - point.Z < 0 && direction < 0 ||
  396. // pt.Z - point.Z > 0 && direction > 0)
  397. // return false;
  398. // }
  399. // //else
  400. // //point = item;
  401. // //}
  402. // }
  403. //}
  404. //}
  405. //return true;
  406. }
  407. //标记错误点
  408. private void MakeFlag(Point3d pt,int blc)
  409. {
  410. double flagradius = 4.0 * blc / 1000;//根据比例尺画圆标记半径
  411. LayerControl layerscontrol = new LayerControl();
  412. string layname = "点线矛盾标记图层";
  413. if (layerscontrol.haslayername(layname) == false)
  414. {
  415. colorgb col = new colorgb(255, 0, 0);
  416. layerscontrol.creatlayer(layname, col);
  417. }
  418. layname = "点线矛盾高程点图层";
  419. if (layerscontrol.haslayername(layname) == false)
  420. {
  421. colorgb col = new colorgb(255, 255, 0);
  422. layerscontrol.creatlayer(layname, col);
  423. layerscontrol.movelayertofront(layname);
  424. }
  425. Database db = HostApplicationServices.WorkingDatabase;
  426. DocumentLock doclock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  427. using (Transaction traction = db.TransactionManager.StartTransaction())
  428. {
  429. BlockTable blocktable= traction.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
  430. BlockTableRecord blocktablerecord = traction.GetObject(blocktable[BlockTableRecord.ModelSpace],
  431. OpenMode.ForWrite) as BlockTableRecord;
  432. Circle circ = new Circle();
  433. circ.Layer = "点线矛盾标记图层";
  434. circ.Center = pt;
  435. circ.Radius = flagradius;
  436. circ.Normal = new Vector3d(0, 0, 1);
  437. circ.SetDatabaseDefaults();
  438. blocktablerecord.AppendEntity(circ);
  439. traction.AddNewlyCreatedDBObject(circ, true);
  440. traction.Commit();
  441. traction.Dispose();
  442. }
  443. doclock.Dispose();
  444. }
  445. private bool CheckPl(List<IntersectionResult> horizontal, List<IntersectionResult> vertical, Point3d pt)
  446. {
  447. bool constradition = true;
  448. bool isclosed = false;
  449. bool isridge = false;
  450. if (horizontal.Count >= 2)
  451. {
  452. //将高程点左右的线分开,分别放到两个list
  453. var side1 = new List<Point3d>();
  454. var side2 = new List<Point3d>();
  455. foreach (var item in horizontal)
  456. {
  457. int side1count = 0;
  458. int side2count = 0;
  459. foreach (var p in item.IntersectionPoint)
  460. {
  461. if (Math.Sign(pt.X - p.X) > 0 & !side1.Contains(p) && !side2.Contains(p)&& side1count==0)
  462. {
  463. side1.Add(p);
  464. side1count = 1;
  465. }
  466. else if (Math.Sign(pt.X - p.X) < 0 & !side1.Contains(p) && !side2.Contains(p)&& side2count==0)
  467. {
  468. side2.Add(p);
  469. side2count = 1;
  470. }
  471. else
  472. continue;
  473. }
  474. }
  475. var pl1 = new Polyline();
  476. var pl2 = new Polyline();
  477. if (side1.Count != 0 && side2.Count != 0)
  478. {
  479. var pts_sorted1 = side1.OrderBy(p => p.DistanceTo(pt)).ToList();
  480. var pts_sorted2 = side2.OrderBy(p => p.DistanceTo(pt)).ToList();
  481. pl1 = horizontal.Find(p => p.IntersectionPoint.Contains(pts_sorted1[0])).Contour;
  482. pl2 = horizontal.Find(p => p.IntersectionPoint.Contains(pts_sorted2[0])).Contour;
  483. if (pl1 != pl2)
  484. {
  485. if (pl1.Elevation < pt.Z && pt.Z < pl2.Elevation ||
  486. pl2.Elevation < pt.Z && pt.Z < pl1.Elevation)
  487. constradition = false;
  488. else if (pl1.Elevation == pl2.Elevation)
  489. {
  490. isridge = true;
  491. foreach (var item1 in pts_sorted1)
  492. {
  493. var pl3 = horizontal.Find(p => p.IntersectionPoint.Contains(item1)).Contour;
  494. if (pl3 != pl1)
  495. {
  496. if (pl3.Elevation < pl1.Elevation && pl1.Elevation < pt.Z ||
  497. pl3.Elevation > pl1.Elevation && pl1.Elevation > pt.Z)
  498. {
  499. foreach (var item2 in pts_sorted2)
  500. {
  501. var pl4 = horizontal.Find(p => p.IntersectionPoint.Contains(item2)).Contour;
  502. if (pl4 != pl2)
  503. {
  504. if (pl4.Elevation < pl2.Elevation && pl2.Elevation < pt.Z ||
  505. pl4.Elevation > pl2.Elevation && pl2.Elevation > pt.Z)
  506. {
  507. constradition = false;
  508. break;
  509. }
  510. }
  511. }
  512. break;
  513. }
  514. }
  515. }
  516. }
  517. else
  518. constradition = true;
  519. }
  520. else
  521. isclosed = true;
  522. }
  523. if (side1.Count != 0 && side2.Count == 0 || isclosed && side1.Count >= 2)
  524. {
  525. var pts_sorted1 = side1.OrderBy(p => p.DistanceTo(pt)).ToList();
  526. pl1 = horizontal.Find(p => p.IntersectionPoint.Contains(pts_sorted1[0])).Contour;
  527. foreach (var item in pts_sorted1)
  528. {
  529. pl2 = horizontal.Find(p => p.IntersectionPoint.Contains(item)).Contour;
  530. if (pl2 != pl1)
  531. break;
  532. }
  533. }
  534. else if (side1.Count == 0 && side2.Count != 0 || isclosed && side2.Count >= 2)
  535. {
  536. var pts_sorted2 = side2.OrderBy(p => p.DistanceTo(pt)).ToList();
  537. pl1 = horizontal.Find(p => p.IntersectionPoint.Contains(pts_sorted2[0])).Contour;
  538. foreach (var item in pts_sorted2)
  539. {
  540. pl2 = horizontal.Find(p => p.IntersectionPoint.Contains(item)).Contour;
  541. if (pl2 != pl1)
  542. break;
  543. }
  544. }
  545. if (pl1 != pl2 && side1.Count == 0|| pl1 != pl2 && side2.Count == 0|| pl1 != pl2 && isclosed)
  546. {
  547. if (pl2.Elevation < pl1.Elevation && pl1.Elevation < pt.Z ||
  548. pl2.Elevation > pl1.Elevation && pl1.Elevation > pt.Z)
  549. constradition = false;
  550. else
  551. constradition = true;
  552. }
  553. }
  554. if (vertical.Count >= 2 && constradition == true||isridge==true&& vertical.Count >= 2)
  555. {
  556. isclosed = false;
  557. //将高程点左右的线分开,分别放到两个list
  558. var side1 = new List<Point3d>();
  559. var side2 = new List<Point3d>();
  560. foreach (var item in vertical)
  561. {
  562. foreach (var p in item.IntersectionPoint)
  563. {
  564. if (Math.Sign(pt.Y - p.Y) > 0 & !side1.Contains(p) && !side2.Contains(p))
  565. side1.Add(p);
  566. else if (Math.Sign(pt.Y - p.Y) < 0 & !side1.Contains(p) && !side2.Contains(p))
  567. side2.Add(p);
  568. else
  569. continue;
  570. }
  571. }
  572. var pl1 = new Polyline();
  573. var pl2 = new Polyline();
  574. if (side1.Count != 0 && side2.Count != 0)
  575. {
  576. var pts_sorted1 = side1.OrderBy(p => p.DistanceTo(pt)).ToList();
  577. var pts_sorted2 = side2.OrderBy(p => p.DistanceTo(pt)).ToList();
  578. pl1 = vertical.Find(p => p.IntersectionPoint.Contains(pts_sorted1[0])).Contour;
  579. pl2 = vertical.Find(p => p.IntersectionPoint.Contains(pts_sorted2[0])).Contour;
  580. if (pl1 != pl2)
  581. {
  582. if (pl1.Elevation < pt.Z && pt.Z < pl2.Elevation ||
  583. pl2.Elevation < pt.Z && pt.Z < pl1.Elevation)
  584. constradition = false;
  585. else
  586. constradition = true;
  587. }
  588. else if (pl1.Elevation == pl2.Elevation)
  589. {
  590. isridge = true;
  591. foreach (var item1 in pts_sorted1)
  592. {
  593. var pl3 = vertical.Find(p => p.IntersectionPoint.Contains(item1)).Contour;
  594. if (pl3 != pl1)
  595. {
  596. if (pl3.Elevation < pl1.Elevation && pl1.Elevation < pt.Z ||
  597. pl3.Elevation > pl1.Elevation && pl1.Elevation > pt.Z)
  598. {
  599. foreach (var item2 in pts_sorted2)
  600. {
  601. var pl4 = vertical.Find(p => p.IntersectionPoint.Contains(item2)).Contour;
  602. if (pl4 != pl2)
  603. {
  604. if (pl4.Elevation < pl2.Elevation && pl2.Elevation < pt.Z ||
  605. pl4.Elevation > pl2.Elevation && pl2.Elevation > pt.Z)
  606. {
  607. constradition = false;
  608. break;
  609. }
  610. }
  611. }
  612. break;
  613. }
  614. }
  615. }
  616. }
  617. else
  618. isclosed = true;
  619. }
  620. if (side1.Count != 0 && side2.Count == 0 || isclosed && side1.Count >= 2)
  621. {
  622. var pts_sorted1 = side1.OrderBy(p => p.DistanceTo(pt)).ToList();
  623. pl1 = vertical.Find(p => p.IntersectionPoint.Contains(pts_sorted1[0])).Contour;
  624. foreach (var item in pts_sorted1)
  625. {
  626. pl2 = vertical.Find(p => p.IntersectionPoint.Contains(item)).Contour;
  627. if (pl2 != pl1)
  628. break;
  629. }
  630. }
  631. else if (side1.Count == 0 && side2.Count != 0 || isclosed && side2.Count >= 2)
  632. {
  633. var pts_sorted2 = side2.OrderBy(p => p.DistanceTo(pt)).ToList();
  634. pl1 = vertical.Find(p => p.IntersectionPoint.Contains(pts_sorted2[0])).Contour;
  635. foreach (var item in pts_sorted2)
  636. {
  637. pl2 = vertical.Find(p => p.IntersectionPoint.Contains(item)).Contour;
  638. if (pl2 != pl1)
  639. break;
  640. }
  641. }
  642. if (pl1 != pl2 && side1.Count == 0 || pl1 != pl2 && side2.Count == 0 || pl1 != pl2 && isclosed)
  643. {
  644. if (pl2.Elevation < pl1.Elevation && pl1.Elevation < pt.Z ||
  645. pl2.Elevation > pl1.Elevation && pl1.Elevation > pt.Z)
  646. constradition = false;
  647. else
  648. constradition = true;
  649. }
  650. }
  651. return constradition;
  652. }
  653. }
  654. }