03-02 创建和编辑AutoCAD实体(二) 创建对象(2)

来源:互联网 发布:网络主播实体培训班 编辑:程序博客网 时间:2024/05/14 21:19

4、Create Point Objects创建点对象

Point objects can be useful, for example,as node or reference points that you can snap to and offset objects from. Youcan set the style of the point and its size relative to the screen or inabsolute units.

点对象Point有时会很有用,比如用作捕捉的节点或偏移对象的参考点。我们可以设置点的样式以及相对屏幕的大小或以绝对单位表示的大小。

The Pdmode and Pdsize properties of the Database object controlthe appearance of Point objects. A value of 0, 2, 3, and 4 for Pdmode specify a figure to draw through thepoint. A value of 1 selects nothing to be displayed.

Database对象的Pdmode属性和Pdsize属性用来控制Point对象的外观样式。 Pdmode取值为0、2、3和4指定通过点画的图形外观,取值为1表示什么都不显示。如下图:

            

                                  


Adding 32, 64, or 96 to the previous valueselects a shape to draw around the point in addition to the figure drawnthrough it:

上述Pdmode值分别加上32、64、96表示分别在上述点的外形周围加画上不同的形状,如下图:


                                 


Pdsize controls the size of the point figures,except for when Pdmode is 0 and 1. A 0 setting generates the point at 5 percentof the graphics area height. Setting Pdsize to a positive value specifies an absolutesize for the point figures. A negative value is interpreted as a percentage ofthe viewport size. The size of all points is recalculated when the drawing isregenerated.

Pdsize控制点形状的大小(Pdmode取值为0和1时除外)。Pdsize为0时生成的点是图像区域高的5%。Pdsize正值表示点形状的绝对大小,Pdsize正值表示相对视口大小的百分比。图形重新生成时,会重新计算所有点的大小。

After you change Pdmode and Pdsize, the appearance of existing pointschanges the next time the drawing is regenerated.

修改Pdmode和Pdsize的值后,已存在的点的形状会在下次重新生成图形时改变。

Create a Point object and change itsappearance 创建一个点对象并修改其外观

The following example creates a Pointobject in Model space at the coordinate (4, 3, 0). The Pdmode and Pdsize properties are then updated.

下例在Model空间创建一个Point对象,坐标为(4, 3, 0),然后更新Pdmode和Pdzise属性。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

ImportsAutodesk.AutoCAD.Geometry

 

<CommandMethod("AddPointAndSetPointStyle")>_

Public SubAddPointAndSetPointStyle()

  '' 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 Modelspace for write

      Dim acBlkTblRec As BlockTableRecord

      acBlkTblRec =acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _

                                     OpenMode.ForWrite)

 

      '' Create a point at (4, 3, 0) in Modelspace

      Dim acPoint As DBPoint = New DBPoint(NewPoint3d(4, 3, 0))

 

      '' Add the new object to the block tablerecord and the transaction

      acBlkTblRec.AppendEntity(acPoint)

      acTrans.AddNewlyCreatedDBObject(acPoint,True)

 

      '' Set the style for all point objects inthe drawing

      acCurDb.Pdmode = 34

      acCurDb.Pdsize = 1

 

      '' Save the new object to the database

      acTrans.Commit()

  End Using

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

usingAutodesk.AutoCAD.ApplicationServices;

usingAutodesk.AutoCAD.DatabaseServices;

usingAutodesk.AutoCAD.Geometry;

 

[CommandMethod("AddPointAndSetPointStyle")]

public static voidAddPointAndSetPointStyle()

{

  // 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以读打开Block表

      BlockTable acBlkTbl;

      acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId,

                                  OpenMode.ForRead) as BlockTable;

 

      // Open the Block table record Modelspace for write

      //以写打开Block表的Model空间记录

      BlockTableRecord acBlkTblRec;

      acBlkTblRec =acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],

                                      OpenMode.ForWrite)as BlockTableRecord;

 

      // Create a point at (4, 3, 0) in Modelspace在Model空间创建一个点

      DBPoint acPoint = new DBPoint(newPoint3d(4, 3, 0));

 

      // Add the new object to the block tablerecord and the transaction

      acBlkTblRec.AppendEntity(acPoint);

      acTrans.AddNewlyCreatedDBObject(acPoint,true);

 

      // Set the style for all point objects inthe drawing

      //设置图形中所有点对象的样式

      acCurDb.Pdmode = 34;

      acCurDb.Pdsize = 1;

 

      // Save the new object to the database保存新对象到数据库

      acTrans.Commit();

  }

}

