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.

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.