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; 
        }
    }
}
AutoCAD .net开发人员手册中文版 文档介绍: 当前版本为20101128版,为第一个CHM版本,如需更新版本,请及时关注http://www.01vb.com,也可以查看CHM文件中的前言部分的版本通知。 因本版本制作仓促,还有如下不完善的内容。 1、还有一章内容没有翻译完成; 2、目录部分和索引部分还是英文版本,但具体页面中全部是中英文对照(有些在提示中有些在翻译上面)。 本《AutoCAD .NET 开发人员手册》由01VB编程站翻译并提供,版权所有,原英文版本版权归原版权所有者所有。本手册为免费版本,可在网上随意发送,但必须注明出处(01VB编程站及网站链接http://www.01vb.com)及翻译者。 序言 自从 AutoCAD 支持使用 .NET 开发以来,所有关于 .NET 的官方开发资料全部是英文版本,给国内开发者的学习带来了一定的阻碍,为了给广大 .NET 爱好者提供更多方便,于是决定翻译一部分资料。 因本人英文水平及CAD二次开发水平有限,翻译的资料中也许有表达不清楚的地方,请大家谅解,也可以在资料底部找到留言的链接,给我留言或直接点击QQ联系我。 01VB编程站是一个非营利性的网站,但是,网站要生存,必须有经济来源。因此,本人在本手册中的投放了广告,但是,广告都是在正文内容的底部,不影响阅读。原则上我不鼓励大家点击上面的广告,除非真的对广告内容感兴趣。因广告给您带来的不便,还望谅解。 最近一段时间,老婆一直生病,始终没有痊愈,在此,我希望老婆能快点儿好起来, 并想对她说一句:老婆,别哭,好好养病,病痛在你身,也疼在我心。你累了,我会背你;钱花光了,我会去挣,身体是第一位的。如果你也想给我老婆送上祝福,请留言,谢谢!(2010.11.24) 翻译历史 2010年8月中旬 开始,期间由于本人生病,中断了几天,还有部分内容没有翻译完成,仍然在翻译中。 11.6 更新《图层状态管理器的使用》部分。 11.07 更新《文字样式》部分 11.08 更新完《创建和编辑AutoCAD图元》这章。 11.11 更新 《标注的概念》 部分 11.12 更新《创建标注》部分 11.15 更新完 《创建引线和注释》 部分 11.20 更新完《形位公差》 部分 11.24 更新到 《在三维空间中编辑 》 11.28 修正手册中的脚本错误,进行CHM格式文档的制作并在01VB编程站首发。 感谢 《AutoCAD .NET 开发人员手册》的翻译过程得到 明经通道 网站 "明经 AutoCAD.NetApi 群"中许多网友的帮助,像 MCCAD、雪山飞狐、Still等等,另外还有其它人记不清楚了,因为太多太多,总之两个字,谢谢。 版权 本开发人员手册版权属01VB编程站网站所有。 翻译者:黄明新(平凡)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值