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

Create Objects創建對象

AutoCAD often offers several differentways to create the same graphical object. While the .NET API does not offer thesame combinations of creating objects, it does offer a basic object constructorfor each object type but also offers overrides for many of the objectconstructors as well.

對於創建相同的圖形對象,AutoCAD常常提供幾個不同的方法。.NET API雖然沒有提供相同的創建方法組合,但爲每個對象類型都提供了基本對象構造函數,並且對許多對象的構造函數還提供了重載。

For example, in AutoCAD there are fourdifferent ways you can create a circle: (1) by specifying the center andradius, (2) by two points defining the diameter, (3) by three points definingthe circumference, or (4) by two tangents and a radius. However, in .NET APIthere is two creation methods provided to create a circle. One method acceptsno parameters, while the second requires a center point, the normal directionfor the circle, and a radius.

例如,AutoCAD中繪製一個圓有4個不同的方法:(1)通過指定圓心和半徑;(2)通過兩點定義的直徑;(3)通過三個點定義的圓周;(4)通過兩條切線和半徑。可是,在.NETAPI中,創建一個圓有兩個方法,一個方法是無參數方法,另一個方法需要圓心、方向和半徑。

Note Objects are created using the New keywordand then appended to the parent object using Add or AppendEntity based on if you are working with acontainer (symbol table or dictionary) or a BlockTableRecord object.

注意:對象通過使用New關鍵字創建,然後使用Add或AppendEntity方法追加到其父對象中。具體是使用Add方法還是使用AppendEntity方法,取決於父對象是一個容器對象(符號表或字典)還是BlockTableRecord對象。

Set the default property values for anobject 設置對象的默認屬性值

When a new graphical object is created,the following entity property values are assigned the current entity values definedin the database of the current document:

一個新對象創建時,下列實體屬性值被設置成當前文檔數據庫所定義的當前實體值:

·        Color 顏色

·        Layer 圖層

·        Linetype 線型

·        Linetype scale 線形比例

·        Lineweight 線寬

·        Plot style name 打印樣式名字

·        Visibility 可見性

·        Transparency 透明度

Note If the properties of an object need to beset to the default values of the current database, call the SetDatabaseDefaults method of the object to be changed.

注意:如果需要將對象的屬性設置爲當前數據庫的默認值,可調用要修改對象的SetDatabaseDefaults方法。

Topics in this section本節主題

·        Determine the Parent Object 確定父對象

·        Create Lines 創建直線

·        Create Curved Objects 創建曲線類對象

·        Create Point Objects 創建點對象

·        Create Solid-Filled Areas 創建實體填充區域

·        Work with Regions 使用面域

·        Create Hatches 創建圖案填充

 

1、Determinethe Parent Object確定父對象

Graphical objects are appended to aBlockTableRecord object, such as Model or Paper space. You reference the blocksthat represent Model and Paper space through the BlockTable object. If you wantto work in the current space instead of a specific space, you get the ObjectIdfor the current space from the current database with the CurrentSpaceId property.

圖形對象被添加到像模型空間或圖紙空間這樣的BlockTableRecord對象中。我們通過BlockTable對象引用代表Model空間或Paper空間的塊。如果要使用當前空間而不是指定某空間,可以使用CurrentSpaceId屬性從當前數據庫獲取當前空間的ObjectId。

The ObjectId for the block table recordsof Model and Paper space can be retrieved from the BlockTable object using aproperty or the GetBlockModelSpaceId and GetBlockPaperSpaceId methods of the SymbolUtilityServicesclass under the DatabaseServices namespace.

塊表記錄Model空間和塊表記錄Paper空間的ObjectId可以使用屬性從BlockTable對象取得,或者使用DatabaseServices命名空間下SymbolUtilitySErvices類的GetBlockModelSpaceId方法和GetBlockPaperSpaceId方法取得。

Access Model space, Paper space or thecurrent space 訪問模型空間、圖紙空間或當前空間

The following example demonstrates how toaccess the block table records associated with Model space, Paper space or thecurrent space. Once the block table record is referenced, a new line is addedto the block table record.

下面這個例子演示如何訪問與Model空間、Paper空間或當前空間關聯的塊表記錄。獲得對塊表記錄的引用後,往裏添加一條新直線。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

ImportsAutodesk.AutoCAD.Geometry

ImportsAutodesk.AutoCAD.EditorInput

<CommandMethod("AccessSpace")>_

