03-02 創建和編輯AutoCAD實體(二) 創建對象(2)

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

 

 

發佈了4 篇原創文章 · 獲贊 20 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章