Wednesday, May 31, 2017

Swap Layout Order

A question was posted in the popular CAD programming discussion forum here, regarding re-ordering layout tabs (setting Layout.TabOrder). In this discussion thread, one of my posts published here quite a few years ago was mentioned.

While the situation is a bit different from the situation described in my old post, I thought the way to re-order layout should be the same: set TabOrder property to desired number.

However, the TabOrder cannot be duplicated, and cannot be 0 (which is always the TabOrder of "MODEL" layout). So, when re-ordering a Layout tab, one must realise that a layout tab can either not be re-ordered (the first one cannot be moved down, the last one cannot be moved up), or the move will affect the layout before or after it (that is, the affected layout needs to swap its TabOrder with the one you want to re-order).

Another thing to know is, once you have Layout object open in a Transaction, you can set its TabOrder to a number that is currently assigned to other Layout. For example, you have 3 layouts. You open the Layout object in the middle, which has TabOrder=2. Then you can have code

Layout2.TabOrder=1

At this time, the first layout also has TabOrder=1. When the code executes the line Layout2.TabOrder=1, there is no exception raised. Error only raise when the Transaction is committing, because of the duplicated TabOrder.

OK, enough explanation. Here is my sample project showing how easy to re-order layout tabs by swapping TabOrder values of 2 Layout objects.

Firstly, I wrote a class doing the CAD processing, including retrieving Layout information for UI, and the TabOrder swaping code:
using System.Collections.Generic;
using System.Linq;
using Autodesk.AutoCAD.DatabaseServices;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
 
namespace SwapLayoutTabs
{
    public class CadUtil
    {
        public static IEnumerable<LayoutInfo> GetLayoutInformation()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
 
            var lst = new List<LayoutInfo>();
 
            using (var tran = dwg.TransactionManager.StartTransaction())
            {
                var dic = (DBDictionary)tran.GetObject(
                    dwg.Database.LayoutDictionaryId, OpenMode.ForRead);
                foreach (DBDictionaryEntry entry in dic)
                {
                    var layout = (Layout)tran.GetObject(entry.Value, OpenMode.ForRead);
                    if (layout.LayoutName.ToUpper() != "MODEL")
                    {
                        lst.Add(new LayoutInfo()
                        {
                            LayoutId = entry.Value,
                            TabOrder = layout.TabOrder,
                            LayoutName = layout.LayoutName
                        });
                    }
                }
                tran.Commit();
            }
 
            return from l in lst orderby l.TabOrder ascending select l;
        }
 
        public static void SwapLayoutTabOrder(ObjectId lay1Id, ObjectId lay2Id)
        {
            using (var tran = lay1Id.Database.TransactionManager.StartTransaction())
            {
                var layout1 = (Layout)tran.GetObject(lay1Id, OpenMode.ForWrite);
                var layout2 = (Layout)tran.GetObject(lay2Id, OpenMode.ForWrite);
 
                var order1 = layout1.TabOrder;
                var order2 = layout2.TabOrder;
 
                layout1.TabOrder = order2;
                layout2.TabOrder = order1;
 
                tran.Commit();
            }
        }
 
        public static void UpdateEditorScreen()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            dwg.Editor.Regen();
        }
    }
}


I created a dialog box to allow user to re-order the layout:


To fill up the ListView and then let user to manipulate the layout orders I create a class LayoutInfo to hold layout information:
using Autodesk.AutoCAD.DatabaseServices;
 
namespace SwapLayoutTabs
{
    public class LayoutInfo
    {
        public string LayoutName { setget; }
        public int TabOrder { setget; }
        public ObjectId LayoutId { setget; }
    }
}

Here is the dialog form's code-behind:
using System;
using System.Windows.Forms;
 
using Autodesk.AutoCAD.DatabaseServices;
 
namespace SwapLayoutTabs
{
    public partial class dlgLayoutOrder : Form
    {
        public dlgLayoutOrder()
        {
            InitializeComponent();
        }
 
