Helchan的博客园

导航

【AutoCAD.NET】移动图元(用Editor类获取图元Entity,GetPoint获取点)

//03.移动图元
        [CommandMethod("MoveEnt")]
        public static void MoveEnt()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                PromptEntityResult EntityResult = ed.GetEntity("请选择实体图元:");
                if (EntityResult.Status == PromptStatus.OK)
                {
                    Entity ent = (Entity)tran.GetObject(EntityResult.ObjectId, OpenMode.ForWrite);
                    Point3d startPt = new Point3d(0,0,0);
                    Point3d endPt = new Point3d(1000, 1000, 0);
                    ent.Move(startPt,endPt);
                    ent.DowngradeOpen();
                }
                tran.Commit();
            }
        }

 

//05.复制图元
        [CommandMethod("CopyEnt2")]
        public static void CopyEnt2()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                PromptEntityResult EntityResult = ed.GetEntity("请选择图元:");
                if (EntityResult.Status == PromptStatus.OK)
                {
                    Entity Ent = (Entity)trans.GetObject(EntityResult.ObjectId, OpenMode.ForWrite);
                    Point3d startPt = ed.GetPoint("点取起点").Value;
                    Point3d endPt = ed.GetPoint("点取终点").Value;
                    Ent.Id.Copy(startPt, endPt);
                    Ent.DowngradeOpen();
                }
                trans.Commit();
            }
        }

 

关键:在于使用事务处理的GetObject来获得Entity,以及Editor的GetPoint的valuej即为点,然后进行图元的操作

/// <summary>
        /// 移动图元
        /// </summary>
        /// <param name="ent"></param>实体(Entity类型)
        /// <param name="sourcePt"></param>源点
        /// <param name="targetPt"></param>目标点
        public static void Move(this Entity ent, Point3d sourcePt, Point3d targetPt)
        {
            if (ent.IsNewObject)
            {
                Vector3d vector = sourcePt.GetVectorTo(targetPt);
                Matrix3d mt = Matrix3d.Displacement(vector);
                ent.TransformBy(mt);
            }
            else
            {
                ent.ObjectId.Move(sourcePt,targetPt);
            }
        }

 

/// <summary>
        /// 复制图元
        /// </summary>
        /// <param name="id"></param>图元ObjectId
        /// <param name="sourcePt"></param>原点
        /// <param name="targetPt"></param>目标点
        /// <returns></returns>
        public static ObjectId Copy(this ObjectId id, Point3d sourcePt, Point3d targetPt)
        {
            Vector3d vector = sourcePt.GetVectorTo(targetPt);
            Matrix3d mt = Matrix3d.Displacement(vector);
            Entity ent = (Entity)id.GetObject(OpenMode.ForRead);
            Entity entCopy = ent.GetTransformedCopy(mt);
            ObjectId copyId = id.Database.AddToModelSpace(entCopy);
            return copyId;
        }

 

<<end>>

posted on 2015-07-12 16:18  Helchan  阅读(991)  评论(0编辑  收藏  举报