AutoCAD .NET: EntityJig – Dynamic Dimension and Line Jig

转载自:http://spiderinnet1.typepad.com/blog/2012/03/autocad-net-entityjig-dynamic-dimension-and-line-jig.html

AutoCAD .NET: EntityJig – Dynamic Dimension and Line Jig

AutoCAD .NET API provides two concrete Jig classes for us to jig different entities in different circumstances, EntityJig and DrawJig. EntityJig is to jig a specific entity as its name indicates and the DrawJig is to jig anything that has graphics to draw, which can be a single entity, a group of entities, or something that is not available natively in AutoCAD.

We have demonstrated jigging a line segment or multiple segments using the same line jig earlier. In this article, let us see how to add the dynamic dimension support for line jigging.

Here is the core code of the DynDimLineJigger along with a test command:

#region Namespaces

using System;
using System.Text;
using System.Linq;
using System.Xml;
using System.Reflection;
using System.ComponentModel;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

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 MgdAcApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using MgdAcDocument = Autodesk.AutoCAD.ApplicationServices.Document;
using AcWindowsNS = Autodesk.AutoCAD.Windows;

#endregion


namespace AcadNetAddinWizard_Namespace
{
    public class DynDimLineJigger : EntityJig
    {
        #region Fields

        public int mCurJigFactorIndex = 1;  // Jig Factor Index

        public Point3d mStartPoint; // Jig Factor #1
        public Point3d mEndPoint;   // Jig Factor #2

        private DynamicDimensionDataCollection dddCollection = new DynamicDimensionDataCollection();

        #endregion

        #region Constructors

        public DynDimLineJigger(Line ent)
            : base(ent)
        {
            // Initialize and transform the Entity.
            Entity.SetDatabaseDefaults();
            Entity.TransformBy(UCS);

            // Build up the Dynamic Dimension objects.
            Dimension dim1 = new AlignedDimension();
            dim1.SetDatabaseDefaults();
            dim1.TransformBy(UCS);
            dddCollection.Add(new DynamicDimensionData(dim1, true, true));
            dddCollection[0].Focal = false;
            dddCollection[0].Editable = false;
        }

        #endregion

        #region Properties

        private Editor Editor
        {
            get
            {
                return MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
            }
        }

        private Matrix3d UCS
        {
            get
            {
                return MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem;
            }
        }

        #endregion

        #region Overrides

        public new Line Entity  // Overload the Entity property for convenience.
        {
            get
            {
                return base.Entity as Line;
            }
        }

        protected override bool Update()
        {
            switch (mCurJigFactorIndex)
            {
                case 1:
                    Entity.StartPoint = Entity.EndPoint = mStartPoint;
                    Entity.StartPoint.TransformBy(UCS);
                    Entity.EndPoint.TransformBy(UCS);
                    break;
                case 2:
                    Entity.EndPoint = mEndPoint;
                    Entity.EndPoint.TransformBy(UCS);

                    // Change the Dynamic Dimension data below if necessary
                    AlignedDimension dim1 = (AlignedDimension)dddCollection[0].Dimension;
                    dim1.XLine1Point = mStartPoint;
                    dim1.XLine1Point.TransformBy(UCS);
                    dim1.XLine2Point = mEndPoint;
                    dim1.XLine2Point.TransformBy(UCS);
                    dim1.DimLinePoint = mEndPoint.RotateBy(Math.PI/10, Vector3d.ZAxis, mStartPoint);
                    dim1.DimLinePoint.TransformBy(UCS);

                    break;
                default:
                    return false;
            }

            return true;
        }

        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            switch (mCurJigFactorIndex)
            {
                case 1:
                    JigPromptPointOptions prOptions1 = new JigPromptPointOptions("\nStart point:");
                    prOptions1.UseBasePoint = false;
                                    
                    PromptPointResult prResult1 = prompts.AcquirePoint(prOptions1);
                    if (prResult1.Status == PromptStatus.Cancel) return SamplerStatus.Cancel;
                    if (prResult1.Status == PromptStatus.Error) return SamplerStatus.Cancel;

                    if (prResult1.Value.Equals(mStartPoint))  //Use better comparison method if necessary.
                    {
                        return SamplerStatus.NoChange;
                    }
                    else
                    {
                        mStartPoint = prResult1.Value;
                        return SamplerStatus.OK;
                    }
                case 2:
                    JigPromptPointOptions prOptions2 = new JigPromptPointOptions("\nEnd point:");
                    prOptions2.BasePoint = Entity.StartPoint;
                    prOptions2.UseBasePoint = true;
                    
                    PromptPointResult prResult2 = prompts.AcquirePoint(prOptions2);
                    if (prResult2.Status == PromptStatus.Cancel) return SamplerStatus.Cancel;
                    if (prResult2.Status == PromptStatus.Error) return SamplerStatus.Cancel;

                    if (prResult2.Value.Equals(mEndPoint))  //Use better comparison method if necessary.
                    {
                        return SamplerStatus.NoChange;
                    }
                    else
                    {
                        mEndPoint = prResult2.Value;
                        return SamplerStatus.OK;
                    }
                default:
                    break;
            }

