AutoCad 二次开发 jig操作之标注跟随线移动

在autocad当中,我认为的jig操作的意思就是即时绘图的意思,它能够实时的显示出当前的操作,以便我们直观的感受到当前的绘图操作是什么样子会有什么样的结果。比如我们自己写命令话一条直线,不用jig操作,只提示输入两个端点,我们在绘制过程中无法预先的感受到这条直线是在哪个位置,有多长,与x轴成什么角度,而是直接输入两点后就得到一条直线的结果了。还有当有了jig操作后,我们确定了一个端点后,在未输入第二个端点的时候,可以拖动鼠标拉长或缩短这条直线,也可以任意的绕第一个端点旋转这条直线。

我写了一个例子,是输入一条直线,并且在这条直线上建立一个对齐标注,拖动直线的同时,标注也跟随直线变化。我就以这个例子来具体的说说怎样实现一个jig操作。

首先要实现jig操作需要继承抽象类DrawJig, 它就是用来实现自定义的jig操作的,它的继承关系如图:

可以看到 DrawJig是继承自Jig的,有关jig的介绍:

  • Gets a user drag movement
  • Interprets this as a distance, angle, or point
  • Uses this value to update the entity's data
  • Calls the entity's WorldDraw method to redraw it on screen

这也是实现jig操作的步骤,大概意思就是用实时输入的距离、角度、点的数据去更新实体数据在屏幕上。要实现这个得在自定义的jig操作类中实现两个方法:

protected override SamplerStatus Sampler(JigPrompts prompts)

  这个方法主要作用就是获取提供实时的数据输入的,这个JigPrompts和我们常见的那些输入数据的方法类似。这是它的继承结构:

Autodesk.AutoCAD.EditorInput.PromptOptions
Autodesk.AutoCAD.EditorInput.JigPromptOptions
Autodesk.AutoCAD.EditorInput.JigPromptGeometryOptions
Autodesk.AutoCAD.EditorInput.JigPromptAngleOptions
Autodesk.AutoCAD.EditorInput.JigPromptDistanceOptions
Autodesk.AutoCAD.EditorInput.JigPromptPointOptions

我们可以看到,我们能输入4种数据,这里我是选的JigPromptPointOptions,我们就用这里输入的数据来实时的更新直线的EndPoint,和对齐标注的xLine2Point。代码:

InputFunc = (prmpts) =>
{ pointOpts.Message = msg; var res = prmpts.AcquirePoint(pointOpts);
//Point就是我们要更新实体数据的点
if (res.Value == Point)
{
return SamplerStatus.NoChange;
}
else if (res.Value != Point)
{
Point = res.Value;
return SamplerStatus.OK;
}
else
{
return SamplerStatus.Cancel;
} };
protected override bool WorldDraw(WorldDraw draw)

这个方法就是把更新数据后的实体实时的绘制到屏幕上去。代码:

if (JigEnts.Count > )
{
//这是个委托,主要实现你要如何去更新你的实体
JigUpdateAction(this);
foreach (var ent in JigEnts)
{
ent.WorldDraw(draw);
}
}
return true;

JigUpdateAction代码:

(jig) =>
{
line.EndPoint = jig.Point;
dim.XLine2Point = jig.Point;
dim.DimensionText = line.Length.ToString() ; var centerPt = new Point3d((line.StartPoint.X + jig.Point.X) / , (line.StartPoint.Y + jig.Point.Y) / , (line.StartPoint.Z + jig.Point.Z) / ); dim.DimLinePoint = centerPt+ (jig.Point-line.StartPoint).GetNormal().RotateBy(Math.PI / , Vector3d.ZAxis) * ; });

实现了以上两个方法基本上就可以了,最后你需要把实体加入到空间里面去。

完整代码:

