AutoCAD .NET Addin Wizard (AcadNetAddinWizard) is far more than just some templates scattered around. It is a real-wizard-sense wizard, i.e. multi paged, configurable, flexible and intelligent. It also includes various project item wizards, coders, and widgets to help program AutoCAD .NET addins.
In this article, let us see how the Entity Jigger wizard of AutoCAD .NET Addin Wizard (AcadNetAddinWizard) can help us generate a Power Text EntityJig class quickly, reliably and professionally.
The Entity Jigger wizard can be found from the Add New Item UI of both Visual C# and VB.NET of Visual Studio both full-fledged and Express in versions 2008 and 2010. Let’s take the C# of Visual Studio 2010 for an example. The AutoCAD Addin category will appear under the Visual C# Items node of the Installed Templates and its various item wizards including the Entity Jigger wizard appear in turn in the middle pane:
Here are some critical filled out wizard pages of the Entity Jigger wizard for the coming Power Text EntityJig class:
As can be seen from the Entity Jigger factors wizard page, our Power Text EntityJig is going to support almost all good properties of the DBText of concern like Position, Height, Horizontal Mode, Vertical Mode, Width Factor, Oblique Angle, and Rotation angle.
Without the nice and cool Entity Jigger wizard, it might have taken a few days of hard thinking and designing, and another several days or even week long of coding, debugging, and testing to work out a working version of the same Power Text EntityJig. With the Entity Jigger wizard, fortunately, it only takes about some minutes or at most a couple of hours to make the work done in a more professional and reliable way.
The auto-generated PowerTextJigger Jig class is almost ready to compile with only one line of code needing adjustment. Besides this, we certainly need to add or change some code to make the Power Text Jigger behave better and nicer, e.g. avoiding the eInvalidInput errors for text height, width factor and oblique angle, giving distance and angle input a base point reference, making the vertical and horizontal alignment mode input more user friendly, and addressing some of the DBText alignment limitations, but the changes or enhancements are just a few lines of code as can be checked from the comments in the complete code appended below.
#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 PowerTextJigger : 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 //ADDED initial value
public double mWidthFactor = 1; // Jig Factor #3 //ADDED initial value
public double mOblique; // Jig Factor #4
public Autodesk.AutoCAD.DatabaseServices.TextHorizontalMode mHorizontalMode; // Jig Factor #5
public Autodesk.AutoCAD.DatabaseServices.TextVerticalMode mVerticalMode; // Jig Factor #5 //ADDED
public double mRotation; // Jig Factor #6
#endregion
#region Constructors
public PowerTextJigger(DBText ent)
: base(ent)
{
// Initialize and transform the Entity.
Entity.SetDatabaseDefaults();
Entity.TransformBy(UCS);
}
#endregion
#region Properties
private Editor Editor
{
get
{
return MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
}
}
private 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; //ADDED to avoid the eInvalidInput error
Entity.Height = mHeight;
break;
case 3:
try { Entity.WidthFactor = mWidthFactor; }
catch { } //ADDED the Try/Catch
break;
case 4:
try { Entity.Oblique = mOblique; }
catch { } //ADDED the Try/Catch
break;
case 5:
Entity.HorizontalMode = mHorizontalMode;
//ADDED
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:");
// Set properties such as UseBasePoint and BasePoint of the prompt options object if necessary here.
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.Equals(mPosition)) //Use better comparison method if necessary.
{
return SamplerStatus.NoChange;
}
else
{
mPosition = prResult1.Value;
return SamplerStatus.OK;
}
case 2:
JigPromptDistanceOptions prOptions2 = new JigPromptDistanceOptions("\nHeight:");
// Set properties such as UseBasePoint and BasePoint of the prompt options object if necessary here.
prOptions2.UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.GovernedByUCSDetect;
//ADDED
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)) //Use better comparison method if necessary.
{
return SamplerStatus.NoChange;
}
else
{
mHeight = prResult2.Value;
return SamplerStatus.OK;
}
case 3:
JigPromptDistanceOptions prOptions3 = new JigPromptDistanceOptions("\nWidth factor:");
// Set properties such as UseBasePoint and BasePoint of the prompt options object if necessary here.
prOptions3.UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.GovernedByUCSDetect;
//ADDED
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)) //Use better comparison method if necessary.
{
return SamplerStatus.NoChange;
}
else
{
mWidthFactor = prResult3.Value;
return SamplerStatus.OK;
}
case 4:
JigPromptAngleOptions prOptions4 = new JigPromptAngleOptions("\nOblique:");
// Set properties such as UseBasePoint and BasePoint of the prompt options object if necessary here.
prOptions4.UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.GovernedByUCSDetect;
//ADDED
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)) //Use better comparison method if necessary.
{
return SamplerStatus.NoChange;
}
else
{
mOblique = prResult4.Value;
return SamplerStatus.OK;
}
case 5:
JigPromptPointOptions prOptions5 = new JigPromptPointOptions("\nAlignment mode (both Horizental and Vertical):");
// Set properties such as UseBasePoint and BasePoint of the prompt options object if necessary here.
prOptions5.UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.GovernedByUCSDetect;
//ADDED
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)) //Use better comparison method if necessary.
{
return SamplerStatus.NoChange;
}
else
{
//ADDED
//mHorizontalMode = prResult5.Value;
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:");
// Set properties such as UseBasePoint and BasePoint of the prompt options object if necessary here.
prOptions6.UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.GovernedByUCSDetect;
//ADDED
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)) //Use better comparison method if necessary.
{
return SamplerStatus.NoChange;
}
else
{
mRotation = prResult6.Value;
return SamplerStatus.OK;
}
default:
break;
}
return SamplerStatus.OK;
}
#endregion
#region Methods to Call
public static DBText Jig()
{
PowerTextJigger jigger = null;
try
{
jigger = new PowerTextJigger(new DBText());
//ADDED
string content = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.GetString("\nText content: ").StringResult;
jigger.Entity.TextString = content;
PromptResult pr;
do
{
pr = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.Drag(jigger);
if (pr.Status == PromptStatus.Keyword)
{
// Keyword handling code
}
else
{
jigger.mCurJigFactorIndex++;
}
} while (pr.Status != PromptStatus.Cancel && pr.Status != PromptStatus.Error && jigger.mCurJigFactorIndex <= 6);
if (pr.Status == PromptStatus.Cancel || pr.Status == PromptStatus.Error)
{
if (jigger != null && jigger.Entity != null)
jigger.Entity.Dispose();
return null;
}
else
return jigger.Entity;
}
catch
{
if (jigger != null && jigger.Entity != null)
jigger.Entity.Dispose();
return null;
}
}
#endregion
#region Test Commands
[CommandMethod("TestPowerTextJigger")]
public static void TestPowerTextJigger_Method()
{
try
{
Entity jigEnt = PowerTextJigger.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.ToString());
}
}
#endregion
}
}
All these jig factors or DBText properties can be vividly updated through some meaningful mouse movements, which may JIG really mean, rather than statically changed using some keyword inputs. If the keyword support is really needed, it can be easily and nicely achieved too as commented in the auto-generated code. This has been demonstrated many times before so we will not waste our words here.
The following illustrates how it behaves beautifully in AutoCAD.
By the way, the green point with the special point type serves as the initial position input for the power text jigger sample and every subsequent input will be relative to the point, e.g. the text height will be determined by the distance from the cursor to the point and the oblique angle will be determined by the line from the point to the cursor.
A lot of nice features are provided in the small but reliable and professional jig class, most of which cannot be easily seen from the static screenshot. For example, the text height can be updated with the mouse movement; the width factor and the oblique angle can be dynamically changed; the alignment mode (both vertical and horizontal as prompted) can be jigged with natural and meaningful mouse movements; the UCS is perfectly horned; command line input is also accepted; etc.
Please play around it and enjoy all the wonderful stuffs! You would love it!
The Entity Jigger wizard of the leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) can help you create EntityJig derivatives for various entity types automatically, quickly, reliably and professionally.
Template for creating a power text makes reviewing and analysis easily, which results in success, because it saves time and money.
Sample Templates: http://www.sampletemplates.org/
Posted by: Genebernice | 08/27/2012 at 09:01 AM