多平台统一管理软件接口,如何实现多平台统一管理软件接口
476
2022-06-26
本文主要给大家介绍一下SmartSoft中用C#.Net实现AutoCAD块属性提取的方法,并给出实例代码与大家共享。此类实现AutoCAD块属性提取功能,在VS.Net2003(2005)+AutoCAD2004(2007)下调试通过。
以下是引用片段:
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
{
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
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~