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 a Block (Commonly used name)/BlockReference (API term) /INSERT (User term) using EntityJig.
Here is the test command for the block moving jig we are going to talk about:
[CommandMethod("TestEntityJigger4")]
public static void TestEntityJigger4_Method()
{
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
PromptEntityResult selRes = ed.GetEntity("Pick a block:");
if (selRes.Status == PromptStatus.OK)
{
BlockReference ent = tr.GetObject(selRes.ObjectId, OpenMode.ForWrite) as BlockReference;
if (ent != null)
{
BlockMoving.Jig(ent);
}
}
tr.Commit();
}
}
Here is how the block moving jig behaves in AutoCAD after the test command runs and a block is picked:
As can be seen, the original block stayed at its original place for the reference purpose in this situation and some exact graphics as the block appearance were moving along with the cursor at that moment. After a point (the new block position) was clicked, the picked block was moved to the new place.
If readers would like a different behavior for the block moving such as the original block graphics being highlighted or even making it disappear totally, it can be achieved by tweaking the test command a bit. Some similar code will be introduced in some coming posts, please stay tuned.
Here is the core source about the block moving 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 BlockMoving : EntityJig
{
#region Fields
public int mCurJigFactorNumber = 1;
private Point3d mPosition = new Point3d(); // Factor #1
#endregion
#region Constructors
public BlockMoving(Entity ent) : base(ent)
{
}
#endregion
#region Overrides
protected override bool Update()
{
switch (mCurJigFactorNumber)
{
case 1:
(Entity as BlockReference).Position = mPosition;
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;
}
default:
break;
}
return SamplerStatus.OK;
}
#endregion
#region Method to Call
public static bool Jig(BlockReference ent)
{
try
{
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
BlockMoving jigger = new BlockMoving(ent);
PromptResult pr;
do {
pr = ed.Drag(jigger);
jigger.mCurJigFactorNumber++;
} while ( pr.Status != PromptStatus.Cancel &&
pr.Status != PromptStatus.Error &&
pr.Status != PromptStatus.Keyword &&
jigger.mCurJigFactorNumber <= 1);
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 block position.
• 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 the block position as set in the Sampler().
• The Editor.Draw() is the power to fire the jig.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides a coder, Entity Jigger, to help us create entity jig code automatically, quickly and reliably to address various entity types and circumstances.
Thanks for this technical description. Good knowledge sharing made.
http://www.besttests.com
Posted by: Jaylen Watkins | 02/10/2012 at 02:06 AM