|
- 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.
复制代码 |
|