AutoCad 二次开发 jig操作之标注跟随线移动
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操作之标注跟随线移动的更多相关文章
- AutoCad 二次开发 Jig操作之墙块的拖动
测试结果: 主要思路:选择一段多段线,使用封装的jig类进行实时拖动,其原理就是在拖动的时候,确定被拖动的边,我是选择离输入第一个点最近的边作为拖动边,有了这条边,就能确定需要实时更改的点了,然后当鼠 ...
- AutoCad 二次开发 文字镜像
AutoCad 二次开发 文字镜像 参考:https://adndevblog.typepad.com/autocad/2013/10/mirroring-a-dbtext-entity.html 在 ...
- AutoCad 二次开发 .net 之层表的增加 删除 修改图层颜色 遍历 设置当前层
AutoCad 二次开发 .net 之层表的增加 删除 修改图层颜色 遍历 设置当前层 AutoCad 二次开发 .net 之层表的增加 删除 修改图层颜色 遍历 设置当前层我理解的图层的作用大概是把 ...
- AutoCAD二次开发——AutoCAD.NET API开发环境搭建
AutoCAD二次开发工具:1986年AutoLisp,1989年ADS,1990年DCL,1993年ADS-RX,1995年ObjectARX,1996年Active X Automation(CO ...
- 1,下载和部署开发环境--AutoCAD二次开发
环境需求为: AutoCAD 2020版 ObjectARX SDK 下载地址:https://www.autodesk.com/developer-network/platform-technolo ...
- AutoCAD二次开发-使用ObjectARX向导创建应用程序(HelloWorld例子)
AutoCAD2007+vs2005 首先自己去网上搜索下载AutoCAD2007的ARX开发包. 解压后如下 打开后如下 classmap文件夹为C++类和.net类的框架图,是一个DWG文件. d ...
- 我的AutoCAD二次开发之路 (一)
原帖地址 http://379910987.blog.163.com/blog/static/33523797201011184552167/ 今天在改代码的时候,遇到了AddVertexAt方法的用 ...
- Autocad中使用命令来调用python对Autocad二次开发打包后的exe程序
在Autocad中直接调用Python二次开发程序是有必要的,下面介绍一种方法来实现这个功能: 其基本思路是:先将二次开发的程序打包为可执行程序exe,然后编写lsp文件,该文件写入调用exe程序的语 ...
- 【NX二次开发】PMI线性标注
PMI线性标注,二次开发的难点在于控制尺寸的位置,多花点儿时间都能搞出来,想走捷径最下面就是源码. 只需要摆好工作坐标,然后指定你要标注尺寸的两个点,就可以很方便得利用这个封装函数做出你想要的PMI. ...
随机推荐
- 学习笔记02form
1.<form>为表单标签*如果要把数据提交到服务器,则需要将<input> <textarea> <select>等表单元素放到<from> ...
- [2018-08-03] python开发个人资源共享网--第一天
项目需求-环境搭建 python版本:python 3.6.2 开发工具:PyCharm 数据库:MySql5.7.24 数据库管理工具:Navicat 环境搭建完毕 ---------------- ...
- Kong02-KongA 介绍
KongA 是 Kong 的一个 GUI 工具.GitHub 地址是 https://github.com/pantsel/konga . KongA 概述 KongA 带来的一个最大的便利就是可以很 ...
- group:状压dp,轮廓线
神仙题.但是难得的傻孩子cbx没有喊题解,所以也就难得的自己想出来了一个如此神仙的题. 如果是自己想的,说它神仙是不是有点不合适啊..? 反正的确不好像.关键就在于这个标签.颓完标签就差不多会了. % ...
- Elasticsearch 技术分析(八):剖析 Elasticsearch 的索引原理
前言 创建索引的时候,我们通过Mapping 映射定义好索引的基本结构信息,接下来我们肯定需要往 ES 里面新增业务文档数据了,例如用户,日志等业务数据.新增的业务数据,我们根据 Mapping 来生 ...
- 重置root密码!
偶尔把密码忘记了也不用慌,重置密码只需简单几步: 第1步:开机后在内核上敲击“e”. 第2步:在linux16这行的后面输入“rd.break”并敲击“ctrl+x“. 第3步:进入到了系统的紧急求援 ...
- python——inspect模块
inspect模块常用功能 import inspect # 导入inspect模块 inspect.isfunction(fn) # 检测fn是不是函数 inspect.isgenerator((x ...
- 利用python实现微信小程序游戏跳一跳详细教程
利用python实现微信小程序游戏跳一跳详细教程 1 先安装python 然后再安装pip <a href="http://newmiracle.cn/wp-content/uploa ...
- sublime3中运行python文件
sublime3中运行python文件 tools->build system->new build stystem 粘贴下面代码{"cmd":["pytho ...
- jquery mobiscroll移动端日期选择控件(无乱码)
jquery mobiscroll移动端日期选择控件(无乱码) <pre><!DOCTYPE html><html lang="en">< ...