VBA/ActiveX CodeReference

SubAddPointAndSetPointStyle()

    Dim pointObj As AcadPoint

    Dim location(0 To 2) As Double

 

    ' Define the location of the point

    location(0) = 4#: location(1) = 3#: location(2)= 0#

 

    ' Create the point

    Set pointObj =ThisDrawing.ModelSpace.AddPoint(location)

    ThisDrawing.SetVariable "PDMODE",34

    ThisDrawing.SetVariable "PDSIZE",1

 

    ZoomAll

End Sub

 

5、Create Solid-Filled Areas创建实体填充区域

You can create triangular andquadrilateral areas filled with a color. When creating filled areas, set theFILLMODE system variable to off to improve performance and back on once thefills have been created.

我们可以创建填充有色彩的三角形区域或四边形区域。创建填充区域时,将系统变量FILLMODE设置为off可以提高性能,创建完后可以再设置回on。

When you create a quadrilateralsolid-filled area, the sequence of the third and fourth points determines itsshape. Compare the following illustrations:

创建四边形实体填充区域时,第3、4点的顺序决定区域的形状。比较下面的图例:


                                       


The first two points define one edge ofthe polygon. The third point is defined diagonally opposite from the second. Ifthe fourth point is set equal to the third point, then a filled triangle iscreated.

第1、2两点定义多边形的一个边,第3个点定义在第2个点的对角线另一端。如果第4个点设置成和第3个点相等,创建的就是一个三角形填充区域。

For more information about filling solids,see “Create Solid-Filled Areas” in the AutoCAD User's Guide.

关于实体填充的更多内容,见AutoCAD用户指南中“创建实体填充区域”一节。

Create a solid-filled object 创建一个实体填充对象

The following example creates aquadrilateral solid (bow-tie) in Model space using the coordinates (0, 0, 0), (5,0, 0), (5, 8, 0), and (0, 8, 0). It also creates a quadrilateral solid in arectangular shape using the coordinates (10, 0, 0), (15, 0, 0), (10, 8, 0), and(15, 8, 0).

下例用坐标(0, 0, 0)、 (5, 0, 0)、 (5, 8, 0)和 (0, 8, 0)在Model空间创建一个四边形实体(蝴蝶结),还用坐标(10, 0, 0)、 (15, 0, 0)、 (10, 8, 0)和 (15, 8, 0)创建了一个长方形的四边实体。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

ImportsAutodesk.AutoCAD.Geometry

 

<CommandMethod("Add2DSolid")>_

Public SubAdd2DSolid()

  '' 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 Modelspace for write

      Dim acBlkTblRec As BlockTableRecord

      acBlkTblRec =acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _

                                     OpenMode.ForWrite)

 

      '' Create a quadrilateral (bow-tie) solidin Model space

      Dim ac2DSolidBow As Solid = New Solid(NewPoint3d(0, 0, 0), _

                                            NewPoint3d(5, 0, 0), _

                                            NewPoint3d(5, 8, 0), _

                                            NewPoint3d(0, 8, 0))

 

      '' Add the new object to the block tablerecord and the transaction

      acBlkTblRec.AppendEntity(ac2DSolidBow)

     acTrans.AddNewlyCreatedDBObject(ac2DSolidBow, True)

 

      '' Create a quadrilateral (square) solidin Model space

      Dim ac2DSolidSqr As Solid = New Solid(NewPoint3d(10, 0, 0), _

                                            NewPoint3d(15, 0, 0), _

                                            NewPoint3d(10, 8, 0), _

                                            NewPoint3d(15, 8, 0))

 

      '' Add the new object to the block tablerecord and the transaction

      acBlkTblRec.AppendEntity(ac2DSolidSqr)

     acTrans.AddNewlyCreatedDBObject(ac2DSolidSqr, True)

 

      '' Save the new object to the database

      acTrans.Commit()

  End Using

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

usingAutodesk.AutoCAD.ApplicationServices;

