打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
AutoCAD .NET API基础(二) 访问AutoCAD 对象层次
userphoto

2012.09.09

关注

AutoCAD .NET API基础(二) 访问AutoCAD 对象层次

分类: AutoCAD .NET API开发 491人阅读 评论(0) 收藏 举报

Access the bject Hierarchy

访问 AutoCAD 对象层次

 

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 APIApplication对象是根对象,我们一般会与当前图形文档的数据库打交道。Application对象的DocumentManager属性允许我们使用它的MdiActiveDocument属性来访问当前文档。从MdiActiveDocument属性返回的Document对象,我们就可以用其Database属性访问文档的数据库了。

VB.NET

Application.DocumentManager.MdiActiveDocument.Database.Clayer

C#

Application.DocumentManager.MdiActiveDocument.Database.Clayer;

VBA/ActiveX Code Reference

ThisDrawing.ActiveLayer

Topics in this section本节主题

·         Reference Objects in the Object Hierarchy 引用对象层次中的对象

·         Access the Application Object 访问Application对象

 

 

 

1Reference 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方法:

VB.NET

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)

C#

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,并将当前数据库赋值给该变量:

VB.NET

Dim acCurDb As Autodesk.AutoCAD.DatabaseServices.Database

acCurDb = Application.DocumentManager.MdiActiveDocument.Database

C#

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自定义变量将一个图形文件附加到数据库:

VB.NET

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)

C#

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);

Retrieve entities from Model space 从模型空间检索实体对象

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:

下面示例代码返回模型空间的所有实体对象列表。类似代码还可以对图纸空间的实体做相同的事情。注意所有图形对象都是实体对象:

VB.NET

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

C#

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

  }

}

 

VBA/ActiveX Code Reference

 

 

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

 

 

2Access 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的主窗口。例如,下列代码行更新应用程序:

VB.NET

Autodesk.AutoCAD.ApplicationServices.Application.UpdateScreen()

C#

Autodesk.AutoCAD.ApplicationServices.Application.UpdateScreen();

VBA/ActiveX Code Reference

ThisDrawing.Application.Update

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
AutoCAD C#二次开发
C#操作CAD
CAD二次开发 学习笔记(1)
安装autocad2014去除AUTODESK 360方法
为什么每双击一个图纸文件就打开一个CAD窗口。
Autodesk公司CAD初级工程师认证考试题
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服