EaBIM一直以来积极响应国家“十二五”推进建筑业信息化的号召,对建筑领域的信息技术开展深入技术交流和探讨!致力于打造“BIM-建筑师-生态技术”三位一体综合资源交流共享平台,希望为BIM与可持续设计理念及技术的普及做出微小的贡献!!!

EaBIM

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
查看: 642|回复: 0
打印 上一主题 下一主题

[资料] 创建和编辑AutoCAD实体(四) 编辑二维命名对象 (2)

[复制链接]

1514

主题

7465

帖子

1万

积分

admin

Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10Rank: 10

积分
12408

社区QQ达人

跳转到指定楼层
楼主
发表于 2014-1-8 15:44:51 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
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.
下面示例新建一个圆,然后用直接复制该圆的方法创建第二个圆。

  1. VB.NET
  2. ImportsAutodesk.AutoCAD.Runtime
  3. ImportsAutodesk.AutoCAD.ApplicationServices
  4. ImportsAutodesk.AutoCAD.DatabaseServices
  5. ImportsAutodesk.AutoCAD.Geometry

  6. <CommandMethod("SingleCopy")>_
  7. Public SubSingleCopy()
  8.   '' Get the current document and database
  9.   Dim acDoc As Document =Application.DocumentManager.MdiActiveDocument
  10.   Dim acCurDb As Database = acDoc.Database

  11.   '' Start a transaction
  12.   Using acTrans As Transaction =acCurDb.TransactionManager.StartTransaction()

  13.       '' Open the Block table for read
  14.       Dim acBlkTbl As BlockTable
  15.       acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,OpenMode.ForRead)

  16.       '' Open the Block table record Modelspace for write
  17.       Dim acBlkTblRec As BlockTableRecord
  18.       acBlkTblRec =acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
  19.                                       OpenMode.ForWrite)

  20.       '' Create a circle that is at 2,3 with aradius of 4.25
  21.       Dim acCirc As Circle = New Circle()
  22.       acCirc.Center = New Point3d(2, 3, 0)
  23.       acCirc.Radius = 4.25

  24.       '' Add the new object to the block tablerecord and the transaction
  25.       acBlkTblRec.AppendEntity(acCirc)
  26.       acTrans.AddNewlyCreatedDBObject(acCirc,True)

  27.       '' Create a copy of the circle and changeits radius
  28.       Dim acCircClone As Circle =acCirc.Clone()
  29.       acCircClone.Radius = 1

  30.       '' Add the cloned circle
  31.       acBlkTblRec.AppendEntity(acCircClone)
  32.      acTrans.AddNewlyCreatedDBObject(acCircClone, True)

  33.       '' Save the new object to the database
  34.       acTrans.Commit()
  35.   End Using
  36. End Sub
复制代码
  1. <div align="left"><strong>C#</strong></div><div align="left">usingAutodesk.AutoCAD.Runtime;</div><div align="left">using Autodesk.AutoCAD.ApplicationServices;</div><div align="left">usingAutodesk.AutoCAD.DatabaseServices;</div><div align="left">usingAutodesk.AutoCAD.Geometry;</div>
  2. <div align="left">[CommandMethod("SingleCopy")]</div><div align="left">public static voidSingleCopy()</div><div align="left">{</div><div align="left">  // Get the current document and database获取当前文档和数据库</div><div align="left">  Document acDoc = Application.DocumentManager.MdiActiveDocument;</div><div align="left">  Database acCurDb = acDoc.Database;</div>
  3. <div align="left">  // Start a transaction启动事务</div><div align="left">  using (Transaction acTrans =acCurDb.TransactionManager.StartTransaction())</div><div align="left">  {</div><div align="left">      // Open the Block table for read以读打开块表</div><div align="left">      BlockTable acBlkTbl;</div><div align="left">      acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId,</div><div align="left">                                  OpenMode.ForRead) as BlockTable;</div>
  4. <div align="left">      // Open the Block table record Modelspace for write</div><div align="left">      //以写打开块表记录模型空间</div><div align="left">      BlockTableRecord acBlkTblRec;</div><div align="left">      acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],</div><div align="left">                                     OpenMode.ForWrite) as BlockTableRecord;</div>
  5. <div align="left">      // Create a circle that is at 2,3 with aradius of 4.25</div><div align="left">      // 创建圆,圆心2,3半径4.25</div><div align="left">      Circle acCirc = new Circle();</div><div align="left">      acCirc.Center = new Point3d(2, 3, 0);</div><div align="left">      acCirc.Radius = 4.25;</div>
  6. <div align="left">      // Add the new object to the block tablerecord and the transaction</div><div align="left">      // 将新对象添加到块表记录和事务</div><div align="left">      acBlkTblRec.AppendEntity(acCirc);</div><div align="left">      acTrans.AddNewlyCreatedDBObject(acCirc,true);</div>
  7. <div align="left">      // Create a copy of the circle and changeits radius</div><div align="left">      // 创建圆的拷贝,修改拷贝的半径</div><div align="left">      Circle acCircClone = acCirc.Clone() asCircle;</div><div align="left">      acCircClone.Radius = 1;</div>
  8. <div align="left">      // Add the cloned circle将拷贝的圆添加到块表记录和事务</div><div align="left">      acBlkTblRec.AppendEntity(acCircClone);</div><div align="left">     acTrans.AddNewlyCreatedDBObject(acCircClone, true);</div>
  9. <div align="left">      // Save the new object to the database保存新对象到数据库</div><div align="left">      acTrans.Commit();</div><div align="left">  }</div><div align="left">}</div>
复制代码
  1. VBA/ActiveX Code Reference
  2. Sub SingleCopy()
  3.     ' Define the Circle object
  4.     Dim centerPoint(0 To 2) As Double
  5.     centerPoint(0) = 2: centerPoint(1) = 3:centerPoint(2) = 0

  6.     ' Define the radius for the initial circle
  7.     Dim radius As Double
  8.     radius = 4.25

  9.     ' Define the radius for the copied circle
  10.     Dim radiusCopy As Double
  11.     radiusCopy = 1#

  12.     ' Add the new circle to model space
  13.     Dim circleObj As AcadCircle
  14.     Set circleObj = ThisDrawing.ModelSpace.AddCircle(centerPoint,radius)

  15.     ' Create a copy of the circle
  16.     Dim circleObjCopy As AcadCircle
  17.     Set circleObjCopy = circleObj.Copy()
  18.     circleObjCopy.radius = radiusCopy
  19. 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方法创建新对象并添加到模型空间。


  1. VB.NET
  2. Imports Autodesk.AutoCAD.Runtime
  3. ImportsAutodesk.AutoCAD.ApplicationServices
  4. ImportsAutodesk.AutoCAD.DatabaseServices
  5. ImportsAutodesk.AutoCAD.Geometry

  6. <CommandMethod("MultipleCopy")>_
  7. Public SubMultipleCopy()
  8.   '' Get the current document and database
  9.   Dim acDoc As Document =Application.DocumentManager.MdiActiveDocument
  10.   Dim acCurDb As Database = acDoc.Database

  11.   '' Start a transaction
  12.   Using acTrans As Transaction =acCurDb.TransactionManager.StartTransaction()

  13.       '' Open the Block table for read
  14.       Dim acBlkTbl As BlockTable
  15.       acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)

  16.       '' Open the Block table record Modelspace for write
  17.       Dim acBlkTblRec As BlockTableRecord
  18.       acBlkTblRec =acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
  19.                                      OpenMode.ForWrite)

  20.       '' Create a circle that is at (0,0,0)with a radius of 5
  21.       Dim acCirc1 As Circle = New Circle()
  22.       acCirc1.Center = New Point3d(0, 0, 0)
  23.       acCirc1.Radius = 5

  24.       '' Add the new object to the block tablerecord and the transaction
  25.       acBlkTblRec.AppendEntity(acCirc1)
  26.       acTrans.AddNewlyCreatedDBObject(acCirc1,True)

  27.       '' Create a circle that is at (0,0,0)with a radius of 7
  28.       Dim acCirc2 As Circle = New Circle()
  29.       acCirc2.Center = New Point3d(0, 0, 0)
  30.       acCirc2.Radius = 7

  31.       '' Add the new object to the block tablerecord and the transaction
  32.       acBlkTblRec.AppendEntity(acCirc2)
  33.       acTrans.AddNewlyCreatedDBObject(acCirc2,True)

  34.       '' Add all the objects to clone
  35.       Dim acDBObjColl As DBObjectCollection =New DBObjectCollection()
  36.       acDBObjColl.Add(acCirc1)
  37.       acDBObjColl.Add(acCirc2)

  38.       For Each acEnt As Entity In acDBObjColl
  39.           Dim acEntClone As Entity
  40.           acEntClone = acEnt.Clone()
  41.           acEntClone.ColorIndex = 1

  42.           '' Create a matrix and move eachcopied entity 15 units
  43.          acEntClone.TransformBy(Matrix3d.Displacement(New Vector3d(15, 0, 0)))

  44.           '' Add the cloned object
  45.           acBlkTblRec.AppendEntity(acEntClone)
  46.          acTrans.AddNewlyCreatedDBObject(acEntClone, True)
  47.       Next

  48.       '' Save the new object to the database
  49.       acTrans.Commit()
  50.   End Using
  51. End Sub
复制代码

  1. C#
  2. usingAutodesk.AutoCAD.Runtime;
  3. usingAutodesk.AutoCAD.ApplicationServices;
  4. usingAutodesk.AutoCAD.DatabaseServices;
  5. usingAutodesk.AutoCAD.Geometry;

  6. [CommandMethod("MultipleCopy")]
  7. public static voidMultipleCopy()
  8. {
  9.   // Get the current document and database获取当前文档和数据库
  10.   Document acDoc = Application.DocumentManager.MdiActiveDocument;
  11.   Database acCurDb = acDoc.Database;

  12.   // Start a transaction启动事务
  13.   using (Transaction acTrans =acCurDb.TransactionManager.StartTransaction())
  14.   {
  15.       // Open the Block table for read以读打开块表
  16.       BlockTable acBlkTbl;
  17.       acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId,
  18.                                   OpenMode.ForRead) as BlockTable;

  19.       // Open the Block table record Modelspace for write
  20.       // 以写打开块表记录模型空间
  21.       BlockTableRecord acBlkTblRec;
  22.       acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
  23.                                      OpenMode.ForWrite) as BlockTableRecord;

  24.       // Create a circle that is at (0,0,0)with a radius of 5
  25.       // 创建圆,圆心0,0,0半径5
  26.       Circle acCirc1 = new Circle();
  27.       acCirc1.Center = new Point3d(0, 0, 0);
  28.       acCirc1.Radius = 5;

  29.       // Add the new object to the block tablerecord and the transaction
  30.       // 将新对象添加到块表记录和事务
  31.       acBlkTblRec.AppendEntity(acCirc1);
  32.       acTrans.AddNewlyCreatedDBObject(acCirc1,true);

  33.       // Create a circle that is at (0,0,0)with a radius of 7
  34.       // 创建圆,圆心0,0,0半径7
  35.       Circle acCirc2 = new Circle();
  36.       acCirc2.Center = new Point3d(0, 0, 0);
  37.       acCirc2.Radius = 7;

  38.       // Add the new object to the block tablerecord and the transaction
  39.       // 将新对象添加到块表记录和事务
  40.       acBlkTblRec.AppendEntity(acCirc2);
  41.       acTrans.AddNewlyCreatedDBObject(acCirc2,true);

  42.       // Add all the objects to clone添加所有对象到集合
  43.       DBObjectCollection acDBObjColl = newDBObjectCollection();
  44.       acDBObjColl.Add(acCirc1);
  45.       acDBObjColl.Add(acCirc2);

  46.       foreach (Entity acEnt in acDBObjColl)
  47.       {
  48.           Entity acEntClone;
  49.           acEntClone = acEnt.Clone() as Entity;
  50.           acEntClone.ColorIndex = 1;

  51.           // Create a matrix and move eachcopied entity 15 units
  52.           // 创建一个变换矩阵,移动每个复本实体15个单位
  53.          acEntClone.TransformBy(Matrix3d.Displacement(new Vector3d(15, 0, 0)));

  54.           // Add the cloned object
  55.           // 将克隆对象添加到块表记录和事务
  56.           acBlkTblRec.AppendEntity(acEntClone);
  57.          acTrans.AddNewlyCreatedDBObject(acEntClone, true);
  58.       }

  59.       // Save the new object to the database
  60.       // 保存新对象到数据库
  61.       acTrans.Commit();
  62.   }
  63. }
  64. VBA/ActiveX Code Reference
  65. Sub MultipleCopy()
  66.     ' Define the Circle object
  67.     Dim centerPoint(0 To 2) As Double
  68.     centerPoint(0) = 0: centerPoint(1) = 0:centerPoint(2) = 0

  69.     Dim radius1 As Double, radius2 As Double
  70.     radius1 = 5#: radius2 = 7#

  71.     ' Add two circles to the current drawing
  72.     Dim circleObj1 As AcadCircle, circleObj2 AsAcadCircle
  73.     Set circleObj1 =ThisDrawing.ModelSpace.AddCircle _
  74.                                            (centerPoint, radius1)

  75.     Set circleObj2 = ThisDrawing.ModelSpace.AddCircle_
  76.                                            (centerPoint, radius2)

  77.     ' First put the objects to be copied into aform compatible
  78.     ' with CopyObjects
  79.     Dim objCollection(0 To 1) As Object
  80.     Set objCollection(0) = circleObj1
  81.     SetobjCollection(1) = circleObj2

  82.     ' Copy the objects into the model space. A
  83.     ' collection of the new (copied) objects isreturned.
  84.     Dim retObjects As Variant
  85.     retObjects =ThisDrawing.CopyObjects(objCollection)

  86.     Dim ptFrom(0 To 2) As Double
  87.     ptFrom(0) = 0: ptFrom(1) = 0: ptFrom(2) = 0

  88.     Dim ptTo(0 To 2) As Double
  89.     ptTo(0) = 15: ptTo(1) = 0: ptTo(2) = 0

  90.     Dim nCount As Integer
  91.     For nCount = 0 To UBound(retObjects)
  92.          Dim entObj As AcadEntity
  93.          Set entObj = retObjects(nCount)

  94.          entObj.color = acRed
  95.          entObj.Move ptFrom, ptTo
  96.     Next
  97. 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创建新图形。

  1. VB.NET
  2. ImportsAutodesk.AutoCAD.Runtime
  3. ImportsAutodesk.AutoCAD.ApplicationServices
  4. ImportsAutodesk.AutoCAD.DatabaseServices
  5. ImportsAutodesk.AutoCAD.Geometry

  6. <CommandMethod("CopyObjectsBetweenDatabases",CommandFlags.Session)> _
  7. Public SubCopyObjectsBetweenDatabases()
  8.   Dim acObjIdColl As ObjectIdCollection = NewObjectIdCollection()

  9.   '' Get the current document and database
  10.   Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  11.   Dim acCurDb As Database = acDoc.Database

  12.   '' Lock the current document
  13.   Using acLckDocCur As DocumentLock =acDoc.LockDocument()

  14.       '' Start a transaction
  15.       Using acTrans As Transaction =acCurDb.TransactionManager.StartTransaction()

  16.           '' Open the Block table for read
  17.           Dim acBlkTbl As BlockTable
  18.           acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)

  19.           '' Open the Block table record Modelspace for write
  20.           Dim acBlkTblRec As BlockTableRecord
  21.           acBlkTblRec =acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
  22.                                          OpenMode.ForWrite)

  23.           '' Create a circle that is at (0,0,0)with a radius of 5
  24.           Dim acCirc1 As Circle = New Circle()
  25.           acCirc1.Center = New Point3d(0, 0, 0)
  26.           acCirc1.Radius = 5

  27.           '' Add the new object to the blocktable record and the transaction
  28.           acBlkTblRec.AppendEntity(acCirc1)
  29.           acTrans.AddNewlyCreatedDBObject(acCirc1,True)

  30.           '' Create a circle that is at (0,0,0)with a radius of 7
  31.           Dim acCirc2 As Circle = New Circle()
  32.           acCirc2.Center = New Point3d(0, 0, 0)
  33.           acCirc2.Radius = 7

  34.           '' Add the new object to the blocktable record and the transaction
  35.           acBlkTblRec.AppendEntity(acCirc2)
  36.          acTrans.AddNewlyCreatedDBObject(acCirc2, True)

  37.           '' Add all the objects to copy to thenew document
  38.           acObjIdColl = New ObjectIdCollection()
  39.           acObjIdColl.Add(acCirc1.ObjectId)
  40.           acObjIdColl.Add(acCirc2.ObjectId)

  41.           '' Save the new objects to thedatabase
  42.           acTrans.Commit()
  43.       End Using

  44.       '' Unlock the document
  45.   End Using

  46.   '' Change the file and path to match adrawing template on your workstation
  47.   Dim sLocalRoot As String =Application.GetSystemVariable("LOCALROOTPREFIX")
  48.   Dim sTemplatePath As String = sLocalRoot +"Template\acad.dwt"

  49.   '' Create a new drawing to copy the objectsto
  50.   Dim acDocMgr As DocumentCollection =Application.DocumentManager
  51.   Dim acNewDoc As Document =acDocMgr.Add(sTemplatePath)
  52.   Dim acDbNewDoc As Database =acNewDoc.Database

  53.   '' Lock the new document
  54.   Using acLckDoc As DocumentLock = acNewDoc.LockDocument()

  55.       '' Start a transaction in the newdatabase
  56.       Using acTrans =acDbNewDoc.TransactionManager.StartTransaction()

  57.           '' Open the Block table for read
  58.           Dim acBlkTblNewDoc As BlockTable
  59.           acBlkTblNewDoc = acTrans.GetObject(acDbNewDoc.BlockTableId,_
  60.                                             OpenMode.ForRead)

  61.           '' Open the Block table record Modelspace for read
  62.           Dim acBlkTblRecNewDoc AsBlockTableRecord
  63.           acBlkTblRecNewDoc = acTrans.GetObject(acBlkTblNewDoc(BlockTableRecord.ModelSpace),_
  64.                                                OpenMode.ForRead)

  65.           '' Clone the objects to the newdatabase
  66.           Dim acIdMap As IdMapping = NewIdMapping()
  67.           acCurDb.WblockCloneObjects(acObjIdColl,acBlkTblRecNewDoc.ObjectId, acIdMap, _
  68.                                     DuplicateRecordCloning.Ignore, False)

  69.           '' Save the copied objects to thedatabase
  70.           acTrans.Commit()
  71.       End Using

  72.       '' Unlock the document
  73.   End Using

  74.   '' Set the new document current
  75.   acDocMgr.MdiActiveDocument = acNewDoc
  76. End Sub
复制代码


  1. <div align="left"><strong>C#</strong></div><div align="left">usingAutodesk.AutoCAD.Runtime;</div><div align="left">usingAutodesk.AutoCAD.ApplicationServices;</div><div align="left">usingAutodesk.AutoCAD.DatabaseServices;</div><div align="left">usingAutodesk.AutoCAD.Geometry;</div>
  2. <div align="left">[CommandMethod("CopyObjectsBetweenDatabases",CommandFlags.Session)]</div><div align="left">public static voidCopyObjectsBetweenDatabases()</div><div align="left">{</div><div align="left">  ObjectIdCollection acObjIdColl = newObjectIdCollection();</div>
  3. <div align="left">  // Get the current document and database获取当前文档和数据库</div><div align="left">  Document acDoc = Application.DocumentManager.MdiActiveDocument;</div><div align="left">  Database acCurDb = acDoc.Database;</div>
  4. <div align="left">  // Lock the current document锁定当前文档</div><div align="left">  using (DocumentLock acLckDocCur =acDoc.LockDocument())</div><div align="left">  {</div><div align="left">      // Start a transaction启动事务</div><div align="left">      using (Transaction acTrans =acCurDb.TransactionManager.StartTransaction())</div><div align="left">      {</div><div align="left">          // Open the Block table record forread以读打开块表</div><div align="left">          BlockTable acBlkTbl;</div><div align="left">          acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId,</div><div align="left">                                      OpenMode.ForRead) as BlockTable;</div>
  5. <div align="left">          // Open the Block table record Modelspace for write</div><div align="left">          // 以写打开块表记录模型空间</div><div align="left">          BlockTableRecord acBlkTblRec;</div><div align="left">          acBlkTblRec =acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],</div><div align="left">                                         OpenMode.ForWrite) as BlockTableRecord;</div>
  6. <div align="left">          // Create a circle that is at (0,0,0)with a radius of 5</div><div align="left">          // 创建圆,圆心0,0,0半径5</div><div align="left">          Circle acCirc1 = new Circle();</div><div align="left">          acCirc1.Center = new Point3d(0, 0,0);</div><div align="left">          acCirc1.Radius = 5;</div>
  7. <div align="left">          // Add the new object to the blocktable record and the transaction</div><div align="left">          // 添加到块表记录和事务</div><div align="left">          acBlkTblRec.AppendEntity(acCirc1);</div><div align="left">         acTrans.AddNewlyCreatedDBObject(acCirc1, true);</div>
  8. <div align="left">          // Create a circle that is at (0,0,0)with a radius of 7</div><div align="left">          // 创建圆,圆心0,0,0半径7</div><div align="left">          Circle acCirc2 = new Circle();</div><div align="left">          acCirc2.Center = new Point3d(0, 0,0);</div><div align="left">          acCirc2.Radius = 7;</div>
  9. <div align="left">          // Add the new object to the blocktable record and the transaction</div><div align="left">          // 添加到块表记录和事务</div><div align="left">          acBlkTblRec.AppendEntity(acCirc2);</div><div align="left">         acTrans.AddNewlyCreatedDBObject(acCirc2, true);</div>
  10. <div align="left">          // Add all the objects to copy to thenew document</div><div align="left">          // 添加到要复制对象集合内</div><div align="left">          acObjIdColl = newObjectIdCollection();</div><div align="left">          acObjIdColl.Add(acCirc1.ObjectId);</div><div align="left">          acObjIdColl.Add(acCirc2.ObjectId);</div>
  11. <div align="left">          // Save the new objects to thedatabase</div><div align="left">          // 保存到数据库</div><div align="left">          acTrans.Commit();</div><div align="left">      }</div>
  12. <div align="left">      // Unlock the document解锁文档</div><div align="left">  }</div>
  13. <div align="left">  // Change the file and path to match adrawing template on your workstation</div><div align="left">  // 获取图形模板路径和文件</div><div align="left">  string sLocalRoot =Application.GetSystemVariable("LOCALROOTPREFIX") as string;</div><div align="left">  string sTemplatePath = sLocalRoot + "Template\\acad.dwt";</div>
  14. <div align="left">  // Create a new drawing to copy the objectsto</div><div align="left">  // 新建一个图形,我们将两个圆复制到这个新图形里</div><div align="left">  DocumentCollection acDocMgr =Application.DocumentManager;</div><div align="left">  Document acNewDoc =acDocMgr.Add(sTemplatePath);</div><div align="left">  Database acDbNewDoc = acNewDoc.Database;</div>
  15. <div align="left">  // Lock the new document锁定新文档</div><div align="left">  using (DocumentLock acLckDoc =acNewDoc.LockDocument())</div><div align="left">  {</div><div align="left">      // Start a transaction in the newdatabase启动新文档的事务</div><div align="left">      using (Transaction acTrans =acDbNewDoc.TransactionManager.StartTransaction())</div><div align="left">      {</div><div align="left">          // Open the Block table for read以读打开块表</div><div align="left">          BlockTable acBlkTblNewDoc;</div><div align="left">          acBlkTblNewDoc =acTrans.GetObject(acDbNewDoc.BlockTableId,</div><div align="left">                                            OpenMode.ForRead) as BlockTable;</div>
  16. <div align="left">          // Open the Block table record Modelspace for read</div><div align="left">          // 以写打开块表记录模型空间</div><div align="left">          BlockTableRecord acBlkTblRecNewDoc;</div><div align="left">          acBlkTblRecNewDoc =acTrans.GetObject(acBlkTblNewDoc[BlockTableRecord.ModelSpace],</div><div align="left">                                             OpenMode.ForRead) as BlockTableRecord;</div>
  17. <div align="left">          // Clone the objects to the newdatabase</div><div align="left">          // 克隆对象到新数据库</div><div align="left">          IdMapping acIdMap = new IdMapping();</div><div align="left">         acCurDb.WblockCloneObjects(acObjIdColl, acBlkTblRecNewDoc.ObjectId,acIdMap,</div><div align="left">                                    DuplicateRecordCloning.Ignore, false);</div>
  18. <div align="left">          // Save the copied objects to thedatabase</div><div align="left">          // 保存复制的对象到数据库</div><div align="left">          acTrans.Commit();</div><div align="left">      }</div>
  19. <div align="left">      // Unlock the document解锁文档</div><div align="left">  }</div>
  20. <div align="left">  // Set the new document current将新文档设置为当前文档</div><div align="left">  acDocMgr.MdiActiveDocument = acNewDoc;</div><div align="left">}</div>
复制代码

  1. 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.
  2. 我们可以为数据库中的大多数非图形对象和图形对象创建一个拷贝。创建对象拷贝用Clone方法。对象一旦被复制,我们就可以对返回的对象进行修改,然后将其添加到数据库内。通过使用Clone方法和TransformBy方法,我们可以模仿AutoCAD的许多修改命令。关于TransformBy方法的更多内容,见“变换对象”一节。
  3. 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.
  4. 除了创建对象的直接拷贝外,我们还可以使用Clone方法和TransformBy方法来偏移对象、镜像对象及阵列对象。关于复制对象的更多内容,见AutoCAD用户指南中“复制、偏移或镜像对象”一节。
  5. Topics in this section本小节内容
  6. ·        Copy an Object 复制一个对象
  7. ·        Copy Objects between Databases 在数据库间复制对象

  8. 3.1、Copyan Object复制一个对象
  9. 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.
  10. 要复制一个对象,对该对象应用Clone方法即可。Clone方法创建原对象的一个复本新对象。复本对象创建后,我们可以在将其添加到数据库前对其修改。如果没有变换这个新对象也没有修改它的位置,那么新对象就与原对象在相同位置上。
  11. 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.
  12. 如果需要复制大量对象,可以将每个对象的objectid添加到ObjectIdCollection对象,然后遍历其中的每个对象。遍历每个对象时,就可以为每个对象调用Clone方法,然后将新对象添加或追加到数据库。
  13. Copy a single object 复制单个对象

  14. The following example creates a new circleand then creates a direct copy of the circle to create a second circle.
  15. 下面示例新建一个圆,然后用直接复制该圆的方法创建第二个圆。
  16. VB.NET
  17. ImportsAutodesk.AutoCAD.Runtime
  18. ImportsAutodesk.AutoCAD.ApplicationServices
  19. ImportsAutodesk.AutoCAD.DatabaseServices
  20. ImportsAutodesk.AutoCAD.Geometry

  21. <CommandMethod("SingleCopy")>_
  22. Public SubSingleCopy()
  23.   '' Get the current document and database
  24.   Dim acDoc As Document =Application.DocumentManager.MdiActiveDocument
  25.   Dim acCurDb As Database = acDoc.Database

  26.   '' Start a transaction
  27.   Using acTrans As Transaction =acCurDb.TransactionManager.StartTransaction()

  28.       '' Open the Block table for read
  29.       Dim acBlkTbl As BlockTable
  30.       acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,OpenMode.ForRead)

  31.       '' Open the Block table record Modelspace for write
  32.       Dim acBlkTblRec As BlockTableRecord
  33.       acBlkTblRec =acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
  34.                                       OpenMode.ForWrite)

  35.       '' Create a circle that is at 2,3 with aradius of 4.25
  36.       Dim acCirc As Circle = New Circle()
  37.       acCirc.Center = New Point3d(2, 3, 0)
  38.       acCirc.Radius = 4.25

  39.       '' Add the new object to the block tablerecord and the transaction
  40.       acBlkTblRec.AppendEntity(acCirc)
  41.       acTrans.AddNewlyCreatedDBObject(acCirc,True)

  42.       '' Create a copy of the circle and changeits radius
  43.       Dim acCircClone As Circle =acCirc.Clone()
  44.       acCircClone.Radius = 1

  45.       '' Add the cloned circle
  46.       acBlkTblRec.AppendEntity(acCircClone)
  47.      acTrans.AddNewlyCreatedDBObject(acCircClone, True)

  48.       '' Save the new object to the database
  49.       acTrans.Commit()
  50.   End Using
  51. End Sub
  52. C#
  53. usingAutodesk.AutoCAD.Runtime;
  54. using Autodesk.AutoCAD.ApplicationServices;
  55. usingAutodesk.AutoCAD.DatabaseServices;
  56. usingAutodesk.AutoCAD.Geometry;

  57. [CommandMethod("SingleCopy")]
  58. public static voidSingleCopy()
  59. {
  60.   // Get the current document and database获取当前文档和数据库
  61.   Document acDoc = Application.DocumentManager.MdiActiveDocument;
  62.   Database acCurDb = acDoc.Database;

  63.   // Start a transaction启动事务
  64.   using (Transaction acTrans =acCurDb.TransactionManager.StartTransaction())
  65.   {
  66.       // Open the Block table for read以读打开块表
  67.       BlockTable acBlkTbl;
  68.       acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId,
  69.                                   OpenMode.ForRead) as BlockTable;

  70.       // Open the Block table record Modelspace for write
  71.       //以写打开块表记录模型空间
  72.       BlockTableRecord acBlkTblRec;
  73.       acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
  74.                                      OpenMode.ForWrite) as BlockTableRecord;

  75.       // Create a circle that is at 2,3 with aradius of 4.25
  76.       // 创建圆,圆心2,3半径4.25
  77.       Circle acCirc = new Circle();
  78.       acCirc.Center = new Point3d(2, 3, 0);
  79.       acCirc.Radius = 4.25;

  80.       // Add the new object to the block tablerecord and the transaction
  81.       // 将新对象添加到块表记录和事务
  82.       acBlkTblRec.AppendEntity(acCirc);
  83.       acTrans.AddNewlyCreatedDBObject(acCirc,true);

  84.       // Create a copy of the circle and changeits radius
  85.       // 创建圆的拷贝,修改拷贝的半径
  86.       Circle acCircClone = acCirc.Clone() asCircle;
  87.       acCircClone.Radius = 1;

  88.       // Add the cloned circle将拷贝的圆添加到块表记录和事务
  89.       acBlkTblRec.AppendEntity(acCircClone);
  90.      acTrans.AddNewlyCreatedDBObject(acCircClone, true);

  91.       // Save the new object to the database保存新对象到数据库
  92.       acTrans.Commit();
  93.   }
  94. }
  95. VBA/ActiveX Code Reference
  96. Sub SingleCopy()
  97.     ' Define the Circle object
  98.     Dim centerPoint(0 To 2) As Double
  99.     centerPoint(0) = 2: centerPoint(1) = 3:centerPoint(2) = 0

  100.     ' Define the radius for the initial circle
  101.     Dim radius As Double
  102.     radius = 4.25

  103.     ' Define the radius for the copied circle
  104.     Dim radiusCopy As Double
  105.     radiusCopy = 1#

  106.     ' Add the new circle to model space
  107.     Dim circleObj As AcadCircle
  108.     Set circleObj = ThisDrawing.ModelSpace.AddCircle(centerPoint,radius)

  109.     ' Create a copy of the circle
  110.     Dim circleObjCopy As AcadCircle
  111.     Set circleObjCopy = circleObj.Copy()
  112.     circleObjCopy.radius = radiusCopy
  113. End Sub

  114. Copy multiple objects 复制多个对象

  115. 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.
  116. 下例使用ObjectIdCollection对象来跟踪要复制的对象。将对象的objectid添加到集合后,遍历该集合,然后用Clone方法创建新对象并添加到模型空间。
  117. VB.NET
  118. Imports Autodesk.AutoCAD.Runtime
  119. ImportsAutodesk.AutoCAD.ApplicationServices
  120. ImportsAutodesk.AutoCAD.DatabaseServices
  121. ImportsAutodesk.AutoCAD.Geometry

  122. <CommandMethod("MultipleCopy")>_
  123. Public SubMultipleCopy()
  124.   '' Get the current document and database
  125.   Dim acDoc As Document =Application.DocumentManager.MdiActiveDocument
  126.   Dim acCurDb As Database = acDoc.Database

  127.   '' Start a transaction
  128.   Using acTrans As Transaction =acCurDb.TransactionManager.StartTransaction()

  129.       '' Open the Block table for read
  130.       Dim acBlkTbl As BlockTable
  131.       acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)

  132.       '' Open the Block table record Modelspace for write
  133.       Dim acBlkTblRec As BlockTableRecord
  134.       acBlkTblRec =acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
  135.                                      OpenMode.ForWrite)

  136.       '' Create a circle that is at (0,0,0)with a radius of 5
  137.       Dim acCirc1 As Circle = New Circle()
  138.       acCirc1.Center = New Point3d(0, 0, 0)
  139.       acCirc1.Radius = 5

  140.       '' Add the new object to the block tablerecord and the transaction
  141.       acBlkTblRec.AppendEntity(acCirc1)
  142.       acTrans.AddNewlyCreatedDBObject(acCirc1,True)

  143.       '' Create a circle that is at (0,0,0)with a radius of 7
  144.       Dim acCirc2 As Circle = New Circle()
  145.       acCirc2.Center = New Point3d(0, 0, 0)
  146.       acCirc2.Radius = 7

  147.       '' Add the new object to the block tablerecord and the transaction
  148.       acBlkTblRec.AppendEntity(acCirc2)
  149.       acTrans.AddNewlyCreatedDBObject(acCirc2,True)

  150.       '' Add all the objects to clone
  151.       Dim acDBObjColl As DBObjectCollection =New DBObjectCollection()
  152.       acDBObjColl.Add(acCirc1)
  153.       acDBObjColl.Add(acCirc2)

  154.       For Each acEnt As Entity In acDBObjColl
  155.           Dim acEntClone As Entity
  156.           acEntClone = acEnt.Clone()
  157.           acEntClone.ColorIndex = 1

  158.           '' Create a matrix and move eachcopied entity 15 units
  159.          acEntClone.TransformBy(Matrix3d.Displacement(New Vector3d(15, 0, 0)))

  160.           '' Add the cloned object
  161.           acBlkTblRec.AppendEntity(acEntClone)
  162.          acTrans.AddNewlyCreatedDBObject(acEntClone, True)
  163.       Next

  164.       '' Save the new object to the database
  165.       acTrans.Commit()
  166.   End Using
  167. End Sub
  168. C#
  169. usingAutodesk.AutoCAD.Runtime;
  170. usingAutodesk.AutoCAD.ApplicationServices;
  171. usingAutodesk.AutoCAD.DatabaseServices;
  172. usingAutodesk.AutoCAD.Geometry;

  173. [CommandMethod("MultipleCopy")]
  174. public static voidMultipleCopy()
  175. {
  176.   // Get the current document and database获取当前文档和数据库
  177.   Document acDoc = Application.DocumentManager.MdiActiveDocument;
  178.   Database acCurDb = acDoc.Database;

  179.   // Start a transaction启动事务
  180.   using (Transaction acTrans =acCurDb.TransactionManager.StartTransaction())
  181.   {
  182.       // Open the Block table for read以读打开块表
  183.       BlockTable acBlkTbl;
  184.       acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId,
  185.                                   OpenMode.ForRead) as BlockTable;

  186.       // Open the Block table record Modelspace for write
  187.       // 以写打开块表记录模型空间
  188.       BlockTableRecord acBlkTblRec;
  189.       acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
  190.                                      OpenMode.ForWrite) as BlockTableRecord;

  191.       // Create a circle that is at (0,0,0)with a radius of 5
  192.       // 创建圆,圆心0,0,0半径5
  193.       Circle acCirc1 = new Circle();
  194.       acCirc1.Center = new Point3d(0, 0, 0);
  195.       acCirc1.Radius = 5;

  196.       // Add the new object to the block tablerecord and the transaction
  197.       // 将新对象添加到块表记录和事务
  198.       acBlkTblRec.AppendEntity(acCirc1);
  199.       acTrans.AddNewlyCreatedDBObject(acCirc1,true);

  200.       // Create a circle that is at (0,0,0)with a radius of 7
  201.       // 创建圆,圆心0,0,0半径7
  202.       Circle acCirc2 = new Circle();
  203.       acCirc2.Center = new Point3d(0, 0, 0);
  204.       acCirc2.Radius = 7;

  205.       // Add the new object to the block tablerecord and the transaction
  206.       // 将新对象添加到块表记录和事务
  207.       acBlkTblRec.AppendEntity(acCirc2);
  208.       acTrans.AddNewlyCreatedDBObject(acCirc2,true);

  209.       // Add all the objects to clone添加所有对象到集合
  210.       DBObjectCollection acDBObjColl = newDBObjectCollection();
  211.       acDBObjColl.Add(acCirc1);
  212.       acDBObjColl.Add(acCirc2);

  213.       foreach (Entity acEnt in acDBObjColl)
  214.       {
  215.           Entity acEntClone;
  216.           acEntClone = acEnt.Clone() as Entity;
  217.           acEntClone.ColorIndex = 1;

  218.           // Create a matrix and move eachcopied entity 15 units
  219.           // 创建一个变换矩阵,移动每个复本实体15个单位
  220.          acEntClone.TransformBy(Matrix3d.Displacement(new Vector3d(15, 0, 0)));

  221.           // Add the cloned object
  222.           // 将克隆对象添加到块表记录和事务
  223.           acBlkTblRec.AppendEntity(acEntClone);
  224.          acTrans.AddNewlyCreatedDBObject(acEntClone, true);
  225.       }

  226.       // Save the new object to the database
  227.       // 保存新对象到数据库
  228.       acTrans.Commit();
  229.   }
  230. }
  231. VBA/ActiveX Code Reference
  232. Sub MultipleCopy()
  233.     ' Define the Circle object
  234.     Dim centerPoint(0 To 2) As Double
  235.     centerPoint(0) = 0: centerPoint(1) = 0:centerPoint(2) = 0

  236.     Dim radius1 As Double, radius2 As Double
  237.     radius1 = 5#: radius2 = 7#

  238.     ' Add two circles to the current drawing
  239.     Dim circleObj1 As AcadCircle, circleObj2 AsAcadCircle
  240.     Set circleObj1 =ThisDrawing.ModelSpace.AddCircle _
  241.                                            (centerPoint, radius1)

  242.     Set circleObj2 = ThisDrawing.ModelSpace.AddCircle_
  243.                                            (centerPoint, radius2)

  244.     ' First put the objects to be copied into aform compatible
  245.     ' with CopyObjects
  246.     Dim objCollection(0 To 1) As Object
  247.     Set objCollection(0) = circleObj1
  248.     SetobjCollection(1) = circleObj2

  249.     ' Copy the objects into the model space. A
  250.     ' collection of the new (copied) objects isreturned.
  251.     Dim retObjects As Variant
  252.     retObjects =ThisDrawing.CopyObjects(objCollection)

  253.     Dim ptFrom(0 To 2) As Double
  254.     ptFrom(0) = 0: ptFrom(1) = 0: ptFrom(2) = 0

  255.     Dim ptTo(0 To 2) As Double
  256.     ptTo(0) = 15: ptTo(1) = 0: ptTo(2) = 0

  257.     Dim nCount As Integer
  258.     For nCount = 0 To UBound(retObjects)
  259.          Dim entObj As AcadEntity
  260.          Set entObj = retObjects(nCount)

  261.          entObj.color = acRed
  262.          entObj.Move ptFrom, ptTo
  263.     Next
  264. End Sub

  265. 3.2、CopyObjects between Databases在数据库间复制对象
  266. 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:
  267. 可以在两个数据库之间复制对象。Clone方法用于在一个数据库内复制对象,而WblockCloneObjects方法用于从一个数据库负责制对象到另一个数据库。WblockCloneObjects方法是Database对象的成员。WblockCloneObjects方法需要下列参数:
  268. ·        ObjectIdCollection - List of objects to be cloned. 要复制的对象列表;
  269. ·        ObjectId - ObjectId of the new parent object forthe objects being cloned. 容纳复本的新父对象的objectid;
  270. ·        IdMapping - Data structure of the current and newObjectIds for the objects being cloned. 要复制对象的当前objectid和新objectid的数据结构;
  271. ·        DuplicateRecordCloning - Determines how duplicate objects shouldbe handled. 定义出现相同对象时怎样处理;
  272. ·        Defer Translation - Controls if object ids should betranslated. 控制是否需要翻译objectid;
  273. Copy an object from one database toanother 从一个数据库向另一个复制对象

  274. 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.
  275. 本例先创建两个圆,然后使用WblockCloneObjects方法复制新图形中。在复制圆之前,本例还演示了用acad.dwt创建新图形。
  276. VB.NET
  277. ImportsAutodesk.AutoCAD.Runtime
  278. ImportsAutodesk.AutoCAD.ApplicationServices
  279. ImportsAutodesk.AutoCAD.DatabaseServices
  280. ImportsAutodesk.AutoCAD.Geometry

  281. <CommandMethod("CopyObjectsBetweenDatabases",CommandFlags.Session)> _
  282. Public SubCopyObjectsBetweenDatabases()
  283.   Dim acObjIdColl As ObjectIdCollection = NewObjectIdCollection()

  284.   '' Get the current document and database
  285.   Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  286.   Dim acCurDb As Database = acDoc.Database

  287.   '' Lock the current document
  288.   Using acLckDocCur As DocumentLock =acDoc.LockDocument()

  289.       '' Start a transaction
  290.       Using acTrans As Transaction =acCurDb.TransactionManager.StartTransaction()

  291.           '' Open the Block table for read
  292.           Dim acBlkTbl As BlockTable
  293.           acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)

  294.           '' Open the Block table record Modelspace for write
  295.           Dim acBlkTblRec As BlockTableRecord
  296.           acBlkTblRec =acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
  297.                                          OpenMode.ForWrite)

  298.           '' Create a circle that is at (0,0,0)with a radius of 5
  299.           Dim acCirc1 As Circle = New Circle()
  300.           acCirc1.Center = New Point3d(0, 0, 0)
  301.           acCirc1.Radius = 5

  302.           '' Add the new object to the blocktable record and the transaction
  303.           acBlkTblRec.AppendEntity(acCirc1)
  304.           acTrans.AddNewlyCreatedDBObject(acCirc1,True)

  305.           '' Create a circle that is at (0,0,0)with a radius of 7
  306.           Dim acCirc2 As Circle = New Circle()
  307.           acCirc2.Center = New Point3d(0, 0, 0)
  308.           acCirc2.Radius = 7

  309.           '' Add the new object to the blocktable record and the transaction
  310.           acBlkTblRec.AppendEntity(acCirc2)
  311.          acTrans.AddNewlyCreatedDBObject(acCirc2, True)

  312.           '' Add all the objects to copy to thenew document
  313.           acObjIdColl = New ObjectIdCollection()
  314.           acObjIdColl.Add(acCirc1.ObjectId)
  315.           acObjIdColl.Add(acCirc2.ObjectId)

  316.           '' Save the new objects to thedatabase
  317.           acTrans.Commit()
  318.       End Using

  319.       '' Unlock the document
  320.   End Using

  321.   '' Change the file and path to match adrawing template on your workstation
  322.   Dim sLocalRoot As String =Application.GetSystemVariable("LOCALROOTPREFIX")
  323.   Dim sTemplatePath As String = sLocalRoot +"Template\acad.dwt"

  324.   '' Create a new drawing to copy the objectsto
  325.   Dim acDocMgr As DocumentCollection =Application.DocumentManager
  326.   Dim acNewDoc As Document =acDocMgr.Add(sTemplatePath)
  327.   Dim acDbNewDoc As Database =acNewDoc.Database

  328.   '' Lock the new document
  329.   Using acLckDoc As DocumentLock = acNewDoc.LockDocument()

  330.       '' Start a transaction in the newdatabase
  331.       Using acTrans =acDbNewDoc.TransactionManager.StartTransaction()

  332.           '' Open the Block table for read
  333.           Dim acBlkTblNewDoc As BlockTable
  334.           acBlkTblNewDoc = acTrans.GetObject(acDbNewDoc.BlockTableId,_
  335.                                             OpenMode.ForRead)

  336.           '' Open the Block table record Modelspace for read
  337.           Dim acBlkTblRecNewDoc AsBlockTableRecord
  338.           acBlkTblRecNewDoc = acTrans.GetObject(acBlkTblNewDoc(BlockTableRecord.ModelSpace),_
  339.                                                OpenMode.ForRead)

  340.           '' Clone the objects to the newdatabase
  341.           Dim acIdMap As IdMapping = NewIdMapping()
  342.           acCurDb.WblockCloneObjects(acObjIdColl,acBlkTblRecNewDoc.ObjectId, acIdMap, _
  343.                                     DuplicateRecordCloning.Ignore, False)

  344.           '' Save the copied objects to thedatabase
  345.           acTrans.Commit()
  346.       End Using

  347.       '' Unlock the document
  348.   End Using

  349.   '' Set the new document current
  350.   acDocMgr.MdiActiveDocument = acNewDoc
  351. End Sub
  352. C#
  353. usingAutodesk.AutoCAD.Runtime;
  354. usingAutodesk.AutoCAD.ApplicationServices;
  355. usingAutodesk.AutoCAD.DatabaseServices;
  356. usingAutodesk.AutoCAD.Geometry;

  357. [CommandMethod("CopyObjectsBetweenDatabases",CommandFlags.Session)]
  358. public static voidCopyObjectsBetweenDatabases()
  359. {
  360.   ObjectIdCollection acObjIdColl = newObjectIdCollection();

  361.   // Get the current document and database获取当前文档和数据库
  362.   Document acDoc = Application.DocumentManager.MdiActiveDocument;
  363.   Database acCurDb = acDoc.Database;

  364.   // Lock the current document锁定当前文档
  365.   using (DocumentLock acLckDocCur =acDoc.LockDocument())
  366.   {
  367.       // Start a transaction启动事务
  368.       using (Transaction acTrans =acCurDb.TransactionManager.StartTransaction())
  369.       {
  370.           // Open the Block table record forread以读打开块表
  371.           BlockTable acBlkTbl;
  372.           acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId,
  373.                                       OpenMode.ForRead) as BlockTable;

  374.           // Open the Block table record Modelspace for write
  375.           // 以写打开块表记录模型空间
  376.           BlockTableRecord acBlkTblRec;
  377.           acBlkTblRec =acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
  378.                                          OpenMode.ForWrite) as BlockTableRecord;

  379.           // Create a circle that is at (0,0,0)with a radius of 5
  380.           // 创建圆,圆心0,0,0半径5
  381.           Circle acCirc1 = new Circle();
  382.           acCirc1.Center = new Point3d(0, 0,0);
  383.           acCirc1.Radius = 5;

  384.           // Add the new object to the blocktable record and the transaction
  385.           // 添加到块表记录和事务
  386.           acBlkTblRec.AppendEntity(acCirc1);
  387.          acTrans.AddNewlyCreatedDBObject(acCirc1, true);

  388.           // Create a circle that is at (0,0,0)with a radius of 7
  389.           // 创建圆,圆心0,0,0半径7
  390.           Circle acCirc2 = new Circle();
  391.           acCirc2.Center = new Point3d(0, 0,0);
  392.           acCirc2.Radius = 7;

  393.           // Add the new object to the blocktable record and the transaction
  394.           // 添加到块表记录和事务
  395.           acBlkTblRec.AppendEntity(acCirc2);
  396.          acTrans.AddNewlyCreatedDBObject(acCirc2, true);

  397.           // Add all the objects to copy to thenew document
  398.           // 添加到要复制对象集合内
  399.           acObjIdColl = newObjectIdCollection();
  400.           acObjIdColl.Add(acCirc1.ObjectId);
  401.           acObjIdColl.Add(acCirc2.ObjectId);

  402.           // Save the new objects to thedatabase
  403.           // 保存到数据库
  404.           acTrans.Commit();
  405.       }

  406.       // Unlock the document解锁文档
  407.   }

  408.   // Change the file and path to match adrawing template on your workstation
  409.   // 获取图形模板路径和文件
  410.   string sLocalRoot =Application.GetSystemVariable("LOCALROOTPREFIX") as string;
  411.   string sTemplatePath = sLocalRoot + "Template\\acad.dwt";

  412.   // Create a new drawing to copy the objectsto
  413.   // 新建一个图形,我们将两个圆复制到这个新图形里
  414.   DocumentCollection acDocMgr =Application.DocumentManager;
  415.   Document acNewDoc =acDocMgr.Add(sTemplatePath);
  416.   Database acDbNewDoc = acNewDoc.Database;

  417.   // Lock the new document锁定新文档
  418.   using (DocumentLock acLckDoc =acNewDoc.LockDocument())
  419.   {
  420.       // Start a transaction in the newdatabase启动新文档的事务
  421.       using (Transaction acTrans =acDbNewDoc.TransactionManager.StartTransaction())
  422.       {
  423.           // Open the Block table for read以读打开块表
  424.           BlockTable acBlkTblNewDoc;
  425.           acBlkTblNewDoc =acTrans.GetObject(acDbNewDoc.BlockTableId,
  426.                                             OpenMode.ForRead) as BlockTable;

  427.           // Open the Block table record Modelspace for read
  428.           // 以写打开块表记录模型空间
  429.           BlockTableRecord acBlkTblRecNewDoc;
  430.           acBlkTblRecNewDoc =acTrans.GetObject(acBlkTblNewDoc[BlockTableRecord.ModelSpace],
  431.                                              OpenMode.ForRead) as BlockTableRecord;

  432.           // Clone the objects to the newdatabase
  433.           // 克隆对象到新数据库
  434.           IdMapping acIdMap = new IdMapping();
  435.          acCurDb.WblockCloneObjects(acObjIdColl, acBlkTblRecNewDoc.ObjectId,acIdMap,
  436.                                     DuplicateRecordCloning.Ignore, false);

  437.           // Save the copied objects to thedatabase
  438.           // 保存复制的对象到数据库
  439.           acTrans.Commit();
  440.       }

  441.       // Unlock the document解锁文档
  442.   }

  443.   // Set the new document current将新文档设置为当前文档
  444.   acDocMgr.MdiActiveDocument = acNewDoc;
  445. }
  446. VBA/ActiveX Code Reference
  447. SubCopyObjectsBetweenDatabases()
  448.     Dim DOC0 As AcadDocument
  449.     DimcircleObj1 As AcadCircle, circleObj2 As AcadCircle
  450.     Dim centerPoint(0 To 2) As Double
  451.     Dim radius1 As Double, radius2 As Double
  452.     Dim objCollection(0 To 1) As Object
  453.     Dim retObjects As Variant

  454.     ' Define the Circle object
  455.     centerPoint(0) = 0: centerPoint(1) = 0:centerPoint(2) = 0
  456.     radius1 = 5#: radius2 = 7#

  457.     ' Add two circles to the current drawing
  458.     Set circleObj1 =ThisDrawing.ModelSpace.AddCircle _
  459.                      (centerPoint, radius1)
  460.     Set circleObj2 = ThisDrawing.ModelSpace.AddCircle_
  461.                      (centerPoint, radius2)

  462.     ' Save pointer to the current drawing
  463.     Set DOC0 =ThisDrawing.Application.ActiveDocument

  464.     ' Copy objects
  465.     '
  466.     ' First put the objects to be copied into aform compatible
  467.     ' with CopyObjects
  468.     Set objCollection(0) = circleObj1
  469.     Set objCollection(1) = circleObj2

  470.     ' Create a new drawing and point to itsmodel space
  471.     Dim Doc1MSpace As AcadModelSpace
  472.     Dim DOC1 As AcadDocument

  473.     Set DOC1 = Documents.Add
  474.     Set Doc1MSpace = DOC1.ModelSpace

  475.     ' Copy the objects into the model space ofthe new drawing. A
  476.     ' collection of the new (copied) objects isreturned.
  477.     retObjects =DOC0.CopyObjects(objCollection, Doc1MSpace)
  478. 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.NETImportsAutodesk.AutoCAD.Runtime
  1. <div align="left"><strong></strong></div><div align="left">ImportsAutodesk.AutoCAD.ApplicationServices</div><div align="left">ImportsAutodesk.AutoCAD.DatabaseServices</div>
  2. <div align="left"><CommandMethod("OffsetObject")>_</div><div align="left">  Public Sub OffsetObject()</div><div align="left">  '' Get the current document and database</div><div align="left">  Dim acDoc As Document =Application.DocumentManager.MdiActiveDocument</div><div align="left">  Dim acCurDb As Database = acDoc.Database</div>
  3. <div align="left">  '' Start a transaction</div><div align="left">  Using acTrans As Transaction =acCurDb.TransactionManager.StartTransaction()</div>
  4. <div align="left">      '' Open the Block table for read</div><div align="left">      Dim acBlkTbl As BlockTable</div><div align="left">      acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId, _</div><div align="left">                                  OpenMode.ForRead)</div>
  5. <div align="left">      '' Open the Block table record Modelspace for write</div><div align="left">      Dim acBlkTblRec As BlockTableRecord</div><div align="left">      acBlkTblRec =acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _</div><div align="left">                                     OpenMode.ForWrite)</div>
  6. <div align="left">      '' Create a lightweight polyline</div><div align="left">      Dim acPoly As Polyline = New Polyline()</div><div align="left">      acPoly.AddVertexAt(0, New Point2d(1, 1),0, 0, 0)</div><div align="left">      acPoly.AddVertexAt(1, New Point2d(1, 2),0, 0, 0)</div><div align="left">      acPoly.AddVertexAt(2, New Point2d(2, 2),0, 0, 0)</div><div align="left">      acPoly.AddVertexAt(3, New Point2d(3, 2),0, 0, 0)</div><div align="left">      acPoly.AddVertexAt(4, New Point2d(4, 4),0, 0, 0)</div><div align="left">      acPoly.AddVertexAt(5, New Point2d(4, 1),0, 0, 0)</div>
  7. <div align="left">      '' Add the new object to the block tablerecord and the transaction</div><div align="left">      acBlkTblRec.AppendEntity(acPoly)</div><div align="left">      acTrans.AddNewlyCreatedDBObject(acPoly,True)</div>
  8. <div align="left">      '' Offset the polyline a given distance</div><div align="left">      Dim acDbObjColl As DBObjectCollection =acPoly.GetOffsetCurves(0.25)</div>
  9. <div align="left">      '' Step through the new objects created</div><div align="left">      For Each acEnt As Entity In acDbObjColl</div><div align="left">          '' Add each offset object</div><div align="left">          acBlkTblRec.AppendEntity(acEnt)</div><div align="left">         acTrans.AddNewlyCreatedDBObject(acEnt, True)</div><div align="left">      Next</div>
  10. <div align="left">      '' Save the new objects to the database</div><div align="left">      acTrans.Commit()</div><div align="left">  End Using</div><div align="left">End Sub</div>
