To copy an object, use the Clone function provided for that object. This method creates a new object that is a duplicate of the original object. Once the duplicate object is created, you can then modify it prior to adding or appending it to the database. If you do not transform the object or change its position, the new object will be located in the same position as the original.
若要复制一个对象,使用为对象提供的 Clone 函数。这个方法创建一个新对象,它是原始对象的副本。一旦复制的对象被创建,然后就可以添加或追加它到数据库前修改它。如果用户不转换对象或修改它的位置,新对象将被放置在与原始对象相同的位置。
If you have a large number of objects you might want to copy, you can add each of the object ids to an ObjectIdCollection object and then iterate each of the objects. As you iterate each object, you can then use the Clone function for each object and then add or append the new object to the database.
如果用户有很多对象想复制,可以添加每一个 ObjectID 到一个 ObjectIDCollection 对象中然后迭代每个对象。当迭代每一个对象时,可以为每个对象使用 Clone 函数,然后添加或追加新对象到数据库中。
The following example creates a new circle and then creates a direct copy of the circle to create a second circle.
下面的示例创建一个新的圆,然后创建一个圆的直接副本作为创建的第二个圆。
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
<CommandMethod("SingleCopy")> _
Public Sub SingleCopy()
'' 获得当前文档和数据库 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 Model space for write
Dim acBlkTblRec As BlockTableRecord
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
OpenMode.ForWrite)
'' 创建一个中心点在 (2,3,0) ,半径为4.25 的圆 Create a circle that is at 2,3 with a radius of 4.25
Dim acCirc As Circle = New Circle()
acCirc.SetDatabaseDefaults()
acCirc.Center = New Point3d(2, 3, 0)
acCirc.Radius = 4.25
'' 添加新对象到块表记录和事务中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acCirc)
acTrans.AddNewlyCreatedDBObject(acCirc, True)
'' 创建一个圆的副本并修改它的半径 Create a copy of the circle and change its 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
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
[CommandMethod("SingleCopy")]
public static void SingleCopy()
{
// 获得当前文档和数据库 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 Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
// 创建一个中心点在 (2,3,0) ,半径为4.25 的圆 Create a circle that is at 2,3 with a radius of 4.25
Circle acCirc = new Circle();
acCirc.SetDatabaseDefaults();
acCirc.Center = new Point3d(2, 3, 0);
acCirc.Radius = 4.25;
// 添加新对象到块表记录和事务中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acCirc);
acTrans.AddNewlyCreatedDBObject(acCirc, true);
// 创建一个圆的副本并修改它的半径 Create a copy of the circle and change its radius
Circle acCircClone = acCirc.Clone() as Circle;
acCircClone.Radius = 1;
// 添加复制的圆 Add the cloned circle
acBlkTblRec.AppendEntity(acCircClone);
acTrans.AddNewlyCreatedDBObject(acCircClone, true);
// 保存新对象到数据库中 Save the new object to the database
acTrans.Commit();
}
}
The following example uses an ObjectIdCollection object to track the objects that should be copied. Once the object ids are added to the collection, the collection is iterated and new objects are created by using the Clone method and then are added to Model space.
下面的示例使用一个 ObjectIdCollection 对象跟踪应该被复制的对象。一旦 ObjectID 被添加到集合中,
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
<CommandMethod("MultipleCopy")> _
Public Sub MultipleCopy()
'' 获得当前文档和数据库 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 Model space for write
Dim acBlkTblRec As BlockTableRecord
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
OpenMode.ForWrite)
'' 创建一个圆心为(0,0,0),半径为 5 的圆 Create a circle that is at (0,0,0) with a radius of 5
Dim acCirc1 As Circle = New Circle()
acCirc1.SetDatabaseDefaults()
acCirc1.Center = New Point3d(0, 0, 0)
acCirc1.Radius = 5
'' 添加新对象到块表记录和事务中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acCirc1)
acTrans.AddNewlyCreatedDBObject(acCirc1, True)
'' 创建一个圆心为(0,0,0),半径为 7 的圆 Create a circle that is at (0,0,0) with a radius of 7
Dim acCirc2 As Circle = New Circle()
acCirc2.SetDatabaseDefaults()
acCirc2.Center = New Point3d(0, 0, 0)
acCirc2.Radius = 7
'' 添加新对象到块表记录和事务中 Add the new object to the block table record 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
'' 创建一个矩阵并移动每个复制的图元15个单位 Create a matrix and move each copied 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
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
[CommandMethod("MultipleCopy")]
public static void MultipleCopy()
{
// 获得当前文档和数据库 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 Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
// 创建一个圆心为(0,0,0),半径为 5 的圆 Create a circle that is at (0,0,0) with a radius of 5
Circle acCirc1 = new Circle();
acCirc1.SetDatabaseDefaults();
acCirc1.Center = new Point3d(0, 0, 0);
acCirc1.Radius = 5;
// 添加新对象到块表记录和事务中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acCirc1);
acTrans.AddNewlyCreatedDBObject(acCirc1, true);
// 创建一个圆心为(0,0,0),半径为 7 的圆 Create a circle that is at (0,0,0) with a radius of 7
Circle acCirc2 = new Circle();
acCirc2.SetDatabaseDefaults();
acCirc2.Center = new Point3d(0, 0, 0);
acCirc2.Radius = 7;
// 添加新对象到块表记录和事务中 Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acCirc2);
acTrans.AddNewlyCreatedDBObject(acCirc2, true);
// 添加所有用于复制的对象 Add all the objects to clone
DBObjectCollection acDBObjColl = new DBObjectCollection();
acDBObjColl.Add(acCirc1);
acDBObjColl.Add(acCirc2);
foreach (Entity acEnt in acDBObjColl)
{
Entity acEntClone;
acEntClone = acEnt.Clone() as Entity;
acEntClone.ColorIndex = 1;
// 创建一个矩阵并移动每个复制的图元15个单位 Create a matrix and move each copied entity 15 units
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();
}
}