Public SubAccessSpace()

  '' 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 & "Enterwhich 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

          '' Get the ObjectID for Model spacefrom the Block table

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

                                          OpenMode.ForWrite)

      ElseIf pKeyRes.StringResult ="Paper" Then

          '' Get the ObjectID for Paper spacefrom the Block table

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

                                         OpenMode.ForWrite)

      Else

          '' Get the ObjectID for the currentspace from the database

          acBlkTblRec =acTrans.GetObject(acCurDb.CurrentSpaceId, _

                                          OpenMode.ForWrite)

      End If

      '' Create a line that starts at 2,5 andends at 10,7

      Dim acLine As Line = New Line(NewPoint3d(2, 5, 0), _

                                    NewPoint3d(10, 7, 0))

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

      acBlkTblRec.AppendEntity(acLine)

      acTrans.AddNewlyCreatedDBObject(acLine,True)

      '' Save the new line to the database

      acTrans.Commit()

  End Using

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.ApplicationServices;

usingAutodesk.AutoCAD.DatabaseServices;

usingAutodesk.AutoCAD.Geometry;

usingAutodesk.AutoCAD.EditorInput;


[CommandMethod("AccessSpace")]

public static voidAccessSpace()

{

  // 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 = newPromptKeywordOptions("");

      pKeyOpts.Message = "\nEnter whichspace 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")

      {

          // Get the ObjectID for Model spacefrom the Block table

          //從Block表獲取Model空間的ObjectID

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

                                         OpenMode.ForWrite) as BlockTableRecord;

      }

      else if (pKeyRes.StringResult =="Paper")

      {

          // Get the ObjectID for Paper spacefrom the Block table

          //從Block表獲取Paper空間的ObjectID

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

                                         OpenMode.ForWrite) as BlockTableRecord;

      }

      else

      {

          // Get the ObjectID for the currentspace from the database

          //從數據庫獲取當前空間的ObjectID

          acBlkTblRec =acTrans.GetObject(acCurDb.CurrentSpaceId,

                                         OpenMode.ForWrite) as BlockTableRecord;

      }

      // Create a line that starts at 2,5 andends at 10,7

      //從2,5到10,7畫一條直線

      Line acLine = new Line(new Point3d(2, 5,0),

                             new Point3d(10, 7,0));

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

      //添加新對象到塊表記錄並添加事務

      acBlkTblRec.AppendEntity(acLine);

      acTrans.AddNewlyCreatedDBObject(acLine,true);

      // Save the new line to the database保存新直線到數據庫

      acTrans.Commit();

  }

}

VBA/ActiveX Code Reference

Public SubAccessSpace()

    ' Define the valid keywords

    Dim keywordList As String

    keywordList = "Model PaperCurrent"

 

    ' Call InitializeUserInput to setup thekeywords

    ThisDrawing.Utility.InitializeUserInput 1,keywordList

 

    ' Get the user input

    Dim retVal As Variant

    retVal =ThisDrawing.Utility.GetKeyword(vbLf & _

                                   "Enterwhich space to create the line in " & _

                                  "[Model/Paper/Current]: ")

 

    ' Get the entered keyword

    Dim strVal As String

    strVal = ThisDrawing.Utility.GetInput

 

    Dim acSpaceObj As Object

 

    If strVal = "Model" Or _

      (strVal = "Current" AndThisDrawing.ActiveSpace = acModelSpace) Then

        '' Get the Model space object

        Set acSpaceObj = ThisDrawing.ModelSpace

    Else

        '' Get the Paper space object

        Set acSpaceObj = ThisDrawing.PaperSpace

    End If

 

    '' Create a line that starts at 2,5 andends at 10,7

    Dim acLine As AcadLine

    Dim dPtStr(0 To 2) As Double

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

 

    Dim dPtEnd(0 To 2) As Double

    dPtEnd(0) = 10: dPtEnd(1) = 7: dPtEnd(2) =0#

 

    Set acLine = acSpaceObj.AddLine(dPtStr,dPtEnd)

End Sub

 

2、CreateLines創建直線

The line is the most basic object inAutoCAD. You can create a variety of lines—single lines, and multiple linesegments with and without arcs. In general, you draw lines by specifyingcoordinate points. Lines when created, inherit the current settings from thedrawing database, such as layer, linetype and color.

直線是AutoCAD中最基本的對象。我們可以創建各種不同的直線——單一的直線、帶圓弧或不帶圓弧的複合線段。通常是通過指定座標點來繪製直線。創建的直線從當前數據庫繼承當前設置,如圖層、線型及顏色等。

To create a line, you create a newinstance of one of the following objects:

創建一條直線,就是創建下列對象之一的一個新實例:

Line

Creates a line. 創建一條直線;

Polyline

Creates a 2D lightweight polyline. 創建二維輕量級多段線;

