03-04 創建和編輯AutoCAD實體(四) 編輯二維命名對象 (2)

3、CopyObjects複製對象

You can create a copy of most nongraphicaland graphical objects in a database. You create a copy of an object with the Clone function. Once an object is cloned, youcan modify the returned object before it is added to the database. You canmimic many of the modifying commands in AutoCAD through the use of the Clone and TransformBy methods. For more information about the TransformBy method, seeTransformObjects.

我們可以爲數據庫中的大多數非圖形對象和圖形對象創建一個拷貝。創建對象拷貝用Clone方法。對象一旦被複制,我們就可以對返回的對象進行修改,然後將其添加到數據庫內。通過使用Clone方法和TransformBy方法,我們可以模仿AutoCAD的許多修改命令。關於TransformBy方法的更多內容,見“變換對象”一節。

Along with creating a direct copy of anobject, you can also use the Clone and TransformBy methods to offset, mirror and arrayobjects. For more information about copying objects, see “Copy, Offset, orMirror Objects” in theAutoCAD User's Guide.

除了創建對象的直接拷貝外,我們還可以使用Clone方法和TransformBy方法來偏移對象、鏡像對象及陣列對象。關於複製對象的更多內容,見AutoCAD用戶指南中“複製、偏移或鏡像對象”一節。

Topics in this section本小節內容

·        Copy an Object 複製一個對象

·        Copy Objects between Databases 在數據庫間複製對象

 

3.1、Copyan Object複製一個對象

To copy an object, use the Clone function provided for that object. Thismethod creates a new object that is a duplicate of the original object. Oncethe duplicate object is created, you can then modify it prior to adding orappending it to the database. If you do not transform the object or change itsposition, the new object will be located in the same position as the original.

要複製一個對象,對該對象應用Clone方法即可。Clone方法創建原對象的一個複本新對象。複本對象創建後,我們可以在將其添加到數據庫前對其修改。如果沒有變換這個新對象也沒有修改它的位置,那麼新對象就與原對象在相同位置上。

If you have a large number of objects youmight want to copy, you can add each of the object ids to an ObjectIdCollectionobject and then iterate each of the objects. As you iterate each object, youcan then use the Clone function for each object and then add or append the newobject to the database.

如果需要複製大量對象,可以將每個對象的objectid添加到ObjectIdCollection對象,然後遍歷其中的每個對象。遍歷每個對象時,就可以爲每個對象調用Clone方法,然後將新對象添加或追加到數據庫。

Copy a single object 複製單個對象

The following example creates a new circleand then creates a direct copy of the circle to create a second circle.

下面示例新建一個圓,然後用直接複製該圓的方法創建第二個圓。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

ImportsAutodesk.AutoCAD.Geometry

 

<CommandMethod("SingleCopy")>_

Public SubSingleCopy()

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

 

      '' Create a copy of the circle and changeits radius

      Dim acCircClone As Circle =acCirc.Clone()

      acCircClone.Radius = 1

 

      '' Add the cloned circle

      acBlkTblRec.AppendEntity(acCircClone)

     acTrans.AddNewlyCreatedDBObject(acCircClone, True)

 

      '' Save the new object to the database

      acTrans.Commit()

  End Using

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.ApplicationServices;

usingAutodesk.AutoCAD.DatabaseServices;

usingAutodesk.AutoCAD.Geometry;

 

[CommandMethod("SingleCopy")]

public static voidSingleCopy()

{

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

 

      // Create a copy of the circle and changeits radius

      // 創建圓的拷貝,修改拷貝的半徑

      Circle acCircClone = acCirc.Clone() asCircle;

      acCircClone.Radius = 1;

 

      // Add the cloned circle將拷貝的圓添加到塊表記錄和事務

      acBlkTblRec.AppendEntity(acCircClone);

     acTrans.AddNewlyCreatedDBObject(acCircClone, true);

 

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

      acTrans.Commit();

  }

}

VBA/ActiveX Code Reference

Sub SingleCopy()

    ' Define the Circle object

    Dim centerPoint(0 To 2) As Double

    centerPoint(0) = 2: centerPoint(1) = 3:centerPoint(2) = 0

 

    ' Define the radius for the initial circle

    Dim radius As Double

    radius = 4.25

 

    ' Define the radius for the copied circle

    Dim radiusCopy As Double

    radiusCopy = 1#

 

    ' Add the new circle to model space

    Dim circleObj As AcadCircle

    Set circleObj = ThisDrawing.ModelSpace.AddCircle(centerPoint,radius)

 

    ' Create a copy of the circle

    Dim circleObjCopy As AcadCircle

    Set circleObjCopy = circleObj.Copy()

    circleObjCopy.radius = radiusCopy