        private void SetListView()
        {
            var layouts = CadUtil.GetLayoutInformation();
 
            lvLayouts.Items.Clear();
 
            foreach (var layout in layouts)
            {
                var item = new ListViewItem(layout.TabOrder.ToString());
                item.SubItems.Add(layout.LayoutName);
                item.Tag = layout.LayoutId;
                item.Selected = false;
 
                lvLayouts.Items.Add(item);
            }
 
            btnUp.Enabled = false;
            btnDown.Enabled = false;
        }
 
        private void OrderLayoutTab(bool increment)
        {
            var selectedIndex = lvLayouts.SelectedItems[0].Index;
            var otherIndex = increment ? selectedIndex + 1 : selectedIndex - 1;
 
            ObjectId id1 = (ObjectId)lvLayouts.Items[selectedIndex].Tag;
            ObjectId id2 = (ObjectId)lvLayouts.Items[otherIndex].Tag;
 
            try
            {
                Cursor = Cursors.WaitCursor;
 
                CadUtil.SwapLayoutTabOrder(id1, id2);
                CadUtil.UpdateEditorScreen(); //Regen is require to actually see the tab change in AutoCAD editor
                SetListView();
            }
            catch(System.Exception ex)
            {
                MessageBox.Show("Swapping layout tab error:\n\n" + ex.Message);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }
 
        private void lvLayouts_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvLayouts.SelectedItems.Count == 0)
            {
                btnUp.Enabled = false;
                btnDown.Enabled = false;
            }
            else
            {
                var selectedIndex = lvLayouts.SelectedItems[0].Index;
                btnUp.Enabled = selectedIndex > 0;
                btnDown.Enabled = selectedIndex < lvLayouts.Items.Count - 1;
            }
        }
 
        private void btnUp_Click(object sender, EventArgs e)
        {
            OrderLayoutTab(false);
        }
 
        private void btnDown_Click(object sender, EventArgs e)
        {
            OrderLayoutTab(true);
        }
 
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
 
        private void dlgLayoutOrder_Load(object sender, EventArgs e)
        {
            SetListView();
        }
    }
}

And finally, this the command class:
using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
 
[assemblyCommandClass(typeof(SwapLayoutTabs.MyCommands))]
 
namespace SwapLayoutTabs
{
    public class MyCommands 
    {
        [CommandMethod("OrderLayouts")]
        public static void SetLayoutOrders()
        {
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            var ed = dwg.Editor;
 
            try
            {
                using (var dlg = new dlgLayoutOrder())
                {
                    CadApp.ShowModalDialog(dlg);
                }
            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("\nError: {0}", ex.Message);
                ed.WriteMessage("\n*Cancel*");
            }
            finally
            {
                Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
            }
        }
    }
}

Go to this video clip to see how the code works.














Tuesday, April 4, 2017

Asking User To Select Entity In Other Drawing - 2 of 2

This is the second post on the same topic discussed here. In the previous post I used an approach that the source drawing, in which user needs to select something, should not be pre-opened in current AutoCAD session, because when AutoCAD (since 2015, of course) switch active drawing from one opened drawing to another opened drawing, the currently executing command will be suspended (either Document.CommandEnded or DocumentCommandCancelled event then is raised, depending on the situation).

As the previous article demonstrated, with a session command method, we can let the code open a drawing from file in AutoCAD and the newly opened drawing becomes MdiActiveDocument and the command can continue (as opposed to command being suspended when MdiActiveDocument is switched to another already opened drawing).

In this post, I use a different approach to allow user to switch MdiActiveDocument, select something there and then switch back to original drawing and continue the intended workflow. Here, I handle DocumentCollection and Document events to chain the workflow together. The following code is pretty much self-explanatory:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
 
[assemblyCommandClass(typeof(SelectFromOtherDwg2.TextCommand))]
 
namespace SelectFromOtherDwg2
{
    public class TextCommand
    {
        private static DocChangeReason _changeReason = DocChangeReason.ForNone;
        private static DocumentCollection _dwgManager = null;
        private static string _sourceDwgName = null;
        private static Document _workDwg = null;
        private static CircleInfo _circleInfo = null;
 
        [CommandMethod("SwitchDwg"CommandFlags.Session)]
        public static void SwitchDwg()
        {
            InitializeData();
            SetSourceDocumentCurrent();
        }
 
