AutoCAD .Net 使用 DrawJig 来动态地移动、旋转、缩放多个图元

本实例展示使用 DrawJig 技术,动态交互模式地移动、旋转、缩放多个图元。
如下图所示:
这里写图片描述
翻译自: AutoCAD .NET: Use DrawJig to Dynamically Move Rotate and Scale Multiple Entities of Any Kinds

using System;
using System.Collections.Generic;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.GraphicsSystem;
using Autodesk.AutoCAD.GraphicsInterface;

public class MoveRotateScaleJigSample
{
    [CommandMethod("TRSJIG")]
    public static void TRSJIG()
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        if (MoveRotateScaleJig.Jig())
        {
            doc.Editor.WriteMessage("\nSuccess\n");
        }
        else
        {
            doc.Editor.WriteMessage("\nFailure\n");
        }
    }
}

public class MoveRotateScaleJig : DrawJig
{
    private List<Entity> entities = new List<Entity>();
    private int step = 1;
    private int totalStepNum = 3;

    private Point3d moveStartPnt;
    private Point3d moveEndPnt;
    private Double rotateAngle;
    private Double scaleFactor;

    public MoveRotateScaleJig(Point3d basePnt)
    {
        moveStartPnt = basePnt;
        moveEndPnt = moveStartPnt;
        rotateAngle = 0;
        scaleFactor = 1;
    }

    public Matrix3d Transformation
    {
        get
        {
            return Matrix3d.Scaling(scaleFactor, moveEndPnt).
             PostMultiplyBy(Matrix3d.Rotation(rotateAngle, Vector3d.ZAxis, moveEndPnt)).
             PostMultiplyBy(Matrix3d.Displacement(moveStartPnt.GetVectorTo(moveEndPnt)));
        }
    }

    public void AddEntity(Entity ent)
    {
        entities.Add(ent);
    }

    public void TransformEntities()
    {
        Matrix3d mat = Transformation;
        foreach (Entity ent in entities)
        {
            ent.TransformBy(mat);
        }
    }

    protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
    {
        Matrix3d mat = Transformation;

        WorldGeometry geo = draw.Geometry;
        if (geo != null)
        {
            geo.PushModelTransform(mat);

            foreach (Entity ent in entities)
            {
                geo.Draw(ent);
            }

            geo.PopModelTransform();
        }

        return true;
    }

    protected override SamplerStatus Sampler(JigPrompts prompts)
    {
        switch (step)
        {
            case 1:
                JigPromptPointOptions prOptions1 = new JigPromptPointOptions("\nMove:");
                prOptions1.UserInputControls = UserInputControls.GovernedByOrthoMode 
                    | UserInputControls.GovernedByUCSDetect;
                PromptPointResult prResult1 = prompts.AcquirePoint(prOptions1);
                if (prResult1.Status != PromptStatus.OK)
                    return SamplerStatus.Cancel;

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

            case 2:
                JigPromptAngleOptions prOptions2 = new JigPromptAngleOptions("\nRotate:");
                prOptions2.UseBasePoint = true;
                prOptions2.BasePoint = moveEndPnt;
                prOptions2.UserInputControls = UserInputControls.GovernedByOrthoMode 
                    | UserInputControls.GovernedByUCSDetect;
                PromptDoubleResult prResult2 = prompts.AcquireAngle(prOptions2);
                if (prResult2.Status != PromptStatus.OK)
                    return SamplerStatus.Cancel;

                if (prResult2.Value.Equals(rotateAngle))
                {
                    return SamplerStatus.NoChange;
                }
                else
                {
                    rotateAngle = prResult2.Value;
                    return SamplerStatus.OK;
                }

            case 3:
                JigPromptDistanceOptions prOptions3 = new JigPromptDistanceOptions("\nScale:");
                prOptions3.UseBasePoint = true;
                prOptions3.BasePoint = moveEndPnt;
                prOptions3.UserInputControls = UserInputControls.GovernedByOrthoMode 
                    | UserInputControls.GovernedByUCSDetect;
                PromptDoubleResult prResult3 = prompts.AcquireDistance(prOptions3);
                if (prResult3.Status != PromptStatus.OK)
                    return SamplerStatus.Cancel;

                if (prResult3.Value.Equals(scaleFactor))
                {
                    return SamplerStatus.NoChange;
                }
                else
                {
                    scaleFactor = prResult3.Value;
                    return SamplerStatus.OK;
                }

            default:
                break;
        }

        return SamplerStatus.OK;
    }

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

            // 选择对象
            PromptSelectionResult selRes = doc.Editor.GetSelection();
            if (selRes.Status != PromptStatus.OK)
                return false;

            // 指定起点
            PromptPointResult ppr = doc.Editor.GetPoint("\nStart point:");
            if (ppr.Status != PromptStatus.OK)
                return false;
            Point3d basePnt = ppr.Value;
            basePnt = basePnt.TransformBy(doc.Editor.CurrentUserCoordinateSystem);

            // Draw Jig
            MoveRotateScaleJig jig = new MoveRotateScaleJig(basePnt);
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                foreach (ObjectId id in selRes.Value.GetObjectIds())
                {
                    Entity ent = (Entity)tr.GetObject(id, OpenMode.ForWrite);
                    jig.AddEntity(ent);
                }

                // Draw Jig 交互
                PromptResult pr;
                do
                {
                    pr = doc.Editor.Drag(jig);
                    if (pr.Status == PromptStatus.Keyword)
                    {
                        // Keyword handling code
                    }
                    else
                    {
                        jig.step++;
                    }
                }
                while (pr.Status == PromptStatus.OK
                 && jig.step <= jig.totalStepNum);

                // 结果
                if (pr.Status == PromptStatus.OK &&
                    jig.step == jig.totalStepNum + 1)
                {
                    jig.TransformEntities();
                }
                else
                {
                    return false;
                }

                tr.Commit();
                return true;
            }
        }
        catch
        {
            return false; 
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值