本文主要给大家介绍一下SmartSoft中用C#.Net实现AutoCAD块属性提取的方法,并给出实例代码与大家共享。此类实现AutoCAD块属性提取功能,在VS.Net2003(2005)+AutoCAD2004(2007)下调试通过。
以下是引用片段:
usingSystem;
usingAutoCAD=Autodesk.AutoCAD.Interop;
usingSystem.Runtime.InteropServices;
usingdbx=Autodesk.AutoCAD.Interop.Common;
namespaceSmartSoft.ACAD
{
///
///读取AutoCAD属性信息
///
publicclassAutoCADConnector:IDisposable
{
privateAutoCAD.AcadApplication_Application;
privatebool_Initialized;
privatebool_Disposed;
#region类初始化及析构操作
///
///类初始化,试图获取一个正在运行的AutoCAD实例,
///如果没有则新起动一个实例。
///
publicAutoCADConnector()
{
try
{
//取得一个正在运行的AUTOCAD实例
this._Application=(AutoCAD.AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.17");
}//endoftry
catch
{
try
{
//建立一个新的AUTOCAD实例,并标识已经建立成功。
_Application=newAutoCAD.AcadApplicationClass();
_Initialized=true;
}
catch
{
thrownewException("无法起动AutoCAD应用程序,确认已经安装");
}
}//endofcatch
}//endofAutoCADConnector
~AutoCADConnector()
{
Dispose(false);
}
publicvoidDispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protectedvirtualvoidDispose(booldisposing)
{
if(!this._Disposed&&this._Initialized)
{
//如果建立了AUTOCAD的实列,调用QUIT方法以避免内存漏洞
this._Application.ActiveDocument.Close(false,"");
this._Application.Quit();
this._Disposed=true;
}
}
#endregion
#region公共用户接口属性
///
///取得当前类所获得的AUTOCAD实例
///
publicAutoCAD.AcadApplicationApplication
{
get
{
return_Application;
}
}//endofApplication
#endregion
#region公共用户接口方法
///
///根据给定的文件名以AxDbDocument类型返回该文档
///
publicdbx.AxDbDocumentGetThisDrawing(stringFileName,stringPassWord)
{
ACAD.AutoCADConnectorConnector=newAutoCADConnector();
//这是AutoCAD2004的ProgrameID
stringprogrameID="ObjectDBX.AxDbDocument.17";
AutoCAD.AcadApplicationAcadApp=Connector.Application;
dbx.AxDbDocumentdbxDoc;
dbxDoc=(dbx.AxDbDocument)AcadApp.GetInterfaceObject(programeID);
try
{
if(System.IO.File.Exists(FileName)==false)thrownewException("文件不存在。");
dbxDoc.Open(FileName,PassWord);
}//endoftry
catch(Exceptione)
{
System.Windows.Forms.MessageBox.Show(e.Message);
dbxDoc=null;
}
returndbxDoc;
}//endoffunctionGetThisDrawing
///
///根据当前文档和块名取得当前块的引用
///
publicdbx.AcadBlockReferenceGetBlockReference(dbx.AxDbDocumentthisDrawing,stringblkName)
{
dbx.AcadBlockReferenceblkRef=null;
boolfound=false;
try
{
foreach(dbx.AcadEntityentityinthisDrawing.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;
}
}//endofentity.EntityName=="AcDbBlockReference"
}//endofforeachthisDrawing.ModelSpace
}//endoftry
catch(Exceptione)
{
System.Windows.Forms.MessageBox.Show("图形中有未知的错误,格式不正确或图形数据库需要修愎。系统错误提示:"+e.Message,"信息",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Warning);
thisDrawing=null;
}//endofcatch
if(!found)blkRef=null;
returnblkRef;
}//endoffunctionGetBlockReference
///
///根据给定的块引用(dbx.AcadBlockReference)和属性名返回属性值
///
publicobjectGetValueByAttributeName(dbx.AcadBlockReferenceblkRef,stringAttributeName)
{
object[]Atts=(object[])blkRef.GetAttributes();
objectattValue=null;
for(inti=0;i
{
dbx.AcadAttributeReferenceattRef;
attRef=(dbx.AcadAttributeReference)Atts[i];
if(attRef.TagString==AttributeName)
{
attValue=attRef.TextString;
break;
}
}//endoffori
returnattValue;
}//endoffunction
#endregion
}//endofclassCAutoCADConnector
}//endofnamespaceAutoCAD
SmartSoft中用C#.Net实现AutoCAD块属性提取
最新推荐文章于 2024-02-04 15:08:18 发布