MLine

Creates a multiline. 創建多線;

Polyline2D

Creates a 2D polyline. 創建二維多段線;

Polyline3D

Creates a 3D polyline. 創建三維多段線

 

Note Polyline2D objects are the legacy polylineobjects that were in AutoCAD prior to Release 14, and the Polyline objectrepresents the new optimized polyline that was introduced with AutoCAD Release14.

注意:Polyline2D對象是Release14之前版本AutoCAD中的傳統多段線對象,Polyline對象代表AutoCAD Release 14版引入的新的優化多段線。

 

Topics in this section本小節主題

 

·        Create a Line Object 創建一個直線對象

·        Create a Polyline object 創建一個多段線對象

 

2.1、Create a Line Object創建一個直線對象

This example adds a line that starts at(5,5,0) and ends at (12,3,0) to Model space.

本例往模型空間添加一條直線,起點(5,5,0),終點(12,3,0)。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

ImportsAutodesk.AutoCAD.Geometry

 

<CommandMethod("AddLine")>_

Public Sub AddLine()

  '' 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 line that starts at 5,5 andends at 12,3

      Dim acLine As Line = New Line(NewPoint3d(5, 5, 0), _

                                    NewPoint3d(12, 3, 0))

 

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

      acBlkTblRec.AppendEntity(acLine)

      acTrans.AddNewlyCreatedDBObject(acLine,True)

 

      '' 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("AddLine")]

public static voidAddLine()

{

  // 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 line that starts at 5,5 andends at 12,3

      Line acLine = new Line(new Point3d(5, 5,0),

                             new Point3d(12, 3,0));

 

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

      acBlkTblRec.AppendEntity(acLine);

      acTrans.AddNewlyCreatedDBObject(acLine,true);

 

      // Save the new object to the database

      acTrans.Commit();

  }

}

VBA/ActiveX CodeReference

Sub AddLine()

    ' Define the start point

    Dim ptStr(0 To 2) As Double

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

 

    ' Define the end point

    Dim ptEnd(0 To 2) As Double

    ptEnd(0) = 12: ptEnd(1) = 3: ptEnd(2) = 0#

 

    ' Create a Line object in model space

    Dim lineObj As AcadLine

    Set lineObj =ThisDrawing.ModelSpace.AddLine(ptStr, ptEnd)

 

    ThisDrawing.Application.ZoomAll

End Sub

 

2.2、Create a Polyline object創建一個多段線對象

This example adds a lightweight polylinewith two straight segments using the 2D coordinates (2,4), (4,2), and (6,4) toModel space.

本例往模型空間添加一條輕量級多段線,多段線有兩段線段,二維座標爲(2,4)、(4,2)和(6,4)。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

ImportsAutodesk.AutoCAD.Geometry

 

<CommandMethod("AddLightweightPolyline")>_

Public SubAddLightweightPolyline()

  '' 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 polyline with two segments (3points)

      Dim acPoly As Polyline = New Polyline()

      acPoly.AddVertexAt(0, New Point2d(2, 4),0, 0, 0)

      acPoly.AddVertexAt(1, New Point2d(4, 2),0, 0, 0)

      acPoly.AddVertexAt(2, New Point2d(6, 4),0, 0, 0)

 

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

      acBlkTblRec.AppendEntity(acPoly)

      acTrans.AddNewlyCreatedDBObject(acPoly,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("AddLightweightPolyline")]

public static voidAddLightweightPolyline()

{

  // 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 polyline with two segments (3points)

      Polyline acPoly = new Polyline();

      acPoly.AddVertexAt(0, new Point2d(2, 4),0, 0, 0);

      acPoly.AddVertexAt(1, new Point2d(4, 2),0, 0, 0);

      acPoly.AddVertexAt(2, new Point2d(6, 4),0, 0, 0);

 

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

      acBlkTblRec.AppendEntity(acPoly);

      acTrans.AddNewlyCreatedDBObject(acPoly,true);

 

      // Save the new object to the database

      acTrans.Commit();

  }

}

VBA/ActiveX Code Reference

SubAddLightWeightPolyline()

    Dim plineObj As AcadLWPolyline

    Dim points(0 To 5) As Double

 

    ' Define the 2D polyline points

    points(0) = 2: points(1) = 4

    points(2) = 4: points(3) = 2

    points(4) = 6: points(5) = 4

 

    ' Create a light weight Polyline object inmodel space

    Set plineObj = ThisDrawing.ModelSpace. _

                  AddLightWeightPolyline(points)

    ThisDrawing.Application.ZoomAll

End Sub

 