        [CommandMethod("Selecting"CommandFlags.Session | CommandFlags.NoHistory)]
        public static void SelectingInOtherDwg()
        {
            if (string.IsNullOrEmpty(_sourceDwgName) ||
                _workDwg == nullreturn;
 
            var dwg = CadApp.DocumentManager.MdiActiveDocument;
            if (dwg.Name == _sourceDwgName)
            {
                CircleInfo info;
                bool picked = GetSourceCircle(dwg.Editor, out info);
                if (picked)
                {
                    _changeReason = DocChangeReason.ForDrawing;
                    _circleInfo = info;
                }
                else
                {
                    _changeReason = DocChangeReason.ForNone;
                }
 
                // Return back to original drawing
                CadApp.DocumentManager.MdiActiveDocument = _workDwg;
            }
        }
 
        private static void Document_CommandCancelled(
            object sender, CommandEventArgs e)
        {
            if (e.GlobalCommandName.ToUpper().Contains("SWITCHDWG"))
            {
                //CadApp.ShowAlertDialog("Command is cancelled." +
                //    "\nCurrent Drawing: " +
                //    CadApp.DocumentManager.MdiActiveDocument.Name);
 
                _workDwg.CommandCancelled -= Document_CommandCancelled;
                _workDwg.CommandEnded -= Document_CommandEnded;
 
                if (CadApp.DocumentManager.MdiActiveDocument.Name == _sourceDwgName &&
                    _changeReason == DocChangeReason.ForPicking)
                {
                    CadApp.DocumentManager.MdiActiveDocument.SendStringToExecute(
                        "Selectinig "truefalsefalse);
                }
            }
        }
 
        private static void Document_CommandEnded(object sender, CommandEventArgs e)
        {
            if (e.GlobalCommandName.ToUpper().Contains("SWITCHDWG"))
            {
                //CadApp.ShowAlertDialog("Command is ended." +
                //    "\nCurrent Drawing: " + 
                //    CadApp.DocumentManager.MdiActiveDocument.Name);
 
                _workDwg.CommandCancelled -= Document_CommandCancelled;
                _workDwg.CommandEnded -= Document_CommandEnded;
 
                if (CadApp.DocumentManager.MdiActiveDocument.Name == _sourceDwgName &&
                    _changeReason == DocChangeReason.ForPicking)
                {
                    CadApp.DocumentManager.MdiActiveDocument.SendStringToExecute(
                        "Selecting "truefalsefalse);
                }
            }
        }
 
        private static void DocumentCollection_DocumentBecameCurrent(
            object sender, DocumentCollectionEventArgs e)
        {
            if (e.Document.Name==_workDwg.Name && 
                _changeReason== DocChangeReason.ForDrawing)
            {
                _dwgManager.DocumentBecameCurrent -= 
                    DocumentCollection_DocumentBecameCurrent;
                //CadApp.ShowAlertDialog("Draw circle here...");
                DrawCircle(e.Document, _circleInfo.Center, _circleInfo.Radius);
                _workDwg = null;
            }
        }
 
        private static void InitializeData()
        {
            _dwgManager = CadApp.DocumentManager;
            _dwgManager.DocumentBecameCurrent += 
                DocumentCollection_DocumentBecameCurrent;
            
            _circleInfo = null;
            _changeReason = DocChangeReason.ForNone;
 
            _workDwg = CadApp.DocumentManager.MdiActiveDocument;
            _workDwg.CommandCancelled += Document_CommandCancelled;
            _workDwg.CommandEnded += Document_CommandEnded;
        }
        
        private static void SetSourceDocumentCurrent()
        {
            Document dwg = null;
 
            if (!string.IsNullOrEmpty(_sourceDwgName))
            {
                // if source drawing already open in AutoCAD session?
                foreach (Document d in _dwgManager)
                {
                    if (d.Name.ToUpper() == _sourceDwgName.ToUpper())
                    {
                        dwg = d;
                        break;
                    }
                }
            }
 
            if (dwg == null)
            {
                _sourceDwgName = SelectSourceFile();
                if (!string.IsNullOrEmpty(_sourceDwgName))
                {
                    // Open drawing in AutoCAD, the opened drawing
                    // becomes MdiActiveDocument, then the command ends,
                    // which results in CommandEnded event being raised
                    _changeReason = DocChangeReason.ForPicking;
                    _dwgManager.Open(_sourceDwgName, true);
                }
                else
                {
                    _changeReason = DocChangeReason.ForNone;
                }
            }
            else
            {
                // Switch MdiActiveDocument, which results in 
                // current command ("SwitchDwg") being cancelled
                _changeReason = DocChangeReason.ForPicking;
                _dwgManager.MdiActiveDocument = dwg;
            }
        }
 
        private static string SelectSourceFile()
        {
            var fName = "";
 
            using (var dlg = new System.Windows.Forms.OpenFileDialog())
            {
                dlg.Title = "Select Source Drawing";
                dlg.Filter = "AutoCAD Drawing (*.dwg)|*.dwg";
                dlg.Multiselect = false;
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    fName = dlg.FileName;
                }
            }
 
            return fName;
        }
 
