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

ContourLineFix.cs 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using GrxCAD.ApplicationServices;
  2. using GrxCAD.DatabaseServices;
  3. using GrxCAD.EditorInput;
  4. using GrxCAD.Geometry;
  5. using GrxCAD.GraphicsInterface;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace HCTools
  12. {
  13. class ContourLineFix
  14. {
  15. public void Clfix()
  16. {
  17. Database db = HostApplicationServices.WorkingDatabase;
  18. Document doc = Application.DocumentManager.MdiActiveDocument;
  19. DocumentLock doclock = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  20. ObjectId jigid = new ObjectId();
  21. PromptPointResult pPtRes;
  22. PromptPointOptions pPtOpts = new PromptPointOptions("");
  23. pPtOpts.Message = "选择起点\n ";
  24. pPtRes = doc.Editor.GetPoint(pPtOpts);
  25. Point3d ptStart = pPtRes.Value;
  26. if (pPtRes.Status == PromptStatus.OK)
  27. {
  28. LineJig lJig = new LineJig(ptStart);
  29. PromptResult PR = doc.Editor.Drag(lJig);
  30. if (PR.Status == PromptStatus.OK)
  31. {
  32. jigid = BasicFunction.AddObj(doc, lJig.line_1);
  33. GrxCAD.DatabaseServices.Line jigl = (GrxCAD.DatabaseServices.Line)BasicFunction.GetDBObject(jigid);
  34. List<ObjectId> plllist = Pllsort(jigl);
  35. GrxCAD.DatabaseServices.Polyline pll_s = (GrxCAD.DatabaseServices.Polyline)BasicFunction.GetDBObject(plllist[0]);
  36. GrxCAD.DatabaseServices.Polyline pll_w = (GrxCAD.DatabaseServices.Polyline)BasicFunction.GetDBObject(plllist[plllist.Count - 1]);
  37. double dgj = (pll_s.Elevation - pll_w.Elevation)/(plllist.Count - 1);
  38. double ele = pll_s.Elevation;
  39. using (Transaction tr = db.TransactionManager.StartTransaction())
  40. {
  41. for (int i = 1; i < plllist.Count - 1; i++)
  42. {
  43. GrxCAD.DatabaseServices.Polyline pll = (GrxCAD.DatabaseServices.Polyline)tr.GetObject(plllist[i], OpenMode.ForWrite, true);
  44. pll.Elevation = ele - dgj;
  45. ele = ele - dgj;
  46. }
  47. GrxCAD.DatabaseServices.Line jl = (GrxCAD.DatabaseServices.Line)tr.GetObject(jigid, OpenMode.ForWrite, true);
  48. jl.Erase();
  49. tr.Commit();
  50. }
  51. Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  52. ed.WriteMessage("修改完成\n");
  53. }
  54. }
  55. doclock.Dispose();
  56. }
  57. private static List<ObjectId> Pllsort(Line jigl)
  58. {
  59. List<ObjectId> sortedid = new List<ObjectId>();
  60. Point3dCollection ptcoll = new Point3dCollection();
  61. var plane = new Plane(Point3d.Origin, Vector3d.ZAxis);
  62. ptcoll.Add(jigl.StartPoint);
  63. ptcoll.Add(jigl.EndPoint);
  64. Database db = HostApplicationServices.WorkingDatabase;
  65. Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  66. TypedValue[] value = new TypedValue[]
  67. {
  68. new TypedValue((int)DxfCode.Start,"LWPOLYLINE"),
  69. };//设置筛选条件
  70. SelectionFilter filter = new SelectionFilter(value);
  71. // 要求在图形区域中手动选择对象
  72. PromptSelectionResult psr = ed.SelectFence(ptcoll, filter);
  73. if (psr.Status == PromptStatus.OK)
  74. {
  75. SelectionSet ss = psr.Value;
  76. ObjectIdCollection idcoll = new ObjectIdCollection(ss.GetObjectIds());
  77. using (Transaction trans = db.TransactionManager.StartTransaction())
  78. {
  79. for (int i = 1; i < idcoll.Count; i++)
  80. {
  81. GrxCAD.DatabaseServices.Polyline pll = (GrxCAD.DatabaseServices.Polyline)BasicFunction.GetDBObject(idcoll[i]);
  82. Point3dCollection itsresult1 = new Point3dCollection();
  83. pll.IntersectWith(jigl, Intersect.OnBothOperands, plane, itsresult1, IntPtr.Zero, IntPtr.Zero);
  84. double x1 = itsresult1[0].X;
  85. double y1 = itsresult1[0].Y;
  86. double x2 = jigl.StartPoint.X;
  87. double y2 = jigl.StartPoint.Y;
  88. double tempdist = Math.Abs(Math.Sqrt(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2)));
  89. ObjectId temp = idcoll[i];
  90. for (int j = i - 1; j >=0; j--)
  91. {
  92. GrxCAD.DatabaseServices.Polyline pll2 = (GrxCAD.DatabaseServices.Polyline)BasicFunction.GetDBObject(idcoll[j]);
  93. Point3dCollection itsresult2 = new Point3dCollection();
  94. pll2.IntersectWith(jigl, Intersect.OnBothOperands, plane, itsresult2, IntPtr.Zero, IntPtr.Zero);
  95. double x3 = itsresult2[0].X;
  96. double y3 = itsresult2[0].Y;
  97. double tempdist2 = Math.Abs(Math.Sqrt(Math.Pow(x3 - x2, 2) + Math.Pow(y3 - y2, 2)));
  98. if (tempdist < tempdist2)
  99. {
  100. idcoll[j + 1] = idcoll[j];
  101. idcoll[j] = temp;
  102. }
  103. else
  104. break;
  105. }
  106. }
  107. foreach (ObjectId id in idcoll)
  108. {
  109. sortedid.Add(id);
  110. }
  111. trans.Commit();
  112. }
  113. }
  114. return sortedid;
  115. }
  116. }
  117. public class LineJig : DrawJig
  118. {
  119. public Line line_1;
  120. public Point3d BasePnt;
  121. public Point3d AcquirePnt;
  122. public LineJig(Point3d _basePnt)
  123. {
  124. line_1 = new Line();
  125. BasePnt = _basePnt;
  126. }
  127. protected override SamplerStatus Sampler(JigPrompts prompts)
  128. {
  129. JigPromptPointOptions JPPO = new JigPromptPointOptions();
  130. JPPO.Message = "选择终点\n";
  131. JPPO.UserInputControls = (UserInputControls.Accept3dCoordinates | UserInputControls.NullResponseAccepted | UserInputControls.AnyBlankTerminatesInput);
  132. PromptPointResult PR = prompts.AcquirePoint(JPPO);
  133. if (PR.Status != PromptStatus.OK)
  134. return SamplerStatus.Cancel;
  135. if (PR.Value == AcquirePnt)
  136. return SamplerStatus.NoChange;
  137. AcquirePnt = PR.Value;
  138. return SamplerStatus.OK;
  139. }
  140. protected override bool WorldDraw(WorldDraw draw)
  141. {
  142. Point3d point_1 = new Point3d(BasePnt.X, BasePnt.Y, 0);
  143. Point3d point_2 = new Point3d(AcquirePnt.X, AcquirePnt.Y, 0);
  144. line_1.StartPoint = point_1;
  145. line_1.EndPoint = point_2;
  146. line_1.Color = GrxCAD.Colors.Color.FromColorIndex(GrxCAD.Colors.ColorMethod.ByColor, 7);
  147. draw.Geometry.Draw(line_1);
  148. return true;
  149. }
  150. }
  151. }