Reference Objects in the Object Hierarchy (.NET)

When you work with objects in the AutoCAD .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:

C# Example

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:

C# Example

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:

C# Example

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 first entity object in Model space. Similar code can do the same for Paper space entities. Note that all graphical objects are defined as an Entity object:

C# Example

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
  }
}