        private static bool GetSourceCircle(
            Editor ed, out CircleInfo circleInfo)
        {
            circleInfo = null;
 
            var opt = new PromptEntityOptions("\nSelect a circle:");
            opt.SetRejectMessage("\nInvalid: not a circle.");
            opt.AddAllowedClass(typeof(Circle), true);
 
            var res = ed.GetEntity(opt);
            if (res.Status == PromptStatus.OK)
            {
                using (var tran =
                    ed.Document.TransactionManager.StartTransaction())
                {
                    var c = (Circle)tran.GetObject(
                        res.ObjectId, OpenMode.ForRead);
                    circleInfo = new CircleInfo
                    {
                        Center = c.Center, Radius = c.Radius
                    };
                    tran.Commit();
                }
 
                return true;
            }
            else
            {
                return false;
            }
        }
 
        private static void DrawCircle(Document dwg, Point3d pt, double r)
        {
            using (var lck = dwg.LockDocument())
            {
                using (var tran =
                    dwg.TransactionManager.StartTransaction())
                {
                    var model = (BlockTableRecord)tran.GetObject(
                        SymbolUtilityServices.GetBlockModelSpaceId(
                            dwg.Database), OpenMode.ForWrite);
 
                    var c = new Circle();
                    c.Center = pt;
                    c.Radius = r;
                    c.SetDatabaseDefaults(dwg.Database);
 
                    model.AppendEntity(c);
                    tran.AddNewlyCreatedDBObject(c, true);
 
                    tran.Commit();
                }
            }
        }
    }
}

Watch this video clip to see the result of running this code.

See we can see, with this approach, the source drawing can stay open in current AutoCAD session, so that user can switch to it for selecting as many times as needed, as opposed to in the previous approach where the source drawing has to be opened/closed each time user needs to select something from it.

Monday, April 3, 2017

Asking User To Select Entity In Other Drawing - 1 of 2

A question recently posted in AutoCAD discussion forum reminded me the very same situation I had dealt with when my office upgraded to AutoCAD 2015: a couple of existing AutoCAD add-in apps developed prior to AutoCAD 2015 were broken: during the execution of a custom command (with CommandFlags.Session being set), user has to switch another drawing (if it is not currently open in the same AutoCAD session, it then would be opened first, of course) and pick something (either merely picking a point, or picking one or more entities); after picking, AutoCAD should switch back to the drawing where the command starts from and continue.

There is no problem of doing this operation with AutoCAD prior to AutoCAD 2015. But since AutoCAD2015, a quite impacting change was introduced: switching active drawing from one drawing to another drawing in AutoCAD session will cancel currently executing command. It is this change broke our existing applications, and I had to fix the breaks before our office can move up to AutoCAD2015. It was done quite a while ago. At that time, I thought to post my solution in my blog here and wrote something down as draft. However I had never found time to complete it. Now seeing the question is raised, a few days ago, I dug the old stuff out and updated it, so that it can be shared by people who may be interested in.

I tried 2 different ways to deal with this issue. Here is the discussion on the first approach.

