[即时预览] 使用向导创建圆JIG
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 for AutoCAD .NET programming.In this article, let us see how the Entity Jigger wizard of AutoCAD .NET Addin Wizard (AcadNetAddinWizard) can create a circle jigger automatically in no time for us.
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 version both 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:
http://spiderinnet1.typepad.com/.a/6a0153928ee38e970b0167644528c5970b-800wi
Here are wizard pages and some sample fill-outs of the Entity Jigger wizard:
http://spiderinnet1.typepad.com/.a/6a0153928ee38e970b0168e9461d40970c-800wi
http://spiderinnet1.typepad.com/.a/6a0153928ee38e970b0167646b2fce970b-800wi
http://spiderinnet1.typepad.com/.a/6a0153928ee38e970b016303503805970d-800wi
http://spiderinnet1.typepad.com/.a/6a0153928ee38e970b0168e96c6946970c-800wi
http://spiderinnet1.typepad.com/.a/6a0153928ee38e970b0168e9461ebb970c-800wi
http://spiderinnet1.typepad.com/.a/6a0153928ee38e970b016303767349970d-800wiThe auto-generated circle jig class and test command may look like the following:
#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 Acad2012NetDemoAddinCS
{
public class EntityJigger1 : EntityJig
{
#region Fields
public int mCurJigFactorIndex = 1;// Jig Factor Index
public Autodesk.AutoCAD.Geometry.Point3d mCenter; // Jig Factor #1
public double mRadius; // Jig Factor #2
#endregion
#region Constructors
public EntityJigger1(Circle ent)
: base(ent)
{
// Initialize and transform the Entity.
Entity.SetDatabaseDefaults();
Entity.TransformBy(UCS);
Entity.Radius = 1e-10;//Here is the code manually added to get rid of the degeration error.
}
#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 Circle Entity// Overload the Entity property for convenience.
{
get
{
return base.Entity as Circle;
}
}
protected override bool Update()
{
switch (mCurJigFactorIndex)
{
case 1:
Entity.Center = mCenter;
//Entity.Center.TransformBy(UCS); //Turn it on for UCS transformation or tweak it if not compile.
break;
case 2:
Entity.Radius = mRadius;
//Entity.Radius.TransformBy(UCS); //Turn it on for UCS transformation or tweak it if not compile.
break;
default:
return false;
}
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
switch (mCurJigFactorIndex)
{
case 1:
JigPromptPointOptions prOptions1 = new JigPromptPointOptions("\nCircle center:");
// Set properties such as UseBasePoint and BasePoint of the prompt options object if necessary here.
prOptions1.UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.GovernedByUCSDetect | UserInputControls.UseBasePointElevation;
PromptPointResult prResult1 = prompts.AcquirePoint(prOptions1);
if (prResult1.Status == PromptStatus.Cancel && prResult1.Status == PromptStatus.Error)
return SamplerStatus.Cancel;
if (prResult1.Value.Equals(mCenter))//Use better comparison method if necessary.
{
return SamplerStatus.NoChange;
}
else
{
mCenter = prResult1.Value;
return SamplerStatus.OK;
}
case 2:
JigPromptDistanceOptions prOptions2 = new JigPromptDistanceOptions("\nRadius:");
// Set properties such as UseBasePoint and BasePoint of the prompt options object if necessary here.
prOptions2.UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.GovernedByUCSDetect | UserInputControls.UseBasePointElevation;
//Here is the code manully added to make the radius jigging effect better:
prOptions2.BasePoint = mCenter;
prOptions2.UseBasePoint = true;
PromptDoubleResult prResult2 = prompts.AcquireDistance(prOptions2);
if (prResult2.Status == PromptStatus.Cancel && prResult2.Status == PromptStatus.Error)
return SamplerStatus.Cancel;
if (prResult2.Value.Equals(mRadius))//Use better comparison method if necessary.
{
return SamplerStatus.NoChange;
}
else
{
mRadius = prResult2.Value;
return SamplerStatus.OK;
}
default:
break;
}
return SamplerStatus.OK;
}
#endregion
#region Methods to Call
public static Circle Jig()
{
EntityJigger1 jigger = null;
try
{
jigger = new EntityJigger1(new Circle());
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 <= 2);
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
public static void TestEntityJigger1_Method()
{
try
{
Entity jigEnt = EntityJigger1.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
}
}
For this case, only two small changes need to be tweaked manually as commented in the above auto-generated code. One is to make the circle radius jig effect look better and the other one is to avoid the degeneration issue. All other stuffs complex or simpler are all taken care of well, including but not limited to honoring UCS, canceling jig input, and adding the entity to the right space.
Now we are ready to go. Press F5 to start up AutoCAD and type TestEntityJigger1 into the AutoCAD command line window and see what it will give you, either in WCS or UCS, either in Model Space or Paper Space, either finishing the jig to the end or canceling the jig in middle way, etc.
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. You name it.
The Entity Jigger wizard of the leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard)can help you create various entity jiggers
页:
[1]