对象层次结构中的引用对象 (.NET)
在 AutoCAD .NET API 中处理对象时,可以直接引用某些对象,也可以根据正在处理的对象通过用户定义的变量引用某些对象。若要直接引用对象,请将该对象包含在调用层次结构中。例如,以下语句将图形文件附加到当前图形的数据库中。请注意,层次结构从应用程序开始,然后转到对象。从对象中调用该方法:DatabaseDatabaseAttachXref VB.NETDim 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); 若要通过用户定义的变量引用对象,请将变量定义为所需类型,然后将变量设置为适当的对象。例如,以下代码定义一个类型为 () 的变量,并将该变量设置为等于当前数据库:acCurDbAutodesk.AutoCAD.DatabaseServices.Database VB.NETDim acCurDb As Autodesk.AutoCAD.DatabaseServices.Database acCurDb = Application.DocumentManager.MdiActiveDocument.Database C#Autodesk.AutoCAD.DatabaseServices.Database acCurDb; acCurDb = Application.DocumentManager.MdiActiveDocument.Database; 然后,以下语句使用用户定义的变量将图形文件附加到数据库:acCurDb VB.NETDim 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); 从模型空间检索实体下面的示例返回模型空间中的第一个实体对象。类似的代码可以对 Paper 空间实体执行相同的操作。请注意,所有图形对象都定义为一个对象:Entity VB.NETImports 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 代码参考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 相关概念父主题: |
|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 苏公网安备32011402011833)
GMT+8, 2024-12-15 11:46
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.