AutoCAD .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);
ユーザ定義変数を使ってオブジェクトを参照するには、変数をオブジェクトのタイプとして定義し、次に変数を適当なオブジェクトに設定します。たとえば、次のコードは、タイプ 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;
次の文は、ユーザ定義変数 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);
次の例は、モデル空間の最初の図形オブジェクトを返します。ペーパー空間の図形についても、同様のコードが同じ働きをします。すべてのグラフィカル オブジェクトは、Entity オブジェクトとして定義されます。
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