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());
            }
         }
        
    }
}

                                                                              更多内容,请关注公众号

                                                                     

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
自定义实体jig 创建是指使用 jig 软件开发自己的实体类型,以便在对话系统中能够识别和理解特定的实体。以下是关于如何创建自定义实体jig步骤: 1. 在 jig 编辑器中创建一个新的 jig 项目。这可以通过在 jig 菜单中选择 &quot;创建新 jig&quot; 来实现。 2. 在新项目中,选择 &quot;实体&quot; 选项卡,然后点击 &quot;创建新实体&quot; 按钮。这将打开一个实体编辑器。 3. 在实体编辑器中,输入实体的名称和描述。例如,如果要创建一个名为 &quot;食物&quot; 的实体,描述可以是 &quot;各种类型的食物&quot;。 4. 在 &quot;词汇&quot; 部分,输入与实体相关的词汇,以帮助 jig 识别并关联实体。例如,对于 &quot;食物&quot; 实体,可以输入词汇如 &quot;食物&quot;、&quot;菜单&quot;、&quot;饭菜&quot; 等。 5. 在 &quot;值&quot; 部分,添加实体的具体值。例如,对于 &quot;食物&quot; 实体,可以添加 &quot;汉堡&quot;、&quot;比萨&quot;、&quot;饭&quot; 等值。 6. 确认并保存创建的实体。 7. 在对话系统中使用创建的自定义实体。在对话流程中,可以通过设定规则和条件来引用和匹配实体。例如,可以使用 &quot;如果用户提到实体 &#39;食物&#39;&quot; 这样的条件来触发相应的回应。 通过以上步骤,我们可以创建一个个性化的实体类型,使对话系统能够识别和理解特定的实体,在与用户的对话中提供更准确和个性化的回应。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yGIS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值