.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Running Commands project wide synchronously

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
mcoH3VZV
1431 Views, 7 Replies

Running Commands project wide synchronously

Hi,

 

I am attempting to run various commands synchronously over a group of opened drawings. The attached code is functional if only one drawing is open. It fails when more than one drawing is open, and if I add CommandFlags.Session to the Command Method Line, then ed.Command no longer works. 

 

What is the best way of performing multiple commands project wide? Do I have to have all drawings closed and open one at a time, perform updates, and then save/close?

 

Thanks in advance,

 

 

7 REPLIES 7
Message 2 of 8
Keith.Brown
in reply to: mcoH3VZV

I did not analyze your code in great detail but the big blaring mistake that I see is that you set the editor immediately with the current active document.  When you switch documents you change the active document but you never change the editor.  You should grab the editor from the active document everytime that you switch documents, otherwise all you are doing is performing the command in the same document over and over.  Also don't forget to check for null on your document before doing anything else.  If the start page is up then it is considered a null document.

Message 3 of 8
Keith.Brown
in reply to: mcoH3VZV

If all you are doing is running commands on drawings, then you might want to take a look at ScriptPro.  It will run a script on a set of drawings for you.  It comes with source code so you can modify it also.

 

https://knowledge.autodesk.com/support/autocad/downloads/caas/downloads/content/autodesk-customizati...

Message 4 of 8
mcoH3VZV
in reply to: Keith.Brown

I do change the editor with the Line (ed = d.Editor) as I cycle through the open documents (drawings).

 

What I do is, for each document that is open, set the active document, and then set the editor, and then I run the command. This works and I have tested it with a Editor.WriteMessage() line of code.

 

ed.command() just won't fire for each drawing. It only works on one and then stops. I need a way to cycle through all drawings in some kind of list and perform a command on all of them.

Message 5 of 8
mcoH3VZV
in reply to: mcoH3VZV

Once I get this working, there is plenty more I wish to do. I do not simply want to run a couple of commands and then stop.

Message 6 of 8
_gile
in reply to: mcoH3VZV

Hi,

 

If you're using AutoCAD 2016 or upper, you can try this:

        [CommandMethod("ZoomOnAll", CommandFlags.Session)]
        public async static void ZoomOnAll()
        {
            var docs = Application.DocumentManager;
            foreach (Document doc in docs)
            {
                docs.MdiActiveDocument = doc;
                await docs.ExecuteInCommandContextAsync(
                    async (obj) =>
                    {
                        var ed = doc.Editor;
                        await ed.CommandAsync("_.ZOOM", "_All");
                    },
                    null);
            }
        }

 

Editor.Command() only works in document context, the CommandFlags.Session forces the application context.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 8
Keith.Brown
in reply to: mcoH3VZV

My mistake, i missed that in the image.

 

Anyway, as Gile pointed out, you need to have the commandflag.sessions set in order to work in separate documents.

Message 8 of 8
mcoH3VZV
in reply to: _gile

Works perfectly.

 

I have tested and can perform other things after it as well.

 

Thanks for the help.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost