浅谈JIG之EntityJig&DrawJig

转载:http://www.cnblogs.com/wangyong/archive/2010/01/20/1652412.html

在CAD的二次开发中,有时会使用JIG技术来实现操作的动态性、交互,最常见的比如说是在CAD中定制自己的移动和复制,当然要保持CAD中原有的动态显示效果。

      在JIG中我们可以继承EntityJig和DrawJig,对于这两个类,那这两个类有什么区别呢?他们的使用场合又有什么区别呢?

     其实,他们之间在操作一些简单的实体的时候是没有多大区别的,硬要说区别的话,那就是在实现一个相同功能时DrawJig比EntityJig的代码简单一些。(注意:是指在操作简单的实体的时候,比如实体的数量就一个)。

        EntityJig:一般用于图元实体的拖动,要求先生成实体Entity

        protected override bool Update()
        {
            throw new NotImplementedException();
        }

        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            throw new NotImplementedException();
        }

        DrawJig:一般用于复杂图形的拖动

       protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
        {
            throw new NotImplementedException();
        }

        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            throw new NotImplementedException();
        }

      在拖动一个实体的时候,不管是用EntityJig还是DrawJig效果都一样,但是当我们在处理多个实体的移动,我们要求鼠标可以实时动态显示要拖动的多个实体,这点,EntityJig却无法实现了,但如果是DrawJig却很容易实现。

而且对于动态生成实体(比如指定一个圆心,动态生成一个圆),用DrawJig中的WorlDraw可以灵活控制实现生成。

  相比之下,是不是发现DrawJig优胜于EntityJig(个人理解).

  下面贴出上图实现的代码:

代码
#region DrawJig动态拖动多个实体
    public class DrawJigEnt : DrawJig
    {
        //变量
        Point3d oldPt;//实体移动之前的位置
        Point3d newPt;//实体移动之后的位置
        Vector3d v;//实体移动的向量

        List<Entity> ents = new List<Entity>();
        List<Point3d> oldPts = new List<Point3d>();
        public DrawJigEnt(List<Entity> ents)
        {
            oldPt = Point3d.Origin;
            newPt = Point3d.Origin;
            v = new Vector3d();
            this.ents = ents;

            Autodesk.AutoCAD.DatabaseServices.Entity ent = ents[0];
            if (ent is BlockReference)
            {
                BlockReference br = ent as BlockReference;
                oldPt = br.Position;
            }

            for (int i = 1; i < ents.Count; i++)
            {
                if (ents[i] is BlockReference)
                {
                    BlockReference br = ents[i] as BlockReference;
                    oldPts.Add(br.Position);
                }
            }
        }

        //更新
        protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
        {
            Entity ent = ents[0];
            ent.UpgradeOpen();
            if (ent is BlockReference)
            {
                BlockReference br = ent as BlockReference;
                br.Position = newPt;
                draw.Geometry.Draw(br);
            }

            v = newPt.GetVectorTo(oldPt);
           
            for(int i =1;i<ents.Count;i++)
            {
                Entity entity = ents[i];
                entity.UpgradeOpen();
                if (entity is BlockReference)
                {
                    BlockReference br = entity as BlockReference;
                    br.Position = oldPts[i – 1] + v;
                    draw.Geometry.Draw(entity);
                }
            }

            return true;
        }
        //取样函数
        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            JigPromptOptions opt = new JigPromptOptions();
            opt.UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.NoNegativeResponseAccepted | UserInputControls.NullResponseAccepted;
            opt.UseBasePoint = true;
            opt.Cursor = CursorType.RubberBand;
            opt.BasePoint = oldPt;
            opt.Message = “选择移动到的终点”;

            PromptPointResult res = prompts.AcquirePoint(opt);
            if (PromptStatus.OK == res.Status)
            {
                if (res.Value == newPt)
                {
                    return SamplerStatus.NoChange;
                }
                newPt = res.Value;
            }
            else
            {
                return SamplerStatus.Cancel;
            }
            return SamplerStatus.OK;
        }
    }
    #endregion

 

此条目发表在ObjectArx.Net分类目录。将固定链接加入收藏夹。

发表评论