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 how to use EntityJig to drag various AutoCAD entities many times before. We also provide an item wizard, Entity Jigger in the AutoCAD .NET Addin Wizard (AcadNetAddinWizard) now, to help create the starter code of any EntityJig derivatives for any kind of entities quickly, reliably and professionaly.
From this moment on, we will discuss about how to use the DrawJig to drag entities or graphics. Let us start from a simple but still useful case, jigging a Text (API name DBText). Here is the test command first.
[CommandMethod("TestTextDrawJigger")]
public static void TestTextDrawJigger_Method()
{
try
{
PromptStringOptions prOpt = new PromptStringOptions("\nText content: ");
PromptResult pr = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.GetString(prOpt);
if (pr.Status != PromptStatus.OK)
return;
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
TextStyleTableRecord ts = tr.GetObject(db.Textstyle, OpenMode.ForRead) as TextStyleTableRecord;
if (ts != null)
{
TextDrawJigger jigger = new TextDrawJigger(pr.StringResult, ts);
PromptResult jigRes = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.Drag(jigger);
if (jigRes.Status == PromptStatus.OK)
{
using (DBText text = new DBText())
{
text.TextStyleId = db.Textstyle;
text.TextString = pr.StringResult;
text.Position = jigger.Position;
text.Normal = Vector3d.ZAxis;
text.Rotation = 0;
text.TransformBy(jigger.UCS);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
btr.AppendEntity(text);
tr.AddNewlyCreatedDBObject(text, true);
}
}
}
tr.Commit();
}
}
catch (System.Exception ex)
{
MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
}
}
Here is the complete Text DrawJig implementation.
#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 Autodesk.AutoCAD.GraphicsSystem;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.BoundaryRepresentation;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.ComponentModel;
using Autodesk.AutoCAD.Customization;
using Autodesk.AutoCAD.DataExtraction;
using Autodesk.AutoCAD.Diagnostics;
using Autodesk.AutoCAD.Internal;
using Autodesk.AutoCAD.LayerManager;
using Autodesk.AutoCAD.MacroRecorder;
using Autodesk.AutoCAD.Publishing;
using Autodesk.AutoCAD.PlottingServices;
using Autodesk.AutoCAD.Ribbon;
using MgdAcApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using MgdAcDocument = Autodesk.AutoCAD.ApplicationServices.Document;
using AcWindowsNS = Autodesk.AutoCAD.Windows;
#endregion
namespace AcadNetAddinWizard_Namespace
{
public class TextDrawJigger : DrawJig
{
#region Fields
private string mTextString;
private Point3d mPosition;
private TextStyle mDefaultStyle;
#endregion
#region Constructors
public TextDrawJigger(string str, TextStyleTableRecord style)
{
mTextString = str;
mPosition = Point3d.Origin;
mDefaultStyle = new TextStyle(
style.FileName,
style.BigFontFileName,
style.TextSize,
style.XScale,
style.ObliquingAngle,
1.0,
false,
false,
style.IsVertical,
false,
false,
"Default");
}
#endregion
#region Properties
public Point3d Position
{
get { return mPosition; }
set { mPosition = value; }
}
private Editor Editor
{
get
{
return MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
}
}
public Matrix3d UCS
{
get
{
return MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem;
}
}
#endregion
#region Overrides
protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
{
WorldGeometry geo = draw.Geometry;
if (geo != null)
{
geo.PushModelTransform(UCS);
geo.Text(
mPosition,
Vector3d.ZAxis,
Vector3d.XAxis,
mTextString,
true,
mDefaultStyle
);
geo.PopModelTransform();
}
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptPointOptions prOptions1 = new JigPromptPointOptions("\nLocation:");
prOptions1.UseBasePoint = false;
PromptPointResult prResult1 = prompts.AcquirePoint(prOptions1);
if (prResult1.Status == PromptStatus.Cancel || prResult1.Status == PromptStatus.Error)
return SamplerStatus.Cancel;
Point3d tempPt = prResult1.Value.TransformBy(UCS.Inverse());
if (tempPt.Equals(mPosition)) //Use better comparison method if necessary.
{
return SamplerStatus.NoChange;
}
else
{
mPosition = tempPt;
return SamplerStatus.OK;
}
}
#endregion
}
}
It is not fancy. When the test command is run, it asks to input a string first, then it will drag the DBText on the screen with quite a few things hard coded such as the text is on the current UCS, its direction is parallel to the UCS X axis (or its rotation angle is zero), its text style uses the default AutoCAD database one, and its position moves with the cursor.
However, it does shows the fundamentals of the DrawJig. A few highlights about the code may be helpful to understand it.
• The key of the DrawJig is its WorldDraw() method. It provides the abilities to draw any graphics.
• The WorldGeometry instance privides various methods to draw anything we want.
• It also provides us a structural and easy way for us to handle any matrix transformations as we did to the UCS.
• The Sampler() override is another core method which is used to collect the user input such as the cursor position in the case.
• Though it looks like exackly the same as the one in EntityJig, it has fundamental different. In the EntityJig, the collected point is in UCS unless explicitly specified with the Input Control; but in the DrawJig, the point coordinate is in WCS. That is why we have to transform it back to UCS to make things consistent.
• The WorldGeometry Text needs a TextStyle to set varios properties but the DBText uses a TextStyleTableRecord for the same purpose.
• The two text styles share most of common properties but are total different creatures. One former is in memory only and the latter is database resident. That is why we have to copy properties from one to another.
• In generally, we do not pass the entity pointer to the DrawJig as it should not only care about graphics of a particular entity. Otherwise, it becomes right the EntityJig with quite some overheads brought about from the WorldGeometry such as the TextStyle.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides a coder, Draw Jigger, to help us implement DrawJig automatically, quickly and reliably.
Posted by: |