Access the bject Hierarchy
While the Application is the root object in the AutoCAD .NET API, you commonly will be working with the database of the current drawing. The DocumentManager property of the Application object allows you to access the current document with the MdiActiveDocument property. From the Document object returned by the MdiActiveDocument property, you can access its database with the Database property.
AutoCAD .NET API中Application对象是根对象,我们一般会与当前图形文档的数据库打交道。Application对象的DocumentManager属性允许我们使用它的MdiActiveDocument属性来访问当前文档。从MdiActiveDocument属性返回的Document对象,我们就可以用其Database属性访问文档的数据库了。
Application.DocumentManager.MdiActiveDocument.Database.Clayer
Application.DocumentManager.MdiActiveDocument.Database.Clayer;
ThisDrawing.ActiveLayer
Topics in this section本节主题
· Reference Objects in the Object Hierarchy 引用对象层次中的对象
· Access the Application Object 访问Application对象
1、Reference Objects in the Object Hierarchy引用对象层次中的对象
When you work with objects in the .NET API, you can reference some objects directly or through a user-defined variable based on the object you are working with. To reference an objects directly, include the object in the calling hierarchy. For example, the following statement attaches a drawing file to the database of the current drawing. Notice that the hierarchy starts with the Application and then goes to the Database object. From the Database object, the AttachXref method is called:
我们使用.NET API中的对象时,可以直接引用某些对象,或者通过基于所用对象的用户自定义变量引用该对象。要直接引用一个对象,只要在调用层次中包含该对象即可。例如,下列代码将一个图形文件附加到当前图形的数据库中。注意其调用层次是从Application对象开始一直到Database对象,最后在Database对象上调用AttachXref方法:
Dim strFName As String, strBlkName As String
Dim objId As Autodesk.AutoCAD.DatabaseServices.ObjectId
strFName = "c:/clients/Proj 123/grid.dwg"
strBlkName = System.IO.Path.GetFileNameWithoutExtension(strFName)
objId = Application.DocumentManager.MdiActiveDocument.Database.AttachXref(strFName, strBlkName)
string strFName, strBlkName;
Autodesk.AutoCAD.DatabaseServices.ObjectId objId;
strFName = "c:/clients/Proj 123/grid.dwg";
strBlkName = System.IO.Path.GetFileNameWithoutExtension(strFName);
objId = Application.DocumentManager.MdiActiveDocument.Database.AttachXref(strFName, strBlkName);
To reference the objects through a user-defined variable, define the variable as the desired type, then set the variable to the appropriate object. For example, the following code defines a variable (acCurDb) of type Autodesk.AutoCAD.DatabaseServices.Database and sets the variable equal to the current database:
要通过用户自定义变量引用对象,首先定义一个所需类型的变量,然后将相应对象赋值给该变量。例如下列代码定义了一个Autodesk.AutoCAD.DatabaseServices.Database类型的变量acCurDb,并将当前数据库赋值给该变量:
Dim acCurDb As Autodesk.AutoCAD.DatabaseServices.Database
acCurDb = Application.DocumentManager.MdiActiveDocument.Database
Autodesk.AutoCAD.DatabaseServices.Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
The following statement then attaches a drawing file to the database using the acCurDb user-defined variable:
下面示例代码就是使用acCurDb自定义变量将一个图形文件附加到数据库:
Dim strFName As String, strBlkName As String
Dim objId As Autodesk.AutoCAD.DatabaseServices.ObjectId
strFName = "c:/clients/Proj 123/grid.dwg"
strBlkName = System.IO.Path.GetFileNameWithoutExtension(strFName)
objId = acCurDb.AttachXref(strFName, strBlkName)
string strFName, strBlkName;
Autodesk.AutoCAD.DatabaseServices.ObjectId objId;
strFName = "c:/clients/Proj 123/grid.dwg";
strBlkName = System.IO.Path.GetFileNameWithoutExtension(strFName);
objId = acCurDb.AttachXref(strFName, strBlkName);
The following example returns the list of entity objects in Model space. Similar code can do the same for Paper space entities. Note that all graphical objects are defined as an Entity object:
下面示例代码返回模型空间的所有实体对象列表。类似代码还可以对图纸空间的实体做相同的事情。注意所有图形对象都是实体对象:
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
<CommandMethod("ListEntities")> _
Public Sub ListEntities()
'' Get the current document and database, and start a transaction
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
'' Open the Block table record for read
Dim acBlkTbl As BlockTable
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, _
OpenMode.ForRead)
'' Open the Block table record Model space for read
Dim acBlkTblRec As BlockTableRecord
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
OpenMode.ForRead)
Dim nCnt As Integer = 0
acDoc.Editor.WriteMessage(vbLf & "Model space objects: ")
'' Step through each object in Model space and
'' display the type of object found
For Each acObjId As ObjectId In acBlkTblRec
acDoc.Editor.WriteMessage(vbLf & acObjId.ObjectClass().DxfName)
nCnt = nCnt + 1
Next
'' If no objects are found then display the following message
If nCnt = 0 Then
acDoc.Editor.WriteMessage(vbLf & " No objects found")
End If
'' Dispose of the transaction
End Using
End Sub
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
[CommandMethod("ListEntities")]
public static void ListEntities()
{
// Get the current document and database, and start a transaction
// 获取当前文档和数据库,并启动一个事务;
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table record for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for read
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForRead) as BlockTableRecord;
int nCnt = 0;
acDoc.Editor.WriteMessage("/nModel space objects: ");
// Step through each object in Model space and
// display the type of object found
foreach (ObjectId acObjId in acBlkTblRec)
{
acDoc.Editor.WriteMessage("/n" + acObjId.ObjectClass.DxfName);
nCnt = nCnt + 1;
}
// If no objects are found then display a message
if (nCnt == 0)
{
acDoc.Editor.WriteMessage("/n No objects found");
}
// Dispose of the transaction
}
}
Sub ListEntities()
' This example returns the first entity in model space
' 本例返回模型空间里的第一个实体;
On Error Resume Next
Dim entity As AcadEntity
If ThisDrawing.ModelSpace.count <> 0 Then
Set entity = ThisDrawing.ModelSpace.Item(0)
MsgBox entity.ObjectName + _
" is the first entity in model space."
Else
MsgBox "There are no objects in model space."
End If
End Sub
2、Access the Application Object访问Application对象
The Application object is at the root of the object hierarchy and it provides access to the main window of AutoCAD. For example, the following line of code updates the application:
Application对象位于对象层次结构的根部,它用来访问AutoCAD的主窗口。例如,下列代码行更新应用程序:
Autodesk.AutoCAD.ApplicationServices.Application.UpdateScreen()
Autodesk.AutoCAD.ApplicationServices.Application.UpdateScreen();
VBA/ActiveX Code Reference
ThisDrawing.Application.Update