创建面域
 
 

Regions are added to a BlockTableRecord object by creating an instance of a Region object and then appending it to a BlockTableRecord. Before you can add it to a BlockTableRecord object, a region needs to be calculated based on the objects that form the closed loop. The CreateFromCurves function creates a region out of every closed loop formed by the input array of objects. The CreateFromCurves method returns and requires a DBObjectCollection object.

面域是被添加到块表记录对象中的,它是通过创建一个面域的实例然后把它追加到块表记录的。在将面域添加到块表记录中前,需要根据对象创建一个封闭环。CreateFromCurves 函数通过输入的对象数组组成的每一个封闭环创建面域。CreateFromCurves 方法返回和需要一个 DBObjectCollection 对象。

AutoCAD converts closed 2D and planar 3D polylines to separate regions, then converts polylines, lines, and curves that form closed planar loops. If more than two curves share an endpoint, the resulting region might be arbitrary. Because of this, several regions may actually be created with the CreateFromCurves method. You need to append each region created to a BlockTableRecord object.

AutoCAD 将闭合的二维和平面三维多段线转换为独立的面域,然后转换形成闭合平面环的多段线、直线和曲线。如果有两条以上的曲线共用一个端点,得到的面域可能是不确定的。所以,在使用 CreateFromCurves 方法时,实际可能会创建多个面域。用户需要追加每一个创建的面域到块表记录对象中去。

创建一个简单的面域

The following example creates a region from a single circle.

本例创建从一个简单的圆中创建一个面域。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("AddRegion")> _
Public Sub AddRegion()
  '' 获得当前文档和数据库   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 Model space for write
      Dim acBlkTblRec As BlockTableRecord
      acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
                                      OpenMode.ForWrite)
 
      '' 在内存中创建圆  Create an in memory circle
      Using acCirc As Circle = New Circle()
          acCirc.SetDatabaseDefaults()
          acCirc.Center = New Point3d(2, 2, 0)
          acCirc.Radius = 5
 
          '' 添加圆到对象数组中   Adds the circle to an object array
          Dim acDBObjColl As DBObjectCollection = New DBObjectCollection()
          acDBObjColl.Add(acCirc)
 
          '' 根据每一个闭合环计算面域   Calculate the regions based on each closed loop
          Dim myRegionColl As DBObjectCollection = New DBObjectCollection()
          myRegionColl = Region.CreateFromCurves(acDBObjColl)
          Dim acRegion As Region = myRegionColl(0)
 
          '' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
          acBlkTblRec.AppendEntity(acRegion)
          acTrans.AddNewlyCreatedDBObject(acRegion, True)
 
          '' Dispose of the in memory object not appended to the database
      End Using
 
      '' 保存新对象到数据库中   Save the new object 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;
 
[CommandMethod("AddRegion")]
public static void AddRegion()
{
  // 获得当前文档和数据库   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 Model space for write
      BlockTableRecord acBlkTblRec;
      acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                      OpenMode.ForWrite) as BlockTableRecord;
 
      // 在内存中创建圆  Create an in memory circle
      using (Circle acCirc = new Circle())
      {
          acCirc.SetDatabaseDefaults();
          acCirc.Center = new Point3d(2, 2, 0);
          acCirc.Radius = 5;
 
          // 添加圆到对象数组中   Adds the circle to an object array
          DBObjectCollection acDBObjColl = new DBObjectCollection();
          acDBObjColl.Add(acCirc);
 
          // 根据每一个闭合环计算面域   Calculate the regions based on each closed loop
          DBObjectCollection myRegionColl = new DBObjectCollection();
          myRegionColl = Region.CreateFromCurves(acDBObjColl);
          Region acRegion = myRegionColl[0] as Region;
 
          // 添加新对象到块表记录和事务中   Add the new object to the block table record and the transaction
          acBlkTblRec.AppendEntity(acRegion);
          acTrans.AddNewlyCreatedDBObject(acRegion, true);
 
          // Dispose of the in memory circle not appended to the database
      }
 
      // 保存新对象到数据库中   Save the new object to the database
      acTrans.Commit();
  }
}
VBA/ActiveX 代码参考