How to execute command from command that has session flag

How to execute command from command that has session flag

Anonymous
Not applicable
5,855 Views
6 Replies
Message 1 of 7

How to execute command from command that has session flag

Anonymous
Not applicable

I have a command that is decorated with session flag - because it needs to have it. From within that command, I want to execute an AutoCAD command - specifically LAYOUT command. I have tried following ways:

 

ed.Command("-LAYOUT", "S", "REPORT" });

ed.Command(new object[] { "-LAYOUT", "S", "REPORT" });

 

I have also tried the CommandAsync as well. I have also tried calling them from inside of a transaction as well. I have also tried Document.SendStringToExecute().

 

But they all result in eInvalidInput exception.

 

I have several such commands to execute in synchronized order. How can I call AutoCAD commands? I am using AutoCAD 2017, C#, .NET 4.5.2.

 

I have googled and found many thread but none has helped me specifically with this problem. Any help is much appreciated.

0 Likes
Accepted solutions (1)
5,856 Views
6 Replies
Replies (6)
Message 2 of 7

_gile
Consultant
Consultant

Hi,

 

Editor.Command() only works in document context, not in application context.

 

So, with the CommandFlags.Session in the parent method, you cannot use Editor.Command() (idem for CommandAsync()).

 

instead of:

ed.Command("-LAYOUT", "S", "REPORT" });

you can simply do:

Application.SetSystemVariable("CVPORT", "REPORT");


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 7

Anonymous
Not applicable

Thanks Giles. But I have  many other commands to execute and I don't suppose they all can be somehow be achieved by your suggestion.

 

Can I implement another  command that doesn't use sessions flag, and call that command from my command that uses sessions flag?

0 Likes
Message 4 of 7

_gile
Consultant
Consultant
Accepted solution

Yes, you can call a custom command from the command with session flag using SendStringToExecute() which autoamically switch AutoCAD into docuement context..

 

Here's a trivial example:

 

        [CommandMethod("SESSIONCMD", CommandFlags.Session)]
        public void TestCmd()
        {
            foreach (Document doc in Application.DocumentManager)
            {
                doc.SendStringToExecute("DOCCMD ", false, false, false);
            }
        }

        [CommandMethod("DOCCMD")]
        public void DocumentCmd()
        {
            var ed = AcAp.DocumentManager.MdiActiveDocument.Editor;
            ed.Command("_circle", new Point3d(10.0, 20.0, 0.0), 10.0);
            ed.Command("_zoom", "_extents");
            ed.Command("_zoom", "0.8xp");
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 5 of 7

ActivistInvestor
Mentor
Mentor
To add to @_gile's advice, you should also add the CommandFlags.NoHistory flag to your document command, to prevent it from repeating if the user presses Enter afterwards.

If you need to do other things after all calls to Command() have completed, you should do them from the document command, as the session command will not wait for the call to SendStringToExecute() to return.
Message 6 of 7

hennadii_k
Participant
Participant

So, how the SESSIONCMD command can be invoked from AutoCAD? When no document is opened, there is no command line present where this command can be entered.

0 Likes
Message 7 of 7

ActivistInvestor
Mentor
Mentor

Commands are issued via the command line, which is not available when there are no open documents.

If you need to execute some process or operation when no documents are open, you will need to provide your own modeless UI, such as a ribbon element; a pallete, or modeless dialog/window.