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, see TransformObjects.

我们可以为数据库中的大多数非图形对象和图形对象创建一个拷贝。创建对象拷贝用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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值