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 in many early posts. In this article, let us see how to use jig to copy an entity of any type.
Here is the core code of dynamic copy 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 EntityCopyJigger : EntityJig
{
#region Fields
public int mCurJigFactorIndex = 1;
private Point3d mBasePoint = new Point3d();
private Point3d mNewPoint; // Factor #1
#endregion
#region Constructors
public EntityCopyJigger(Entity ent, Point3d basePoint) : base(ent)
{
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()
{
Matrix3d mat = Matrix3d.Displacement(mBasePoint.GetVectorTo(mNewPoint));
Entity.TransformBy(mat);
mBasePoint = mNewPoint;
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 bool Jig(Entity ent, Point3d basePt)
{
EntityCopyJigger jigger = null;
try
{
jigger = new EntityCopyJigger(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("TestEntityCopyJigger")]
public static void TestEntityCopyJigger_Method()
{
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;
try
{
PromptEntityResult selRes = ed.GetEntity("\nPick an entity:");
PromptPointResult ptRes = ed.GetPoint("\nBase Point: ");
if (selRes.Status == PromptStatus.OK && ptRes.Status == PromptStatus.OK)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Entity ent = tr.GetObject(selRes.ObjectId, OpenMode.ForRead) as Entity;
if (ent != null)
{
Entity clone = ent.Clone() as Entity;
if (EntityCopyJigger.Jig(clone, ptRes.Value))
{
BlockTableRecord ms = tr.GetObject(ent.OwnerId, OpenMode.ForWrite) as BlockTableRecord;
if (ms != null)
{
ms.AppendEntity(clone);
tr.AddNewlyCreatedDBObject(clone, true);
tr.Commit();
}
}
else
tr.Abort();
}
}
}
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.ToString());
}
}
#endregion
}
}
Here is what the the sceen looks like when a solid cylinder is being copied to a new place by the dynamic copy jig:
NOTE: The isometric view is in a UCS instead of WCS, indicating our move Jigger honors UCS perfectly.
A few highlights about the code may be helpful:
• No entity type is needed for our Copy Jig since it works for any type of AutoCAD Entity.
• A base point needs to be picked to determine where to jig the Entity from.
• The Sampler() override is to acquire input for the new location of the base point.
• Some UserInputControls flags are used here, GovernedByUCSDetect 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 when needed, as commented about.
• Only after the jig succeeds should the entity be copied to the new location.
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: |