End Sub


Copy multiple objects 複製多個對象

The following example uses an ObjectIdCollectionobject to track the objects that should be copied. Once the object ids areadded to the collection, the collection is iterated and new objects are createdby using the Clone method and then are added to Model space.

下例使用ObjectIdCollection對象來跟蹤要複製的對象。將對象的objectid添加到集合後,遍歷該集合,然後用Clone方法創建新對象並添加到模型空間。

VB.NET

Imports Autodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

ImportsAutodesk.AutoCAD.Geometry

 

<CommandMethod("MultipleCopy")>_

Public SubMultipleCopy()

  '' 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 (0,0,0)with a radius of 5

      Dim acCirc1 As Circle = New Circle()

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

      acCirc1.Radius = 5

 

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

      acBlkTblRec.AppendEntity(acCirc1)

      acTrans.AddNewlyCreatedDBObject(acCirc1,True)

 

      '' Create a circle that is at (0,0,0)with a radius of 7

      Dim acCirc2 As Circle = New Circle()

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

      acCirc2.Radius = 7

 

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

      acBlkTblRec.AppendEntity(acCirc2)

      acTrans.AddNewlyCreatedDBObject(acCirc2,True)

 

      '' Add all the objects to clone

      Dim acDBObjColl As DBObjectCollection =New DBObjectCollection()

      acDBObjColl.Add(acCirc1)

      acDBObjColl.Add(acCirc2)

 

      For Each acEnt As Entity In acDBObjColl

          Dim acEntClone As Entity

          acEntClone = acEnt.Clone()

          acEntClone.ColorIndex = 1

 

          '' Create a matrix and move eachcopied entity 15 units

         acEntClone.TransformBy(Matrix3d.Displacement(New Vector3d(15, 0, 0)))

 

          '' Add the cloned object

          acBlkTblRec.AppendEntity(acEntClone)

         acTrans.AddNewlyCreatedDBObject(acEntClone, True)

      Next

 

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

public static voidMultipleCopy()

{

  // 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 (0,0,0)with a radius of 5

      // 創建圓,圓心0,0,0半徑5

      Circle acCirc1 = new Circle();

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

      acCirc1.Radius = 5;

 

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

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

      acBlkTblRec.AppendEntity(acCirc1);

      acTrans.AddNewlyCreatedDBObject(acCirc1,true);

 

      // Create a circle that is at (0,0,0)with a radius of 7

      // 創建圓,圓心0,0,0半徑7

      Circle acCirc2 = new Circle();

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

      acCirc2.Radius = 7;

 

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

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

      acBlkTblRec.AppendEntity(acCirc2);

      acTrans.AddNewlyCreatedDBObject(acCirc2,true);

 

      // Add all the objects to clone添加所有對象到集合

      DBObjectCollection acDBObjColl = newDBObjectCollection();

      acDBObjColl.Add(acCirc1);

      acDBObjColl.Add(acCirc2);

 

      foreach (Entity acEnt in acDBObjColl)

      {

          Entity acEntClone;

          acEntClone = acEnt.Clone() as Entity;

          acEntClone.ColorIndex = 1;

 

          // Create a matrix and move eachcopied entity 15 units

          // 創建一個變換矩陣,移動每個複本實體15個單位

         acEntClone.TransformBy(Matrix3d.Displacement(new Vector3d(15, 0, 0)));

 

          // Add the cloned object

          // 將克隆對象添加到塊表記錄和事務

          acBlkTblRec.AppendEntity(acEntClone);

         acTrans.AddNewlyCreatedDBObject(acEntClone, true);

      }

 

      // Save the new object to the database

      // 保存新對象到數據庫

      acTrans.Commit();

  }

}

VBA/ActiveX Code Reference

