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,
Solved! Go to Solution.
Solved by _gile. Go to Solution.
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.
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.
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.
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.
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.
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.