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.

As 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.

3 comments: