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

Attributeget.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using GrxCAD.ApplicationServices;
  2. using GrxCAD.DatabaseServices;
  3. using GrxCAD.EditorInput;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace HCTools
  10. {
  11. class Attributeget
  12. {
  13. private static List<ObjectId> objid_all = new List<ObjectId>();
  14. public void attribt()
  15. {
  16. //存放所有图层名
  17. List<string> alllayername = new List<string>();
  18. List<List<string>> info_all = new List<List<string>>();
  19. Database db = HostApplicationServices.WorkingDatabase;
  20. Editor ed = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  21. using (Transaction trans = db.TransactionManager.StartTransaction())
  22. {
  23. LayerTable layertable = trans.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
  24. foreach (ObjectId objid in layertable)
  25. {
  26. LayerTableRecord layertablerecord = trans.GetObject(objid, OpenMode.ForRead) as LayerTableRecord;
  27. alllayername.Add(layertablerecord.Name);
  28. }
  29. alllayername.Sort();
  30. trans.Commit();
  31. }
  32. int num = 0;
  33. for (int i = 0; i < alllayername.Count; i++)
  34. {
  35. List<List<string>> info = Getlyobjinfo(alllayername[i], num);
  36. num = num + info.Count;
  37. for (int j = 0; j < info.Count; j++)
  38. {
  39. string id = info[j][0];
  40. string name = info[j][1];
  41. string type = info[j][2];
  42. string X = info[j][3];
  43. string Y = info[j][4];
  44. string Z = info[j][5];
  45. string lenth = info[j][6];
  46. string area = info[j][7];
  47. List<string> temp = new List<string>();
  48. temp.Add(id);
  49. temp.Add(name);
  50. temp.Add(alllayername[i]);
  51. temp.Add(type);
  52. temp.Add(X);
  53. temp.Add(Y);
  54. temp.Add(Z);
  55. temp.Add(lenth);
  56. temp.Add(area);
  57. info_all.Add(temp);
  58. }
  59. }
  60. AttributeForm Frm = new AttributeForm();
  61. Frm.GetInfo(info_all,objid_all);
  62. Frm.Show();
  63. }
  64. private static List<List<string>> Getlyobjinfo(string layername, int num)
  65. {
  66. List<List<string>> info = new List<List<string>>();//图层中obj信息
  67. Database database = HostApplicationServices.WorkingDatabase;
  68. using (Transaction traction = database.TransactionManager.StartTransaction())
  69. {
  70. // 获得当前文档的编辑器
  71. Editor editor = GrxCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  72. // 创建一个 TypedValue 数组,用于定义过滤条件
  73. TypedValue[] typedvalue = new TypedValue[1];
  74. typedvalue.SetValue(new TypedValue((int)DxfCode.LayerName, layername), 0);
  75. // 赋值过滤条件给 SelectionFilter 对象
  76. SelectionFilter selectionfilter = new SelectionFilter(typedvalue);
  77. // 要求在图形区域中手动选择对象
  78. PromptSelectionResult psr = editor.SelectAll(selectionfilter);
  79. if (psr.Status == PromptStatus.OK)
  80. {
  81. SelectionSet selectionset = psr.Value;
  82. ObjectId[] obj = new ObjectId[selectionset.Count];
  83. obj = selectionset.GetObjectIds();
  84. for (int i = 0; i < obj.Length; i++)
  85. {
  86. //获取实体
  87. ObjectId objid = obj[i];
  88. Entity entity = (Entity)traction.GetObject(objid, OpenMode.ForRead);
  89. objid_all.Add(objid);
  90. if (entity is Polyline)
  91. {
  92. num++;
  93. Polyline pll = (Polyline)entity;
  94. string name = "无";
  95. string type = "多段线";
  96. double X = pll.StartPoint.X;
  97. double Y = pll.StartPoint.Y;
  98. double Z = pll.Elevation;
  99. double length = pll.Length;
  100. string area;
  101. if(pll.Closed)
  102. {
  103. area = pll.Area.ToString("0.000");
  104. }
  105. else
  106. {
  107. area = "无";
  108. }
  109. List<string> temp = new List<string>();
  110. temp.Add(num.ToString());
  111. temp.Add(name);
  112. temp.Add(type);
  113. temp.Add(X.ToString("0.000"));
  114. temp.Add(Y.ToString("0.000"));
  115. temp.Add(Z.ToString("0.000"));
  116. temp.Add(length.ToString("0.000"));
  117. temp.Add(area);
  118. info.Add(temp);
  119. }
  120. if (entity is Polyline2d)
  121. {
  122. num++;
  123. Polyline2d pll = (Polyline2d)entity;
  124. string name = "无";
  125. string type = "二维多段线";
  126. double X = pll.StartPoint.X;
  127. double Y = pll.StartPoint.Y;
  128. double Z = pll.Elevation;
  129. double length = pll.Length;
  130. string area;
  131. if (pll.Closed)
  132. {
  133. area = pll.Area.ToString("0.000");
  134. }
  135. else
  136. {
  137. area = "无";
  138. }
  139. List<string> temp = new List<string>();
  140. temp.Add(num.ToString());
  141. temp.Add(name);
  142. temp.Add(type);
  143. temp.Add(X.ToString("0.000"));
  144. temp.Add(Y.ToString("0.000"));
  145. temp.Add(Z.ToString("0.000"));
  146. temp.Add(length.ToString("0.000"));
  147. temp.Add(area);
  148. info.Add(temp);
  149. }
  150. else if (entity is Line)
  151. {
  152. num++;
  153. Line line = (Line)entity;
  154. string name = "无";
  155. string type = "直线";
  156. double X = line.StartPoint.X;
  157. double Y = line.StartPoint.Y;
  158. double Z = line.StartPoint.Z;
  159. double length = line.Length;
  160. string area;
  161. if (line.Closed)
  162. {
  163. area = line.Area.ToString("0.000");
  164. }
  165. else
  166. {
  167. area = "无";
  168. }
  169. List<string> temp = new List<string>();
  170. temp.Add(num.ToString());
  171. temp.Add(name);
  172. temp.Add(type);
  173. temp.Add(X.ToString("0.000"));
  174. temp.Add(Y.ToString("0.000"));
  175. temp.Add(Z.ToString("0.000"));
  176. temp.Add(length.ToString("0.000"));
  177. temp.Add(area);
  178. info.Add(temp);
  179. }
  180. else if (entity is DBText)
  181. {
  182. num++;
  183. DBText txt = (DBText)entity;
  184. string name = txt.TextString;
  185. string type = "文字";
  186. double X = txt.Position.X;
  187. double Y = txt.Position.Y;
  188. double Z = txt.Position.Z;
  189. string length = "无";
  190. string area = "无";
  191. List<string> temp = new List<string>();
  192. temp.Add(num.ToString());
  193. temp.Add(name);
  194. temp.Add(type);
  195. temp.Add(X.ToString("0.000"));
  196. temp.Add(Y.ToString("0.000"));
  197. temp.Add(Z.ToString("0.000"));
  198. temp.Add(length);
  199. temp.Add(area);
  200. info.Add(temp);
  201. }
  202. else if (entity is BlockReference)
  203. {
  204. num++;
  205. BlockReference blk = (BlockReference)entity;
  206. string name = blk.Name;
  207. string type = "块参照";
  208. double X = blk.Position.X;
  209. double Y = blk.Position.Y;
  210. double Z = blk.Position.Z;
  211. string length = "无";
  212. string area = "无";
  213. List<string> temp = new List<string>();
  214. temp.Add(num.ToString());
  215. temp.Add(name);
  216. temp.Add(type);
  217. temp.Add(X.ToString("0.000"));
  218. temp.Add(Y.ToString("0.000"));
  219. temp.Add(Z.ToString("0.000"));
  220. temp.Add(length);
  221. temp.Add(area);
  222. info.Add(temp);
  223. }
  224. else if(entity is Hatch)
  225. {
  226. num++;
  227. Hatch hc = (Hatch)entity;
  228. string name = hc.PatternName;
  229. string type = "图案填充";
  230. double X = hc.Origin.X;
  231. double Y = hc.Origin.Y;
  232. double Z = 0;
  233. string length = "无";
  234. string area = hc.Area.ToString();
  235. List<string> temp = new List<string>();
  236. temp.Add(num.ToString());
  237. temp.Add(name);
  238. temp.Add(type);
  239. temp.Add(X.ToString("0.000"));
  240. temp.Add(Y.ToString("0.000"));
  241. temp.Add(Z.ToString("0.000"));
  242. temp.Add(length);
  243. temp.Add(area);
  244. info.Add(temp);
  245. }
  246. }
  247. }
  248. traction.Commit();
  249. }
  250. return info;
  251. }
  252. }
  253. }