We introduced before how the Entity Jigger wizard of AutoCAD .NET Addin Wizard (AcadNetAddinWizard) can help us generate various EntityJig derivatives such as Move Jig, Rotate Jig, Scale Jig and even Mirror Jig quickly, reliably and professionally.
However, in those posts, readers might not have realized how good those jiggers were because only some static images were provided to show how those jiggers behaved in AutoCAD. Because of the dynamic nature of the jig behavior, videos are obviously better to demonstrate the wonderful movements of the jiggers.
We are going to merge the Move Jig, Rotate Jig, and Scale Jig that we demonstrated before into a single EntityJig class this time and clean up the code a bit along the way. Here is the video first to show how the MoveRotateScaleJig behaves nicely in AutoCAD.
As can be noticed, the MoveRotateScaleJig works with UCS perfectly, honors the Ortho Mode, and shows a temporary highlighted copy of the same entity at its original position for graphics comparison purpose, which is achieved by using a sub transaction. So, it’s a real, practical and good use of the AutoCAD .NET sub transaction.
Here is the succinct code for such a powerful and useful jig.
#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 MoveRotateScaleJig : EntityJig
{
#region Fields
public int mCurJigFactorIndex = 1;
private Point3d mBasePoint = Point3d.Origin;
private double mLastAngle = 0;
private double mLastScaleFactor = 1;
private Point3d mPosition; //Factor #1
private double mRotationAngle; //Factor #2
private double mScaleFactor; //Factor #3
#endregion
#region Constructors
public MoveRotateScaleJig(Entity ent, Point3d basePoint)
: base(ent)
{
mBasePoint = basePoint;
}
#endregion
#region Properties
private static Editor Editor
{
get
{
return MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
}
}
private Matrix3d UCS
{
get
{
return Editor.CurrentUserCoordinateSystem;
}
}
#endregion
#region Overrides
protected override bool Update()
{
switch (mCurJigFactorIndex)
{
case 1:
Matrix3d mat1 = Matrix3d.Displacement(mBasePoint.GetVectorTo(mPosition).TransformBy(UCS));
Entity.TransformBy(mat1);
mBasePoint = mPosition;
break;
case 2:
Matrix3d mat2 = Matrix3d.Rotation(mRotationAngle - mLastAngle, Vector3d.ZAxis.TransformBy(UCS), mBasePoint.TransformBy(UCS));
Entity.TransformBy(mat2);
mLastAngle = mRotationAngle;
break;
case 3:
Matrix3d mat3 = Matrix3d.Scaling(mScaleFactor/mLastScaleFactor, mBasePoint.TransformBy(UCS));
Entity.TransformBy(mat3);
mLastScaleFactor = mScaleFactor;
break;
default:
break;
}
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
switch (mCurJigFactorIndex)
{
case 1:
JigPromptPointOptions prOptions1 = new JigPromptPointOptions("\nNew location:");
PromptPointResult prResult1 = prompts.AcquirePoint(prOptions1);
if (prResult1.Status == PromptStatus.Cancel) return SamplerStatus.Cancel;
Point3d tempPt = prResult1.Value.TransformBy(UCS.Inverse());
if (tempPt.Equals(mPosition))
{
return SamplerStatus.NoChange;
}
else
{
mPosition = tempPt;
return SamplerStatus.OK;
}
case 2:
JigPromptAngleOptions prOptions2 = new JigPromptAngleOptions("\nRotation angle:");
prOptions2.BasePoint = mBasePoint.TransformBy(UCS);
prOptions2.UseBasePoint = true;
PromptDoubleResult prResult2 = prompts.AcquireAngle(prOptions2);
if (prResult2.Status == PromptStatus.Cancel) return SamplerStatus.Cancel;
if (prResult2.Value.Equals(mRotationAngle))
{
return SamplerStatus.NoChange;
}
else
{
mRotationAngle = prResult2.Value;
return SamplerStatus.OK;
}
case 3:
JigPromptDistanceOptions prOptions3 = new JigPromptDistanceOptions("\nScale factor:");
prOptions3.BasePoint = mBasePoint.TransformBy(UCS);
prOptions3.UseBasePoint = true;
PromptDoubleResult prResult3 = prompts.AcquireDistance(prOptions3);
if (prResult3.Status == PromptStatus.Cancel) return SamplerStatus.Cancel;
if (prResult3.Value.Equals(mScaleFactor))
{
return SamplerStatus.NoChange;
}
else
{
mScaleFactor = prResult3.Value;
return SamplerStatus.OK;
}
default:
break;
}
return SamplerStatus.OK;
}
#endregion
#region Methods to Call
public static bool Jig(Entity ent, Point3d basePt)
{
MoveRotateScaleJig jigger = null;
try
{
jigger = new MoveRotateScaleJig(ent, basePt);
PromptResult pr;
do
{
pr = Editor.Drag(jigger);
jigger.mCurJigFactorIndex++;
} while (pr.Status != PromptStatus.Cancel && pr.Status != PromptStatus.Error && jigger.mCurJigFactorIndex <= 3);
if (pr.Status == PromptStatus.Cancel || pr.Status == PromptStatus.Error)
return CleanUp(jigger);
else
return true;
}
catch
{
return CleanUp(jigger);
}
}
private static bool CleanUp(MoveRotateScaleJig jigger)
{
if (jigger != null && jigger.Entity != null)
jigger.Entity.Dispose();
return false;
}
#endregion
#region Test Commands
[CommandMethod("MoveRotateScaleJig")]
public static void MoveRotateScaleJig_Method()
{
Database db = HostApplicationServices.WorkingDatabase;
try
{
PromptEntityResult selRes = Editor.GetEntity("\nPick an entity:");
PromptPointResult ptRes = Editor.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.ForWrite) as Entity;
if (ent != null)
{
using (Transaction tr1 = db.TransactionManager.StartTransaction())
{
ent.Highlight();
tr1.Commit();
}
if (MoveRotateScaleJig.Jig(ent, ptRes.Value))
tr.Commit();
}
}
}
}
catch (System.Exception ex)
{
Editor.WriteMessage(ex.ToString());
}
}
#endregion
}
}
Enjoy it! More wonderful stuffs will be coming soon. Please stay tuned!
Posted by: |