Sub MultipleCopy()

    ' Define the Circle object

    Dim centerPoint(0 To 2) As Double

    centerPoint(0) = 0: centerPoint(1) = 0:centerPoint(2) = 0

 

    Dim radius1 As Double, radius2 As Double

    radius1 = 5#: radius2 = 7#

 

    ' Add two circles to the current drawing

    Dim circleObj1 As AcadCircle, circleObj2 AsAcadCircle

    Set circleObj1 =ThisDrawing.ModelSpace.AddCircle _

                                           (centerPoint, radius1)

 

    Set circleObj2 = ThisDrawing.ModelSpace.AddCircle_

                                           (centerPoint, radius2)

 

    ' First put the objects to be copied into aform compatible

    ' with CopyObjects

    Dim objCollection(0 To 1) As Object

    Set objCollection(0) = circleObj1

    SetobjCollection(1) = circleObj2

 

    ' Copy the objects into the model space. A

    ' collection of the new (copied) objects isreturned.

    Dim retObjects As Variant

    retObjects =ThisDrawing.CopyObjects(objCollection)

 

    Dim ptFrom(0 To 2) As Double

    ptFrom(0) = 0: ptFrom(1) = 0: ptFrom(2) = 0

 

    Dim ptTo(0 To 2) As Double

    ptTo(0) = 15: ptTo(1) = 0: ptTo(2) = 0

 

    Dim nCount As Integer

    For nCount = 0 To UBound(retObjects)

         Dim entObj As AcadEntity

         Set entObj = retObjects(nCount)

 

         entObj.color = acRed

         entObj.Move ptFrom, ptTo

    Next

End Sub

 

3.2、CopyObjects between Databases在數據庫間複製對象

You can copy objects between twodatabases. The Clone function is used to copy objects within the same database,while the WblockCloneObjects method is used to copy objects from onedatabase to another. The WblockCloneObjects method is a member of the Databaseobject. The WblockCloneObjects method requires the following parameters:

可以在兩個數據庫之間複製對象。Clone方法用於在一個數據庫內複製對象,而WblockCloneObjects方法用於從一個數據庫負責制對象到另一個數據庫。WblockCloneObjects方法是Database對象的成員。WblockCloneObjects方法需要下列參數:

·        ObjectIdCollection - List of objects to be cloned. 要複製的對象列表;

·        ObjectId - ObjectId of the new parent object forthe objects being cloned. 容納複本的新父對象的objectid;

·        IdMapping - Data structure of the current and newObjectIds for the objects being cloned. 要複製對象的當前objectid和新objectid的數據結構;

·        DuplicateRecordCloning - Determines how duplicate objects shouldbe handled. 定義出現相同對象時怎樣處理;

·        Defer Translation - Controls if object ids should betranslated. 控制是否需要翻譯objectid;

Copy an object from one database toanother 從一個數據庫向另一個複製對象

This example creates two Circle objects,then uses the WblockCloneObjects method to copy the circles into a newdrawing. The example also creates a new drawing using theacad.dwt filebefore the circles are copied.

本例先創建兩個圓,然後使用WblockCloneObjects方法複製新圖形中。在複製圓之前,本例還演示了用acad.dwt創建新圖形。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

ImportsAutodesk.AutoCAD.Geometry

 

<CommandMethod("CopyObjectsBetweenDatabases",CommandFlags.Session)> _

