访问AutoCAD对象
2019-03-12 本文已影响0人
using__
- Application对象的DocumentManager属性允许我们使用它的MdiActiveDocument属性来访问当前文档,从MdiActiveDocument属性返回Document对象,我们可以用Document对象的Database属性访问文档的数据库。
Autodesk.AutoCAD.DatabaseServices.Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
- 将一个图形文件附加到当前图形的数据库中
string strFName, strBlkName;
Autodesk.AutoCAD.DatabaseServices.ObjectId objId;
strFName = "c:/test.dwg";
strBlkName = System.IO.Path.GetFileNameWithoutExtension(strName);
objId = Application.DocumentManager.MdiActiveDocument.Database.AttachXref(strFName,strBlkName);
- 使用acCurDb,将一个图形文件添加到acCurDb数据库中
string strFName, strBlkName;
Autodesk.AutoCAD.DatabaseServices.ObjectId objId;
Autodesk.AutoCAD.DatabaseServices.Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
strFName = "c:/test.dwg";
strBlkName = System.IO.Path.GetFileNameWithoutExtension(strName);
objId = acCurDb.AttachXref(strFName,strBlkName);
- 返回模型空间中所有实体对象的列表
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
[CommandMethod("ListEntities")]
public static void ListEntities()
{
//获取当前数据库,启动事务
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//以读模式打开块表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,OpenMode.ForRead) as BlockTable;
//以读模式打开块表记录模型空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],OpenMode.ForRead) as BlockTableRecord;
int nCnt = 0;
acDoc.Editor.WriteMessage("\nModel space objects: ");
//遍历模型控件里的每个对象,并显示找到的对象类型
foreach(ObjectId acObjId in acBlkTblRec)
{
acDoc.Editor.WriteMessage("\n" + acObjId.ObjectClass.DxfName);
nCnt = nCnt + 1;
}
//如果没发现对象则显示提示信息
if(nCnt == 0)
{
acDoc.Editor.WriteMessage("\n No objects found");
}
//关闭事务
}
}