飞诗(fsxm) 发表于 2009-12-29 23:18:00

[原创]封装简化Jig操作,函数式实现DrawJig

测试代码:

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..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
{
    ///
    /// 封装简化Jig操作,函数式实现DrawJig
    ///
    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
      /// 回调函数之参数
      public class Result
      {
            PromptResult rst;
            WorldDraw draw;
            internal Result(PromptResult promptResult, WorldDraw worldDraw)
            {
                rst = promptResult;
                draw = worldDraw;
            }
            /// 原始返回值
            public PromptResult PromptResult
            {
                get { return rst; }
            }
            /// 绘图对象
            public Geometry Geometry
            {
                get { return draw.Geometry; }
            }
            /// 绘图方法
            public void Draw(Drawable entity)
            {
                if (draw != null)
                  draw.Geometry.Draw(entity);
            }
            /// GetPoint模式下的当前点位置
            public Point3d Point
            {
                get { return _point3d; }
            }
            /// GetDist模式下的当前距离
            public Double Dist
            {
                get { return _double; }
            }
            /// GetAngle模式下的当前角度
            public Double Angle
            {
                get { return _double; }
            }
            /// 返回值指示
            public PromptStatus Status
            {
                get { return rst.Status; }
            }
            /// 返回的字串or关键字
            public string String
            {
                get { return rst.StringResult; }
            }
      }
      static Action _callBack;
      static AcquireMod _acquireMod;
      /// 简便快捷执行Jig托动
      /// 选项
      /// 回调函数
      public static PromptResult StartDrag(JigPromptOptions options, Action 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;
      }
      /// 简便快捷执行Jig托动
      /// 提示信息
      /// 关键字
      /// 回调函数
      public static PromptResult StartDrag(string msg, string kwd, Action callFun)
      {
            return StartDrag(new JigPromptPointOptions(msg, kwd), callFun);
      }
      /// 简便快捷执行Jig托动
      /// 提示信息
      /// 回调函数
      public static PromptResult StartDrag(string msg, Action 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
}

飞诗(fsxm) 发表于 2009-12-31 00:06:00

将上次的函数做了优化,最大可能的减少强制类型转换
接下来准备着手输出函数,给Lisp调用
偶是Lisp转过来的

wwwliuyu 发表于 2010-1-18 20:26:00

飞诗你人真强,.NET学得这么好了。

游天居士 发表于 2010-1-20 18:05:00

飞诗。支持。我还不是很懂。收下了。

chase_wang 发表于 2010-9-13 09:32:00

平台:vs.net2008(C#), Autodesk Map3d 2010.
求用C#实现命令“创建要素”的方法。
谢谢

jxphklibin 发表于 2011-5-19 11:06:00

还是c语言强大,顶

lionguns 发表于 2011-6-8 09:22:00

飞诗也学C#了呀,我也从LSP跑到C#了呵呵

Liszt 发表于 2011-6-20 09:44:00

错误      1      无效的表达式项“>”

cooolseee 发表于 2011-6-20 12:06:00

共同学习中,一起进步中,我向你们学习,学习你们的谨慎

魔子幻灭 发表于 2012-8-31 21:19:00

收藏了,等C#有基础了再看!
页: [1]
查看完整版本: [原创]封装简化Jig操作,函数式实现DrawJig