Public SubCopyObjectsBetweenDatabases()

  Dim acObjIdColl As ObjectIdCollection = NewObjectIdCollection()

 

  '' Get the current document and database

  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument

  Dim acCurDb As Database = acDoc.Database

 

  '' Lock the current document

  Using acLckDocCur As DocumentLock =acDoc.LockDocument()

 

      '' 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 (0,0,0)with a radius of 5

          Dim acCirc1 As Circle = New Circle()

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

          acCirc1.Radius = 5

 

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

          acBlkTblRec.AppendEntity(acCirc1)

          acTrans.AddNewlyCreatedDBObject(acCirc1,True)

 

          '' Create a circle that is at (0,0,0)with a radius of 7

          Dim acCirc2 As Circle = New Circle()

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

          acCirc2.Radius = 7

 

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

          acBlkTblRec.AppendEntity(acCirc2)

         acTrans.AddNewlyCreatedDBObject(acCirc2, True)

 

          '' Add all the objects to copy to thenew document

          acObjIdColl = New ObjectIdCollection()

          acObjIdColl.Add(acCirc1.ObjectId)

          acObjIdColl.Add(acCirc2.ObjectId)

 

          '' Save the new objects to thedatabase

          acTrans.Commit()

      End Using

 

      '' Unlock the document

  End Using

 

  '' Change the file and path to match adrawing template on your workstation

  Dim sLocalRoot As String =Application.GetSystemVariable("LOCALROOTPREFIX")

  Dim sTemplatePath As String = sLocalRoot +"Template\acad.dwt"

 

  '' Create a new drawing to copy the objectsto

  Dim acDocMgr As DocumentCollection =Application.DocumentManager

  Dim acNewDoc As Document =acDocMgr.Add(sTemplatePath)

  Dim acDbNewDoc As Database =acNewDoc.Database

 

  '' Lock the new document

  Using acLckDoc As DocumentLock = acNewDoc.LockDocument()

 

      '' Start a transaction in the newdatabase

      Using acTrans =acDbNewDoc.TransactionManager.StartTransaction()

 

          '' Open the Block table for read

          Dim acBlkTblNewDoc As BlockTable

          acBlkTblNewDoc = acTrans.GetObject(acDbNewDoc.BlockTableId,_

                                            OpenMode.ForRead)

 

          '' Open the Block table record Modelspace for read

          Dim acBlkTblRecNewDoc AsBlockTableRecord

          acBlkTblRecNewDoc = acTrans.GetObject(acBlkTblNewDoc(BlockTableRecord.ModelSpace),_

                                               OpenMode.ForRead)

 

          '' Clone the objects to the newdatabase

          Dim acIdMap As IdMapping = NewIdMapping()

          acCurDb.WblockCloneObjects(acObjIdColl,acBlkTblRecNewDoc.ObjectId, acIdMap, _

                                    DuplicateRecordCloning.Ignore, False)

 

          '' Save the copied objects to thedatabase

          acTrans.Commit()

      End Using

 

      '' Unlock the document

  End Using

 

  '' Set the new document current

  acDocMgr.MdiActiveDocument = acNewDoc

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

usingAutodesk.AutoCAD.ApplicationServices;

usingAutodesk.AutoCAD.DatabaseServices;

usingAutodesk.AutoCAD.Geometry;

 

[CommandMethod("CopyObjectsBetweenDatabases",CommandFlags.Session)]

public static voidCopyObjectsBetweenDatabases()

{

  ObjectIdCollection acObjIdColl = newObjectIdCollection();

 

  // Get the current document and database獲取當前文檔和數據庫

  Document acDoc = Application.DocumentManager.MdiActiveDocument;

  Database acCurDb = acDoc.Database;

 

  // Lock the current document鎖定當前文檔

  using (DocumentLock acLckDocCur =acDoc.LockDocument())

  {

      // Start a transaction啓動事務

      using (Transaction acTrans =acCurDb.TransactionManager.StartTransaction())

      {

          // Open the Block table record forread以讀打開塊表

          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 (0,0,0)with a radius of 5

          // 創建圓,圓心0,0,0半徑5

          Circle acCirc1 = new Circle();

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

          acCirc1.Radius = 5;

 

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

          // 添加到塊表記錄和事務

          acBlkTblRec.AppendEntity(acCirc1);

         acTrans.AddNewlyCreatedDBObject(acCirc1, true);

 

          // Create a circle that is at (0,0,0)with a radius of 7

          // 創建圓,圓心0,0,0半徑7

          Circle acCirc2 = new Circle();

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

          acCirc2.Radius = 7;

 

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

          // 添加到塊表記錄和事務

          acBlkTblRec.AppendEntity(acCirc2);

         acTrans.AddNewlyCreatedDBObject(acCirc2, true);

 

          // Add all the objects to copy to thenew document

          // 添加到要複製對象集合內

          acObjIdColl = newObjectIdCollection();

          acObjIdColl.Add(acCirc1.ObjectId);

          acObjIdColl.Add(acCirc2.ObjectId);

 

          // Save the new objects to thedatabase

          // 保存到數據庫

          acTrans.Commit();

      }

 

      // Unlock the document解鎖文檔

  }

 

  // Change the file and path to match adrawing template on your workstation

  // 獲取圖形模板路徑和文件

  string sLocalRoot =Application.GetSystemVariable("LOCALROOTPREFIX") as string;

  string sTemplatePath = sLocalRoot + "Template\\acad.dwt";

 

  // Create a new drawing to copy the objectsto

  // 新建一個圖形,我們將兩個圓複製到這個新圖形裏

  DocumentCollection acDocMgr =Application.DocumentManager;

  Document acNewDoc =acDocMgr.Add(sTemplatePath);

  Database acDbNewDoc = acNewDoc.Database;

 

  // Lock the new document鎖定新文檔

  using (DocumentLock acLckDoc =acNewDoc.LockDocument())

  {

      // Start a transaction in the newdatabase啓動新文檔的事務

      using (Transaction acTrans =acDbNewDoc.TransactionManager.StartTransaction())

      {

          // Open the Block table for read以讀打開塊表

          BlockTable acBlkTblNewDoc;

          acBlkTblNewDoc =acTrans.GetObject(acDbNewDoc.BlockTableId,

                                            OpenMode.ForRead) as BlockTable;

 

          // Open the Block table record Modelspace for read

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

          BlockTableRecord acBlkTblRecNewDoc;

          acBlkTblRecNewDoc =acTrans.GetObject(acBlkTblNewDoc[BlockTableRecord.ModelSpace],

                                             OpenMode.ForRead) as BlockTableRecord;

 

          // Clone the objects to the newdatabase

          // 克隆對象到新數據庫

          IdMapping acIdMap = new IdMapping();

         acCurDb.WblockCloneObjects(acObjIdColl, acBlkTblRecNewDoc.ObjectId,acIdMap,

                                    DuplicateRecordCloning.Ignore, false);

 

          // Save the copied objects to thedatabase

          // 保存複製的對象到數據庫

          acTrans.Commit();

      }

 

      // Unlock the document解鎖文檔

  }

 

  // Set the new document current將新文檔設置爲當前文檔

  acDocMgr.MdiActiveDocument = acNewDoc;

}

