Author Topic: Switching "control" to another document  (Read 1747 times)

0 Members and 1 Guest are viewing this topic.

autogis

  • Guest
Switching "control" to another document
« on: May 16, 2014, 08:44:40 AM »
Another newbie question...

I am trying to switch "control" to an EXISTING document that I will open in read-only mode.  Here are the steps:

1.  I do a netload of the class library containing the code I am going to execute.
2.  From autocad, in a NEW drawing, I type in TEST1 which executes below opening an EXISTING drawing:

[CommandMethod("TEST1")]
        public void test1()
        {
            string fileName = "C:\\room.dwg";  // EXISTING drawing I wish to manipulate
            openAndActivateDocument(fileName);
        }

        public void openAndActivateDocument(string filename)
        {

            Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
            DocumentCollection docs = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
            Document doc;

            // Open Dwg File
            try
            {
                // Open File
                doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open(filename, true);

            }
            catch (System.Exception ex)
            {
                ed.WriteMessage("\nError: {0}", ex.Message);
                return;
            }

            // Call next command to continue process in newly activated document
            doc.SendStringToExecute("StartExtraction" + Char13 + Char27, false, true, true);

            // Activate Document
            if (docs.MdiActiveDocument != doc)
            {
                docs.MdiActiveDocument = doc;
            }


        }


[CommandMethod("StartExtraction")]
        public void startExtraction()
        {
...
// WE NEVER GET TO THIS METHOD
...

3. At this point it switches to the new document.  I have another method called "StartExtraction" (as you can see I am calling it above from the new document).  But, it never hits the breakpoint in the code.

My question is how do I succesfully switch control to the new document.  It looks like the issues has something to do with the fact that I used netload from another document.  One of the requirements is that I do this from a new drawing as above and then switch control over to an existing drawing.

Thanks Before Hand.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Switching "control" to another document
« Reply #1 on: May 16, 2014, 09:46:43 AM »
Hi,

First, use a 'using' instruction at the top of your file to avoid repeating 'Autodesk.AutoCAD.ApplicationServices' everywhere in your code, it will be more readable.
You can also use an alias for the 'Autodesk.AutoCAD.ApplicationServices.Application' class.

If you want a command continue in another document, you have to use CommandFlags.Session in the CommandMethodAttribute arguments.

You have to make the newly opened document active before executing methods in it.

You don't need to call SendStringToExecute() as the the defined is a method you can call directly.

You may lock the newly opened document if you make changes in it.

Here's a little sample which mimics your code structure:

Code - C#: [Select]
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.Runtime;
  3. using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
  4.  
  5. namespace SwitchDocumentSample
  6. {
  7.     public class Commands
  8.     {
  9.         [CommandMethod("Test", CommandFlags.Session)]
  10.         public void Test()
  11.         {
  12.             string fileName = @"C:\room.dwg";
  13.             OpenAndActivateDocument(fileName);
  14.         }
  15.  
  16.         public void OpenAndActivateDocument(string fileName)
  17.         {
  18.             DocumentCollection docs = AcAp.DocumentManager;
  19.             Document doc;
  20.             try
  21.             {
  22.                 doc = docs.Open(fileName);
  23.             }
  24.             catch (System.Exception exn)
  25.             {
  26.                 AcAp.ShowAlertDialog(exn.Message);
  27.                 return;
  28.             }
  29.             docs.MdiActiveDocument = doc;
  30.             using (doc.LockDocument())
  31.             {
  32.                 // rather than: doc.SendStringToExecute("StartAction ", false, false, true);
  33.                 StartAction();
  34.             }
  35.         }
  36.  
  37.         [CommandMethod("StartAction")]
  38.         public void StartAction()
  39.         {
  40.             Document doc = AcAp.DocumentManager.MdiActiveDocument;
  41.             AcAp.ShowAlertDialog(string.Format("Active document:\n{0}", doc.Name));
  42.         }
  43.     }
  44. }
  45.  
« Last Edit: May 16, 2014, 10:33:53 AM by gile »
Speaking English as a French Frog

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Switching "control" to another document
« Reply #2 on: May 16, 2014, 11:48:12 AM »
Gile,  I am always amazed by how well you explain the issues and solutions and how easy you make it look. 
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Switching "control" to another document
« Reply #3 on: May 16, 2014, 12:22:03 PM »
Thanks Keith.
This may be due to the fact that English is not my native language which forces me to make an effort of writing.
Speaking English as a French Frog