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 various AutoCAD entities in many early posts. In this article, let us see how to jig a Solid Wedge using the EntityJig class and the Solid3d.CreateWedge method.
Here is the core code of the Solid Wedge jig 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 SolidWedgeJigger : EntityJig
{
#region Fields
public int mCurJigFactorIndex = 1; // Jig Factor Index
public Point3d mPosition = new Point3d(); // Jig Factor #1
public double mLength = 1.0; // Jig Factor #2
public double mWidth = 1.0; // Jig Factor #3
public double mHeight = 1.0; // Jig Factor #4
#endregion
#region Constructors
public SolidWedgeJigger(Solid3d ent)
: base(ent)
{
// Initialize and transform the Entity.
Entity.SetDatabaseDefaults();
Entity.CreateWedge(mLength,mWidth, mHeight);
Entity.TransformBy(UCS);
}
#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 Solid3d Entity // Overload the Entity property for convenience.
{
get
{
return base.Entity as Solid3d;
}
}
protected override bool Update()
{
try { Entity.CreateWedge(mLength, mWidth, mHeight); }
catch { } // Skip the eOutOfRange exception if any.
// The CreateWedge() method resets everything back, so the displacement and UCS has to be applied every time.
Matrix3d mat = Matrix3d.Displacement(mPosition.GetAsVector());
Entity.TransformBy(UCS.PreMultiplyBy(mat));
//Entity.TransformBy(mat);
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
switch (mCurJigFactorIndex)
{
case 1:
JigPromptPointOptions prOptions1 = new JigPromptPointOptions("\nLocation:");
prOptions1.UseBasePoint = false;
PromptPointResult prResult1 = prompts.AcquirePoint(prOptions1);
if (prResult1.Status == PromptStatus.Cancel && prResult1.Status == PromptStatus.Error)
return SamplerStatus.Cancel;
if (prResult1.Value.Equals(mPosition)) //Use better comparison method if necessary.
{
return SamplerStatus.NoChange;
}
else
{
mPosition = prResult1.Value;
return SamplerStatus.OK;
}
case 2:
JigPromptDistanceOptions prOptions2 = new JigPromptDistanceOptions("\nLength:");
prOptions2.BasePoint = mPosition;
prOptions2.UseBasePoint = true;
PromptDoubleResult prResult2 = prompts.AcquireDistance(prOptions2);
if (prResult2.Status == PromptStatus.Cancel || prResult2.Status == PromptStatus.Error)
return SamplerStatus.Cancel;
if (prResult2.Value.Equals(mLength)) //Use better comparison method if necessary.
{
return SamplerStatus.NoChange;
}
else
{
mLength = prResult2.Value;
return SamplerStatus.OK;
}
case 3:
JigPromptDistanceOptions prOptions3 = new JigPromptDistanceOptions("\nWidth:");
prOptions3.BasePoint = mPosition;
prOptions3.UseBasePoint = true;
PromptDoubleResult prResult3 = prompts.AcquireDistance(prOptions3);
if (prResult3.Status == PromptStatus.Cancel || prResult3.Status == PromptStatus.Error)
return SamplerStatus.Cancel;
if (prResult3.Value.Equals(mLength)) //Use better comparison method if necessary.
{
return SamplerStatus.NoChange;
}
else
{
mWidth = prResult3.Value;
return SamplerStatus.OK;
}
case 4:
JigPromptDistanceOptions prOptions4 = new JigPromptDistanceOptions("\nHeight:");
prOptions4.BasePoint = mPosition;
prOptions4.UseBasePoint = true;
PromptDoubleResult prResult4 = prompts.AcquireDistance(prOptions4);
if (prResult4.Status == PromptStatus.Cancel || prResult4.Status == PromptStatus.Error)
return SamplerStatus.Cancel;
if (prResult4.Value.Equals(mLength)) //Use better comparison method if necessary.
{
return SamplerStatus.NoChange;
}
else
{
mHeight = prResult4.Value;
return SamplerStatus.OK;
}
default:
break;
}
return SamplerStatus.OK;
}
#endregion
#region Methods to Call
public static Solid3d Jig()
{
SolidWedgeJigger jigger = null;
try
{
jigger = new SolidWedgeJigger(new Solid3d());
PromptResult pr;
do
{
pr = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.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 <= 4);
if (pr.Status == PromptStatus.Cancel || pr.Status == PromptStatus.Error)
{
if (jigger != null && jigger.Entity != null)
jigger.Entity.Dispose();
return null;
}
else
return jigger.Entity;
}
catch
{
if( jigger != null && jigger.Entity != null )
jigger.Entity.Dispose();
return null;
}
}
#endregion
#region Test Commands
[CommandMethod("TestSolidWedgeJigger")]
public static void TestSolidWedgeJigger_Method()
{
try
{
Entity jigEnt = SolidWedgeJigger.Jig();
if (jigEnt != null)
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, 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 solid Wedge looks like in AutoCAD when its dimension is being jigged:
NOTE: The isometric view is in a UCS instead of WCS, indicating our Solid Wedge Jigger honors UCS perfectly.
A few highlights about the code may be helpful:
• An entity type needs to be specified in the EntityJig derivative, as Solid3d here.
• Since Solid3d only has some common properties for all solids, for the special Wedge shape we need to call a particular method of the Solid3d class, CreateWedge().
• 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 properties of the entity 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 entity 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.
Posted by: |