AutoCAD .Net EntityJig – Jig Line by Start and End Points

本实例展示使用 EntityJig 技术,动态交互模式创建直线。
这里写图片描述
翻译自: AutoCAD .NET: EntityJig – Jig Line by Start and End Points

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;

public class JigLineSample
{
    [CommandMethod("JigLine")]
    public static void JigLine()
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        if (LineJigger.Jig())
        {
            doc.Editor.WriteMessage("\nsuccess\n");
        }
        else
        {
            doc.Editor.WriteMessage("\nfailure\n");
        }
    }
}

public class LineJigger : EntityJig
{
    public Point3d endPnt = new Point3d();

    public LineJigger(Line line)
        : base(line)
    {
    }

    protected override bool Update()
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;

        (Entity as Line).EndPoint = endPnt;
        return true;
    }

    protected override SamplerStatus Sampler(JigPrompts prompts)
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;

        JigPromptPointOptions prOptions1 = new JigPromptPointOptions("\nNext point:");
        prOptions1.BasePoint = (Entity as Line).StartPoint;
        prOptions1.UseBasePoint = true;
        prOptions1.UserInputControls = UserInputControls.Accept3dCoordinates
            | UserInputControls.AnyBlankTerminatesInput 
            | UserInputControls.GovernedByOrthoMode 
            | UserInputControls.GovernedByUCSDetect 
            | UserInputControls.UseBasePointElevation
            | UserInputControls.InitialBlankTerminatesInput 
            | UserInputControls.NullResponseAccepted;
        PromptPointResult prResult1 = prompts.AcquirePoint(prOptions1);
        if (prResult1.Status == PromptStatus.Cancel)
            return SamplerStatus.Cancel;

        if (prResult1.Value.Equals(endPnt))
        {
            return SamplerStatus.NoChange;
        }
        else
        {
            endPnt = prResult1.Value;
            return SamplerStatus.OK;
        }
    }

    #region

    public static bool Jig()
    {
        try
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;

            PromptPointResult ppr = doc.Editor.GetPoint("\nStart point:");
            if (ppr.Status != PromptStatus.OK)
                return false;
            Point3d pt = ppr.Value;
            Line line = new Line(pt, pt);
            line.TransformBy(doc.Editor.CurrentUserCoordinateSystem);

            LineJigger jigger = new LineJigger(line);
            PromptResult pr = doc.Editor.Drag(jigger);
            if (pr.Status == PromptStatus.OK)
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = tr.GetObject(db.BlockTableId,
                        OpenMode.ForRead) as BlockTable;
                    BlockTableRecord modelSpace = tr.GetObject(
                        bt[BlockTableRecord.ModelSpace],
                        OpenMode.ForWrite) as BlockTableRecord;

                    modelSpace.AppendEntity(jigger.Entity);
                    tr.AddNewlyCreatedDBObject(jigger.Entity, true);
                    tr.Commit();
                }
            }
            else
            {
                line.Dispose();
                return false;
            }

            return true;
        }
        catch
        {
            return false;
        }
    }

    #endregion
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值