usingAutodesk.AutoCAD.DatabaseServices;

usingAutodesk.AutoCAD.Geometry;

 

[CommandMethod("Add2DSolid")]

public static voidAdd2DSolid()

{

  // 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 Modelspace for write

      BlockTableRecord acBlkTblRec;

      acBlkTblRec =acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],

                                     OpenMode.ForWrite) as BlockTableRecord;

 

      // Create a quadrilateral (bow-tie) solidin Model space

      //在Model空间创建四边形实体(蝴蝶结)

      Solid ac2DSolidBow = new Solid(newPoint3d(0, 0, 0),

                                     newPoint3d(5, 0, 0),

                                     newPoint3d(5, 8, 0),

                                     newPoint3d(0, 8, 0));

 

      // Add the new object to the block tablerecord and the transaction

      //将新对象添加到块表记录并登记事务

      acBlkTblRec.AppendEntity(ac2DSolidBow);

     acTrans.AddNewlyCreatedDBObject(ac2DSolidBow, true);

 

      // Create a quadrilateral (square) solidin Model space

      //在Model空间创建矩形实体

      Solid ac2DSolidSqr = new Solid(new Point3d(10,0, 0),

                                     newPoint3d(15, 0, 0),

                                     newPoint3d(10, 8, 0),

                                     newPoint3d(15, 8, 0));

      // Add the new object to the block tablerecord and the transaction

      //将新对象添加到块表记录并登记事务

      acBlkTblRec.AppendEntity(ac2DSolidSqr);

     acTrans.AddNewlyCreatedDBObject(ac2DSolidSqr, true);

 

      // Save the new object to the database

      //提交事务,将新对象保存到数据库

      acTrans.Commit();

  }

}

VBA/ActiveX Code Reference

Sub Add2DSolid()

    Dim solidObj As AcadSolid

    Dim point1(0 To 2) As Double

    Dim point2(0 To 2) As Double

    Dim point3(0 To 2) As Double

    Dim point4(0 To 2) As Double

 

    ' Define the solid

    point1(0) = 0#: point1(1) = 0#: point1(2) =0#

    point2(0) = 5#: point2(1) = 0#: point2(2) =0#

    point3(0) = 5#: point3(1) = 8#: point3(2) =0#

    point4(0) = 0#: point4(1) = 8#: point4(2) =0#

    ' Create the solid object in model space

    Set solidObj =ThisDrawing.ModelSpace.AddSolid (point1, point2,point3, point4)

    ' Define the solid

    point1(0) = 10#: point1(1) = 0#: point1(2)= 0#

    point2(0) = 15#: point2(1) = 0#: point2(2)= 0#

    point3(0) = 10#: point3(1) = 8#: point3(2)= 0#

    point4(0) = 15#: point4(1) = 8#: point4(2)= 0#

    ' Create the solid object in model space

    Set solidObj =ThisDrawing.ModelSpace.AddSolid (point1,point2, point3, point4)

    ZoomAll

End Sub

 

6、Work with Regions使用面域

Regions are two-dimensional enclosed areasyou create from closed shapes called loops. A loop is a closed boundary that ismade up of straight and curved objects which do not intersect themselves. Loopscan be combinations of lines, lightweight polylines, 2D and 3D polylines,circles, arcs, ellipses, elliptical arcs, splines, 3D faces, traces, andsolids.

面域是由称之为环的闭合形状构成的二维闭合区域。环是由互不相交的直的和弯曲的对象组成的闭合边界。环可以是直线、轻量级多段线、2D和3D多段线、圆、圆弧、椭圆、椭圆弧、样条曲线、3D面、宽线和填充实体等的组合。(译者注:Trace绘制宽线,该命令在AutoCAD2012版已废弃。)

The objects that make up the loops musteither be closed or form closed areas by sharing endpoints with other objects.They must also be coplanar (on the same plane). The loops that make up a regionmust be defined as an array of objects.

形成环的对象必须是闭合的,或与其他对象共用端点形成闭合区域。这些对象还必须是共面的(在同一平面上)。构成面域的环必须定义为对象数组。

For more information about working withregions, see “Create and Combine Areas (Regions)” in the AutoCAD User'sGuide.

关于使用面域的更多内容,见AutoCAD用户指南的“创建并合并区域(面域)”一节。

 