using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime; namespace JigDimension
{
public class JigDim
{
[CommandMethod("myjig")]
public void MyJigDimension()
{
var Doc = Application.DocumentManager.MdiActiveDocument;
var Ed = Doc.Editor; var pointRes = Ed.GetPoint(new PromptPointOptions("请输入一地个点:\n")); if (pointRes.Status != PromptStatus.OK) return; Line line = new Line() { StartPoint = pointRes.Value };
AlignedDimension dim = new AlignedDimension() { XLine1Point = line.StartPoint, DimensionStyle = ObjectId.Null }; JigPromptPointOptions jigOpts = new JigPromptPointOptions(); jigOpts.BasePoint = pointRes.Value;
jigOpts.UseBasePoint = true; MyJig myJig = new MyJig(); myJig.PromptInput(jigOpts, "输入第二个点");
myJig.JigEnts.Add(line);
myJig.JigEnts.Add(dim);
myJig.SetJigUpdate((jig) =>
{
line.EndPoint = jig.Point;
dim.XLine2Point = jig.Point;
dim.DimensionText = line.Length.ToString() ; var centerPt = new Point3d((line.StartPoint.X + jig.Point.X) / , (line.StartPoint.Y + jig.Point.Y) / , (line.StartPoint.Z + jig.Point.Z) / ); dim.DimLinePoint = centerPt+ (jig.Point-line.StartPoint).GetNormal().RotateBy(Math.PI / , Vector3d.ZAxis) * ; }); if (myJig.Drag() != PromptStatus.OK)
{
return;
} myJig.JigEnts.ToSpace(); } }
public class MyJig : DrawJig
{ public Point3d Point = Point3d.Origin; Func<JigPrompts, SamplerStatus> InputFunc; public List<Entity> JigEnts = new List<Entity>();
Action<MyJig> JigUpdateAction; public MyJig()
{
JigEnts.Clear();
InputFunc = null;
} public void SetJigUpdate(Action<MyJig> action)
{
JigUpdateAction = action;
} public void PromptInput(JigPromptPointOptions pointOpts,string msg)
{
InputFunc = (prmpts) =>
{ pointOpts.Message = msg; var res = prmpts.AcquirePoint(pointOpts);
//Point就是我们要更新实体数据的点
if (res.Value == Point)
{
return SamplerStatus.NoChange;
}
else if (res.Value != Point)
{
Point = res.Value;
return SamplerStatus.OK;
}
else
{
return SamplerStatus.Cancel;
} }; } protected override SamplerStatus Sampler(JigPrompts prompts)
{
if (InputFunc == null)
{
return SamplerStatus.NoChange;
} return InputFunc.Invoke(prompts);
} protected override bool WorldDraw(WorldDraw draw)
{
if (JigEnts.Count > )
{
//这是个委托,主要实现你要如何去更新你的实体
JigUpdateAction(this);
foreach (var ent in JigEnts)
{
ent.WorldDraw(draw);
}
}
return true;
}
public PromptStatus Drag()
{
return Application.DocumentManager.MdiActiveDocument.Editor
.Drag(this).Status;
}
}
}

