Using DrawJig class we can quite easily to create our own commands that behave similar to, or the same way as, AutoCAD built-in commands, like Move, Copy, Scale, Stretch...
Here I show some code to create a custom "Scale" command that behaves very similar to AutoCAD built-in "Scale" command: user selects an entity, picks a base point, then moves/drags the mouse cursor until the mouse is clicked or a scale number is entered. During mouse dragging, a ghost image of the selected entity dynamically scales in or out, depending on the distance between the cursor's location and the base point.
Here is the class MyScaleJig:
Code Snippet
- using System;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Geometry;
- namespace ScaleJig
- {
- public class MyScaleJig : DrawJig
- {
- private Document _dwg;
- private Editor _ed;
- private Database _db;
- private ObjectId _entID;
- private Entity _entity=null;
- private int _colorIndex = 0;
- private Point3d _basePoint;
- private double _scale = 1.0;
- private bool _picked = false;
- public MyScaleJig(Document dwg):base()
- {
- _dwg = dwg;
- _ed = _dwg.Editor;
- _db = _dwg.Database;
- }
- public void ScaleEntity(ObjectId entID, int ghostLineColorIndex)
- {
- _entID = entID;
- _colorIndex = ghostLineColorIndex;
- //Highlight entity
- HighlightEntity(true);
- //Pick base point
- if (PickBasePoint())
- {
- //Use a non-database residing
- //entity for showing jig ghost line
- using (_entity = GetEntityClone(_entID))
- {
- _ed.Drag(this);
- }
- //Unhighlight
- HighlightEntity(false);
- //Tramsform(scale) entity
- //if user did not cancel the jig
- if (_picked) ScaleEntity();
- }
- }
- #region override Jig methods
- protected override SamplerStatus Sampler(JigPrompts prompts)
- {
- JigPromptDistanceOptions opt = new JigPromptDistanceOptions(
- "\nMove cursor to scale entity or enter scale:");
- opt.DefaultValue = _scale;
- opt.UseBasePoint = true;
- opt.BasePoint = _basePoint;
- opt.UserInputControls =
- UserInputControls.Accept3dCoordinates |
- UserInputControls.AcceptOtherInputString |
- UserInputControls.NoNegativeResponseAccepted |
- UserInputControls.NoZeroResponseAccepted |
- UserInputControls.NullResponseAccepted;
- opt.Cursor = CursorType.RubberBand;
- PromptDoubleResult res = prompts.AcquireDistance(opt);
- SamplerStatus status;
- if (res.Status == PromptStatus.OK)
- {
- double newScale = res.Value;
- if (Math.Abs(_scale-newScale)<0.0001)
- {
- status = SamplerStatus.NoChange;
- }
- else
- {
- Matrix3d mt;
- //Restore to previous scale
- mt = Matrix3d.Scaling(1 / _scale, _basePoint);
- _entity.TransformBy(mt);
- //Transform to new scale
- _scale = newScale;
- mt = Matrix3d.Scaling(_scale, _basePoint);
- _entity.TransformBy(mt);
- status = SamplerStatus.OK;
- }
- _picked = true;
- }
- else
- {
- _picked = false;
- status = SamplerStatus.Cancel;
- }
- return status;
- }
- protected override bool WorldDraw(
- Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
- {
- draw.Geometry.Draw(_entity);
- return true;
- }
- #endregion
- #region private methods
- private Entity GetEntityClone(ObjectId entID)
- {
- Entity ent = null;
- using (Transaction tran =
- _db.TransactionManager.StartTransaction())
- {
- Entity e = (Entity)tran.GetObject(entID, OpenMode.ForRead);
- ent = e.Clone() as Entity;
- ent.ColorIndex = _colorIndex;
- tran.Commit();
- }
- return ent;
- }
- private void HighlightEntity(bool highlight)
- {
- using (Transaction tran =
- _db.TransactionManager.StartTransaction())
- {
- Entity ent = (Entity)tran.GetObject(
- _entID, OpenMode.ForWrite);
- SubentityId subEntId =
- new SubentityId(SubentityType.Null, IntPtr.Zero);
- ObjectId[] ids = new ObjectId[1];
- ids[0] = ent.ObjectId;
- FullSubentityPath path =
- new FullSubentityPath(ids, subEntId);
- if (highlight)
- ent.Highlight(path, true);
- else
- ent.Unhighlight(path, true);
- }
- }
- private bool PickBasePoint()
- {
- PromptPointOptions opt = new PromptPointOptions(
- "\nPick scale base point:");
- opt.AllowNone = false;
- PromptPointResult res = _ed.GetPoint(opt);
- if (res.Status == PromptStatus.OK)
- {
- _basePoint = res.Value;
- return true;
- }
- else
- {
- return false;
- }
- }
- private void ScaleEntity()
- {
- using (Transaction tran =
- _db.TransactionManager.StartTransaction())
- {
- Entity e = (Entity)tran.GetObject(_entID, OpenMode.ForWrite);
- Matrix3d mt = Matrix3d.Scaling(_scale, _basePoint);
- e.TransformBy(mt);
- tran.Commit();
- }
- }
- #endregion
- }
- }
Here is a command class that uses MyScaleJig to scale selected entity:
Code Snippet
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Runtime;
- [assembly: CommandClass(typeof(ScaleJig.MyCommands))]
- namespace ScaleJig
- {
- public class MyCommands
- {
- [CommandMethod("MyScale")]
- public static void RunMyScaleJig()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Editor ed = doc.Editor;
- //Pick an entity
- ObjectId entID = GetEntity(ed);
- if (entID == ObjectId.Null)
- {
- ed.WriteMessage("\n*Cancel*");
- return;
- }
- //Use MyScaleJig to scale entity
- //With red jib ghost line
- MyScaleJig jig = new MyScaleJig(doc);
- jig.ScaleEntity(entID, 1);
- }
- private static ObjectId GetEntity(Editor ed)
- {
- PromptEntityOptions opt =
- new PromptEntityOptions("\nPick an entity: ");
- PromptEntityResult res = ed.GetEntity(opt);
- if (res.Status == PromptStatus.OK)
- {
- return res.ObjectId;
- }
- else
- {
- return ObjectId.Null;
- }
- }
- }
- }
The key part of the code lies in the 2 overridden methods Sampler() and WorldDraw() and it is quite simple and straightforward.
I uses a non-database residing, cloned entity for generating ghost image of the entity to be scaled. This way, the real entity to scaled is not changed if user cancels dragging, and only be changed when the dragging is ended with a distance (scale) is picked or entered.
As we can see, the whole purpose to use jig here is to get a scale input from user with very user-friendly visual hint help.
With some easy code modification, we can pass a collection/array of selected entities (ObjectIds, actually), and scale them together.
However, comparing to AutoCAD built-in Scale command, one thing is missing: there is no rubber-band line from the base point to the mouse cursor. That is because I have use JigPromptDistanceOptions to acquire user input as scale. In spite I set its UseBasePoint property to True and set its BasePoint property to a Point3d value, the rubber-band line still does not show. I guess this is by design: rubber-band line only shows with JigPromptPointOptions class.
I do not know how to overcome this problem with Jig class. Maybe I can implement another custom Scale command with TransientGraphics in conjunction with Editor.PointMonitor event handler later.
Update: Thanks to Maxence's comment, it turned out showing the rubber-band line is simple. I added one line code, showing in red. Thank you, Maxence.