[原创]封装简化Jig操作,函数式实现DrawJig
本帖最后由 作者 于 2009-12-31 0:03:40 编辑测试代码:
public void cc()
{
Circle cir = new Circle(new Point3d(), Vector3d.ZAxis, 10.0);
var v = Drag.StartDrag("\n开始Jig托动:",
rst => { cir.Center = rst.Point; rst.Draw(cir); }
);
if (v.Status != PromptStatus.OK)
return;
v = Drag.StartDrag(
new JigPromptDistanceOptions
("\n指定半径:"),
rst =>
{
cir.Radius = rst.Dist == 0.0 ? 1e-6 : rst.Dist;
rst.Draw(cir);
}
);
if (v.Status == PromptStatus.OK)
CAD.AppendEntity(cir);
}
封装成函数的jig~调用是不是很方便呢???
CAD.AppendEntity()函数请自写!(加入Entity到当前图面)删除这行也能单独测试Jig
下面函数式Jig模块代码:
#define 测试中
using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Runtime;
using System.Windows.Forms;
using System.Drawing;
namespace FsxmAcad
{
/// <summary>
/// 封装简化Jig操作,函数式实现DrawJig
/// </summary>
public class Drag : DrawJig
{
protected Drag() { }
#region =====封装Jig方法=====
protected override bool WorldDraw(WorldDraw draw)
{
_callBack(new Result(_rst, draw));
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
return _acquireMod(prompts);
}
#endregion
#region 定义各种Jig模式
static PromptResult _rst;
delegate SamplerStatus AcquireMod(JigPrompts prompts);
//AcquirePoint
static Point3d _point3d;
static JigPromptPointOptions _PointOptions;
static SamplerStatus GetPoint(JigPrompts prompts)
{
PromptPointResult rst = prompts.AcquirePoint(_PointOptions);
_rst = rst;
if (rst.Value != _point3d)
{
_point3d = rst.Value;
return SamplerStatus.OK;
}
else
{
return SamplerStatus.NoChange;
}
}
//AcquireDistance
static Double _double;
static JigPromptDistanceOptions _DistanceOptions;
static SamplerStatus GetDistance(JigPrompts prompts)
{
var rst = prompts.AcquireDistance(_DistanceOptions);
_rst = rst;
if (rst.Value != _double)
{
_double = rst.Value;
return SamplerStatus.OK;
}
else
{
return SamplerStatus.NoChange;
}
}
//AcquireAngle
static JigPromptAngleOptions _AngleOptions;
static SamplerStatus GetAngle(JigPrompts prompts)
{
var rst = prompts.AcquireAngle(_AngleOptions);
_rst = rst;
if (rst.Value != _double)
{
_double = rst.Value;
return SamplerStatus.OK;
}
else
{
return SamplerStatus.NoChange;
}
}
//AcquireString
static Point _cur;
static JigPromptStringOptions _StringOptions;
static SamplerStatus GetString(JigPrompts prompts)
{
var rst = prompts.AcquireString(_StringOptions);
_rst = rst;
var cur = Form.MousePosition;
if (Form.MousePosition != _cur)
{
_cur = cur;
return SamplerStatus.OK;
}
else
{
return SamplerStatus.NoChange;
}
}
#endregion
/// <summary>回调函数之参数</summary>
public class Result
{
PromptResult rst;
WorldDraw draw;
internal Result(PromptResult promptResult, WorldDraw worldDraw)
{
rst = promptResult;
draw = worldDraw;
}
/// <summary>原始返回值</summary>
public PromptResult PromptResult
{
get { return rst; }
}
/// <summary>绘图对象</summary>
public Geometry Geometry
{
get { return draw.Geometry; }
}
/// <summary>绘图方法</summary>
public void Draw(Drawable entity)
{
if (draw != null)
draw.Geometry.Draw(entity);
}
/// <summary>GetPoint模式下的当前点位置</summary>
public Point3d Point
{
get { return _point3d; }
}
/// <summary>GetDist模式下的当前距离</summary>
public Double Dist
{
get { return _double; }
}
/// <summary>GetAngle模式下的当前角度</summary>
public Double Angle
{
get { return _double; }
}
/// <summary>返回值指示</summary>
public PromptStatus Status
{
get { return rst.Status; }
}
/// <summary>返回的字串or关键字</summary>
public string String
{
get { return rst.StringResult; }
}
}
static Action<Result> _callBack;
static AcquireMod _acquireMod;
/// <summary>简便快捷执行Jig托动</summary>
/// <param name="options">选项</param>
/// <param name="callFun">回调函数</param>
public static PromptResult StartDrag(JigPromptOptions options, Action<Result> callFun)
{
if (options is JigPromptPointOptions)
{
_PointOptions = options as JigPromptPointOptions;
_acquireMod = GetPoint;
}
else if (options is JigPromptDistanceOptions)
{
_DistanceOptions = options as JigPromptDistanceOptions;
_acquireMod = GetDistance;
}
else if (options is JigPromptAngleOptions)
{
_AngleOptions = options as JigPromptAngleOptions;
_acquireMod = GetAngle;
}
else if (options is JigPromptStringOptions)
{
_StringOptions = options as JigPromptStringOptions;
_acquireMod = GetString;
}
_callBack = callFun;
Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument.Editor.Drag(new Drag());
_callBack(new Result(_rst, null));
return _rst;
}
/// <summary>简便快捷执行Jig托动</summary>
/// <param name="msg">提示信息</param>
/// <param name="kwd">关键字</param>
/// <param name="callFun">回调函数</param>
public static PromptResult StartDrag(string msg, string kwd, Action<Result> callFun)
{
return StartDrag(new JigPromptPointOptions(msg, kwd), callFun);
}
/// <summary>简便快捷执行Jig托动</summary>
/// <param name="msg">提示信息</param>
/// <param name="callFun">回调函数</param>
public static PromptResult StartDrag(string msg, Action<Result> callFun)
{
return StartDrag(new JigPromptPointOptions(msg), callFun);
}
}
#if 测试中
public class test
{
public void tt()
{
Circle cir = new Circle(new Point3d(), Vector3d.ZAxis, 10.0);
var v = Drag.StartDrag("\n指定圆中心点:",
rst => { cir.Center = rst.Point; rst.Draw(cir); }
);
if (v.Status != PromptStatus.OK)
return;
v = Drag.StartDrag(
new JigPromptDistanceOptions("\n指定圆半径:"),
rst =>
{
cir.Radius = rst.Dist == 0.0 ? 1e-6 : rst.Dist;
rst.Draw(cir);
}
);
if (v.Status == PromptStatus.OK)
CAD.AppendEntity(cir);
}
}
#endif
}
<p>将上次的函数做了优化,最大可能的减少强制类型转换</p><p>接下来准备着手输出函数,给Lisp调用</p><p> 偶是Lisp转过来的</p> 飞诗你人真强,.NET学得这么好了。 飞诗。支持。我还不是很懂。收下了。
[求助]C#实现命令“创建要素”的方法
<p>平台:vs.net2008(C#), Autodesk Map3d 2010.</p><p>求用C#实现命令“创建要素”的方法。</p>
<p>谢谢</p> 还是c语言强大,顶 飞诗也学C#了呀,我也从LSP跑到C#了呵呵 错误 1 无效的表达式项“>” 共同学习中,一起进步中,我向你们学习,学习你们的谨慎 收藏了,等C#有基础了再看!
页:
[1]
2