VBA/ActiveX Code Reference

SubCopyObjectsBetweenDatabases()

    Dim DOC0 As AcadDocument

    DimcircleObj1 As AcadCircle, circleObj2 As AcadCircle

    Dim centerPoint(0 To 2) As Double

    Dim radius1 As Double, radius2 As Double

    Dim objCollection(0 To 1) As Object

    Dim retObjects As Variant

 

    ' Define the Circle object

    centerPoint(0) = 0: centerPoint(1) = 0:centerPoint(2) = 0

    radius1 = 5#: radius2 = 7#

 

    ' Add two circles to the current drawing

    Set circleObj1 =ThisDrawing.ModelSpace.AddCircle _

                     (centerPoint, radius1)

    Set circleObj2 = ThisDrawing.ModelSpace.AddCircle_

                     (centerPoint, radius2)

 

    ' Save pointer to the current drawing

    Set DOC0 =ThisDrawing.Application.ActiveDocument

 

    ' Copy objects

    '

    ' First put the objects to be copied into aform compatible

    ' with CopyObjects

    Set objCollection(0) = circleObj1

    Set objCollection(1) = circleObj2

 

    ' Create a new drawing and point to itsmodel space

    Dim Doc1MSpace As AcadModelSpace

    Dim DOC1 As AcadDocument

 

    Set DOC1 = Documents.Add

    Set Doc1MSpace = DOC1.ModelSpace

 

    ' Copy the objects into the model space ofthe new drawing. A

    ' collection of the new (copied) objects isreturned.

    retObjects =DOC0.CopyObjects(objCollection, Doc1MSpace)

End Sub

 

 

4、OffsetObjects偏移對象

Offsetting an object creates a new objectat a specified offset distance from the original object. You can offset arcs,circles, ellipses, lines, lightweight polylines, polylines, splines, andxlines.

偏移對象就是在距原對象指定偏移距離處創建一個新對象。可以偏移圓弧、圓、橢圓、直線、輕量級多段線、多段線、樣條曲線以及構造線等。

