auto CAD 二次开发 c#.net 之JIG画多段线(polyline)
在CAD 命令中画多段线的命令为:pline。下面将介绍一种JIG画多段线的方法。
首先,来介绍一下JIG这个东西。
EntityJig(实体动态预览),按照指定步骤模仿特定类型的单个实体对象。首先,定义一个类,该类继承于: Autodesk.AutoCAD.EditorInput.EntityJig。继承该类后,需要重写两个方法。 protected override SamplerStatus Sampler(JigPrompts prompts)和 protected override bool Update()这两个方法。
与正常的Prompt一样需要处理JIG的Prompt。
在Sampler方法中编写如下代码:处理提示消息。并获得用户输入信息
JigPromptPointOptions jigOpts = new JigPromptPointOptions();
jigOpts.UserInputControls = (UserInputControls.Accept3dCoordinates |
UserInputControls.NullResponseAccepted |
UserInputControls.NoNegativeResponseAccepted);
在Update方法中更新每一次的拖拽。
完整代码如下:
public class ClsDrawJigLine : Autodesk.AutoCAD.EditorInput.EntityJig
{
#region 成员变量
public static int color = 0;
public static Point3dCollection m_pts;
Point3d m_tempPoint;
Plane m_plane;
#endregion
#region 构造方法
public ClsDrawJigLine(Matrix3d ucs)
: base(new Polyline())
{
m_pts = new Point3dCollection();
Point3d origin = new Point3d(0, 0, 0);
Vector3d normal = new Vector3d(0, 0, 1);
normal = normal.TransformBy(ucs);
m_plane = new Plane(origin, normal);
Polyline pline = Entity as Polyline;
pline.SetDatabaseDefaults();
pline.Normal = normal;
pline.ColorIndex = color;
pline.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);
}
#endregion
/// <summary>
/// 画线
/// </summary>
/// <param name=""></param>
/// <returns></returns>
public Point3dCollection DragLine(int colorIndex)
{
return ClsDrawJigLine.PolyJig(colorIndex);
}
#region 画线方法
/// <summary>
/// 画线
/// </summary>
/// <param name="colorIndex">颜色</param>
/// <returns></returns>
public static Point3dCollection PolyJig(int colorIndex)
{
color = colorIndex;
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
Matrix3d ucs = ed.CurrentUserCoordinateSystem;
ClsDrawJigLine jig = new ClsDrawJigLine(ucs);
bool bSuccess = true, bComplete = false;
do
{
PromptResult res = ed.Drag(jig);
bSuccess = (res.Status == PromptStatus.OK);
if (bSuccess)
jig.AddLatestVertex();
if (res.Status != PromptStatus.OK)
{
Confirm frm = new Confirm();
frm.ShowDialog();
Confirm.Operate operateType = frm._operrateType;
switch (operateType)
{
case Confirm.Operate.Continue:
bSuccess = true;
bComplete = false;
break;
case Confirm.Operate.Back:
if (m_pts.Count > 0)
{
m_pts.RemoveAt(m_pts.Count - 1);
bSuccess = true;
bComplete = false;
jig.RemoveLastVertex();
}
break;
case Confirm.Operate.Accept:
break;
}
}
} while (bSuccess && !bComplete);
return m_pts;
}
#endregion
#region 用于在完成Drag后,移除最后个虚构的点
/// <summary>
/// 用于在完成Drag后,移除最后个虚构的点
/// </summary>
public void RemoveLastVertex()
{
Polyline pline = Entity as Polyline;
if (pline.NumberOfVertices > 1)
{
pline.RemoveVertexAt(m_pts.Count);
}
}
#endregion
#region 在添加一个点时激发事件
/// <summary>
/// 事件委托
/// </summary>
/// <param name="e">事件参数</param>
public delegate void AddHandle(EventArgs e);
public class EventArgs
{
public EventArgs(Point3dCollection currentPoints, int index)
{
this.currentPoints = currentPoints;
this.index = index;
}
/// <summary>
/// 当前点集合
/// </summary>
private Point3dCollection currentPoints;
/// <summary>
/// 当前点集合
/// </summary>
public Point3dCollection CurrentPoints
{
get { return currentPoints; }
}
/// <summary>
/// 当前点索引
/// </summary>
private int index;
/// <summary>
/// 当前点索引
/// </summary>
public int Index
{
get { return index; }
}
/// <summary>
/// 是否取消点绘制
/// </summary>
private bool isCancel = false;
/// <summary>
/// 是否取消点绘制
/// </summary>
public bool IsCancel
{
get { return isCancel; }
set { isCancel = value; }
}
}
/// <summary>
/// 添加一个点时激发事件
/// </summary>
public static event AddHandle AddPoint;
#endregion
#region 总是设置polyline为一个虚构的点,在完成Drag后,此点会被移除
/// <summary>
/// 总是设置polyline为一个虚构的点,在完成Drag后,此点会被移除
/// </summary>
public void AddLatestVertex()
{
m_pts.Add(m_tempPoint);
Polyline pline = Entity as Polyline;
pline.AddVertexAt(pline.NumberOfVertices, new Point2d(m_tempPoint.X, m_tempPoint.Y), 0, 0, 0);
if (AddPoint != null)
{
EventArgs e = new EventArgs(m_pts, m_pts.Count - 1);
e.IsCancel = false;
AddPoint(e);
if (e.IsCancel)
{
RemoveLastVertex();
}
}
}
#endregion
#region 取样
/// <summary>
/// 取样
/// </summary>
/// <param name="prompts"></param>
/// <returns></returns>
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptPointOptions jigOpts = new JigPromptPointOptions();
jigOpts.UserInputControls = (UserInputControls.Accept3dCoordinates |
UserInputControls.NullResponseAccepted |
UserInputControls.NoNegativeResponseAccepted);
if (m_pts.Count == 0)
{
jigOpts.Message = "\n请输入起点坐标 ";
}
else if (m_pts.Count > 0)
{
jigOpts.BasePoint = m_pts[m_pts.Count - 1];
jigOpts.UseBasePoint = true;
jigOpts.Message = "\n请输入下一个点[或按ESC退出] ";
}
else
return SamplerStatus.Cancel;
PromptPointResult res = prompts.AcquirePoint(jigOpts);
if (m_tempPoint == res.Value)
{
return SamplerStatus.NoChange;
}
else if (res.Status == PromptStatus.OK)
{
m_tempPoint = res.Value;
return SamplerStatus.OK;
}
return SamplerStatus.Cancel;
}
#endregion
#region 更新
/// <summary>
/// 更新
/// </summary>
/// <returns></returns>
protected override bool Update()
{
Polyline pline = Entity as Polyline;
pline.SetPointAt(pline.NumberOfVertices - 1, m_tempPoint.Convert2d(m_plane));
Matrix3d m = Matrix3d.Displacement(new Vector3d(0, 0, 1000));
pline.TransformBy(m);
return true;
}
#endregion
}
其中confirm是一个窗体。用来和用户交互信息,实现交互操作
后台代码为:
/// <summary>
/// </summary>
public partial class Confirm : Form
{
/// <summary>
/// </summary>
// ARXClass _aRXClass = null;
#region 构造方法
/// <summary>
/// 构造方法
/// </summary>
public Confirm()
{
// _aRXClass = new ARXClass();
InitializeComponent();
}
#endregion
#region 操作类型
/// <summary>
/// 操作类型
/// </summary>
public enum Operate
{
/// <summary>
/// 继续
/// </summary>
Continue = 0,
/// <summary>
/// 后退
/// </summary>
Back = 1,
/// <summary>
/// 确定
/// </summary>
Accept = 2
}
#endregion
#region 类成员
/// <summary>
/// 类成员
/// </summary>
public Operate _operrateType = Operate.Continue;
#endregion
#region 点击继续按钮激发事件
/// <summary>
/// 点击继续按钮激发事件
/// </summary>
private void btnContinue_Click(object sender, EventArgs e)
{
this._operrateType = Operate.Continue;
this.Close();
}
#endregion
#region 点击回退按钮激发事件
/// <summary>
/// 点击回退按钮激发事件
/// </summary>
private void btnBack_Click(object sender, EventArgs e)
{
this._operrateType = Operate.Back;
this.Close();
}
#endregion
#region 点击确定按钮激发事件
/// <summary>
/// 点击确定按钮激发事件
/// </summary>
private void btnAccept_Click(object sender, EventArgs e)
{
// 确定
this._operrateType = Operate.Accept;
// 关闭窗体
this.Close();
}
#endregion
}
以上方法仅供参考。
若各位大侠手里有更好的方法,请顺便贴一下,谢谢。
如有问题:请加qq1419226548或QQmail给我。 若转载,请注明出处。谢谢。
auto CAD 二次开发 c#.net 之JIG画多段线(polyline)相关推荐
- autocad.net 画多段线_auto CAD 二次开发 c#.net 之JIG画多段线(polyline)
在CAD 命令中画多段线的命令为:pline.下面将介绍一种JIG画多段线的方法. 首先,来介绍一下JIG这个东西. EntityJig(实体动态预览),按照指定步骤模仿特定类型的单个实体对象.首先, ...
- Auto CAD二次开发:基于VBA语言的画圆软件开发
本文已经首发在个人微信公众号:工业机器人仿真与编程(微信号:IndRobSim),欢迎关注! 不仅是CATIA软件具有VBA二次开发接口,很多机械设计软件也都具有VBA二次开发接口.本期,就来为大家介 ...
- 【CAD二次开发】最完美EntityJig画polyline arc实例
下面是效果图 2021/10/11更新添加可以捕捉拐点的功能 public static class JigCommand{public static void RunBulgePolyJig(str ...
- C# CAD二次开发通过代码模拟 人工 画齿轮的算法思路
[CommandMethod("FirstLine")]public void FirstLine(){Editor ed = Application.DocumentManage ...
- CAD二次开发资料汇总
欢迎加入建筑信息化开发交流群,获取开发资料 群号:711844216 一.背景 对工程出身的朋友来说,CAD就像一位朋友,常伴吾身,为了提高效率,于是小伙伴门开始尝试CAD二次开发,刚入门,资料去哪找 ...
- 常用的CAD二次开发语言:AutoLISP, LISP是List Processor(表处理程序)的缩写,cad系统的第一代开发工具注意和eclipse(IDE编程环境,java实现的)
常用的CAD二次开发语言: 1 AutoLISP 全名是LIST Processing Language,LISP是List Processor(表处理程序)的缩写,cad系统的第一代开发工具,是一种 ...
- cad二次开发程序的绿色安装
此问题的引入是因为一个cad二次开发软件,客户要求做成绿色安装.何为绿色安装呢?软件包copy到一台电脑上(已安装CAD),双击exe,打开的界面包含自定义的菜单.菜单的图标能正确显示.每个功能能使用 ...
- intersect 相交 范围_关于CAD二次开发中(范围线自相交)相交线的问题
CAD二次开发对于毕业没多久,又是半路出家的我来说,是一个很大的挑战,遇到过很多难以解决的问题,这次在CAD二次开发遇到一个很苦恼的问题:判断 一个由线段组成的闭合区域中,是否存在着相互之间相交的线段 ...
- CAD 二次开发 图层操作(1)创建图层
CAD二次开发的资料比较少,除了李冠亿先生的<深居浅出AutoCAD二次开发>这本书之外,目前没有找到合适的参考资料.现将自己工作中用的的一些方法贴出来.方便各位网友快速入门. #regi ...
最新文章
- Android应用程序消息处理机制(Looper、Handler)分析(2)
- Spring详解(一):简介
- Keepalived简介
- 手机知识:手机蓝牙有6个使用场景,你都知道吗?
- 读《持续交付2.0》
- 第八十四期: Java、Web 和移动程序员学习的 12 个框架
- linux-简单进程查询
- OpenCV数据结构
- Delphi实现带有格式的Excel导出功能
- sql两张表,分组或row_number()取最新的记录SQL
- Ansys APDL的超声换能器的模态分析(更新中)
- 【转载】Log4j配置详解之log4j.xml
- 数据新闻大趋势 释放可视化报道的力量------读后随笔
- D2RQ 的安装和基本使用
- 底层码农的Stanford梦 --- 从SCPD开始 [转]
- 【微信公众号】7、SpringBoot整合WxJava新增临时、永久素材
- VMware Workstation安装教程
- 数据仓库系列:初识数仓
- c程序语言难么,c语言难不难
- unicode转中文 中文转unicode的简单方式