Question re: Send Command Executes after exiting command method

Question re: Send Command Executes after exiting command method

Soojung_Kim
Enthusiast Enthusiast
353 Views
2 Replies
Message 1 of 3

Question re: Send Command Executes after exiting command method

Soojung_Kim
Enthusiast
Enthusiast

Hi!

 

I'm working on the Civil3D add-in, and I want to send a command to select elements and then run the next line of codes using the selected objects. 

In this previous thread, I got an answer on how to save the selected objects into a variable. https://forums.autodesk.com/t5/net/how-to-receive-selected-object-data-from-autocad-with-mouse/m-p/1...

 

But I'm having a hard time running the next line of code because the SendStringToCommand is asycnrhonous.

Then I ran into this thread, which was recommended in several other threads. 

https://forums.autodesk.com/t5/net/send-command-executes-after-exiting-command-method/td-p/3882929

 

But when I use the wrapper, I get 'null' value for the MethodInfo method and that throws an error (message: {"Value cannot be null.\r\nParameter name: method"}.
Does anyone know why this is giving me null value? Is it because I'm using C3D? Or versioning issue?

 

Or is there any other way to run the GETOBJECT command and run next line of code in this example?

 

 

namespace exampleTool
{

    public static class EditorInputExtensionMethods
    {
        public static PromptStatus Command_(this Editor editor, params object[] args)
        {
            if (editor == null)
                throw new ArgumentNullException("editor");
            return runCommand(editor, args);
        }

        static Func<Editor, object[], PromptStatus> runCommand = GenerateRunCommand();

        static Func<Editor, object[], PromptStatus> GenerateRunCommand()
        {
            MethodInfo method = typeof(Editor).GetMethod("RunCommand",
               BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            var instance = System.Linq.Expressions.Expression.Parameter(typeof(Editor), "instance");
            var args = Expression.Parameter(typeof(object[]), "args");
            return Expression.Lambda<Func<Editor, object[], PromptStatus>>(
               Expression.Call(instance, method, args), instance, args)
                  .Compile();
        }
    }


     public partial class NewMainWindow : System.Windows.Window
    {
        private void Get_Object(object sender, RoutedEventArgs e)
        {
            _document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor editor = _document.Editor;
            editor.Command_("._GETOBJECT");

            // next code runs..
        }
    

        ObjectId[] _currentSelection;


        [CommandMethod("GETOBJECT")]
        public void GetObject()
        {
           Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
           DocumentLock dl = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
           var ed = doc.Editor;
            

           // Request for objects to be selected in the drawing area
           PromptSelectionOptions pso = new PromptSelectionOptions();
           pso.MessageForAdding = "Select Pipes";
           PromptSelectionResult acSSPrompt = ed.GetSelection(pso);


           _currentSelection = null;
           if (acSSPrompt.Status != PromptStatus.OK)
               return;

           _currentSelection = acSSPrompt.Value.GetObjectIds();
            
        } 
    } 
}

 

Thanks!

0 Likes
354 Views
2 Replies
Replies (2)
Message 2 of 3

_gile
Consultant
Consultant

Hi,

 

Since AutoCAD 2015, you have to use the built-in Editor.Command method instead of the extension method but you can only call it from the Document context (i.e. a modal dialog).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 3

Soojung_Kim
Enthusiast
Enthusiast

Hi Gilles,
I'm running the command from windows form. So does that mean I can't run the command in the Document context? I've read that I need to use SendStringToCommand if I'm in the Application Context, but then I can't run the following code after that, as I mentioned in the post. I'm still not familiar with Document context vs Application context, so please correct me if I'm not understanding correctly here!

I've read below information. Do you think this is the way I need to go with?

"You can also register a command (without CommandFlags.Session) that runs in the document context, and makes calls to Editor.Command() to get the work done, and then simply invoke that registered command via SendStringToExecute() from your button click handler in the application context. That makes the solution more complicated but also more flexible, because the registered command handler can do other things besides execute an AutoCAD command. For example, If you must do something else after you run an AutoCAD command to completion, then you almost have no choice but to use a registered command, since anything that follows a call to SendStringToExecute() cannot rely on side-effects of the command(s) that are run."

0 Likes