1. 程式人生 > >AutoCAD二次開發&多個實體Jig拖拽(步驟模擬)

AutoCAD二次開發&多個實體Jig拖拽(步驟模擬)

值中秋之際,和小夥伴出去後,借點時間看一下關於多實時拖拽效果(jig),在AutoCAD中,通過滑鼠的移動,動態的展示效果會給繪圖者直觀的感覺。在AutoCAD中關於拖拽有兩個實現類,第一是EntityJig,另外一個是DrawJig類。其中前者只能針對一個實體,而另外一個是針對多個實體,可以實現拖拽效果。而對於前者者繼承類中需要實現Sampler和Updata兩個函式,Sampler用於資料的互動,比如提示使用者輸入一定的資料,Updata是用於接收資料後在圖形介面實時更新。同EntityJig類,DrawJig也需要重寫Samper函式,另外該類中含有WorldDraw函式需要重寫,用於實時展示動態圖形效果。

下面的這個測試demo是在圖形介面拾取多個實體,然後再指定另外一個移動點,在Samper函式中提示使用者拾取,將拾取的多個實體在WorldDraw中實時展示出來。整個工程的原始碼如下所示。其中我們製作一個效果視屏。

這是jig實現類:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;


namespace MoveMultipleEntityJig
{
  public  class JigMoveMultipleEntity : DrawJig
    {
        private Point3d mBase;
        private Point3d mLocation;
        List<Entity> mEntities;

        public JigMoveMultipleEntity(Point3d basePt)
        {
            mBase = basePt.TransformBy(UCS);
            mEntities = new List<Entity>();
        }

        public Point3d Base
        {
            get { return mLocation; }
            set { mLocation = value; }
        }

        public Point3d Location
        {
            get { return mLocation; }
            set { mLocation = value; }
        }

        public Matrix3d UCS
        {
            get
            {
                return Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem;
            }
        }

        public void AddEntity(Entity ent)
        {
            mEntities.Add(ent);
        }

        public void TransformEntities()
        {
            Matrix3d mat = Matrix3d.Displacement(mBase.GetVectorTo(mLocation));

            foreach (Entity ent in mEntities)
            {
                ent.TransformBy(mat);
            }
        }

        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            JigPromptPointOptions prOptions1 = new JigPromptPointOptions("\n新的位置:");
            prOptions1.UseBasePoint = false;

            PromptPointResult prResult1 = prompts.AcquirePoint(prOptions1);
            if (prResult1.Status == PromptStatus.Cancel || prResult1.Status == PromptStatus.Error)
                return SamplerStatus.Cancel;

            if (!mLocation.IsEqualTo(prResult1.Value, new Tolerance(10e-10, 10e-10)))
            {
                mLocation = prResult1.Value;
                return SamplerStatus.OK;
            }
            else
                return SamplerStatus.NoChange;
        }
        protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
        {
            Matrix3d mat = Matrix3d.Displacement(mBase.GetVectorTo(mLocation));

            WorldGeometry geo = draw.Geometry;
            if (geo != null)
            {
                geo.PushModelTransform(mat);

                foreach (Entity ent in mEntities)
                {
                    geo.Draw(ent);
                }

                geo.PopModelTransform();
            }

            return true;
        }
    }
}

這是使用的程式碼;

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

namespace MoveMultipleEntityJig
{
    public class Class1
    {
        public static JigMoveMultipleEntity jigger;
        [CommandMethod("tsjig")]
        public static void demo(){
            try
            {
                Database db = HostApplicationServices.WorkingDatabase;
                Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

                PromptSelectionResult selRes = ed.GetSelection();
                if (selRes.Status != PromptStatus.OK) return;

                PromptPointOptions prOpt = new PromptPointOptions("\n選擇基點:");
                PromptPointResult pr = ed.GetPoint(prOpt);
                if (pr.Status != PromptStatus.OK) return;

                jigger = new JigMoveMultipleEntity(pr.Value);
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    foreach (ObjectId id in selRes.Value.GetObjectIds())
                    {
                        Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
                        jigger.AddEntity(ent);
                    }

                    PromptResult jigRes = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.Drag(jigger);
                    if (jigRes.Status == PromptStatus.OK)
                    {
                        jigger.TransformEntities();
                        tr.Commit();
                    }
                    else
                        tr.Abort();
                }
            }
            catch (System.Exception ex)
            {
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
            }
         }
        
    }
}

                                                                              更多內容,請關注公眾號