Friday, November 5, 2010

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.

1 comment: