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.
In this article, let us see how to move and rotate a Block (a commonly used but confusing term)/BlockReference (API term) /INSERT (AutoCAD term) using EntityJig.
Here is the test command for the block moving and rotating jig we are going to talk about:
[CommandMethod("TestEntityJigger6")]
public static void TestEntityJigger6_Method()
{
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;
PromptEntityResult selRes = ed.GetEntity("Pick a block:");
if (selRes.Status == PromptStatus.OK)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockReference ent = tr.GetObject(selRes.ObjectId, OpenMode.ForWrite) as BlockReference;
if (ent != null)
{
using (Transaction tr1 = db.TransactionManager.StartTransaction())
{
ent.Highlight();
tr1.Commit();
}
if (BlockMovingRotating.Jig(ent))
tr.Commit();
else
tr.Abort();
}
}
}
}
Here is how the block moving and rotating jig behaves in AutoCAD after the test command runs and a block is picked:
As can be seen, the block moving and rotating behavior looks very natural, almost the same as the AutoCAD move and rotate command exhibits. This is achieved easily and reliably by applying a sub transaction as demonstrated in the succinct test command. And better, a few other situations have been nicely handled as well. The block’s graphics is only highlighted temporarily during the jigging period. In case of anything wrong during the moving or rotating or users just cancel or escape out the operation, the block will be restored to its very original status automatically thus nothing messy about the database.
So this is a simple but very useful practical example that demonstrates the usage of the AutoCAD .NET sub transaction. It is not something fancy that just makes people wonder.
Here is the core source about the block moving and rotating jig class derived from the EntityJig:
#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 BlockMovingRotating : EntityJig
{
#region Fields
public int mCurJigFactorNumber = 1;
private Point3d mPosition; // Factor #1
private double mRotation; // Factor #2
#endregion
#region Constructors
public BlockMovingRotating(Entity ent)
: base(ent)
{
}
#endregion
#region Overrides
protected override bool Update()
{
switch (mCurJigFactorNumber)
{
case 1:
(Entity as BlockReference).Position = mPosition;
break;
case 2:
(Entity as BlockReference).Rotation = mRotation;
break;
default:
return false;
}
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
switch (mCurJigFactorNumber)
{
case 1:
JigPromptPointOptions prOptions1 = new JigPromptPointOptions("\nBlock position:");
PromptPointResult prResult1 = prompts.AcquirePoint(prOptions1);
if (prResult1.Status == PromptStatus.Cancel) return SamplerStatus.Cancel;
if (prResult1.Value.Equals(mPosition))
{
return SamplerStatus.NoChange;
}
else
{
mPosition = prResult1.Value;
return SamplerStatus.OK;
}
case 2:
JigPromptAngleOptions prOptions2 = new JigPromptAngleOptions("\nBlock rotation angle:");
prOptions2.BasePoint = (Entity as BlockReference).Position;
prOptions2.UseBasePoint = true;
PromptDoubleResult prResult2 = prompts.AcquireAngle(prOptions2);
if (prResult2.Status == PromptStatus.Cancel) return SamplerStatus.Cancel;
if (prResult2.Value.Equals(mRotation))
{
return SamplerStatus.NoChange;
}
else
{
mRotation = prResult2.Value;
return SamplerStatus.OK;
}
default:
break;
}
return SamplerStatus.OK;
}
#endregion
#region Method to Call
public static bool Jig(BlockReference ent)
{
try
{
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
BlockMovingRotating jigger = new BlockMovingRotating(ent);
PromptResult pr;
do
{
pr = ed.Drag(jigger);
} while (pr.Status != PromptStatus.Cancel &&
pr.Status != PromptStatus.Error &&
pr.Status != PromptStatus.Keyword &&
jigger.mCurJigFactorNumber++ <= 2);
return pr.Status == PromptStatus.OK;
}
catch
{
return false;
}
}
#endregion
}
}
A few highlights about the code may be helpful:
• An entity type needs to be specified in the EntityJig derivative, as BlockReference here.
• The Sampler() override is to acquire input for the new position and rotation angle.
• 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 both the block position and the block rotation properties as acquired in the Sampler().
• The Editor.Draw() is the power to fire the jig.
• The last but important, please return a good status value to the caller so that it knows whether to really modify the block or roll any changes back.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides a coder, Entity Jigger, to help us create entity jig code automatically, quickly and reliably.
Hi spider,
is it possible to auto align jig to object like alignment in dynamic block
only I want it in 3D
Also is it possible to rotate the jig entity using mouse scroll? lets say 1 scroll is 90°
Sorry I'm absolute beginner here
Posted by: Indrawan Adi wicaksono | 04/15/2012 at 10:36 AM
They seem all possible to me. If a bit more details could be provided, some posts may be addressing these cases in the future.
For example, how would you like a line or circle jig to auto aligh in a dynamic block?
Posted by: Spiderinnet1 | 04/15/2012 at 06:45 PM
The recent post addresses your second query:
AutoCAD .NET: EntityJig – Mouse Wheel Up/Down as Rotation Increment/Decrement During Block/Insert Rotating
http://spiderinnet1.typepad.com/blog/2012/04/autocad-net-entityjig-mouse-wheel-updown-as-rotation-incrementdecrement-during-blockinsert-rotating.html
Posted by: Spiderinnet1 | 04/17/2012 at 04:09 AM