000

Index Labels

Dynamically Draw An Array Of Entities

.
One of the exciting things when an AutoCAD VBA programmer moves into AutoCAD .NET API world is that he/she suddenly gain a capability to create dynamic visual hint (ghost image) when the code ask user to interact with the drawing editor, namely the capability to create JIG. Besides, since AutoCAD 2009 introduced TransientGraphics, it is even easier do add visual hint in the code when the user has to interact with AutoCAD editor during code execution.

In this post I am going to show a piece of code that creates an array of entities in following operation steps:

1. User pick an entity he/she wants to create an array of copy of this entity;
2. The user does not know how many entities could fit into a space, but he/she has a desired distance between each 2 entities. Or the user simply does not bother to calculate how many entities could fit in, he/she just want to move the mouse and see how the array of entities fits in a give space. So, the user would enter a desired space increment for the entities in the array;
3. The user pick a base point;
4. The user moves the mouse around, the ghost image of an array of entities shows dynamically, which automatically show the count of entities in the array, depending on how far the mouse pointer is from the base point;
5. If user clicks the mouse again, an array of entity copies is created. If user cancels the pick for the second point, the ghost image is gone, no entity array is created.

There is a link at the bottom of this article that leads you to see a video clip of the result of running the code.

Again, I use TransientGraphics (I just love it!) to achieve my goal. Here is the code.

Firstly, I created an Interface IDynamicDrawTool. It is not a must. Currently, the code only create an array of entities along a straight line. Later I could create the array along an arc, circle...I would like all the later possible tool all implement this interface.

namespace DynamicDrawTool
{
public interface IDynamicDrawTool
{
void DrawEntities();
}
}

Then here is the command class:

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;

[assembly: CommandClass(typeof(DynamicDrawTool.DynamicDrawCommands))]

namespace DynamicDrawTool
{
public class DynamicDrawCommands
{
[CommandMethod("DynDraw")]
public static void RunThisMethod()
{
Document dwg = Autodesk.AutoCAD.ApplicationServices.
Application.DocumentManager.MdiActiveDocument;

Editor ed = dwg.Editor;

//Pick and entity
PromptEntityOptions opt = new PromptEntityOptions
("\nPick a source entity:");

PromptEntityResult res = ed.GetEntity(opt);

if (res.Status != PromptStatus.OK) return;

IDynamicDrawTool drawTool = new LinearDynamicDrawTool(dwg, res.ObjectId);

try
{
drawTool.DrawEntities();

dwg.Editor.WriteMessage(
"\nMyCommand executed successfully.");
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
dwg.Editor.WriteMessage(
"\nMyCommand execution failed:\n" + ex.Message);
}
}
}
}

Finally, the code doing the real work:

using System;
using System.Collections.Generic;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;

