CAD 复制多个对象

下面示例使用ObjectIdCollection对象来跟踪应该复制的对象。一旦对象ids被添加进集合,集合就会进行迭代,新的对象会被Clone函数进行创建,然后添加进模型空间。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

namespace SingleCopy
{
    public class DYCopy
    {
        [CommandMethod("SingleCopy")]
        public static void SCopy()
        { 
            //获取当前文件和数据库
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            //开始事务管理
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //以读的方式打开块表
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
                //打开块表记录模型空间,用于写
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                //创建圆心为(2,3),半径为4.25的圆
                Circle acCirc = new Circle();
                acCirc.SetDatabaseDefaults();
                acCirc.Center = new Point3d(2, 3, 0);
                acCirc.Radius = 4.25;
                //添加新对象到块表记录和事务
                acBlkTblRec.AppendEntity(acCirc);
                acTrans.AddNewlyCreatedDBObject(acCirc, true);
                //创建圆的副本,并改变它的半径
                Circle acCircClone = acCirc.Clone() as Circle;
                acCircClone.Radius = 1;
                //添加圆的副本
                acBlkTblRec.AppendEntity(acCircClone);
                acTrans.AddNewlyCreatedDBObject(acCircClone, true);
                //将新对象保存到数据库中
                acTrans.Commit();
            }
        }

        [CommandMethod("MultipleCopy")]
        public static void MultipleCopy()
        { 
            //获取当前文件和数据库
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            //开始事务管理
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                //打开块表用于读
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
                //打开块表记录的模型空间,用于写
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                //创建圆,以(0,0,0)为圆心,以5为半径
                Circle acCircl = new Circle();
                acCircl.SetDatabaseDefaults();
                acCircl.Center = new Point3d(0, 0, 0);
                acCircl.Radius = 5;
                //添加新对象到块表记录和事务中
                acBlkTblRec.AppendEntity(acCircl);
                acTrans.AddNewlyCreatedDBObject(acCircl, true);
                //创建圆,以(0,0,0)为圆心,以7为半径
                Circle acCirc2 = new Circle();
                acCirc2.SetDatabaseDefaults();
                acCirc2.Center = new Point3d(0, 0, 0);
                acCirc2.Radius = 7;
                //添加新对象到块表记录和事务中
                acBlkTblRec.AppendEntity(acCirc2);
                acTrans.AddNewlyCreatedDBObject(acCirc2, true);
                //添加所有要克隆的对象
                DBObjectCollection acDbobjColl = new DBObjectCollection();
                acDbobjColl.Add(acCircl);
                acDbobjColl.Add(acCirc2);
                foreach (Entity acEnt in acDbobjColl)
                {
                    Entity acEntClone;
                    acEntClone = acEnt.Clone() as Entity;
                    acEntClone.ColorIndex = 1;
                    //创建一个矩阵,移动每个副本实体15个单位;
                    acEntClone.TransformBy(Matrix3d.Displacement(new Vector3d(15, 0, 0)));
                    //添加副本对象
                    acBlkTblRec.AppendEntity(acEntClone);
                    acTrans.AddNewlyCreatedDBObject(acEntClone, true);
                }
                //将对象保存进数据库
                acTrans.Commit();
            }
        }
    }
}

posted @ 2023-06-04 22:08  chenlight  阅读(52)  评论(1编辑  收藏  举报