zoukankan      html  css  js  c++  java
  • 【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>>

  • 相关阅读:
    mall
    将UNICODE编码转换为中文
    460. LFU Cache
    957. Prison Cells After N Days
    455. Assign Cookies
    453. Minimum Moves to Equal Array Elements
    434. Number of Segments in a String
    1203. Sort Items by Groups Respecting Dependencies
    641. Design Circular Deque
    441. Arranging Coins
  • 原文地址:https://www.cnblogs.com/Helchan/p/4641109.html
Copyright © 2011-2022 走看看