AutoCADConnector.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. foreach (dbx.AcadEntity entity in thisDrawing.ModelSpace)
  169. {
  170. if (entity is dbx.AcadBlockReference blockReference)
  171. {
  172. var cadObject = new TestCad.Model.BlockReference();
  173. cadObject.Handle = entity.Handle;
  174. cadObject.Layer = entity.Layer;
  175. cadObject.Rotation = blockReference.Rotation;
  176. cadObject.Position = ExportUtils.Point2XYZ(blockReference.InsertionPoint);
  177. mbiDoc.AddObject(cadObject);
  178. }
  179. else if (entity is dbx.AcadLine line)
  180. {
  181. var cadObject = new TestCad.Model.Line();
  182. cadObject.Handle = entity.Handle;
  183. cadObject.Layer = entity.Layer;
  184. cadObject.Start = ExportUtils.Point2XYZ(line.StartPoint);
  185. cadObject.End = ExportUtils.Point2XYZ(line.EndPoint);
  186. mbiDoc.AddObject(cadObject);
  187. }
  188. else
  189. {
  190. if (entity is dbx.AcadLWPolyline polyline)
  191. {
  192. var tt = thisDrawing.Database.ObjectIdToObject(polyline.ObjectID);
  193. var cadObject = ExportUtils.Coordinates2PloyLine(polyline.Coordinates);
  194. cadObject.Handle = entity?.Handle;
  195. cadObject.Layer = entity.Layer;
  196. mbiDoc.AddObject(cadObject);
  197. }
  198. else if (entity is dbx.AcadCircle circle)
  199. {
  200. var cadObject = new TestCad.Model.Circle();
  201. cadObject.Handle = entity.Handle;
  202. cadObject.Layer = entity.Layer;
  203. cadObject.Center = ExportUtils.Point2XYZ(circle.Center);
  204. cadObject.Radius = circle.Radius;
  205. mbiDoc.AddObject(cadObject);
  206. }
  207. else if (entity is dbx.AcadText text)
  208. {
  209. var cadObject = new TestCad.Model.DBText();
  210. cadObject.Handle = entity.Handle;
  211. cadObject.Layer = entity.Layer;
  212. cadObject.Position = ExportUtils.Point2XYZ(text.InsertionPoint);
  213. cadObject.TextString = text.TextString;
  214. mbiDoc.AddObject(cadObject);
  215. }
  216. else if (entity is dbx.AcadHatch hatch)
  217. {
  218. }
  219. else if (entity is dbx.AcadMText mtext)
  220. {
  221. var cadObject = new TestCad.Model.DBText();
  222. cadObject.Handle = entity.Handle;
  223. cadObject.Layer = entity.Layer;
  224. cadObject.Position = ExportUtils.Point2XYZ(mtext.InsertionPoint);
  225. cadObject.TextString = mtext.TextString;
  226. mbiDoc.AddObject(cadObject);
  227. }
  228. else if (entity is dbx.AcadSolid solid)
  229. {
  230. }
  231. else if (entity is dbx.AcadPoint point)
  232. {
  233. }
  234. else if (entity is dbx.AcadArc arc)
  235. {
  236. var cadObject = new TestCad.Model.Arc();
  237. cadObject.Handle = entity.Handle;
  238. cadObject.Layer = entity.Layer;
  239. cadObject.Center = ExportUtils.Point2XYZ(arc.Center);
  240. cadObject.Radius = arc.Radius;
  241. cadObject.StartPoint = ExportUtils.Point2XYZ(arc.StartPoint);
  242. cadObject.EndPoint = ExportUtils.Point2XYZ(arc.EndPoint);
  243. cadObject.Normal = ExportUtils.Point2XYZ(arc.Normal);
  244. mbiDoc.AddObject(cadObject);
  245. }
  246. else if (entity is dbx.AcadAttribute attribute)
  247. {
  248. }
  249. else
  250. {
  251. var layer = entity.Layer;
  252. if (layer == "PUB_DIM")
  253. { }
  254. else
  255. {
  256. var tt = entity;
  257. }
  258. }
  259. }
  260. }// end of foreach thisDrawing.ModelSpace
  261. mbiDoc.GroupElements();
  262. string dataStr = JsonConvert.SerializeObject(mbiDoc);
  263. string fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
  264. string path = Path.Combine($"{thisDrawing.Name}_{fileName}.json");
  265. File.AppendAllText(path, dataStr);
  266. MessageBox.Show("导出成功");
  267. //thisDrawing.SaveAs("D:\\ww.dwg");
  268. //thisDrawing.DxfOut("D:\\dxf.dxf", Type.Missing, Type.Missing);
  269. }//end of try
  270. catch (Exception e)
  271. {
  272. System.Windows.Forms.MessageBox.Show("图形中有未知的错误,格式不正确或图形数据库需要修愎。系统错误提示:" + e.Message, "信息", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
  273. thisDrawing = null;
  274. }//end of catch
  275. }//end of function GetBlockReference
  276. #endregion
  277. }//end of class CAutoCADConnector
  278. }//end of namespace AutoCAD