namespace DynamicDrawTool
{
public class LinearDynamicDrawTool : IDynamicDrawTool
{
private Document _dwg;
private Editor _editor;
private ObjectId _sourceEntId;

private Point3d _startPoint;
private Point3d _endPoint;
private double _increment = 0.0;

private Line _guideLine = null;
private List _clonedEntities = new List();

private int _colorIndex = 1;
private int _originalColorIndex = 0;

public LinearDynamicDrawTool(Document dwg, ObjectId sourceEntId)
{
_dwg = dwg;
_editor = _dwg.Editor;
_sourceEntId = sourceEntId;
}

public int GuideLineColorIndex
{
set { _colorIndex = value; }
get { return _colorIndex; }
}

public void DrawEntities()
{
//Pick start point
if (!GetPoint("Pick start point:", out _startPoint)) return;

//Get incremting distance
if (!GetIncrement(out _increment)) return;

//Hook up to PointerMoniter event
_editor.PointMonitor +=
new PointMonitorEventHandler(_editor_PointMonitor);

try
{
//Pick end point
if (GetPoint("Pick end point:", out _endPoint))
{
//Draw real entities exactly as
//the transient graphics shows
AddEntities();
}
}
finally
{
//Clear transient graphics and remove PointMonitor handler
ClearTransientGraphics();
_editor.PointMonitor -= _editor_PointMonitor;
}
}

#region private methods: draw transient graphics

private void _editor_PointMonitor(
object sender, PointMonitorEventArgs e)
{
DrawTransientGrapgics(e.Context.RawPoint);
}

private void DrawTransientGrapgics(Point3d pt)
{
//Clear existing transient graphics
ClearTransientGraphics();

//Draw guideline
_guideLine = new Line(_startPoint, pt);
_guideLine.SetDatabaseDefaults(_dwg.Database);
_guideLine.ColorIndex = _colorIndex;

IntegerCollection col = new IntegerCollection();
TransientManager.CurrentTransientManager.AddTransient(
_guideLine, TransientDrawingMode.DirectShortTerm, 128, col);

//Draw cloned entities
DrawClonedEntityTransientGraphics(pt);
}

private void DrawClonedEntityTransientGraphics(Point3d pt)
{
//Calculate count of cloned entities
int count = CalculateCloneCount(pt);
if (count < 1) return;

Entity sourceEnt = GetSourceEntity();
_originalColorIndex = sourceEnt.ColorIndex;

//Draw cloned entities as transient graphics
for (int i = 1; i <= count; i++)
{
Entity ent = sourceEnt.Clone() as Entity;
ent.ColorIndex = _colorIndex;

//Move to target location
SetClonedEntityPosition(i, ent);

//Draw as transient graphics
IntegerCollection col = new IntegerCollection();
TransientManager.CurrentTransientManager.AddTransient(
ent, TransientDrawingMode.DirectShortTerm, 128, col);

_clonedEntities.Add(ent);
}
}

private int CalculateCloneCount(Point3d pt)
{
double dis = _startPoint.DistanceTo(pt);

if (dis <= _increment)
return 0;
else
return Convert.ToInt32(Math.Floor(dis / _increment));
}

private Entity GetSourceEntity()
{
Entity ent = null;

using (Transaction tran =
_dwg.Database.TransactionManager.StartTransaction())
{
ent = (Entity)tran.GetObject(_sourceEntId, OpenMode.ForRead);
tran.Commit();
}

return ent;
}

private void SetClonedEntityPosition(int index, Entity ent)
{
double dist = _increment * index;
Point3d pt = _guideLine.GetPointAtDist(dist);

ent.TransformBy(
Matrix3d.Displacement(_startPoint.GetVectorTo(pt)));
}

private void ClearTransientGraphics()
{
//Clear guide line
if (_guideLine != null)
{
IntegerCollection col = new IntegerCollection();
TransientManager.CurrentTransientManager.
EraseTransient(_guideLine, col);

_guideLine.Dispose();
_guideLine = null;
}

//Clear cloned entities
foreach (Entity ent in _clonedEntities)
{
if (ent != null)
{
IntegerCollection col = new IntegerCollection();
TransientManager.CurrentTransientManager.
EraseTransient(ent, col);

ent.Dispose();
}
}

_clonedEntities.Clear();
}

private void AddEntities()
{
using (DocumentLock lk = _dwg.LockDocument())
{
using (Transaction tran =
_dwg.Database.TransactionManager.StartTransaction())
{
BlockTableRecord br =
(BlockTableRecord)tran.GetObject(
_dwg.Database.CurrentSpaceId, OpenMode.ForWrite);

foreach (Entity ent in _clonedEntities)
{
Entity newEnt = ent.Clone() as Entity;
newEnt.SetDatabaseDefaults(_dwg.Database);
newEnt.ColorIndex = _originalColorIndex;

br.AppendEntity(newEnt);
tran.AddNewlyCreatedDBObject(newEnt, true);
}

tran.Commit();
}
}
}

#endregion

#region private methods: miscellaneous

private bool GetPoint(string prompt, out Point3d point)
{
point = new Point3d(0.0, 0.0, 0.0);

PromptPointOptions opt =
new PromptPointOptions("\n" + prompt);
opt.AllowNone = false;

PromptPointResult res = _editor.GetPoint(opt);

if (res.Status == PromptStatus.OK)
{
point = res.Value;
return true;
}

return false;
}

private bool GetIncrement(out double increment)
{
increment = 0.0;

PromptDoubleOptions opt =
new PromptDoubleOptions("\nIncrementing distance:");

PromptDoubleResult res = _editor.GetDouble(opt);

if (res.Status == PromptStatus.OK)
{
increment = res.Value;
return true;
}

return false;
}

#endregion
}
}

Click here to see the a video clip showing how it works.

