• 【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>>

  • 相关阅读:
    剑指 Offer 22. 链表中倒数第k个节点
    1221. 分割平衡字符串
    1528. 重新排列字符串
    1450. 在既定时间做作业的学生人数
    1266. 访问所有点的最小时间
    1572. 矩阵对角线元素的和
    使用Python进行数据传输
    分布式系统的原理与设计
    分布式缓存
    Centos6 配置最新yum源
  • 原文地址:https://www.cnblogs.com/Helchan/p/4641109.html
Copyright © 2020-2023  润新知