• CAD(C#)——浅谈JIG之EntityJig&DrawJig


          在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实现)

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

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

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

    代码
     1  #region DrawJig动态拖动多个实体
     2     public class DrawJigEnt : DrawJig
     3     {
     4         //变量
     5         Point3d oldPt;//实体移动之前的位置
     6         Point3d newPt;//实体移动之后的位置
     7         Vector3d v;//实体移动的向量
     8 
     9         List<Entity> ents = new List<Entity>();
    10         List<Point3d> oldPts = new List<Point3d>();
    11         public DrawJigEnt(List<Entity> ents)
    12         {
    13             oldPt = Point3d.Origin;
    14             newPt = Point3d.Origin;
    15             v = new Vector3d();
    16             this.ents = ents;
    17 
    18             Autodesk.AutoCAD.DatabaseServices.Entity ent = ents[0];
    19             if (ent is BlockReference)
    20             {
    21                 BlockReference br = ent as BlockReference;
    22                 oldPt = br.Position;
    23             }
    24 
    25             for (int i = 1; i < ents.Count; i++)
    26             {
    27                 if (ents[i] is BlockReference)
    28                 {
    29                     BlockReference br = ents[i] as BlockReference;
    30                     oldPts.Add(br.Position);
    31                 }
    32             }
    33         }
    34 
    35         //更新
    36         protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
    37         {
    38             Entity ent = ents[0];
    39             ent.UpgradeOpen();
    40             if (ent is BlockReference)
    41             {
    42                 BlockReference br = ent as BlockReference;
    43                 br.Position = newPt;
    44                 draw.Geometry.Draw(br);
    45             }
    46 
    47             v = newPt.GetVectorTo(oldPt); 
    48             
    49             for(int i =1;i<ents.Count;i++)
    50             {
    51                 Entity entity = ents[i];
    52                 entity.UpgradeOpen();
    53                 if (entity is BlockReference)
    54                 {
    55                     BlockReference br = entity as BlockReference;
    56                     br.Position = oldPts[i - 1+ v;
    57                     draw.Geometry.Draw(entity);
    58                 }
    59             }
    60 
    61             return true;
    62         }
    63         //取样函数
    64         protected override SamplerStatus Sampler(JigPrompts prompts)
    65         {
    66             JigPromptOptions opt = new JigPromptOptions();
    67             opt.UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.NoNegativeResponseAccepted | UserInputControls.NullResponseAccepted;
    68             opt.UseBasePoint = true;
    69             opt.Cursor = CursorType.RubberBand;
    70             opt.BasePoint = oldPt;
    71             opt.Message = "选择移动到的终点";
    72 
    73             PromptPointResult res = prompts.AcquirePoint(opt);
    74             if (PromptStatus.OK == res.Status)
    75             {
    76                 if (res.Value == newPt)
    77                 {
    78                     return SamplerStatus.NoChange;
    79                 }
    80                 newPt = res.Value;
    81             }
    82             else
    83             {
    84                 return SamplerStatus.Cancel;
    85             }
    86             return SamplerStatus.OK;
    87         }
    88     }
    89     #endregion
  • 相关阅读:
    Linux操作系统原理
    html标签简介(常用)
    Git常用命名
    Nuxt.js vue服务端渲染
    Sequelize 和 MySQL 对照Sequelize 和 MySQL 对照
    VScode 自定义用户代码块
    python对一个文本的解析
    API管理工具
    Flutter教程- Dart语言规范-知识点整理
    RESTful API
  • 原文地址:https://www.cnblogs.com/wangyong/p/1652412.html
Copyright © 2020-2023  润新知