Obviously, there are more can be done to improve it. I have exposed ColorIndex as public property so that we can set the color of guide line/ghost image of the entities prior to calling DrawEntities() method. We can also do the similar thing to set LineWeight or line width (if it is polyline), LineType (use dash line would be more in line with AutoCAD standard).

Of course the user input part could also be enhanced to allow user to try different distance increment during mouse move.

Blog Archive

Labels

2D Drafting 3D Modeling 3D models 3D rendering 3D scanning 3D Sketch Inventor 3D visualization AI Design AI in Manufacturing AI Tools AR architectural modeling architectural visualization Architecture architecture design Architecture Productivity Artificial Intelligence augmented reality AutoCAD AutoCAD advice AutoCAD Basics AutoCAD Beginners AutoCAD Civil3D AutoCAD commands AutoCAD efficiency AutoCAD features AutoCAD File Management AutoCAD Layer AutoCAD learning AutoCAD print settings AutoCAD productivity AutoCAD Teaching AutoCAD Techniques AutoCAD tips AutoCAD tools AutoCAD training. AutoCAD tricks AutoCAD Tutorial AutoCAD workflow AutoCAD Xref Autodesk Autodesk 2025 Autodesk 3ds Max Autodesk AI Tools Autodesk Alias Autodesk AutoCAD Autodesk BIM 360 Autodesk Dynamo Autodesk Fusion 360 Autodesk InfraWorks Autodesk Inventor Autodesk Inventor Frame Generator Autodesk Inventor iLogic Autodesk plugins Autodesk Recap Autodesk Revit Autodesk Software Autodesk Tips Autodesk Vault Autodesk Video Autodesk Viewer Automation Automation Tutorial automotive design automotive visualization Basic Commands Basics Beginner Beginner Tips BIM BIM collaboration BIM Implementation BIM software BIM technology BIM workflow Block Editor ByLayer CAD CAD comparison CAD Data Management CAD Design CAD Evolution CAD File Size Reduction CAD line thickness CAD modeling CAD Optimization CAD plugins CAD Productivity CAD software car design software CGI design Civil 3D civil engineering Class-A surfacing clean CAD file cleaning command client engagement Cloud CAD Cloud Collaboration Cloud design platform Cloud-First collaboration command abbreviations concept car construction Construction Technology Contraints Create resizable blocks CTB STB Data Backup Data Reference Data Shortcut Design Automation Design Collaboration design review design software design software tips Design Workflow design-to-construction Digital Design Digital engineering digital fabrication Digital Twin Digital Twins Drafting Standards Drawing Automation Dref Dynamic Block Dynamic Block AutoCAD Dynamic Blocks Dynamic doors Dynamic windows Dynamo automation eco design editing commands energy efficiency Engineering Engineering Design Engineering Innovation engineering software Engineering Technology engineering tools Engineering Workflow Excel Express Tools External Reference Fast Structural Design Fusion 360 Generative Design Global design teams green building Grips Handoff heavy CAD file Heavy CAD Files iLogic Industry 4.0 infrastructure design Infrastructure Monitoring InfraWorks Insight interactive presentation Inventor API Inventor Drawing Template Inventor Frame Generator Inventor Graphics Issues Inventor IDW Inventor Tips Keyboard Shortcuts Large Projects Learn AutoCAD Linked Models Machine Learning in CAD maintenance command Management Manufacturing Innovation Mesh-to-BIM Metal Structure Model Optimization ObjectARX .NET API Organization OVERKILL OVERKILL AutoCAD Palette parametric design PDF Plot Style AutoCAD point cloud Practice Drawing Predictive Maintenance Printing Quality Product Design productivity tools professional printing Professional Tips project management PTC Creo PURGE PURGE AutoCAD ReCap reduce CAD file size rendering software Resizable Block Revit Revit add-ins Revit automation Revit Best Practices Revit Performance Revit Scripting Revit Tips Revit Workflow Ribbon screen shortcut keys Shortcuts Siemens NX Sketch Small Firms Smart Block Smart Factory Smart Infrastructur SolidWorks Steel Structure Design sustainability sustainable design Sustainable Manufacturing time saving tools toolbar Tutorial urban planning User Interface (UI) Vault Best Practices Vault Lifecycle Vault Mistakes vehicle modeling virtual reality visualization workflow VR VRED Workbook workflow Workflow Optimization Worksets Worksharing Workspace XLS Xref