3、CreateCurved Objects創建曲線類對象

You can create a variety of curved objectswith AutoCAD, including splines, helixes, circles, arcs, and ellipses. Allcurves are created on the XY plane of the current UCS.

用AutoCAD可以創建各種不同的曲線類對象,包括樣條曲線、螺旋線、圓、圓弧以及橢圓等。所有這些曲線都創建在當前用戶座標系的XY平面上。

To create a curve, you create a newinstance of one of the following objects:

創建曲線,就是創建下列對象之一的一個新實例:

Arc

Creates an arc given the center point, radius, start andend angles. 已知圓心、半徑、起止角,建圓弧;

Circle

Creates a circle given the center point and radius. 已知圓心、半徑,建圓;

Ellipse

Creates an ellipse, given the center point, a point onthe major axis, and the radius ratio. 已知圓心、主軸上一點、半徑比,建橢圓;

Spline

Creates a quadratic or cubic NURBS (nonuniform rationalB-spline) curve. 創建一條二階或三階不均勻有理B樣條曲線;

Helix

Creates a 2D or 3D helix object. 創建二維或三維螺旋線對象;

 

Topics in this section本小節主題

 

·      Create a Circle object 創建圓對象

·      Create an Arc object 創建圓弧對象

·      Create a Spline object 創建樣條曲線對象

 

 

3.1、Createa Circle object創建圓對象

This example creates a circle in Modelspace with a center point of (2,3,0) and a radius of 4.25.

本例在模型空間創建一個圓,圓心(2,3,0),半徑4.25。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

Imports Autodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

ImportsAutodesk.AutoCAD.Geometry

 

<CommandMethod("AddCircle")>_

Public SubAddCircle()

  '' 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 circle that is at 2,3 with aradius of 4.25

      Dim acCirc As Circle = New Circle()

      acCirc.Center = New Point3d(2, 3, 0)

      acCirc.Radius = 4.25

 

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

      acBlkTblRec.AppendEntity(acCirc)

      acTrans.AddNewlyCreatedDBObject(acCirc,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("AddCircle")]

public static voidAddCircle()

{

  // 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 circle that is at 2,3 with aradius of 4.25

      //創建圓,圓心(2,3),半徑4.25

      Circle acCirc = new Circle();

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

      acCirc.Radius = 4.25;

 

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

      //添加新對象到塊表記錄及事務

      acBlkTblRec.AppendEntity(acCirc);

      acTrans.AddNewlyCreatedDBObject(acCirc,true);

 

      // Save the new object to the database保存新對象到數據庫

      acTrans.Commit();

  }

}

VBA/ActiveX Code Reference

Sub AddCircle()

    ' Define the center point

    Dim ptCen(0 To 2) As Double

    ptCen(0) = 2: ptCen(1) = 3: ptCen(2) = 0#

 

    ' Create a Circle object in model space

    Dim circObj As AcadCircle

    Set circObj =ThisDrawing.ModelSpace.AddCircle(ptCen, 4.25)

 

    ThisDrawing.Application.ZoomAll

End Sub

 

3.2、Createan Arc object創建圓弧對象

This example creates an arc in Model spacewith a center point of (6.25,9.125,0), a radius of 6, start angle of 1.117 (64degrees), and an end angle of 3.5605 (204 degrees).

本例在模型空間創建一個圓弧,圓心(6.25,9.125,0),半徑6,起始角1.117(64度),終止角3.5605(204度)。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

ImportsAutodesk.AutoCAD.Geometry

 

<CommandMethod("AddArc")>_

Public Sub AddArc()

  '' 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 arc that is at 6.25,9.125with a radius of 6, and

      '' starts at 64 degrees and ends at 204degrees

      Dim acArc As Arc = New Arc(NewPoint3d(6.25, 9.125, 0), _

                                 6, 1.117,3.5605)

 

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

      acBlkTblRec.AppendEntity(acArc)

      acTrans.AddNewlyCreatedDBObject(acArc,True)

 

      '' 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("AddArc")]

public static voidAddArc()

{

  // 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) asBlockTable;

 

      // Open the Block table record Modelspace for write

      //以寫打開模型空間記錄

      BlockTableRecord acBlkTblRec;

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

                                      OpenMode.ForWrite)as BlockTableRecord;

 

      // Create an arc that is at 6.25,9.125with a radius of 6, and

      // starts at 64 degrees and ends at 204degrees

      // 已知圓心、半徑、起止角,建圓弧

      Arc acArc = new Arc(new Point3d(6.25,9.125, 0),

                          6, 1.117, 3.5605);

 

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

      //將新對象添加到塊表記錄及事務

      acBlkTblRec.AppendEntity(acArc);

      acTrans.AddNewlyCreatedDBObject(acArc,true);

 

      // Save the new object to the database保存新對象到數據庫

      acTrans.Commit();

  }

}

