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 even something that is not available natively in AutoCAD.
We have demonstrated various ways to jig a block (INSERT as used in AutoCAD users and BlockReference in programmers) such as by its position, rotation or both of these factors, from the block name input on, with the original graphics highlighted or not, with user cancellation handling or not, honoring UCS or not, and so on. We also demonstrated how to input a true color using the AutoCAD ColorDialog during the block (INSRT/BlockReference) inserting jig.
As we know, .NET also provides a ColorDialog. In this article, let us see how to use the .NET ColorDialog to input a true color for the block (INSERT/BlockReference) jig.
Here is the test command for the block insertion jig to get a true color from a .NET ColorDialog:
[CommandMethod("TestEntityJigger11")]
public static void TestEntityJigger11_Method()
{
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;
PromptResult pr = ed.GetString("\nBlock name:");
if (pr.Status == PromptStatus.OK)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[pr.StringResult], OpenMode.ForRead) as BlockTableRecord;
if (btr != null)
{
BlockReference ent = new BlockReference(new Point3d(0, 0, 0), btr.ObjectId);
if (BlockInsertNetColorJig.Jig(ent))
{
BlockTableRecord modelspace = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
modelspace.AppendEntity(ent);
tr.AddNewlyCreatedDBObject(ent, true);
tr.Commit();
}
else
{
ent.Dispose();
tr.Abort();
}
}
}
}
}
Here is what the .NET ColorDialog looks like after the test command runs and the keyword Color is chosen:
Here is what the rectangle block looks like right after the true color input from the .NET ColorDialog:
Please do not forget to set the color property of the rectangle inside the block definition as ByBlock, otherwise, the rectangle will not horror the Color property of the block reference that was just set.
Here is the core source:
#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 BlockInsertNetColorJig : EntityJig
{
#region Fields
public int mCurJigFactorNumber = 1;
private Point3d mPosition; // Factor #1
private double mRotation; // Factor #2
private double mAngleOffset;
#endregion
#region Constructors
public BlockInsertNetColorJig(Entity ent)
: base(ent)
{
ent.TransformBy(MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem);
mAngleOffset = (ent as BlockReference).Rotation;
}
#endregion
#region Overrides
protected override bool Update()
{
switch (mCurJigFactorNumber)
{
case 1:
(Entity as BlockReference).Position = mPosition;
break;
case 2:
(Entity as BlockReference).Rotation = mAngleOffset + mRotation;
break;
default:
return false;
}
return true;
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
switch (mCurJigFactorNumber)
{
case 1:
JigPromptPointOptions prOptions1 = new JigPromptPointOptions("\nBlock position:");
prOptions1.UserInputControls = UserInputControls.GovernedByUCSDetect | UserInputControls.UseBasePointElevation | UserInputControls.Accept3dCoordinates;
prOptions1.Keywords.Add("Color");
PromptPointResult prResult1 = prompts.AcquirePoint(prOptions1);
if (prResult1.Status == PromptStatus.Cancel) return SamplerStatus.Cancel;
if (prResult1.Value.Equals(mPosition))
{
return SamplerStatus.NoChange;
}
else
{
mPosition = prResult1.Value;
return SamplerStatus.OK;
}
case 2:
JigPromptAngleOptions prOptions2 = new JigPromptAngleOptions("\nBlock rotation angle:");
prOptions2.BasePoint = mPosition;
prOptions2.UseBasePoint = true;
prOptions2.Keywords.Add("Color");
PromptDoubleResult prResult2 = prompts.AcquireAngle(prOptions2);
if (prResult2.Status == PromptStatus.Cancel) return SamplerStatus.Cancel;
if (prResult2.Value.Equals(mRotation))
{
return SamplerStatus.NoChange;
}
else
{
mRotation = prResult2.Value;
return SamplerStatus.OK;
}
default:
break;
}
return SamplerStatus.OK;
}
#endregion
#region Method to Call
public static bool Jig(BlockReference ent)
{
try
{
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
BlockInsertNetColorJig jigger = new BlockInsertNetColorJig(ent);
PromptResult pr;
do
{
pr = ed.Drag(jigger);
if (pr.Status == PromptStatus.Keyword)
{
if (pr.StringResult == "Color")
{
System.Windows.Forms.ColorDialog cd = new System.Windows.Forms.ColorDialog();
cd.AllowFullOpen = true;
cd.ShowHelp = true;
cd.AnyColor = true;
cd.SolidColorOnly = false;
System.Drawing.Color netColor = System.Drawing.Color.FromArgb(
jigger.Entity.Color.Red,
jigger.Entity.Color.Green,
jigger.Entity.Color.Blue);
cd.Color = netColor;
if (cd.ShowDialog() == DialogResult.OK)
{
jigger.Entity.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(
cd.Color.R,
cd.Color.G,
cd.Color.B);
}
}
}
else
{
jigger.mCurJigFactorNumber++;
}
} while (pr.Status != PromptStatus.Cancel &&
pr.Status != PromptStatus.Error &&
jigger.mCurJigFactorNumber <= 2);
return pr.Status == PromptStatus.OK;
}
catch
{
return false;
}
}
#endregion
}
}
Some highlights about the code may be helpful:
• The UserInputControls enumerator values GovernedByUCSDetect and Accept3dCoordinates for the JigPromptPointOptions indicate if the point input honors the UCS and if the picked point is in 3D.
• The UCS transformation can be obtained from the static property Editor.CurrentUserCoordinateSystem.
• An entity type needs to be specified in the EntityJig derivative, as BlockReference here.
• The Sampler() override is to acquire input for the block position and rotation angle in a certain order.
• If the input is the same as the stored variable, we’d better return SamplerStatus.NoChange to avoid unnecessary flashing; if not, return SamplerStatus.OK.
• Please do not forget to handle the cancel/escape circumstance as demonstrated.
• The Update() override is to update the position and the rotation properties of the block in the same order as set in the Sampler().
• The Editor.Draw() is the power to fire the jig. If two properties need to be set, the jig needs to be fired off twice. That is why a while loop is used.
• The while loop needs to think about the PromptStatus.Keyword case of the PromptResult after each Jig Drag.
• Only after the jig succeeds should the BlockReference be added to the database to avoid database messy.
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides a coder, Entity Jigger, to help us create entity jig code automatically, quickly and reliably.
Recent Comments