工具箱相关
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

SlopeLine.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using GrxCAD.ApplicationServices;
  7. using GrxCAD.DatabaseServices;
  8. using GrxCAD.EditorInput;
  9. using GrxCAD.Geometry;
  10. namespace HCTools
  11. {
  12. class SlopeLine
  13. {
  14. public static int blc;
  15. public struct ContourElevation
  16. {
  17. public double elevation;
  18. public double length;
  19. public ObjectId objID;
  20. }
  21. ObjectIdCollection idcoll = new ObjectIdCollection();
  22. public void slopeline()
  23. {
  24. Document doc = Application.DocumentManager.MdiActiveDocument;
  25. Database db = doc.Database;
  26. Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  27. TypedValue[] value = new TypedValue[]
  28. {
  29. new TypedValue((int)DxfCode.Start,"LWPOLYLINE"),
  30. new TypedValue((int)DxfCode.LayerName,"8110,8120")
  31. };//设置筛选条件
  32. SelectionFilter filter = new SelectionFilter(value);
  33. //选择区域中全部对象
  34. //PromptSelectionResult psr = ed.SelectAll(filter);
  35. // 要求在图形区域中手动选择对象
  36. PromptSelectionResult psr = ed.GetSelection(filter);
  37. if (psr.Status == PromptStatus.OK)
  38. {
  39. SelectionSet ss = psr.Value;
  40. ObjectId[] ids = new ObjectId[ss.Count];
  41. ids = ss.GetObjectIds();
  42. foreach (ObjectId id in ids)
  43. {
  44. DocumentLock doclock = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  45. using (Transaction trans = db.TransactionManager.StartTransaction())
  46. {
  47. Polyline pll = trans.GetObject(id, OpenMode.ForRead) as Polyline;
  48. if (pll.StartPoint == pll.EndPoint || pll.Closed == true)
  49. {
  50. searchslpline(pll,trans);
  51. }
  52. trans.Commit();
  53. }
  54. doclock.Dispose();
  55. }
  56. }
  57. }
  58. /// <summary>
  59. /// 搜索闭合等高线并画出示坡线
  60. /// </summary>
  61. private void searchslpline(Polyline pll, Transaction trans)
  62. {
  63. if (idcoll.Contains(pll.Id))
  64. return;
  65. Point3dCollection ptcoll = new Point3dCollection();
  66. Point3dCollection ptcoll2 = new Point3dCollection();
  67. int windowwidth = 30 * 5 * blc / 1000;//当前可视范围
  68. int windowheight = 30 * 5 * blc / 1000;
  69. Extents3d extents = pll.GeometricExtents;
  70. Point3d center = extents.MinPoint + (extents.MaxPoint - extents.MinPoint) / 2.0;//计算几何中心
  71. Matrix3d mt1 = Matrix3d.Scaling(2, center);
  72. pll = trans.GetObject(pll.Id,OpenMode.ForWrite)as Polyline;
  73. pll.TransformBy(mt1);
  74. pll.GetStretchPoints(ptcoll);//将闭合等高线缩放至原来的两倍大并获取其节点用于之后的筛选
  75. Matrix3d mt2 = Matrix3d.Scaling(0.5, center);
  76. pll.TransformBy(mt2);//还原闭合等高线
  77. //获取闭合等高线的节点并搜索其内是否还有闭合等高线,若有,则选择最里面的闭合等高线画示坡线
  78. pll.GetStretchPoints(ptcoll2);
  79. Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  80. TypedValue[] value = new TypedValue[]
  81. {
  82. new TypedValue((int)DxfCode.Start,"LWPOLYLINE"),
  83. new TypedValue((int)DxfCode.LayerName,"8110,8120")
  84. };//设置筛选条件
  85. SelectionFilter filter = new SelectionFilter(value);
  86. ViewTableRecord currview = ed.GetCurrentView();
  87. currview.CenterPoint = new Point2d(pll.StartPoint.X, pll.StartPoint.Y);
  88. currview.ViewDirection = new Vector3d(0, 0, 1);
  89. currview.Width = windowwidth;
  90. currview.Height = windowheight;
  91. ed.SetCurrentView(currview);
  92. PromptSelectionResult psr1 = ed.SelectWindowPolygon(ptcoll2, filter);
  93. if (psr1.Status == PromptStatus.OK && psr1.Value.Count > 0)
  94. {
  95. SelectionSet selects = psr1.Value;
  96. ObjectId[] idss = selects.GetObjectIds();
  97. List<ContourElevation> Contourcollect = new List<ContourElevation>();
  98. ContourElevation self = new ContourElevation();
  99. self.length = pll.Length;
  100. self.objID = pll.Id;
  101. Contourcollect.Add(self);
  102. for (int ii = 0; ii < idss.Length; ii++)
  103. {
  104. Polyline pllselected = trans.GetObject(idss[ii], OpenMode.ForRead) as Polyline;
  105. ContourElevation cl = new ContourElevation();
  106. cl.length = pllselected.Length;
  107. cl.objID = pllselected.Id;
  108. Contourcollect.Add(cl);
  109. }
  110. List<ContourElevation> sorted = Contourcollect.OrderBy(s => s.length).ToList();//length 从小到大
  111. pll = trans.GetObject(sorted[0].objID, OpenMode.ForWrite) as Polyline;
  112. while (pll.Closed == false && pll.StartPoint != pll.EndPoint)//判断最短的等高线是否闭合
  113. {
  114. sorted.RemoveAt(0);
  115. pll = trans.GetObject(sorted[0].objID, OpenMode.ForWrite) as Polyline;
  116. }
  117. if (idcoll.Contains(pll.Id))
  118. return;
  119. }
  120. //通过之前得到的增大两倍后的闭合等高线节点进行窗交,获取周围等高线,根据此闭合等高线和
  121. //周围等高线的大小关系决定示坡线的方向
  122. PromptSelectionResult psr2 = ed.SelectCrossingPolygon(ptcoll, filter);
  123. if (psr2.Status == PromptStatus.OK)
  124. {
  125. SelectionSet ss = psr2.Value;
  126. ObjectId[] ids = ss.GetObjectIds();
  127. List<ContourElevation> Contour = new List<ContourElevation>();
  128. for (int ii = 0; ii < ids.Length; ii++)
  129. {
  130. Polyline pllselected = trans.GetObject(ids[ii], OpenMode.ForRead) as Polyline;
  131. //if (pllselected.Closed == true || pllselected.StartPoint == pllselected.EndPoint &&
  132. // pllselected.Id != pll.Id)
  133. // continue;
  134. ContourElevation ce = new ContourElevation();
  135. ce.elevation = pllselected.Elevation;
  136. ce.objID = pllselected.Id;
  137. Contour.Add(ce);
  138. }
  139. List<ContourElevation> sorted = Contour.OrderBy(s => s.elevation).ToList();//elevation 从小到大
  140. if (sorted[Contour.Count - 1].elevation == pll.Elevation)
  141. {
  142. int j = 1;
  143. drawslpline(pll, j, trans);
  144. idcoll.Add(pll.Id);
  145. }
  146. if (sorted[0].elevation == pll.Elevation)
  147. {
  148. int j = -1;
  149. drawslpline(pll, j, trans);
  150. idcoll.Add(pll.Id);
  151. }
  152. }
  153. }
  154. /// <summary>
  155. /// 画示坡线
  156. /// </summary>
  157. private void drawslpline(Polyline pll, double i, Transaction trans)
  158. {
  159. Document doc = Application.DocumentManager.MdiActiveDocument;
  160. Database db = doc.Database;
  161. BlockTable blocktable = trans.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
  162. BlockTableRecord blocktablerecord = trans.GetObject(blocktable[BlockTableRecord.ModelSpace],
  163. OpenMode.ForWrite) as BlockTableRecord;
  164. //创建标注相交点符号的图层
  165. LayerControl layerscontrol = new LayerControl();
  166. string layname = "8200";
  167. if (!layerscontrol.haslayername(layname))
  168. {
  169. colorgb col = new colorgb(0, 255, 0);
  170. layerscontrol.creatlayer(layname, col);
  171. layerscontrol.movelayertofront(layname);
  172. }
  173. else
  174. layerscontrol.movelayertofront(layname);
  175. //此等高线高程最高
  176. if (i == 1)
  177. {
  178. //由于IntersectWith()函数在离原点太远的位置会有误差,所以将需要判断的闭合等高线移动到原点附近
  179. Point3d tagpt = pll.StartPoint;//移动目标点
  180. Point3dCollection ptss = new Point3dCollection();
  181. pll.GetStretchPoints(ptss);
  182. Vector3d acVec3d = pll.StartPoint.GetVectorTo(new Point3d(0, 0, 0));
  183. pll.TransformBy(Matrix3d.Displacement(acVec3d));
  184. //获取几何中心
  185. Extents3d extents = pll.GeometricExtents;
  186. Point3d center = extents.MinPoint + (extents.MaxPoint - extents.MinPoint) / 2.0;
  187. Point3dCollection ptcoll = new Point3dCollection();
  188. //由几何中心到闭合等高线的起点画一条示坡线,将其以几何中心作为基点延长
  189. Line slpline = new Line(center, pll.StartPoint);
  190. blocktablerecord.AppendEntity(slpline);
  191. trans.AddNewlyCreatedDBObject(slpline, true);
  192. slpline = trans.GetObject(slpline.Id,OpenMode.ForWrite)as Line;
  193. Matrix3d mt1 = Matrix3d.Scaling(1.5, center);
  194. slpline.TransformBy(mt1);
  195. //获取示坡线与闭合等高线的交点,将示坡线由此打断,删除闭合等高线内的部分
  196. slpline.IntersectWith(pll, Intersect.OnBothOperands, ptcoll, IntPtr.Zero, IntPtr.Zero);
  197. Point3d pt1 = ptcoll[0];
  198. Point3d pt2 = ptcoll[1];
  199. DBObjectCollection objcoll= slpline.GetSplitCurves(ptcoll);
  200. slpline.Erase();
  201. foreach (Entity ent in objcoll)
  202. {
  203. Line plline = ent as Line;
  204. if (plline.StartPoint != pll.StartPoint)
  205. {
  206. blocktablerecord.AppendEntity(plline);
  207. trans.AddNewlyCreatedDBObject(plline, true);
  208. objcoll.Remove(plline);
  209. plline.Erase();
  210. }
  211. }
  212. Line slpline1 = objcoll[0] as Line;
  213. slpline1.Layer = "8200";
  214. blocktablerecord.AppendEntity(slpline1);
  215. trans.AddNewlyCreatedDBObject(slpline1, true);
  216. Point3dCollection pts = new Point3dCollection();
  217. pll.GetStretchPoints(pts);//获取当前闭合等高线节点
  218. //由闭合等高线的几何中心和中间节点创建另一条示坡线,以几何中心作为基点延长
  219. Line slpline2 = new Line(center, pts[pts.Count / 2 - 1]);
  220. blocktablerecord.AppendEntity(slpline2);
  221. trans.AddNewlyCreatedDBObject(slpline2, true);
  222. slpline2 = trans.GetObject(slpline2.Id, OpenMode.ForWrite) as Line;
  223. Matrix3d mt2 = Matrix3d.Scaling(1.5, center);
  224. slpline2.TransformBy(mt2);
  225. ptcoll.Clear();
  226. //获取示坡线与闭合等高线的交点,将示坡线由此打断,删除闭合等高线内的部分
  227. slpline2.IntersectWith(pll, Intersect.OnBothOperands, ptcoll, IntPtr.Zero, IntPtr.Zero);
  228. DBObjectCollection objcoll1 = slpline2.GetSplitCurves(ptcoll);
  229. slpline2.Erase();
  230. foreach (Entity ent in objcoll1)
  231. {
  232. Line plline = ent as Line;
  233. if (plline.StartPoint != pts[pts.Count / 2 - 1])
  234. {
  235. blocktablerecord.AppendEntity(plline);
  236. trans.AddNewlyCreatedDBObject(plline, true);
  237. objcoll1.Remove(plline);
  238. plline.Erase();
  239. }
  240. }
  241. Line slpline3 = objcoll1[0] as Line;
  242. slpline3.Layer = "8200";
  243. blocktablerecord.AppendEntity(slpline3);
  244. trans.AddNewlyCreatedDBObject(slpline3, true);
  245. //将闭合等高线和示坡线移回原来的位置
  246. Vector3d target1 = pll.StartPoint.GetVectorTo(tagpt);
  247. pll.TransformBy(Matrix3d.Displacement(target1));
  248. slpline1 = trans.GetObject(slpline1.Id, OpenMode.ForWrite) as Line;
  249. Vector3d target2 = slpline1.StartPoint.GetVectorTo(tagpt);
  250. slpline1.TransformBy(Matrix3d.Displacement(target2));
  251. slpline3 = trans.GetObject(slpline3.Id, OpenMode.ForWrite) as Line;
  252. Vector3d target3 = slpline3.StartPoint.GetVectorTo(ptss[ptss.Count / 2 - 1]);
  253. slpline3.TransformBy(Matrix3d.Displacement(target3));
  254. }
  255. //此等高线高程最低
  256. if (i == -1)
  257. {
  258. //获取几何中心
  259. Extents3d extents = pll.GeometricExtents;
  260. Point3d center = extents.MinPoint + (extents.MaxPoint - extents.MinPoint) / 2.0;
  261. Point3dCollection ptcoll = new Point3dCollection();
  262. //由几何中心到闭合等高线的起点画一条示坡线,将其以闭合等高线的起点作为基点缩短
  263. Line slpline = new Line(center, pll.StartPoint);
  264. blocktablerecord.AppendEntity(slpline);
  265. trans.AddNewlyCreatedDBObject(slpline, true);
  266. slpline = trans.GetObject(slpline.Id, OpenMode.ForWrite) as Line;
  267. Matrix3d mt1 = Matrix3d.Scaling(0.5, pll.StartPoint);
  268. slpline.TransformBy(mt1);
  269. slpline.Layer = "8200";
  270. //由闭合等高线的几何中心和中间节点创建另一条示坡线,以中间节点作为基点缩短
  271. Point3dCollection pts = new Point3dCollection();
  272. pll.GetStretchPoints(pts);
  273. Line slpline1 = new Line(center, pts[pts.Count / 2 - 1]);
  274. blocktablerecord.AppendEntity(slpline1);
  275. trans.AddNewlyCreatedDBObject(slpline1, true);
  276. slpline1 = trans.GetObject(slpline1.Id, OpenMode.ForWrite) as Line;
  277. Matrix3d mt2 = Matrix3d.Scaling(0.5, pts[pts.Count / 2 - 1]);
  278. slpline1.TransformBy(mt2);
  279. slpline1.Layer = "8200";
  280. }
  281. }
  282. }
  283. }