The solution is rather simple actually with these key points:
1. Do not have the source drawing (the drawing you want user to go into and select something there) been already open in AutoCAD, so that you DO NOT switch active drawing (in order to pick);
2. In a session command, you can use code to open the source drawing; upon the drawing being opened, it becomes MdiActiveDocument automatically (because of CommandFlags.Session, of course); then you can use Editor.Getxxxx() to let user select; once selecting is done, close the source drawing, so that the original working drawing becomes MdiActiveDocument again, the command continues.

My following code demonstrates 2 commands being used in the situation:

  • Command "GetDataCmd": once the command starts, AutoCAD open the source drawing for user to select a circle; after selecting, the source drawing is closed; then a circle of the same center point and radius is drawn;
  • Command "GetDataUI": the command brings up a modal dialog; user clicks "Get Circle" button to hide to dialog and opens the source drawing for selecting. If selecting is done, the source drawing is closed and the dialog box comes back to show the data obtained from source drawing; clicking "OK" button draws a circle in current drawing with obtained circle data (center point and radius).
  • The source drawing has a few circles in it for user to select. For the simplicity, I omitted the code to show an "Open File" dialog box to allow user to choose a source drawing. Instead, I hard-coded a file name.
See following code.

The CommandClass code:
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
 
[assemblyCommandClass(typeof(SelectFromOtherDwg.MyCommands))]
 
namespace SelectFromOtherDwg
{
    public class MyCommands
    {
        private static string _sourceDwgFile = @"C:\Temp\MyCircles.dwg";
 
        [CommandMethod("GetDataCmd"CommandFlags.Session)]
        public static void GetDataFromOtherDwgViaCmd()
        {
            var doc = CadApp.DocumentManager.MdiActiveDocument;
            if (!doc.IsNamedDrawing)
            {
                CadApp.ShowAlertDialog(
                    "Current drawing is untitled. Please save it first!");
                return;
            }
 
            string curDwgName = doc.Name;
 
            var sourceDwg = OpenDataSourceDrawing(_sourceDwgFile);
            if (sourceDwg != null)
            {
                Point3d point;
                double radius;
                bool picked = false;
 
                picked = GetSourceCircleCenter(
                    sourceDwg.Editor, out point, out radius);
                sourceDwg.CloseAndDiscard();
 
                if (picked)
                {
                    foreach (Document d in CadApp.DocumentManager)
                    {
                        if (d.Name.ToUpper()==curDwgName.ToUpper())
                        {
                            DrawCircle(d, point, radius);
                            CadApp.DocumentManager.MdiActiveDocument = d;
                            break;
                        }
                    }           
                }
            }
            else
            {
                CadApp.ShowAlertDialog("Cannot open source drawing!");
            }
        }
 
        [CommandMethod("GetDataUI"CommandFlags.Session)]
        public static void GetDataFromOtherDwgViaUI()
        {
            var doc = CadApp.DocumentManager.MdiActiveDocument;
            if (!doc.IsNamedDrawing)
            {
                CadApp.ShowAlertDialog(
                    "Current drawing is untitled. Please save it first!");
                return;
            }
 
            string curDwgName = doc.Name;
 
            var dlg = new dlgGetData();
 
            double radius = 0.0;
            Point3d point = Point3d.Origin;
 
            try
            {
                var res = CadApp.ShowModalDialog(dlg);
                if (res== System.Windows.Forms.DialogResult.OK)
                {
                    if (dlg.CloseForPick)
                    {
                        var sourceDwg = OpenDataSourceDrawing(_sourceDwgFile);
                        if (sourceDwg != null)
                        {
                            Point3d pt;
                            double r;
                            bool picked = false;
 
                            picked = GetSourceCircleCenter(
                                sourceDwg.Editor, out pt, out r);
                            sourceDwg.CloseAndDiscard();
 
                            if (picked)
                            {
                                dlg.SetPickedData(pt.X, pt.Y, pt.Z, r);
                                res = CadApp.ShowModalDialog(dlg);
                                if (res == System.Windows.Forms.DialogResult.OK)
                                {
                                    if (!dlg.CloseForPick)
                                    {
                                        radius = dlg.Radius;
                                        point = new Point3d(
                                            dlg.CenterX, dlg.CenterY, dlg.CenterZ);
                                    }
                                }
                            }
                        }
                        else
                        {
                            CadApp.ShowAlertDialog("Cannot open source drawing!");
                        }
                    }
                }
 
                if (radius > 0.0)
                {
                    foreach (Document d in CadApp.DocumentManager)
                    {
                        if (d.Name.ToUpper() == curDwgName.ToUpper())
                        {
                            DrawCircle(d, point, radius);
                            CadApp.DocumentManager.MdiActiveDocument = d;
                            break;
                        }
                    }
                }
            }
            finally
            {
                dlg.Dispose();
            }
        }
 