Topics in this section本小节内容

 

·        Create Regions 创建面域

·        Create Composite Regions 创建组合面域

 

6.1、Create Regions创建面域

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

通过创建一个Region对象实例并将该实例添加到BlockTableRecord上,来实现将面域添加到BlockTableRecord对象。在添加到BlockTableRecord对象之前,需要基于形成闭环的对象,对面域进行计算。CreateFromCurves函数使用输入的对象数组构成的每个闭环创建面域。CreateFromCurves方法请求并返回一个DBObjectCollection对象。

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

AutoCAD将闭合的二维多段线及平面上的三维多段线转变为单独的面域,然后将形成平面闭环的多段线、直线、曲线转变为面域。如果有两条以上的曲线共用一个端点,那最后得到的面域可能是不确定的。这种情况下,CreateFromCurves方法实际上会创建多个面域。要逐个将得到的面域添加到BlockTableRecord对象中去。

Create a simple region 创建一个简单面域

The following example creates a regionfrom a single circle.

下例用一个圆创建一个面域。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

ImportsAutodesk.AutoCAD.Geometry

 

<CommandMethod("AddRegion")>_

Public SubAddRegion()

  '' 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 Modelspace 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.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 oneach closed loop

          Dim myRegionColl AsDBObjectCollection = New DBObjectCollection()

          myRegionColl =Region.CreateFromCurves(acDBObjColl)

          Dim acRegion As Region =myRegionColl(0)

 

          '' Add the new object to the blocktable record and the transaction

          acBlkTblRec.AppendEntity(acRegion)

         acTrans.AddNewlyCreatedDBObject(acRegion, True)

 

          '' Dispose of the in memory objectnot appended to the database

      End Using

 

      '' Save the new object to the database

      acTrans.Commit()

  End Using

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

usingAutodesk.AutoCAD.ApplicationServices;

usingAutodesk.AutoCAD.DatabaseServices;

usingAutodesk.AutoCAD.Geometry;

 

[CommandMethod("AddRegion")]

public static voidAddRegion()

{

  // 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以读打开Block表

      BlockTable acBlkTbl;

      acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId,

                                  OpenMode.ForRead) as BlockTable;

 

      // Open the Block table record Modelspace for write

      //以写打开Block表记录Model空间

      BlockTableRecord acBlkTblRec;

      acBlkTblRec =acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],

                                     OpenMode.ForWrite) as BlockTableRecord;

 

      // Create an in memory circle在内存创建一个圆

      using (Circle acCirc = new Circle())

      {

          acCirc.Center = new Point3d(2, 2, 0);

          acCirc.Radius = 5;

 

          // Adds the circle to an object array将圆添加到对象数组

          DBObjectCollection acDBObjColl = newDBObjectCollection();

          acDBObjColl.Add(acCirc);

 

          // Calculate the regions based oneach closed loop

          //基于每个闭环计算面域

          DBObjectCollection myRegionColl = newDBObjectCollection();

          myRegionColl = Region.CreateFromCurves(acDBObjColl);

          Region acRegion = myRegionColl[0] asRegion;

 

          // Add the new object to the blocktable record and the transaction

          //添加新对象到块表记录并添加事务

          acBlkTblRec.AppendEntity(acRegion);

          acTrans.AddNewlyCreatedDBObject(acRegion,true);

 

          // Dispose of the in memory circlenot appended to the database

          //处置内存中的圆,不添加到数据库;

      }

 

      // Save the new object to the database保存新对象到数据库

      acTrans.Commit();

  }

}

VBA/ActiveX CodeReference

Sub AddRegion()

    ' Define an array to hold the

    ' boundaries of the region.

    Dim curves(0 To 0) As AcadCircle

    ' Create a circle to become a

    ' boundary for the region.

    Dim center(0 To 2) As Double

    Dim radius As Double

    center(0) = 2

    center(1) = 2

    center(2) = 0

    radius = 5#

    Set curves(0) =ThisDrawing.ModelSpace.AddCircle (center,radius)

     ' Create the region

    Dim regionObj As Variant

    regionObj = ThisDrawing.ModelSpace.AddRegion(curves)

    ZoomAll

End Sub

 

6.2、Create Composite Regions创建组合面域