To offset an object, use the GetOffsetCurves method provided for that object. Thefunction requires a positive or negative numeric value for the distance tooffset the object. If the distance is negative, it is interpreted by AutoCAD asbeing an offset to make a “smaller” curve (that is, for an arc it would offsetto a radius that is the given distance less than the starting curve's radius).If “smaller” has no meaning, then AutoCAD would offset in the direction ofsmallerX,Y,Z WCS coordinates.

要偏移一個對象,對該對象應用GetOffsetCurves方法即可。該方法需要一個表示偏移距離的正數或負數值。如果距離爲負值,AutoCAD理解爲偏移產生一個“更小”的曲線(對於圓弧來說,就是偏移產生的圓弧半徑比起始圓弧半徑短了給定偏移長度)。如果“更小”沒有意義,AutoCAD就向座標值減小的方向偏移。

For many objects, the result of thisoperation will be a single new curve (which may not be of the same type as theoriginal curve). For example, offsetting an ellipse will result in a splinebecause the result does fit the equation of an ellipse. In some cases it may benecessary for the offset result to be several curves. Because of this, thefunction returns a DBObjectCollection object, which contains all the objectsthat are created by offsetting the curve. The returned DBObjectCollectionobject needs to be iterated for each object created and then be appended to thedrawing database.

對於多數對象,偏移操作的結果就是一個新的曲線(可能與原對象的類型不同)。例如,偏移橢圓得到的將是一個樣條曲線,因爲結果確實滿足橢圓方程。有些情況下,偏移結果爲多個曲線是必要的。正因爲如此,GetOffsetCurves方法返回的是一個DBObjectCollection對象,其中包含有偏移曲線創建的所有對象。需要遍歷返回的DBObjectCollection對象得到創建的每個對象,然後逐個添加到圖形數據庫中。

For more information about offsettingobjects, see “Copy, Offset, or Mirror Objects” in theAutoCAD User's Guide.

關於偏移對象的更多內容,見AutoCAD用戶指南中“複製、偏移、鏡像對象”一節。

Offset a polyline 偏移多段線

This example creates a lightweightpolyline and then offsets it.

本例創建一個輕量級多段線然後對其進行偏移操作。

VB.NET

ImportsAutodesk.AutoCAD.Runtime

ImportsAutodesk.AutoCAD.ApplicationServices

ImportsAutodesk.AutoCAD.DatabaseServices

 

<CommandMethod("OffsetObject")>_

  Public Sub OffsetObject()

  '' 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 lightweight polyline

      Dim acPoly As Polyline = New Polyline()

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

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

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

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

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

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

 

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

      acBlkTblRec.AppendEntity(acPoly)

      acTrans.AddNewlyCreatedDBObject(acPoly,True)

 

      '' Offset the polyline a given distance

      Dim acDbObjColl As DBObjectCollection =acPoly.GetOffsetCurves(0.25)

 

      '' Step through the new objects created

      For Each acEnt As Entity In acDbObjColl

          '' Add each offset object

          acBlkTblRec.AppendEntity(acEnt)

         acTrans.AddNewlyCreatedDBObject(acEnt, True)

      Next

 

      '' Save the new objects to the database

      acTrans.Commit()

  End Using

End Sub

C#

usingAutodesk.AutoCAD.Runtime;

usingAutodesk.AutoCAD.ApplicationServices;

usingAutodesk.AutoCAD.DatabaseServices;

 

[CommandMethod("OffsetObject")]

public static voidOffsetObject()

{

  // 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 lightweight polyline創建多段線

      Polyline acPoly = new Polyline();

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

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

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

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

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

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

 

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

      acBlkTblRec.AppendEntity(acPoly);

      acTrans.AddNewlyCreatedDBObject(acPoly,true);

 

      // Offset the polyline a given distance偏移0.25距離

      DBObjectCollection acDbObjColl =acPoly.GetOffsetCurves(0.25);

 

      // Step through the new objects created

      foreach (Entity acEnt in acDbObjColl)

      {

          // Add each offset object

          acBlkTblRec.AppendEntity(acEnt);

         acTrans.AddNewlyCreatedDBObject(acEnt, true);

      }

 

      // Save the new objects to the database

      acTrans.Commit();

  }

}

VBA/ActiveX Code Reference

Sub OffsetObject()

    ' Create the polyline

    Dim plineObj As AcadLWPolyline

    Dim points(0 To 11) As Double

    points(0) = 1: points(1) = 1

    points(2) = 1: points(3) = 2

    points(4) = 2: points(5) = 2

    points(6) = 3: points(7) = 2

    points(8) = 4: points(9) = 4

    points(10) = 4: points(11) = 1

    Set plineObj = ThisDrawing.ModelSpace. _

                                   AddLightWeightPolyline(points)

    plineObj.Closed = True

    ZoomAll

 

    ' Offset the polyline

    Dim offsetObj As Variant

    offsetObj = plineObj.Offset(0.25)

 

    ZoomAll

End Sub

 

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