工具箱相关
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PickupPoint.cs 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. using System.Windows.Forms;
  11. using System.IO;
  12. namespace HCTools
  13. {
  14. class PickupPoint
  15. {
  16. public void PickupPt(string num)
  17. {
  18. Document document = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  19. Database db = HostApplicationServices.WorkingDatabase;
  20. Editor ed = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  21. TypedValue[] value = new TypedValue[]
  22. {
  23. new TypedValue((int)DxfCode.Start,"LWPOLYLINE,POLYLINE")
  24. };//设置筛选条件
  25. SelectionFilter filter = new SelectionFilter(value);
  26. //选择区域中全部对象
  27. //PromptSelectionResult psr = ed.SelectAll(filter);
  28. // 要求在图形区域中手动选择对象
  29. PromptSelectionResult psr = ed.GetSelection(filter);
  30. SelectionSet selectionset = psr.Value;
  31. if (psr.Status == PromptStatus.OK)
  32. {
  33. ObjectId[] obj = new ObjectId[selectionset.Count];
  34. obj = selectionset.GetObjectIds();
  35. List<Point3d> AllPoints = new List<Point3d>();
  36. List<string> FinalPoints = new List<string>();
  37. string decimalnum = "F" + num;
  38. string ele = " ";
  39. for (int i = 0; i < obj.Length; i++)
  40. {
  41. DocumentLock documentlock = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
  42. using (Transaction trans = db.TransactionManager.StartTransaction())
  43. {
  44. Point3dCollection coll = new Point3dCollection();
  45. ObjectId objid = obj[i];
  46. Entity entity = (Entity)trans.GetObject(objid, OpenMode.ForWrite);
  47. if (entity is Polyline)
  48. {
  49. Polyline line = entity as Polyline;
  50. //line.GetStretchPoints(coll);
  51. int vertexNum = line.NumberOfVertices;
  52. Point3d point;
  53. // 遍历获取多段线顶点坐标
  54. for (int ii = 0; ii < vertexNum; ii++)
  55. {
  56. point = line.GetPoint3dAt(ii);
  57. coll.Add(point);
  58. }
  59. ele = line.Elevation.ToString();
  60. }
  61. else if (entity is Polyline2d)
  62. {
  63. Polyline2d line = entity as Polyline2d;
  64. //line.GetStretchPoints(coll);
  65. foreach (ObjectId id in line)
  66. {
  67. Vertex2d vx = (Vertex2d)trans.GetObject(id, OpenMode.ForRead);
  68. if (vx.VertexType != Vertex2dType.SplineControlVertex)
  69. {
  70. coll.Add(vx.Position);
  71. }
  72. }
  73. ele = line.Elevation.ToString();
  74. }
  75. else if(entity is Polyline3d)
  76. {
  77. Polyline3d line = entity as Polyline3d;
  78. foreach (ObjectId id in line)
  79. {
  80. PolylineVertex3d vx3d = (PolylineVertex3d)trans.GetObject(id,
  81. OpenMode.ForRead);
  82. coll.Add(vx3d.Position);
  83. }
  84. ele = "三维多段线";
  85. }
  86. #region//无用
  87. ////检查选择的多段线是否有重复点,如果有就移除
  88. //for (int ii = 0; ii < coll.Count - 1; ii++)
  89. //{
  90. // for (int iii = ii + 1; iii < ii + 21; iii++)
  91. // {
  92. // if (iii == coll.Count + 1)
  93. // break;
  94. // string x1 = coll[ii].X.ToString(decimalnum);
  95. // string y1 = coll[ii].Y.ToString(decimalnum);
  96. // string x2 = coll[iii].X.ToString(decimalnum);
  97. // string y2 = coll[iii].Y.ToString(decimalnum);
  98. // if (x1 == x2 && y1 == y2)
  99. // {
  100. // coll.RemoveAt(iii);
  101. // }
  102. // else
  103. // {
  104. // continue;
  105. // }
  106. // }
  107. //}
  108. #endregion
  109. for (int j = 0; j < coll.Count; j++)
  110. {
  111. AllPoints.Add(coll[j]);
  112. }
  113. trans.Commit();
  114. }
  115. documentlock.Dispose();
  116. for (int ii = 0; ii < AllPoints.Count; ii++)
  117. {
  118. if (ii == 0)
  119. FinalPoints.Add(ele);
  120. string x = AllPoints[ii].X.ToString(decimalnum);
  121. string y = AllPoints[ii].Y.ToString(decimalnum);
  122. string z = AllPoints[ii].Z.ToString(decimalnum);
  123. string res = string.Join(" ", new string[] { x, y, z });
  124. FinalPoints.Add(res);
  125. }
  126. AllPoints.Clear();
  127. }
  128. //保存文件对话框
  129. System.Windows.Forms.SaveFileDialog save = new System.Windows.Forms.SaveFileDialog();
  130. save.Filter = "文本文件(*.txt)|*.txt";
  131. //FolderBrowserDialog dialog = new FolderBrowserDialog();
  132. //dialog.Description = "请选择文件路径";
  133. //保存文件是否记录上一次文件目录
  134. save.RestoreDirectory = true;
  135. if (save.ShowDialog() == DialogResult.OK)
  136. {
  137. string savePath = save.FileName;
  138. File.WriteAllLines(savePath, FinalPoints, Encoding.UTF8);
  139. ed.WriteMessage("保存成功\n");
  140. }
  141. else
  142. {
  143. MessageBox.Show("保存失败\n");
  144. }
  145. //if (dialog.ShowDialog() == DialogResult.OK)
  146. //{
  147. // string foldPath = dialog.SelectedPath;
  148. // string savePath = foldPath + "\\" + ele + ".txt";
  149. // File.WriteAllLines(savePath, FinalPoints, Encoding.UTF8);
  150. // ed.WriteMessage("\n保存成功");
  151. //}
  152. }
  153. else
  154. {
  155. ed.WriteMessage("\n多段线选择失败");
  156. }
  157. }
  158. }
  159. }