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.

3 comments:

iron said...

THANKS FOR SHARING SUCH A AMAZING CONTENT
GREAT PIECE OF WORK!!!
REALLY APPRECIATE YOUR WORK!!!
CAD to BIM conversion in USA

Miroslav said...

Wow!! So nice thanks alot!

Jeffrey said...

Gratefull for sharing this

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.