AutoCad 二次开发 jig操作之标注跟随线移动的更多相关文章

  1. AutoCad 二次开发 Jig操作之墙块的拖动

    测试结果: 主要思路:选择一段多段线,使用封装的jig类进行实时拖动,其原理就是在拖动的时候,确定被拖动的边,我是选择离输入第一个点最近的边作为拖动边,有了这条边,就能确定需要实时更改的点了,然后当鼠 ...

  2. AutoCad 二次开发 文字镜像

    AutoCad 二次开发 文字镜像 参考:https://adndevblog.typepad.com/autocad/2013/10/mirroring-a-dbtext-entity.html 在 ...

  3. AutoCad 二次开发 .net 之层表的增加 删除 修改图层颜色 遍历 设置当前层

    AutoCad 二次开发 .net 之层表的增加 删除 修改图层颜色 遍历 设置当前层 AutoCad 二次开发 .net 之层表的增加 删除 修改图层颜色 遍历 设置当前层我理解的图层的作用大概是把 ...

  4. AutoCAD二次开发——AutoCAD.NET API开发环境搭建

    AutoCAD二次开发工具:1986年AutoLisp,1989年ADS,1990年DCL,1993年ADS-RX,1995年ObjectARX,1996年Active X Automation(CO ...

  5. 1,下载和部署开发环境--AutoCAD二次开发

    环境需求为: AutoCAD 2020版 ObjectARX SDK 下载地址:https://www.autodesk.com/developer-network/platform-technolo ...

  6. AutoCAD二次开发-使用ObjectARX向导创建应用程序(HelloWorld例子)

    AutoCAD2007+vs2005 首先自己去网上搜索下载AutoCAD2007的ARX开发包. 解压后如下 打开后如下 classmap文件夹为C++类和.net类的框架图,是一个DWG文件. d ...

  7. 我的AutoCAD二次开发之路 (一)

    原帖地址 http://379910987.blog.163.com/blog/static/33523797201011184552167/ 今天在改代码的时候,遇到了AddVertexAt方法的用 ...

  8. Autocad中使用命令来调用python对Autocad二次开发打包后的exe程序

    在Autocad中直接调用Python二次开发程序是有必要的,下面介绍一种方法来实现这个功能: 其基本思路是:先将二次开发的程序打包为可执行程序exe,然后编写lsp文件,该文件写入调用exe程序的语 ...

  9. 【NX二次开发】PMI线性标注

    PMI线性标注,二次开发的难点在于控制尺寸的位置,多花点儿时间都能搞出来,想走捷径最下面就是源码. 只需要摆好工作坐标,然后指定你要标注尺寸的两个点,就可以很方便得利用这个封装函数做出你想要的PMI. ...

随机推荐

  1. [springboot 开发单体web shop] 4. Swagger生成Javadoc

    Swagger生成JavaDoc 在日常的工作中,特别是现在前后端分离模式之下,接口的提供造成了我们前后端开发人员的沟通 成本大量提升,因为沟通不到位,不及时而造成的[撕币]事件都成了日常工作.特别是 ...

  2. C++学习笔记1_ 指针.引用

    1.引用的本质struct typeA{ int &a;}struct typeB{ int *a;}int main(void){ cout<<sizeof(struct typ ...

  3. IDEA复制多行及对多行代码上下左右移动

    复制: 复制一行可不需要选中 多行需要选中 mac:command+D window:ctrl+D 移动: 选中代码 左移:tab+shift 右移:tab 上移:shift+alt+向上方向键 下移 ...

  4. P3521 [POI2011]ROT-Tree Rotations(线段树合并)

    一句话题意(不用我改了.....):给一棵n(1≤n≤200000个叶子的二叉树,可以交换每个点的左右子树,要求前序遍历叶子的逆序对最少. ......这题输入很神烦呐... 给你一棵二叉树的dfs序 ...

  5. 自动任务调度 - Timer

    一.概述: 最近维护一个老项目,里面使用的是Timer的时间调度器,以前没接触过,对着代码鼓捣了半天,查阅了部分博客,最后总结出自己的见解,新项目一般是不会用这种老掉牙的时间调度器了,但是维护老项目还 ...

  6. Golang 类型定义总结手册| 面试最基础

    变量 var 关键字是 var ,定义后须被调用 支持多个同时定义 支持使用 := 缺省定义 变量定义(声明) //使用var 关键字 进行变量定义 : var + 变量名 + 变量类型 //Var ...

  7. linux4.1内核配置以及编译及千兆网卡dp83867网卡驱动移植

    一  内核配置编译 1首先解压内核 tar jxvf linux-at91-4.1.tar.bz2: 2下载编译链 在ubuntu命令行中输入sudo apt-get install gcc-arm- ...

  8. JavaScript部分案例

    JavaScript 是 Web 的编程语言. 所有现代的 HTML 页面都使用 JavaScript. JavaScript 非常容易学. 阅读本教程,您需要有以下基础: HTML 教程 CSS 教 ...

  9. jquery手机端横屏判断方法

    jquery手机端横屏判断方法<pre>$(function() { var bodywidth = $('body').width(); var bodyheight = $('body ...

  10. MyBatis:统计数量(查询所有)

    返回值的类型:resultType="java.lang.Integer". <select id="count" resultType="ja ...