问题描述: 

实现类似动态绘制直线时,在【动态绘制过程】输入长度,就可以绘制直线,该如何实现呢?
测试了一下Drawjig类,发现【prompts.AcquireDistance】支持输入长度,但是只返回长度,没有角度无法确定下一点。然后【prompts.AcquirePoint】直接返回下一点,但不支持用户输入长度。

步骤指引:

  1. 可以使用AcquirePoint + 接管用户输入
  2. 缓存基点(起点)

  3. 缓存最近一次鼠标方向向量

  4. 如果输入是字符串 / 数字 → 自己算点

    class LineLengthJig : DrawJig
    {
        private Point3d _basePt;
        private Point3d _curPt;
        private Vector3d _dir = Vector3d.XAxis;
    
        public LineLengthJig(Point3d basePt)
        {
            _basePt = basePt;
            _curPt = basePt;
        }
    
        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            JigPromptPointOptions opt =
                new JigPromptPointOptions("\n指定下一点或输入长度:");
    
            opt.BasePoint = _basePt;
            opt.UseBasePoint = true;
    
            opt.UserInputControls =
                UserInputControls.Accept3dCoordinates |
                UserInputControls.AcceptOtherInputString;
    
            PromptPointResult res = prompts.AcquirePoint(opt);
    
            // 鼠标输入
            if (res.Status == PromptStatus.OK)
            {
                if (_curPt == res.Value)
                    return SamplerStatus.NoChange;
    
                _curPt = res.Value;
                _dir = _basePt.GetVectorTo(_curPt);
                return SamplerStatus.OK;
            }
    
            // 键盘输入(长度)
            if (res.Status == PromptStatus.Other)
            {
                if (double.TryParse(res.StringResult, out double len))
                {
                    if (_dir.Length < Tolerance.Global.EqualPoint)
                        _dir = Vector3d.XAxis; // 默认方向
    
                    _curPt = _basePt + _dir.GetNormal() * len;
                    return SamplerStatus.OK;
                }
            }
    
            return SamplerStatus.Cancel;
        }
    
        protected override bool WorldDraw(WorldDraw draw)
        {
            draw.Geometry.WorldLine(_basePt, _curPt);
            return true;
        }
    
        public Point3d EndPoint => _curPt;
    }
    
    [CommandMethod("JIGLINE")]
    public void JigLine()
    {
        Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
    
        PromptPointResult ppr =
            ed.GetPoint("\n指定起点:");
    
        if (ppr.Status != PromptStatus.OK)
            return;
    
        LineLengthJig jig = new LineLengthJig(ppr.Value);
    
        if (ed.Drag(jig).Status == PromptStatus.OK)
        {
            using (Transaction tr =
                ed.Document.Database.TransactionManager.StartTransaction())
            {
                Line ln = new Line(ppr.Value, jig.EndPoint);
                BlockTableRecord btr =
                    (BlockTableRecord)tr.GetObject(
                        SymbolUtilityServices.GetBlockModelSpaceId(
                            ed.Document.Database),
                        OpenMode.ForWrite);
    
                btr.AppendEntity(ln);
                tr.AddNewlyCreatedDBObject(ln, true);
                tr.Commit();
            }
        }
    }
英文空间链接


★中望CAD 二次开发JIG实体如何动态输入长度绘制直线?https://confluence.zwcad.com/pages/viewpage.action?pageId=308806170

  • No labels