AutoCADConnector.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using AutoCAD = Autodesk.AutoCAD.Interop;
  6. using System.Runtime.InteropServices;
  7. using System.Windows.Forms;
  8. using Autodesk.AutoCAD.DatabaseServices;
  9. using Newtonsoft.Json;
  10. using TestCad;
  11. using dbx = Autodesk.AutoCAD.Interop.Common;
  12. namespace SmartSoft.ACAD
  13. {
  14. ///
  15. /// 读取AutoCAD属性信息
  16. ///
  17. public class AutoCADConnector : IDisposable
  18. {
  19. private AutoCAD.AcadApplication _Application;
  20. private bool _Initialized;
  21. private bool _Disposed;
  22. private dbx.AxDbDocument doc_as;
  23. #region 类初始化及析构操作
  24. ///
  25. /// 类初始化,试图获取一个正在运行的AutoCAD实例,
  26. /// 如果没有则新起动一个实例。
  27. ///
  28. public AutoCADConnector()
  29. {
  30. try
  31. {
  32. //取得一个正在运行的AUTOCAD实例
  33. this._Application = (AutoCAD.AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.22");
  34. }//end of try
  35. catch
  36. {
  37. try
  38. {
  39. //建立一个新的AUTOCAD实例,并标识已经建立成功。
  40. _Application = new AutoCAD.AcadApplication();
  41. _Initialized = true;
  42. }
  43. catch
  44. {
  45. throw new Exception("无法起动AutoCAD应用程序,确认已经安装");
  46. }
  47. }//end of catch
  48. }//end of AutoCADConnector
  49. ~AutoCADConnector()
  50. {
  51. Dispose(false);
  52. }
  53. public void Dispose()
  54. {
  55. Dispose(true);
  56. GC.SuppressFinalize(this);
  57. }
  58. protected virtual void Dispose(bool disposing)
  59. {
  60. if (!this._Disposed && this._Initialized)
  61. {
  62. //如果建立了AUTOCAD的实列,调用QUIT方法以避免内存漏洞
  63. this._Application.ActiveDocument.Close(false, "");
  64. this._Application.Quit();
  65. this._Disposed = true;
  66. }
  67. }
  68. #endregion
  69. #region 公共用户接口属性
  70. ///
  71. /// 取得当前类所获得的AUTOCAD实例
  72. ///
  73. public AutoCAD.AcadApplication Application
  74. {
  75. get
  76. {
  77. return _Application;
  78. }
  79. }//end of Application
  80. #endregion
  81. #region 公共用户接口方法
  82. ///
  83. /// 根据给定的文件名以AxDbDocument类型返回该文档
  84. ///
  85. public dbx.AxDbDocument GetThisDrawing(string FileName, string PassWord)
  86. {
  87. //这是AutoCAD2004的Programe ID
  88. string programeID = "ObjectDBX.AxDbDocument.22";
  89. AutoCAD.AcadApplication AcadApp = this.Application;
  90. dbx.AxDbDocument dbxDoc;
  91. dbxDoc = (dbx.AxDbDocument)AcadApp.GetInterfaceObject(programeID);
  92. try
  93. {
  94. if (System.IO.File.Exists(FileName) == false) throw new Exception("文件不存在。");
  95. dbxDoc.Open(FileName, PassWord);
  96. }// end of try
  97. catch (Exception e)
  98. {
  99. System.Windows.Forms.MessageBox.Show(e.Message);
  100. dbxDoc = null;
  101. }
  102. return dbxDoc;
  103. }//end of function GetThisDrawing
  104. ///
  105. /// 根据当前文档和块名取得当前块的引用
  106. ///
  107. public dbx.AcadBlockReference GetBlockReference(dbx.AxDbDocument thisDrawing, string blkName)
  108. {
  109. dbx.AcadBlockReference blkRef = null;
  110. bool found = false;
  111. string str = "";
  112. try
  113. {
  114. foreach (dbx.AcadEntity entity in thisDrawing.ModelSpace)
  115. {
  116. str += entity.EntityName + ",";
  117. if (entity.EntityName == "AcDbBlockReference")
  118. {
  119. blkRef = (dbx.AcadBlockReference)entity;
  120. //System.Windows.Forms.MessageBox.Show(blkRef.Name);
  121. if (blkRef.Name.ToLower() == blkName.ToLower())
  122. {
  123. found = true;
  124. break;
  125. }
  126. }//end of entity.EntityName=="AcDbBlockReference"
  127. }// end of foreach thisDrawing.ModelSpace
  128. }//end of try
  129. catch (Exception e)
  130. {
  131. System.Windows.Forms.MessageBox.Show("图形中有未知的错误,格式不正确或图形数据库需要修愎。系统错误提示:" + e.Message, "信息", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
  132. thisDrawing = null;
  133. }//end of catch
  134. if (!found) blkRef = null;
  135. return blkRef;
  136. }//end of function GetBlockReference
  137. ///
  138. /// 根据给定的块引用(dbx.AcadBlockReference)和属性名返回属性值
  139. ///
  140. public object GetValueByAttributeName(dbx.AcadBlockReference blkRef, string AttributeName)
  141. {
  142. object[] Atts = (object[])blkRef.GetAttributes();
  143. object attValue = null;
  144. for (int i = 0; i < Atts.Length; i++)
  145. {
  146. dbx.AcadAttributeReference attRef;
  147. attRef = (dbx.AcadAttributeReference)Atts[i];
  148. if (attRef.TagString == AttributeName)
  149. {
  150. attValue = attRef.TextString;
  151. break;
  152. }
  153. }//end of for i
  154. return attValue;
  155. }// end of function
  156. //===
  157. ///
  158. /// 根据当前文档和实体名取得当前块的引用
  159. ///
  160. public void GetEntityReference(dbx.AxDbDocument thisDrawing)
  161. {
  162. dbx.AcadEntity entRef = null;
  163. bool found = false;
  164. string str = "", str2 = "";
  165. try
  166. {
  167. var mbiDoc = TestCad.Model.Document.CreateDocument();
  168. #region BlockTable
  169. foreach (dbx.AcadEntity entity in thisDrawing.ModelSpace)
  170. {
  171. if (entity is dbx.AcadBlockReference blockReference)
  172. {
  173. var cadObject = new TestCad.Model.BlockReference();
  174. cadObject.Handle = entity.Handle;
  175. cadObject.Layer = entity.Layer;
  176. cadObject.Rotation = blockReference.Rotation;
  177. cadObject.Position = ExportUtils.Point2XYZ(blockReference.InsertionPoint);
  178. mbiDoc.AddObject(cadObject);
  179. }
  180. else if (entity is dbx.AcadLine line)
  181. {
  182. var cadObject = new TestCad.Model.Line();
  183. cadObject.Handle = entity.Handle;
  184. cadObject.Layer = entity.Layer;
  185. cadObject.Start = ExportUtils.Point2XYZ(line.StartPoint);
  186. cadObject.End = ExportUtils.Point2XYZ(line.EndPoint);
  187. mbiDoc.AddObject(cadObject);
  188. }
  189. else
  190. {
  191. if (entity is dbx.AcadLWPolyline polyline)
  192. {
  193. var tt = thisDrawing.Database.ObjectIdToObject(polyline.ObjectID);
  194. var cadObject = ExportUtils.Coordinates2PloyLine(polyline.Coordinates);
  195. cadObject.Handle = entity?.Handle;
  196. cadObject.Layer = entity.Layer;
  197. mbiDoc.AddObject(cadObject);
  198. }
  199. else if (entity is dbx.AcadCircle circle)
  200. {
  201. var cadObject = new TestCad.Model.Circle();
  202. cadObject.Handle = entity.Handle;
  203. cadObject.Layer = entity.Layer;
  204. cadObject.Center = ExportUtils.Point2XYZ(circle.Center);
  205. cadObject.Radius = circle.Radius;
  206. mbiDoc.AddObject(cadObject);
  207. }
  208. else if (entity is dbx.AcadText text)
  209. {
  210. var cadObject = new TestCad.Model.DBText();
  211. cadObject.Handle = entity.Handle;
  212. cadObject.Layer = entity.Layer;
  213. cadObject.Position = ExportUtils.Point2XYZ(text.InsertionPoint);
  214. cadObject.TextString = text.TextString;
  215. mbiDoc.AddObject(cadObject);
  216. }
  217. else if (entity is dbx.AcadHatch hatch)
  218. {
  219. }
  220. else if (entity is dbx.AcadMText mtext)
  221. {
  222. var cadObject = new TestCad.Model.DBText();
  223. cadObject.Handle = entity.Handle;
  224. cadObject.Layer = entity.Layer;
  225. cadObject.Position = ExportUtils.Point2XYZ(mtext.InsertionPoint);
  226. cadObject.TextString = mtext.TextString;
  227. mbiDoc.AddObject(cadObject);
  228. }
  229. else if (entity is dbx.AcadSolid solid)
  230. {
  231. }
  232. else if (entity is dbx.AcadPoint point)
  233. {
  234. }
  235. else if (entity is dbx.AcadArc arc)
  236. {
  237. var cadObject = new TestCad.Model.Arc();
  238. cadObject.Handle = entity.Handle;
  239. cadObject.Layer = entity.Layer;
  240. cadObject.Center = ExportUtils.Point2XYZ(arc.Center);
  241. cadObject.Radius = arc.Radius;
  242. cadObject.StartPoint = ExportUtils.Point2XYZ(arc.StartPoint);
  243. cadObject.EndPoint = ExportUtils.Point2XYZ(arc.EndPoint);
  244. cadObject.Normal = ExportUtils.Point2XYZ(arc.Normal);
  245. mbiDoc.AddObject(cadObject);
  246. }
  247. else if (entity is dbx.AcadAttribute attribute)
  248. {
  249. }
  250. else
  251. {
  252. var layer = entity.Layer;
  253. if (layer == "PUB_DIM")
  254. { }
  255. else
  256. {
  257. var tt = entity;
  258. }
  259. }
  260. }
  261. }// end of foreach thisDrawing.ModelSpace
  262. #endregion
  263. #region LayerTable
  264. foreach (var entity in thisDrawing.Layers)
  265. {
  266. if (entity is dbx.AcadLayer layer)
  267. {
  268. var cadObject = new TestCad.Model.Layer();
  269. cadObject.Handle = layer.Handle;
  270. cadObject.Layer = layer.Name;
  271. cadObject.IsOff = layer.LayerOn;
  272. mbiDoc.AddObject(cadObject);
  273. }
  274. }
  275. #endregion
  276. mbiDoc.GroupElements();
  277. string dataStr = JsonConvert.SerializeObject(mbiDoc);
  278. string fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
  279. string path = Path.Combine($"{thisDrawing.Name}_{fileName}.json");
  280. File.AppendAllText(path, dataStr);
  281. MessageBox.Show("导出成功");
  282. //thisDrawing.SaveAs("D:\\ww.dwg");
  283. //thisDrawing.DxfOut("D:\\dxf.dxf", Type.Missing, Type.Missing);
  284. }//end of try
  285. catch (Exception e)
  286. {
  287. System.Windows.Forms.MessageBox.Show("图形中有未知的错误,格式不正确或图形数据库需要修愎。系统错误提示:" + e.Message, "信息", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
  288. thisDrawing = null;
  289. }//end of catch
  290. }//end of function GetBlockReference
  291. #endregion
  292. }//end of class CAutoCADConnector
  293. }//end of namespace AutoCAD