复制代码
C#usingAutodesk.AutoCAD.Runtime;
  1. <div align="left"><strong></strong></div><div align="left">usingAutodesk.AutoCAD.ApplicationServices;</div><div align="left">usingAutodesk.AutoCAD.DatabaseServices;</div>
  2. <div align="left">[CommandMethod("OffsetObject")]</div><div align="left">public static voidOffsetObject()</div><div align="left">{</div><div align="left">  // Get the current document and database</div><div align="left">  Document acDoc =Application.DocumentManager.MdiActiveDocument;</div><div align="left">  Database acCurDb = acDoc.Database;</div>
  3. <div align="left">  // Start a transaction</div><div align="left">  using (Transaction acTrans =acCurDb.TransactionManager.StartTransaction())</div><div align="left">  {</div><div align="left">      // Open the Block table for read</div><div align="left">      BlockTable acBlkTbl;</div><div align="left">      acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId,</div><div align="left">                                  OpenMode.ForRead) as BlockTable;</div>
  4. <div align="left">      // Open the Block table record Modelspace for write</div><div align="left">      BlockTableRecord acBlkTblRec;</div><div align="left">      acBlkTblRec =acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],</div><div align="left">                                     OpenMode.ForWrite) as BlockTableRecord;</div>
  5. <div align="left">      // Create a lightweight polyline创建多段线</div><div align="left">      Polyline acPoly = new Polyline();</div><div align="left">      acPoly.AddVertexAt(0, new Point2d(1, 1),0, 0, 0);</div><div align="left">      acPoly.AddVertexAt(1, new Point2d(1, 2),0, 0, 0);</div><div align="left">      acPoly.AddVertexAt(2, new Point2d(2, 2),0, 0, 0);</div><div align="left">      acPoly.AddVertexAt(3, new Point2d(3, 2),0, 0, 0);</div><div align="left">      acPoly.AddVertexAt(4, new Point2d(4, 4),0, 0, 0);</div><div align="left">      acPoly.AddVertexAt(5, new Point2d(4, 1),0, 0, 0);</div>
  6. <div align="left">      // Add the new object to the block tablerecord and the transaction</div><div align="left">      acBlkTblRec.AppendEntity(acPoly);</div><div align="left">      acTrans.AddNewlyCreatedDBObject(acPoly,true);</div>
  7. <div align="left">      // Offset the polyline a given distance偏移0.25距离</div><div align="left">      DBObjectCollection acDbObjColl =acPoly.GetOffsetCurves(0.25);</div>
  8. <div align="left">      // Step through the new objects created</div><div align="left">      foreach (Entity acEnt in acDbObjColl)</div><div align="left">      {</div><div align="left">          // Add each offset object</div><div align="left">          acBlkTblRec.AppendEntity(acEnt);</div><div align="left">         acTrans.AddNewlyCreatedDBObject(acEnt, true);</div><div align="left">      }</div>
  9. <div align="left">      // Save the new objects to the database</div><div align="left">      acTrans.Commit();</div><div align="left">  }</div><div align="left">}</div>
复制代码
VBA/ActiveX Code Reference
  1. <div align="left">Sub OffsetObject()</div><div align="left">    ' Create the polyline</div><div align="left">    Dim plineObj As AcadLWPolyline</div><div align="left">    Dim points(0 To 11) As Double</div><div align="left">    points(0) = 1: points(1) = 1</div><div align="left">    points(2) = 1: points(3) = 2</div><div align="left">    points(4) = 2: points(5) = 2</div><div align="left">    points(6) = 3: points(7) = 2</div><div align="left">    points(8) = 4: points(9) = 4</div><div align="left">    points(10) = 4: points(11) = 1</div><div align="left">    Set plineObj = ThisDrawing.ModelSpace. _</div><div align="left">                                   AddLightWeightPolyline(points)</div><div align="left">    plineObj.Closed = True</div><div align="left">    ZoomAll</div>
  2. <div align="left">    ' Offset the polyline</div><div align="left">    Dim offsetObj As Variant</div><div align="left">    offsetObj = plineObj.Offset(0.25)</div>
  3. <div align="left">    ZoomAll</div><div align="left">End Sub</div>
复制代码

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏 转播转播 分享分享 分享淘帖 支持支持 反对反对

相关帖子

工作时间:工作日的9:00-12:00/13:30-18:00,节假日不在线,请勿留言
*滑块验证:
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|EaBIM网 ( 苏ICP备2020058923号-1  苏公网安备32011502011255号

GMT+8, 2024-11-30 14:17

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表