1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > auto CAD 二次开发 c#.net 之JIG画多段线(polyline)

auto CAD 二次开发 c#.net 之JIG画多段线(polyline)

时间:2019-08-14 17:00:08

相关推荐

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给我。 若转载,请注明出处。谢谢。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。