确定父对象
 
 

Graphical objects are appended to a BlockTableRecord object, such as Model or Paper space. You reference the blocks that represent Model and Paper space through the BlockTable object. If you want to work in the current space instead of a specific space, you get the ObjectId for the current space from the current database with the CurrentSpaceId property.

图形对象被添加到块表记录(BlockTableRecord)对象中,像模型或图纸空间。用户通过块表对象引用代表模型和图纸空间的块。如果想在当前空间代替在指定空间中工作,可以从当前数据库的 CurrentSpaceId 属性获得当前空间的 ObjectId。

The ObjectId for the block table records of Model and Paper space can be retrieved from the BlockTable object using a property or the GetBlockModelSpaceId and GetBlockPaperSpaceId methods of the SymbolUtilityServices class under the DatabaseServices namespace.

模型或图纸空间的块表记录的 ObjectID 可以使用 DatabaseServices 命名空间下的 SymbolUtilityServices 类的属性或 GetBlockModelSpaceIdGetBlockPaperSpaceId 方法从块表对象中获得。

访问模型空间、图纸空间或当前空间

The following example demonstrates how to access the block table records associated with Model space, Paper space or the current space. Once the block table record is referenced, a new line is added to the block table record.

下列示例演示如何访问与模型空间、图纸空间或当前空间相关联的块表记录。一旦块表记录被引用,一个新的直线将被添加到块表记录中。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
 
<CommandMethod("AccessSpace")> _
Public Sub AccessSpace()
  '' 获得当前文档和数据库   Get the current document and database
  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  Dim acCurDb As Database = acDoc.Database
 
  ''启动一个事务   Start a transaction
  Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
 
      '' 以只读方式打开块表   Open the Block table for read
      Dim acBlkTbl As BlockTable
      acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
 
      '' 以只读方式打开块表记录   Open the Block table record for read
      Dim acBlkTblRec As BlockTableRecord
 
      '' 请求打开哪一个表记录  Request which table record to open
      Dim pKeyOpts As PromptKeywordOptions = New PromptKeywordOptions("")
      pKeyOpts.Message = vbLf & "Enter which space to create the line in "
      pKeyOpts.Keywords.Add("Model")
      pKeyOpts.Keywords.Add("Paper")
      pKeyOpts.Keywords.Add("Current")
      pKeyOpts.AllowNone = False
      pKeyOpts.AppendKeywordsToMessage = True
 
      Dim pKeyRes As PromptResult = acDoc.Editor.GetKeywords(pKeyOpts)
 
      If pKeyRes.StringResult = "Model" Then
          '' 从块表获得模型空间的 ObjectID    Get the ObjectID for Model space from the Block table
          acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
                                          OpenMode.ForWrite)
      ElseIf pKeyRes.StringResult = "Paper" Then
          '' 从块表记录获得图纸空间的ObjectID   Get the ObjectID for Paper space from the Block table
          acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.PaperSpace), _
                                          OpenMode.ForWrite)
      Else
          '' 从数据库中获得当前空间的ObjectID    Get the ObjectID for the current space from the database
          acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, _
                                          OpenMode.ForWrite)
      End If
 
      '' 创建一条起点为2,5 终点为 10,7 的直线    Create a line that starts at 2,5 and ends at 10,7
      Dim acLine As Line = New Line(New Point3d(2, 5, 0), _
                                    New Point3d(10, 7, 0))
 
      acLine.SetDatabaseDefaults()
 
      '' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
      acBlkTblRec.AppendEntity(acLine)
      acTrans.AddNewlyCreatedDBObject(acLine, True)
 
      '' 保存新的直线到数据库中    Save the new line to the database
      acTrans.Commit()
  End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
 
[CommandMethod("AccessSpace")]
public static void AccessSpace()
{
  // 获得当前文档和数据库   Get the current document and database
  Document acDoc = Application.DocumentManager.MdiActiveDocument;
  Database acCurDb = acDoc.Database;
 
  // 启动一个事务  Start a transaction
  using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  {
      // 以只读方式打开块表   Open the Block table for read
      BlockTable acBlkTbl;
      acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                   OpenMode.ForRead) as BlockTable;
 
      // 以只读方式打开块表记录   Open the Block table record for read
      BlockTableRecord acBlkTblRec;
 
      // 请求打开哪一个表记录  Request which table record to open
      PromptKeywordOptions pKeyOpts = new PromptKeywordOptions("");
      pKeyOpts.Message = "\nEnter which space to create the line in ";
      pKeyOpts.Keywords.Add("Model");
      pKeyOpts.Keywords.Add("Paper");
      pKeyOpts.Keywords.Add("Current");
      pKeyOpts.AllowNone = false;
      pKeyOpts.AppendKeywordsToMessage = true;
 
      PromptResult pKeyRes = acDoc.Editor.GetKeywords(pKeyOpts);
 
      if (pKeyRes.StringResult == "Model")
      {
          // 从块表获得模型空间的 ObjectID    Get the ObjectID for Model space from the Block table
          acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                          OpenMode.ForWrite) as BlockTableRecord;
      }
      else if (pKeyRes.StringResult == "Paper")
      {
          // 从块表记录获得图纸空间的ObjectID   Get the ObjectID for Paper space from the Block table
          acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.PaperSpace],
                                          OpenMode.ForWrite) as BlockTableRecord;
      }
      else
      {
          // 从数据库中获得当前空间的ObjectID    Get the ObjectID for the current space from the database
          acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId,
                                          OpenMode.ForWrite) as BlockTableRecord;
      }
 
      // 创建一条起点为2,5 终点为 10,7 的直线    Create a line that starts at 2,5 and ends at 10,7
      Line acLine = new Line(new Point3d(2, 5, 0),
                             new Point3d(10, 7, 0));
 
      acLine.SetDatabaseDefaults();
 
      // 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
      acBlkTblRec.AppendEntity(acLine);
      acTrans.AddNewlyCreatedDBObject(acLine, true);
 
      // 保存新的直线到数据库中    Save the new line to the database
      acTrans.Commit();
  }
}
VBA/ActiveX 代码参考