            return SamplerStatus.OK;
        }

        protected override DynamicDimensionDataCollection GetDynamicDimensionData(double dimScale)
        {
            base.GetDynamicDimensionData(dimScale);
            return dddCollection;
        }

        #endregion

        #region Method to Call

        public static Line Jig()
        {
            DynDimLineJigger jigger = null;
            try
            {
                Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
                jigger = new DynDimLineJigger(new Line());
                PromptResult pr;
                do
                {
                    pr = ed.Drag(jigger);
                    if (pr.Status == PromptStatus.Keyword)
                    {
                        // Add keyword handling code below

                    }
                    else
                    {
                        jigger.mCurJigFactorIndex++;
                    }
                } while (pr.Status != PromptStatus.Cancel && pr.Status != PromptStatus.Error && jigger.mCurJigFactorIndex <= 2);

                return jigger.Entity;
            }
            catch
            {
                if( jigger != null && jigger.Entity != null ) jigger.Entity.Dispose();
                return null;
            }
        }

        #endregion

        #region Test Command

        [CommandMethod("TestDynDimLineJigger")]
        public static void TestDynDimLineJigger_Method()
        {
            try
            {
                Entity jigEnt = DynDimLineJigger.Jig();
                if (jigEnt != null)
                {
                    Database db = HostApplicationServices.WorkingDatabase;
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                        BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                        btr.AppendEntity(jigEnt);
                        tr.AddNewlyCreatedDBObject(jigEnt, true);
                        tr.Commit();
                    }
                }
            }
            catch (System.Exception ex)
            {
                MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
            }
        }

        #endregion

    }
}

Here is what the dynamic dimension looks like in AutoCAD when the line is being jigged:

A few highlights about the code may be helpful:

• An entity type needs to be specified in the EntityJig derivative, as Line here.
• A DynamicDimensionDataCollection instance needs to be created and initialized in the constructor of the EntityJig.
• The DynamicDimensionDataCollection instance needs to be returned in the GetDynamicDimensionData override.
• The DynamicDimensionDataCollection has to be populated with good DynamicDimensionData elements such as the AlignedDimension as demonstrated and properly indexed.
• Each DynamicDimensionData element should be updated accordingly in the Update() call if its relevant factors have changed.
• The Sampler() override is to acquire input for line points in a certain order.
• If the input is the same as the stored variable, we’d better return SamplerStatus.NoChange to avoid unnecessary flashing; if not, return SamplerStatus.OK.
• The keywords can be added easily through the Keywords collection of the JigPromptPointOptions or other similar prompt options objects.
• Please do not forget to handle the cancel/escape circumstance as demonstrated. 
• The Update() override is to update the point properties of the line in the same order as set in the Sampler().
• The Editor.Draw() is the power to fire the jig. If two properties need to be set, the jig needs to be fired off twice. That is why a while loop is used.
• The while loop needs to think about the PromptStatus.Keyword case of the PromptResult after each Jig Drag.
• Only after the jig succeeds should the circle be added to the database to avoid database corruption.

The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides a coder, Entity Jigger, to help us create entity jig code automatically, quickly and reliably.

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值