VBA/ActiveX Code Reference

Sub AddArc()

    ' Define the center point

    Dim ptCen(0 To 2) As Double

    ptCen(0) = 6.25: ptCen(1) = 9.125: ptCen(2)= 0#

 

    ' Create an Arc object in model space

    Dim arcObj As AcadArc

    Set arcObj =ThisDrawing.ModelSpace.AddArc(ptCen, 6#, 1.117, 3.5605)

 

    ThisDrawing.Application.ZoomAll

End Sub

 

3.3、Createa Spline object創建樣條曲線對象

This example creates a spline in Modelspace using three points (0, 0, 0), (5, 5, 0), and (10, 0, 0). The spline hasstart and end tangents of (0.5, 0.5, 0.0).

本例用三個點(0, 0, 0)、 (5, 5, 0)和 (10, 0, 0)在模型空間創建一條樣條曲線。該樣條曲線的起止點的切線方向爲(0.5, 0.5, 0.0)。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

ImportsAutodesk.AutoCAD.Geometry

 

<CommandMethod("AddSpline")>_

Public SubAddSpline()

  '' 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)

 

      '' Define the fit points for the spline

      Dim ptColl As Point3dCollection = NewPoint3dCollection()

      ptColl.Add(New Point3d(0, 0, 0))

      ptColl.Add(New Point3d(5, 5, 0))

      ptColl.Add(New Point3d(10, 0, 0))

 

      '' Get a 3D vector from the point(0.5,0.5,0)

      Dim vecTan As Vector3d = New Point3d(0.5,0.5, 0).GetAsVector

 

      '' Create a spline through (0, 0, 0), (5,5, 0), and (10, 0, 0) with a

      '' start and end tangency of (0.5, 0.5,0.0)

      Dim acSpline As Spline = NewSpline(ptColl, vecTan, vecTan, 4, 0.0)

 

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

      acBlkTblRec.AppendEntity(acSpline)

      acTrans.AddNewlyCreatedDBObject(acSpline,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("AddSpline")]

public static voidAddSpline()

{

  // 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;

 

      // Define the fit points for the spline

      // 定義樣條曲線的擬合點

      Point3dCollection ptColl = newPoint3dCollection();

      ptColl.Add(new Point3d(0, 0, 0));

      ptColl.Add(new Point3d(5, 5, 0));

      ptColl.Add(new Point3d(10, 0, 0));

 

      // Get a 3D vector from the point(0.5,0.5,0)

      // 獲取點(0.5,0.5,0)的3D矢量

      Vector3d vecTan = new Point3d(0.5, 0.5,0).GetAsVector();

 

      // Create a spline through (0, 0, 0), (5,5, 0), and (10, 0, 0) with a

      // start and end tangency of (0.5, 0.5,0.0)

      //創建通過3個點的樣條曲線,且起止點的切線方向爲(0.5, 0.5, 0.0);

      //注:在公差值設置爲0.0時(第5個參數),樣條曲線直接通過擬合點。

      Spline acSpline = new Spline(ptColl,vecTan, vecTan, 4, 0.0);

 

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

      //添加新對象到塊表記錄及事務

      acBlkTblRec.AppendEntity(acSpline);

      acTrans.AddNewlyCreatedDBObject(acSpline,true);

 

      // Save the new line to the database保存新對象到數據庫

      acTrans.Commit();

  }

}

VBA/ActiveX Code Reference

Sub AddSpline()

    ' This example creates a spline object inmodel space.

    ' Declare the variables needed

    Dim splineObj As AcadSpline

    Dim startTan(0 To 2) As Double

    Dim endTan(0 To 2) As Double

    Dim fitPoints(0 To 8) As Double

 

    ' Define the variables

    startTan(0) = 0.5: startTan(1) = 0.5:startTan(2) = 0

    endTan(0) = 0.5: endTan(1) = 0.5: endTan(2)= 0

    fitPoints(0) = 1: fitPoints(1) = 1:fitPoints(2) = 0

    fitPoints(3) = 5: fitPoints(4) = 5:fitPoints(5) = 0

    fitPoints(6) = 10: fitPoints(7) = 0:fitPoints(8) = 0

 

    ' Create the spline

    Set splineObj =ThisDrawing.ModelSpace.AddSpline _

                        (fitPoints, startTan,endTan)

    ZoomAll

End Sub

 

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