Thursday, November 18, 2010

Dragging a Line in Certain Angle

This article is inspired by a question posted in the Autodesk's Visual Basic Customization user forum. Baiscally, while drawing a line, after picking the start point, the user wants the ghost line only stretch in certain direction/angle, similar effect as the Ortho-On mode. Well, as a programmer, not a drafter/designer, I am not very sure how often this kind of fuctionality is desired in AutoCAD use. If one wants to draw a line that he knows the line's start/end point, or start point, lenght and direction/angle, he can alway enter them easily at command line. However I can imagine that during designing (nt drafting) process, the designer may want to draw a line, starting at a known point and she'd like it to be stretched at certain angle with undecided length.

Regardless it possible use/benefit an AutoCAD user may find, here is the code to do this. Yes, as you may have guessed, I used TransientGraphics again.

Here is the class that do the dynamic dragging. At the end of AngledDrag() call, the class provides two points (Point3d) - StartPoint and EndPoint as public read-only properties for the calling procedure to use.

using System;

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

namespace AngleLockedDrag
{
    public class AngledDrag
    {
        private Document _dwg;
        private Database _db;
        private Editor _editor;

        private Point3d _startPoint = new Point3d(0.0, 0.0, 0.0);
        private Point3d _endPoint = new Point3d(0.0, 0.0, 0.0);

        private double _dragAngle = 45.0;

        private Line _dragLine = null;
        private int _colorIndex = 1;

        public AngledDrag(Document dwg)
        {
            _dwg = dwg;
            _db = dwg.Database;
            _editor = dwg.Editor;
        }

        public Point3d StartPoint
        {
            get { return _startPoint; }
        }

        public Point3d EndPoint
        {
            get { return _endPoint; }
        }

        #region public methods

        public bool DragAtAngle()
        {
            _endPoint = _startPoint;

            _editor.PointMonitor += 
                new PointMonitorEventHandler(Editor_PointMonitor);

            try
            {
                //Get end point
                if (GetEndPoint())
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            finally
            {
                ClearTransientGraphics();
                _editor.PointMonitor -= Editor_PointMonitor;
            }
        }

        #endregion

        #region private methods

        private void Editor_PointMonitor(
            object sender, PointMonitorEventArgs e)
        {
            DrawDragLine(e.Context.RawPoint);
            if (_dragLine != null)
            {
                e.AppendToolTipText("Angle: " + 
                    _dragAngle.ToString() + "\nLength: " + 
                    _dragLine.Length.ToString());
            }
            else
            {
                e.AppendToolTipText("");
            }
        }

        private void DrawDragLine(Point3d mousePoint)
        {
            ClearTransientGraphics();

            Point3d pt = CalculateEndPoint(mousePoint);

            _dragLine = new Line(_startPoint, pt);
            _dragLine.SetDatabaseDefaults(_db);
            _dragLine.ColorIndex = _colorIndex;

            IntegerCollection col = new IntegerCollection();
            TransientManager.CurrentTransientManager.AddTransient(
                _dragLine, TransientDrawingMode.Highlight, 128, col);

            //whenever the dragged line updated, reset _endPoint
            _endPoint = pt;
        }

        private void ClearTransientGraphics()
        {
            if (_dragLine != null)
            {
                IntegerCollection col = new IntegerCollection();
                TransientManager.CurrentTransientManager.
                    EraseTransient(_dragLine, col);

                _dragLine.Dispose();
                _dragLine = null;
            }
        }

        private Point3d CalculateEndPoint(Point3d mousePoint)
        {
            Point3d pt = mousePoint;

            if (_dragAngle <= 90.0 || _dragAngle >= 270.0)
            {
                if (mousePoint.X <= _startPoint.X)
                {
                    pt = _startPoint;
                }
                else
                {
                    if (_dragAngle <= 45.0 || _dragAngle >= 315.0)
                    {
                        double y = (mousePoint.X - _startPoint.X) * 
                            Math.Tan(_dragAngle * Math.PI / 180);
                        pt = new Point3d(
                            mousePoint.X, _startPoint.Y + y, 0.0);
                    }
                    else
                    {
                        if (_dragAngle > 45.0 && _dragAngle <= 90.0)
                        {
                            if (mousePoint.Y < _startPoint.Y)
                            {
                                pt = _startPoint;
                            }
                            else
                            {
                                double x = (mousePoint.Y - _startPoint.Y) / 
                                    Math.Tan(_dragAngle * Math.PI / 180);
                                pt = new Point3d(
                                    _startPoint.X + x, mousePoint.Y, 0.0);
                            }
                        }
                        else
                        {
                            if (mousePoint.Y > _startPoint.Y)
                            {
                                pt = _startPoint;
                            }
                            else
                            {
                                double x = (mousePoint.Y - _startPoint.Y) / 
                                    Math.Tan(_dragAngle * Math.PI / 180);
                                pt = new Point3d(
                                    _startPoint.X + x, mousePoint.Y, 0.0);
                            }
                        }
                    }

                    return pt;
                }
            }

            if (_dragAngle >= 90.0 && _dragAngle <= 270.0)
            {
                if (mousePoint.X >= _startPoint.X)
                {
                    pt = _startPoint;
                }
                else
                {
                    if (_dragAngle >= 135.0 && _dragAngle <= 225.0)
                    {
                        double y = (mousePoint.X - _startPoint.X) * 
                            Math.Tan(_dragAngle * Math.PI / 180);
                        pt = new Point3d(
                            mousePoint.X, _startPoint.Y + y, 0.0);
                    }
                    else
                    {
                        if (_dragAngle >=90.0 && _dragAngle < 135.0)
                        {
                            if (mousePoint.Y <= _startPoint.Y)
                            {
                                pt = _startPoint;
                            }
                            else
                            {
                                double x = (mousePoint.Y - _startPoint.Y) / 
                                    Math.Tan(_dragAngle * Math.PI / 180);
                                pt = new Point3d(
                                    _startPoint.X + x, mousePoint.Y, 0.0);
                            }
                        }
                        else
                        {
                            if (mousePoint.Y >= _startPoint.Y)
                            {
                                pt = _startPoint;
                            }
                            else
                            {
                                double x = (mousePoint.Y - _startPoint.Y) / 
                                    Math.Tan(_dragAngle * Math.PI / 180);
                                pt = new Point3d(
                                    _startPoint.X + x, mousePoint.Y, 0.0);
                            }
                        }
                        
                    }

                    return pt;
                }
            }

            return pt;
        }

        private bool GetEndPoint()
        {
            //endPoint = new Point3d();

            bool go = true;
            bool picked = false;

            while (go)
            {
                PromptPointOptions opt = new PromptPointOptions("\nPick point:");
                //opt.BasePoint = _startPoint;
                //opt.UseBasePoint = true;
                opt.Keywords.Add("Start point");
                opt.Keywords.Add("Angle");
                opt.Keywords.Add("End point");
                opt.Keywords.Add("eXit");
                opt.Keywords.Default = "End point";
                opt.AppendKeywordsToMessage = true;
                opt.AllowArbitraryInput = false;
                opt.AllowNone = false;

                PromptPointResult res = _editor.GetPoint(opt);
                if (res.Status == PromptStatus.Cancel)
                {
                    go = false; ;
                }
                else
                {
                    switch (res.Status)
                    {
                        case PromptStatus.Keyword:
                            //_editor.WriteMessage("\n" + res.StringResult);
                            if (res.StringResult.StartsWith("Start"))
                            {
                                SetStartPoint();
                                go = true;
                            }
                            if (res.StringResult.StartsWith("Angle"))
                            {
                                SetAngle();
                                go = true;
                            }
                            if (res.StringResult.StartsWith("eXit"))
                            {
                                go = false;
                            }
                            break;
                        case PromptStatus.OK:
                            //endPoint = res.Value;
                            picked = true;
                            go = false;
                            break;
                        default:
                            go = true;
                            break;
                    }
                }
            }

            return picked;
        }

        private void SetStartPoint()
        {
            ClearTransientGraphics();
            _editor.PointMonitor -= Editor_PointMonitor;

            PromptPointOptions opt = 
                new PromptPointOptions("\nStart point:");
            PromptPointResult res = _editor.GetPoint(opt);
            if (res.Status == PromptStatus.OK)
            {
                _startPoint = res.Value;
            }

            _editor.PointMonitor += 
                new PointMonitorEventHandler(Editor_PointMonitor);
        }

        private void SetAngle()
        {
            ClearTransientGraphics();
            _editor.PointMonitor -= Editor_PointMonitor;

            PromptDoubleOptions opt = 
                new PromptDoubleOptions("\nEnter drag-angle in degree [" + 
                    _dragAngle.ToString() + "]: ");
            opt.AllowNegative = false;
            opt.AllowZero = true;
            opt.AllowNone = true;

            PromptDoubleResult res = _editor.GetDouble(opt);

            if (res.Status == PromptStatus.OK)
            {
                _dragAngle = res.Value;
                if (_dragAngle > 360.0) _dragAngle -= 360.0;
            }

            _editor.PointMonitor += 
                new PointMonitorEventHandler(Editor_PointMonitor);
        }

        #endregion
    }
}

Here is the command class that uses the AngleLockedDrag class to draw a Line in AutoCAD.

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

[assembly: CommandClass(typeof(AngleLockedDrag.DragCommand))]

namespace AngleLockedDrag
{
    public class DragCommand
    {
        [CommandMethod("AngledDrag")]
        public void RunThisMethod()
        {
            Document dwg = Autodesk.AutoCAD.ApplicationServices.
                Application.DocumentManager.MdiActiveDocument;

            AngledDrag drag = new AngledDrag(dwg);
            try
            {
                if (drag.DragAtAngle())
                {
                    GenerateLine(dwg, drag.StartPoint, drag.EndPoint);

                    dwg.Editor.WriteMessage("\nMyCommand executed.");
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                dwg.Editor.WriteMessage("\nError: {0}\n", ex.Message);
            }
        }

        private static void GenerateLine(
            Document dwg, Point3d startPt, Point3d endPt)
        {
            using (Transaction tran = 
                dwg.Database.TransactionManager.StartTransaction())
            {
                BlockTableRecord br = (BlockTableRecord)tran.GetObject(
                    dwg.Database.CurrentSpaceId, OpenMode.ForWrite);
                
                Line line = new Line(startPt, endPt);
                line.SetDatabaseDefaults(dwg.Database);

                br.AppendEntity(line);
                tran.AddNewlyCreatedDBObject(line, true);
                tran.Commit();
            }
        }
    }
}

Here is the video clip that shows the "angled dragging" effect.

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.