        #region private methods
 
        private static Document OpenDataSourceDrawing(string dwgFileName)
        {
            if (!File.Exists(dwgFileName)) return null;
 
            var dwg = CadApp.DocumentManager.Open(dwgFileName, true);
            return dwg;
        }
 
        private static bool GetSourceCircleCenter(
            Editor ed, out Point3d centerPoint, out double radius)
        {
            centerPoint = Point3d.Origin;
            radius = 0.0;
 
            var opt = new PromptEntityOptions("\nSelect a circle:");
            opt.SetRejectMessage("\nInvalid: not a circle.");
            opt.AddAllowedClass(typeof(Circle), true);
 
            var res = ed.GetEntity(opt);
            if (res.Status==PromptStatus.OK)
            {
                using (var tran = 
                    ed.Document.TransactionManager.StartTransaction())
                {
                    var c = (Circle)tran.GetObject(
                        res.ObjectId, OpenMode.ForRead);
                    centerPoint = c.Center;
                    radius = c.Radius;
                    tran.Commit();
                }
 
                return true;
            }
            else
            {
                return false;
            }
        }
 
        private static void DrawCircle(Document dwg, Point3d pt, double r)
        {
            using (var lck = dwg.LockDocument())
            {
                using (var tran = 
                    dwg.TransactionManager.StartTransaction())
                {
                    var model = (BlockTableRecord)tran.GetObject(
                        SymbolUtilityServices.GetBlockModelSpaceId(
                            dwg.Database), OpenMode.ForWrite);
 
                    var c = new Circle();
                    c.Center = pt;
                    c.Radius = r;
                    c.SetDatabaseDefaults(dwg.Database);
 
                    model.AppendEntity(c);
                    tran.AddNewlyCreatedDBObject(c, true);
 
                    tran.Commit();
                }
            }
        }
 
        #endregion
    }
}

This is the modal dialog form:


The form's code is here:
using System;
using System.Windows.Forms;
 
namespace SelectFromOtherDwg
{
    public partial class dlgGetData : Form
    {
        private bool _closeForPick = false;
 
        public dlgGetData()
        {
            InitializeComponent();
 
            btnOK.Enabled = false;
        }
 
        public bool CloseForPick
        {
            get { return _closeForPick; }
        }
 
        public double Radius
        {
            get { return double.Parse(txtR.Text); }
        }
 
        public double CenterX
        {
            get { return double.Parse(txtX.Text); }
        }
 
        public double CenterY
        {
            get { return double.Parse(txtY.Text); }
        }
 
        public double CenterZ
        {
            get { return double.Parse(txtZ.Text); }
        }
 
        public void SetPickedData(double x, double y, double z, double r)
        {
            txtX.Text = x.ToString();
            txtY.Text = y.ToString();
            txtZ.Text = z.ToString();
            txtR.Text = r.ToString();
 
            btnOK.Enabled = true;
        }
 
        private void btnGet_Click(object sender, EventArgs e)
        {
            _closeForPick = true;
            this.DialogResult = DialogResult.OK;
        }
 
        private void btnOK_Click(object sender, EventArgs e)
        {
            _closeForPick = false;
            this.DialogResult = DialogResult.OK;
        }
 
        private void dlgGetData_Load(object sender, EventArgs e)
        {
 
        }
    }
}

Now, as usual, go to this video clip to see the code in action.

