python读取cad_SmartSoft中用C#.Net实现AutoCAD块属性提取|python基础教程|python入门|python教程...
https://www.xin3721.com/eschool/pythonxin3721/
本文主要給大家介紹一下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
總結
以上是生活随笔為你收集整理的python读取cad_SmartSoft中用C#.Net实现AutoCAD块属性提取|python基础教程|python入门|python教程...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dg备库怎么逻辑导出exp/expdp?
- 下一篇: 2020知道答案C语言,C语言及逆向20