You can create composite regions bysubtracting, combining, or finding the intersection of regions or 3D solids.You can then extrude or revolve composite regions to create complex solids. Tocreate a composite region, use the BooleanOperation method.

可以通过面域间或3D实体间的差集、并集、交集来创建组合面域,然后通过拉伸或旋转组合面域来创建复杂的实体。创建组合面域,要用到BooleanOperation方法。

Subtract regions 差集面域

When you subtract one region from another, you call the BooleanOperation method from the first region. This is theregion from which you want to subtract. For example, to calculate how muchcarpeting is needed for a floor plan, call the BooleanOperation method from the outer boundary of thefloor space and use the non-carpeted areas, such as pillars and counters, asthe object in the Boolean parameter list.

当从一个面域减去另一个面域时,调用第一个面域的BooleanOperation方法,这第一个面域就是你要从中减去一部分的那个面域。比如,要计算铺地板需要多少地毯,就从整个地板空间的外便捷调用BooleanOperation方法,并使用不需地毯的区域,如柱子、柜子等,作为布尔参数列表中的对象。

Unite regions 并集面域

To unite regions, call the BooleanOperation method and use the constant BooleanOperationType.BoolUnite for the operation instead of BooleanOperationType.BoolSubtract. You can combine regions in any order tounite them.

要合并面域,调用BooleanOperation方法并用操作类型常量BooleanOperationType.BoolUnite代替BooleanOperationType.BoolSubtract即可。可以以任意顺序对各面域进行合并来获得组合面域。

Find the intersection of two regions 求两个面域的交

To find the intersection of two regions,use the constant BooleanOperationType.BoolIntersect. You can combine regions in any order tointersect them.

求两个面域的交集,使用操作类型常量BooleanOperationType.BoolIntersect来调用BooleanOperation方法。可以以任意顺序对各面域求交集来获得组合面域。

Create a composite region 创建一个组合面域

The following example creates two regionsfrom two circles and then subtracts the smaller region from the large one tocreate a wheel.

下面这个示例用两个圆创建两个面域,然后从较大的一个中减去较小的一个来创建一个轮形面域。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

ImportsAutodesk.AutoCAD.Geometry

 

<CommandMethod("CreateCompositeRegions")>_

Public SubCreateCompositeRegions()

  '' 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 Modelspace for write

      Dim acBlkTblRec As BlockTableRecord

      acBlkTblRec =acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _

                                     OpenMode.ForWrite)

 

      '' Create two in memory circles

      Dim acCirc1 As Circle = New Circle()

      acCirc1.Center = New Point3d(4, 4, 0)

      acCirc1.Radius = 2

 

      Dim acCirc2 As Circle = New Circle()

      acCirc2.Center = New Point3d(4, 4, 0)

      acCirc2.Radius = 1

 

      '' Adds the circle to an object array

      DimacDBObjColl As DBObjectCollection = New DBObjectCollection()

      acDBObjColl.Add(acCirc1)

      acDBObjColl.Add(acCirc2)

 

      '' Calculate the regions based on eachclosed loop

      Dim myRegionColl As DBObjectCollection =New DBObjectCollection()

      myRegionColl =Region.CreateFromCurves(acDBObjColl)

      Dim acRegion1 As Region = myRegionColl(0)

      Dim acRegion2 As Region = myRegionColl(1)

 

      '' Subtract region 1 from region 2

      If acRegion1.Area > acRegion2.AreaThen

          '' Subtract the smaller region from thelarger one

         acRegion1.BooleanOperation(BooleanOperationType.BoolSubtract, acRegion2)

          acRegion2.Dispose()

 

          '' Add the final region to thedatabase

          acBlkTblRec.AppendEntity(acRegion1)

         acTrans.AddNewlyCreatedDBObject(acRegion1, True)

      Else

          '' Subtract the smaller region fromthe larger one

         acRegion2.BooleanOperation(BooleanOperationType.BoolSubtract, acRegion1)

          acRegion1.Dispose()

 

         '' Add the final region to the database

          acBlkTblRec.AppendEntity(acRegion2)

         acTrans.AddNewlyCreatedDBObject(acRegion2, True)

      End If

 

      '' Dispose of the in memory objects notappended to the database

      acCirc1.Dispose()

      acCirc2.Dispose()

 

      '' Save the new object to the database

      acTrans.Commit()

  End Using

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