One may notice that at beginning of each command, I check if current drawing is new, untitled drawing or not. That is because if the command is executed with an untitled drawing (usually it is automatically created by AutoCAD, when the system variable "StartUp" is set 0), when AutoCAD opens an existing drawing (in this case, the source drawing), AutoCAD would close the untitled drawing silently (if not change is made to it). Then after user selects in the source drawing and AutoCAD closes the source drawing, there would be no drawing gin AutoCAD left open to continue with the command.

As the code and the video shows, this approach is rather simple. However, if the source drawing is large in size, opening the drawing takes time. Especially, if the process required user to do the selection in the source drawing multiple time, the repeated slow opening/closing would be quite annoying. In next post, I'll discuss the second approach I used. Stay tuned.

Friday, March 3, 2017

Handling Document.UnknownCommand Event, 2 of 2

In previous article I showed how to write a simple on demand application loader by handling Document.UnknownCommand event. We have seen that AutoCAD acts differently from other Document/Document event when it is handling: after running the code in the event handler, AutoCAD try to execute the previously unknown command again. That is, the first time AutoCAD executes an unknown command, Document.UnknownCommand event fires, which allows a chance for custom application to do something - here is how a custom on demand loading process could be plugged in; then AutoCAD tries to execute the same command again; if the command is still not defined, then AutoCAD spills "Unknown command" message at command line; only this time, no UnknownCommand event is fired.

During my exploration of handling UnknownCommand event, I came across another interesting behaviour of this event, which is quite similar to the notorious behaviour of IExtensionApplication.Initialize(): the code in the event handler must make sure any possible exception being handled. If an exception occurs inside the event handler, instead of breaking AutoCAD process, AutoCAD simply silently swallow the exception and keeps going, but the event handler will stop working, as if it is removed.

Here is the code to show this behaviour:
using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
 
[assemblyCommandClass(typeof(UnknownCommandException.MyCommands))]
 
namespace UnknownCommandException
{
    public class MyCommands 
    {
        private bool _handled = false;
 
        [CommandMethod("HandleUkCm")]
        public void HandleUnknownCommandEvent()
        {
            var doc = CadApp.DocumentManager.MdiActiveDocument;
            var ed = doc.Editor;
 
            if (!_handled)
            {
                doc.UnknownCommand += Doc_UnknownCommand;
                _handled = true;
                ed.WriteMessage("\nUnknownCommand event handler has been added.");
            }
            else
            {
                doc.UnknownCommand -= Doc_UnknownCommand;
                _handled = false;
                ed.WriteMessage("\nUnknownCommand event handler has been removed.");
            }
        }
 
        private void Doc_UnknownCommand(object sender, UnknownCommandEventArgs e)
        {
            var msg = "This is custom UnknowCommand event handler." +
                "\n\nClick \"Yes\" to suppress ths event handler" +
                "\nClick \"No\" to keep it" +
                "\n\n Do you want to suppress this event handler?";
            var res = System.Windows.Forms.MessageBox.Show(
                msg, "Custom UnknownCommand Handler",
                System.Windows.Forms.MessageBoxButtons.YesNo,
                System.Windows.Forms.MessageBoxIcon.Question,
                System.Windows.Forms.MessageBoxDefaultButton.Button2);
            if (res== System.Windows.Forms.DialogResult.Yes)
            {
                throw new ApplicationException(
                    "Stop custom UnknownCommand event handler.");
            }
        }
    }
}

As the code logic in the event handler shows, as long as the user does not click "Yes" button in the message box, the event handler gets execution each time an unknown command is entered. Once user clicks "Yes", an exception is raised and left not handled, AutoCAD continues in spite of the exception not being handled. However, from that point on, the UnknownCommand event handler is no longer executed when unknown command is entered. This video clip shows this behaviour.

As we see, unless the code in custom UnknownCommand event handler is very simple and no chance to raise any exception, we'd better always wrap up the code in UnknownCommand event handler in try...catch... block, just as we do in IExtensionApplication.Initialize().



Followers

About Me

My photo
After graduating from university, I worked as civil engineer for more than 10 years. It was AutoCAD use that led me to the path of computer programming. Although I now do more generic business software development, such as enterprise system, timesheet, billing, web services..., AutoCAD related programming is always interesting me and I still get AutoCAD programming tasks assigned to me from time to time. So, AutoCAD goes, I go.