We introduced before how the Entity Jigger wizard of AutoCAD .NET Addin Wizard (AcadNetAddinWizard) can help us generate a Power Text command quickly, reliably and professionally.
However, in that post, readers might not have realized how good the Power Text command was. In fact, it is much powerful than the AutoCAD TEXT command. The most wonderful feature about the Power Text command is that all good DBText properties such as Position, Height, Horizontal Mode, Vertical Mode, Width Factor, Oblique Angle, and Rotation angle can be dynamically input (jigged) conveniently and naturally. In that post, only a static image was provided to show how the Power Text command behaved in AutoCAD. But since so many jigging factors being taken into account and quite some wonderful, natural, and subtle operations in the jig command, it is not possible to demonstrate all these features in a single simple static image.
Therefore, we've decided to create a video for the Power Text jig command this time and clean up the code a bit along the way. Here is the video first.
Here is the complete but still very concise code conparing to its so many nice features.
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using MgdAcApplication = Autodesk.AutoCAD.ApplicationServices.Application;
namespace AcadNetAddinWizard_Namespace
{
public class PowerTextJigger2 : EntityJig
{
#region Fields
public int mCurJigFactorIndex = 1; // Jig Factor Index
public Autodesk.AutoCAD.Geometry.Point3d mPosition; // Jig Factor #1
public double mHeight = 0.001; // Jig Factor #2
public double mWidthFactor = 1; // Jig Factor #3
public double mOblique; // Jig Factor #4
// Jig Factor #5 - Controls both the Horizontal and Vertical mode.
public TextHorizontalMode mHorizontalMode;
public TextVerticalMode mVerticalMode;
public double mRotation; // Jig Factor #6
#endregion
#region Constructors
public PowerTextJigger2(DBText ent)
: base(ent)
{
// Initialize and transform the Entity.
Entity.SetDatabaseDefaults();
Entity.TransformBy(UCS);
}
#endregion
#region Properties
private static Editor Editor
{
get
{
return MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
}
}
private static Matrix3d UCS
{
get
{
return MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem;
}
}
#endregion
#region Overrides
public new DBText Entity // Overload the Entity property for convenience.
{
get
{
return base.Entity as DBText;
}
}
protected override bool Update()
{
switch (mCurJigFactorIndex)
{
case 1:
Entity.Position = mPosition;
break;
case 2:
if (mHeight < 0.001) mHeight = 0.001; //NOTE: to avoid the eInvalidInput error
Entity.Height = mHeight;
break;
case 3:
try { Entity.WidthFactor = mWidthFactor; }
catch { } //NOTE: to avoid the eInvalidInput error
break;
case 4:
try { Entity.Oblique = mOblique; }
catch { } //NOTE: to avoid the eInvalidInput error
break;
case 5:
Entity.HorizontalMode = mHorizontalMode;
Entity.VerticalMode = mVerticalMode;
if (Entity.HorizontalMode == TextHorizontalMode.TextLeft && Entity.VerticalMode == TextVerticalMode.TextBase)
Entity.Position = mPosition;
else
Entity.AlignmentPoint = mPosition;
Entity.AdjustAlignment(HostApplicationServices.WorkingDatabase);
break;
case 6:
Entity.Rotation = mRotation;
break;
default:
return false;
}
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
switch (mCurJigFactorIndex)
{
case 1:
JigPromptPointOptions prOptions1 = new JigPromptPointOptions("\nPosition:");
prOptions1.UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.GovernedByUCSDetect;
PromptPointResult prResult1 = prompts.AcquirePoint(prOptions1);
if (prResult1.Status == PromptStatus.Cancel && prResult1.Status == PromptStatus.Error)
return SamplerStatus.Cancel;
if (prResult1.Value.IsEqualTo(mPosition))
{
return SamplerStatus.NoChange;
}
else
{
mPosition = prResult1.Value;
return SamplerStatus.OK;
}
case 2:
JigPromptDistanceOptions prOptions2 = new JigPromptDistanceOptions("\nHeight:");
prOptions2.UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.GovernedByUCSDetect;
prOptions2.UseBasePoint = true;
prOptions2.BasePoint = mPosition;
PromptDoubleResult prResult2 = prompts.AcquireDistance(prOptions2);
if (prResult2.Status == PromptStatus.Cancel && prResult2.Status == PromptStatus.Error)
return SamplerStatus.Cancel;
if (prResult2.Value.Equals(mHeight))
{
return SamplerStatus.NoChange;
}
else
{
mHeight = prResult2.Value;
return SamplerStatus.OK;
}
case 3:
JigPromptDistanceOptions prOptions3 = new JigPromptDistanceOptions("\nWidth factor:");
prOptions3.UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.GovernedByUCSDetect;
prOptions3.UseBasePoint = true;
prOptions3.BasePoint = mPosition;
PromptDoubleResult prResult3 = prompts.AcquireDistance(prOptions3);
if (prResult3.Status == PromptStatus.Cancel && prResult3.Status == PromptStatus.Error)
return SamplerStatus.Cancel;
if (prResult3.Value.Equals(mWidthFactor))
{
return SamplerStatus.NoChange;
}
else
{
mWidthFactor = prResult3.Value;
return SamplerStatus.OK;
}
case 4:
JigPromptAngleOptions prOptions4 = new JigPromptAngleOptions("\nOblique:");
prOptions4.UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.GovernedByUCSDetect;
prOptions4.UseBasePoint = true;
prOptions4.BasePoint = mPosition;
PromptDoubleResult prResult4 = prompts.AcquireAngle(prOptions4);
if (prResult4.Status == PromptStatus.Cancel && prResult4.Status == PromptStatus.Error)
return SamplerStatus.Cancel;
if (prResult4.Value.Equals(mOblique))
{
return SamplerStatus.NoChange;
}
else
{
mOblique = prResult4.Value;
return SamplerStatus.OK;
}
case 5:
JigPromptPointOptions prOptions5 = new JigPromptPointOptions("\nAlignment mode (both Horizental and Vertical):");
prOptions5.UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.GovernedByUCSDetect;
prOptions5.UseBasePoint = true;
prOptions5.BasePoint = mPosition;
PromptPointResult prResult5 = prompts.AcquirePoint(prOptions5);
if (prResult5.Status == PromptStatus.Cancel && prResult5.Status == PromptStatus.Error)
return SamplerStatus.Cancel;
if (prResult5.Value.Equals(mHorizontalMode))
{
return SamplerStatus.NoChange;
}
else
{
double dist = prResult5.Value.TransformBy(UCS.Inverse()).X - mPosition.TransformBy(UCS.Inverse()).X;
if (dist > mHeight)
mHorizontalMode = TextHorizontalMode.TextLeft;
else if (dist > 0)
mHorizontalMode = TextHorizontalMode.TextCenter;
else
mHorizontalMode = TextHorizontalMode.TextRight;
double distY = prResult5.Value.TransformBy(UCS.Inverse()).Y - mPosition.TransformBy(UCS.Inverse()).Y;
if (distY > mHeight)
mVerticalMode = TextVerticalMode.TextBottom;
else if (distY > mHeight / 2)
mVerticalMode = TextVerticalMode.TextBase;
else if (distY > -mHeight / 2)
mVerticalMode = TextVerticalMode.TextVerticalMid;
else
mVerticalMode = TextVerticalMode.TextTop;
return SamplerStatus.OK;
}
case 6:
JigPromptAngleOptions prOptions6 = new JigPromptAngleOptions("\nRotation:");
prOptions6.UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.GovernedByUCSDetect;
prOptions6.UseBasePoint = true;
prOptions6.BasePoint = mPosition;
PromptDoubleResult prResult6 = prompts.AcquireAngle(prOptions6);
if (prResult6.Status == PromptStatus.Cancel && prResult6.Status == PromptStatus.Error)
return SamplerStatus.Cancel;
if (prResult6.Value.Equals(mRotation))
{
return SamplerStatus.NoChange;
}
else
{
mRotation = prResult6.Value;
return SamplerStatus.OK;
}
default:
break;
}
return SamplerStatus.OK;
}
#endregion
#region Methods to Call
public static DBText Jig()
{
PowerTextJigger2 jigger = null;
try
{
jigger = new PowerTextJigger2(new DBText());
jigger.Entity.TextString = Editor.GetString("\nText content: ").StringResult;
PromptResult pr;
do
{
pr = Editor.Drag(jigger);
jigger.mCurJigFactorIndex++;
} while (pr.Status != PromptStatus.Cancel
&& pr.Status != PromptStatus.Error
&& jigger.mCurJigFactorIndex <= 6);
if (pr.Status == PromptStatus.Cancel || pr.Status == PromptStatus.Error)
return CleanUp(jigger);
else
return jigger.Entity;
}
catch
{
return CleanUp(jigger);
}
}
private static DBText CleanUp(PowerTextJigger2 jigger)
{
if (jigger != null && jigger.Entity != null)
jigger.Entity.Dispose();
return null;
}
#endregion
#region Test Commands
[CommandMethod("TestPowerTextJigger2")]
public static void TestPowerTextJigger2_Method()
{
try
{
Entity jigEnt = PowerTextJigger2.Jig();
if (jigEnt != null)
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
btr.AppendEntity(jigEnt);
tr.AddNewlyCreatedDBObject(jigEnt, true);
tr.Commit();
}
}
}
catch (System.Exception ex)
{
MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.Message);
}
}
#endregion
}
}
Enjoy it, guys. You would love it!
Posted by: |