usingAutodesk.AutoCAD.ApplicationServices;

usingAutodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.Geometry;

 

[CommandMethod("CreateCompositeRegions")]

public static voidCreateCompositeRegions()

{

  // 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以读打开Block表

      BlockTable acBlkTbl;

      acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId,

                                   OpenMode.ForRead) as BlockTable;

 

      // Open the Block table record Modelspace for write

      //以写打开Model空间

      BlockTableRecord acBlkTblRec;

      acBlkTblRec =acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],

                                      OpenMode.ForWrite)as BlockTableRecord;

 

      // Create two in memory circles在内存建两个圆

      Circle acCirc1 = new Circle();

      acCirc1.Center = new Point3d(4, 4, 0);

      acCirc1.Radius = 2;

 

      Circle acCirc2 = new Circle();

      acCirc2.Center = new Point3d(4, 4, 0);

      acCirc2.Radius = 1;

 

      // Adds the circle to an object array将圆添加到对象数组

      DBObjectCollection acDBObjColl = newDBObjectCollection();

      acDBObjColl.Add(acCirc1);

      acDBObjColl.Add(acCirc2);

 

      // Calculate the regions based on eachclosed loop

      //基于每个闭环计算面域

      DBObjectCollection myRegionColl = newDBObjectCollection();

      myRegionColl =Region.CreateFromCurves(acDBObjColl);

      Region acRegion1 = myRegionColl[0] asRegion;

      Region acRegion2 = myRegionColl[1] asRegion;

 

      //Subtract region 1 from region 2

      // 从面域2减去面域1

      if (acRegion1.Area > acRegion2.Area)

      {

          // Subtract the smaller region fromthe larger one

          // 从较大面域中减去较小面域

         acRegion1.BooleanOperation(BooleanOperationType.BoolSubtract,acRegion2);

          acRegion2.Dispose();

 

          // Add the final region to thedatabase

          // 将最终的面域添加到数据库

          acBlkTblRec.AppendEntity(acRegion1);

         acTrans.AddNewlyCreatedDBObject(acRegion1, true);

      }

      else

      {

          // Subtract the smaller region fromthe larger one

          // 从较大面域中减去较小面域

          acRegion2.BooleanOperation(BooleanOperationType.BoolSubtract,acRegion1);

          acRegion1.Dispose();

 

          // Add the final region to thedatabase

          // 将最终的面域添加到数据库

          acBlkTblRec.AppendEntity(acRegion2);

          acTrans.AddNewlyCreatedDBObject(acRegion2,true);

      }

 

      // Dispose of the in memory objects notappended to the database

      // 销毁内存中的两个圆对象

      acCirc1.Dispose();

      acCirc2.Dispose();

 

      // Save the new object to the database

      // 保存新对象到数据库

      acTrans.Commit();

  }

}

VBA/ActiveX 代码参考

SubCreateCompositeRegions()

    ' Create two circles, one representingoutside of the wheel,

    ' the other the center of the wheel

    Dim DonutParts(0 To 1) As AcadCircle

    Dim center(0 To 2) As Double

    Dim radius As Double

    center(0) = 4

    center(1) = 4

    center(2) = 0

    radius = 2#

    Set WheelParts(0) = ThisDrawing.ModelSpace._

                            AddCircle(center,radius)

    radius = 1#

    Set WheelParts(1) = ThisDrawing.ModelSpace._

                            AddCircle(center,radius)

 

    ' Create a region from the two circles

    Dim regions As Variant

    regions =ThisDrawing.ModelSpace.AddRegion(WheelParts)

 

    ' Copy the regions into the regionvariables for ease of use

    Dim WheelOuter As AcadRegion

    Dim WheelInner As AcadRegion

 

    If regions(0).Area > regions(1).AreaThen

        ' The first region is the outer edge ofthe wheel

        Set WheelOuter = regions(0)

        Set WheelInner = regions(1)

    Else

        ' The first region is the inner edge ofthe wheel

        Set WheelInner = regions(0)

        Set WheelOuter = regions(1)

    End If

 

    ' Subtract the smaller circle from thelarger circle

    WheelOuter.Boolean acSubtraction,WheelInner

End Sub

 

 

原创粉丝点击