实例介绍
【实例简介】一个关于CAD文件属性读取的例子,在vs2010中测试通过。
【实例截图】
【核心代码】
using System; using AutoCAD=Autodesk.AutoCAD.Interop; using System.Runtime.InteropServices ; using dbx = Autodesk.AutoCAD.Interop.Common; namespace SmartSoft.ACAD { /// /// 读取AutoCAD属性信息 /// public class AutoCADConnector:IDisposable { private AutoCAD.AcadApplication _Application; private bool _Initialized; private bool _Disposed; #region 类初始化及析构操作 /// /// 类初始化,试图获取一个正在运行的AutoCAD实例, /// 如果没有则新起动一个实例。 /// public AutoCADConnector() { try { //取得一个正在运行的AUTOCAD实例 this._Application = (AutoCAD.AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.17"); }//end of try catch { try { //建立一个新的AUTOCAD实例,并标识已经建立成功。 _Application = new AutoCAD.AcadApplicationClass(); _Initialized=true; } catch { throw new Exception ("无法起动AutoCAD应用程序,确认已经安装"); } }//end of catch }//end of AutoCADConnector ~AutoCADConnector() { Dispose(false); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!this._Disposed && this._Initialized ) { //如果建立了AUTOCAD的实列,调用QUIT方法以避免内存漏洞 this._Application.ActiveDocument.Close (false,""); this._Application.Quit(); this._Disposed=true; } } #endregion #region 公共用户接口属性 /// /// 取得当前类所获得的AUTOCAD实例 /// public AutoCAD.AcadApplication Application { get { return _Application; } }//end of Application #endregion #region 公共用户接口方法 /// /// 根据给定的文件名以AxDbDocument类型返回该文档 /// public dbx.AxDbDocument GetThisDrawing(string FileName,string PassWord) { ACAD.AutoCADConnector Connector=new AutoCADConnector(); //这是AutoCAD2004的Programe ID string programeID ="ObjectDBX.AxDbDocument.17"; AutoCAD.AcadApplication AcadApp = Connector.Application; dbx.AxDbDocument dbxDoc; dbxDoc=(dbx.AxDbDocument)AcadApp.GetInterfaceObject(programeID); try { if (System.IO.File.Exists (FileName)==false) throw new Exception ("文件不存在。"); dbxDoc.Open(FileName,PassWord); }// end of try catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.Message ); dbxDoc=null; } return dbxDoc; }//end of function GetThisDrawing /// /// 根据当前文档和块名取得当前块的引用 /// public dbx.AcadBlockReference GetBlockReference(dbx.AxDbDocument thisDrawing,string blkName) { dbx.AcadBlockReference blkRef=null; bool found = false; try { foreach (dbx.AcadEntity entity in thisDrawing.ModelSpace ) { if (entity.EntityName=="AcDbBlockReference") { blkRef=(dbx.AcadBlockReference)entity; //System.Windows.Forms.MessageBox.Show(blkRef.Name); if (blkRef.Name.ToLower() ==blkName.ToLower() ) { found = true; break; } }//end of entity.EntityName=="AcDbBlockReference" }// end of foreach thisDrawing.ModelSpace }//end of try catch (Exception e) { System.Windows.Forms.MessageBox.Show ("图形中有未知的错误,格式不正确或图形数据库需要修愎。系统错误提示:" e.Message ,"信息",System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning); thisDrawing=null; }//end of catch if (!found) blkRef = null; return blkRef; }//end of function GetBlockReference /// /// 根据给定的块引用(dbx.AcadBlockReference)和属性名返回属性值 /// public object GetValueByAttributeName(dbx.AcadBlockReference blkRef,string AttributeName) { object[] Atts=(object[])blkRef.GetAttributes(); object attValue=null; for (int i=0;i<Atts.Length;i ) { dbx.AcadAttributeReference attRef; attRef=(dbx.AcadAttributeReference)Atts[i]; if (attRef.TagString==AttributeName) { attValue= attRef.TextString ; break; } }//end of for i return attValue; }// end of function #endregion }//end of class CAutoCADConnector }//end of namespace AutoCAD
相关软件
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)