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 such as Line, Circle, and Block, performaing various actions dynamically, using either EntityJig or DrawJig, such as Move, Rotate, Copy, and Mirror, and creating various Solid types such as Cone, Cylinder, Pentagon, and so on in many early posts. In this article, let us see how to use jig to offset an curve entity dynamically.
Here is the core code of dynamic offset 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 EntityOffsetJigger : EntityJig
{
#region Fields
public int mCurJigFactorIndex = 1;
private Point3d mBasePoint = new Point3d();
private Point3d mNewPoint; // Factor #1
public Curve OriginalCurve;
#endregion
#region Constructors
public EntityOffsetJigger(Entity ent, Point3d basePoint) : base(ent)
{
OriginalCurve = ent as Curve;
mNewPoint = mBasePoint = basePoint.TransformBy(UCS);
}
#endregion
#region Properties
private Editor Editor
{
get
{
return MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
}
}
private Matrix3d UCS
{
get
{
return Editor.CurrentUserCoordinateSystem;
}
}
#endregion
#region Overrides
protected override bool Update()
{
double dist = ((Curve)Entity).GetClosestPointTo(mNewPoint, true).DistanceTo(mNewPoint);
Curve crv = (OriginalCurve).GetOffsetCurves(dist)[0] as Curve;
if (crv.GetClosestPointTo(mNewPoint, true).DistanceTo(mNewPoint) > 1e-6)
dist = -dist;
CopyProperties.BetweenSameKind((OriginalCurve).GetOffsetCurves(dist)[0], Entity);
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
switch (mCurJigFactorIndex)
{
case 1:
JigPromptPointOptions prOptions1 = new JigPromptPointOptions("\nLocation:");
prOptions1.UserInputControls = UserInputControls.Accept3dCoordinates |
UserInputControls.GovernedByOrthoMode | UserInputControls.GovernedByUCSDetect;
prOptions1.BasePoint = mBasePoint;
prOptions1.UseBasePoint = true;
PromptPointResult prResult1 = prompts.AcquirePoint(prOptions1);
if (prResult1.Status == PromptStatus.Cancel) return SamplerStatus.Cancel;
if (prResult1.Value.Equals(mNewPoint)) //Use better comparision method if wanted.
{
return SamplerStatus.NoChange;
}
else
{
mNewPoint = prResult1.Value;
return SamplerStatus.OK;
}
default:
break;
}
return SamplerStatus.OK;
}
#endregion
#region Methods to Call
public static EntityOffsetJigger jigger = null;
public static bool Jig(Entity ent, Point3d basePt)
{
try
{
jigger = new EntityOffsetJigger(ent, basePt);
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 <= 1);
if (pr.Status == PromptStatus.Cancel || pr.Status == PromptStatus.Error)
{
if (jigger != null && jigger.Entity != null)
jigger.Entity.Dispose();
return false;
}
else
return true;
}
catch
{
if (jigger != null && jigger.Entity != null)
jigger.Entity.Dispose();
return false;
}
}
#endregion
#region Test Commands
[CommandMethod("TestEntityOffsetJigger")]
public static void TestEntityOffsetJigger_Method()
{
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;
try
{
PromptEntityResult selRes = ed.GetEntity("\nPick an entity to offset:");
if (selRes.Status == PromptStatus.OK )
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Entity ent = tr.GetObject(selRes.ObjectId, OpenMode.ForRead) as Entity;
if (ent != null && ent is Curve)
{
Curve curve = ent as Curve;
DBObjectCollection dbCol = curve.GetOffsetCurves(0);
if (EntityOffsetJigger.Jig((Entity)dbCol[0], selRes.PickedPoint))
{
BlockTableRecord ms = tr.GetObject(ent.OwnerId, OpenMode.ForWrite) as BlockTableRecord;
if (ms != null)
{
ms.AppendEntity(jigger.Entity);
tr.AddNewlyCreatedDBObject(jigger.Entity, true);
ent.UpgradeOpen();
ent.Erase();
tr.Commit();
}
}
else
tr.Abort();
}
}
}
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.ToString());
}
}
#endregion
}
}
Here is what the the sceen looks like when a circle is being offset dynamically by the dynamic offset jig:
NOTE: The isometric view is in a UCS instead of WCS, indicating our offset Jigger honors UCS perfectly.
A few highlights about the code may be helpful:
• The Offset Jig is supposed to support simple Enity of AutoCAD Curve type such as Line, Arc, Circle and even XLine.
• The picked point on the entity will determine where to jig the Entity from and to.
• The Sampler() override is to acquire input for the new location of the base point.
• Some UserInputControls flags are used here, GovernedByUCSDetect, UseBasePointElevation, and Accept3dCoordinates to collect a point from UCS but intepreted in WCS.
• 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.
• Please do not forget to handle the cancel/escape circumstance as demonstrated.
• The Update() override is to update new location of the Entity through the TransformBy() call on a Vector calculated from the previous base point and the new input location since not every Entity type has an Location or BasePoint property.
• The old base point needs to be replaced with the new location just collected so that the next movement can still behave well in every Update() call.
• The Editor.Draw() is the power to fire the jig.
• The while loop needs to think about the PromptStatus.Keyword case of the PromptResult after each Jig Drag.
• Keyword handling code can be added as commented.
• Only after the jig succeeds should the entity be offset to the new location.
• The offset curve DBObjectCollection may contain multiple curves. That case may be addressed in a future post.
• The offset curve DBObjectCollection may need to be disposted of to save potential troubles. This is left as an exercise for readers to explore further.
• There might be some other issues. Please feel free to advise and we will try to address them in the future.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides a coder, Entity Jigger, to help us create entity jig code automatically, quickly and reliably.
Recent Comments