Synchronize call commands in AutoCAD versions before 2015

Synchronize call commands in AutoCAD versions before 2015

dali0312
Contributor Contributor
1,083 Views
10 Replies
Message 1 of 11

Synchronize call commands in AutoCAD versions before 2015

dali0312
Contributor
Contributor
I can use the Command method of Editor to synchronously call commands in AutoCAD 2015 and above versions. However, there is no replaceable method for use in versions below 2015. May I ask if anyone has a good solution? Thanks.
0 Likes
1,084 Views
10 Replies
Replies (10)
Message 2 of 11

_gile
Consultant
Consultant

Hi,

There was a wrapper for the non-public RunCommand() method written by Tony Tanzillo ( @ActivistInvestor or @DiningPhilosopher ) which worked the same as A2015+ Editor.Command, unfortunately this thread has been archived deleted.

I refound an older version of this wrapper at TheSwamp, and the "refection free" version, always at TheSwamp.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 11

ActivistInvestor
Mentor
Mentor

As @_gile has already mentioned, the code I posted here a little over 10 years ago, that solves your problem, has been deleted by Autodesk.

 

Because I harbor no contempt for users of old/outdated releases of AutoCAD, Here it is again:

 

using System.Reflection;
using System;
using System.Linq.Expressions;

namespace Autodesk.AutoCAD.EditorInput
{
   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 = Generate;

      static PromptStatus Generate(this Editor editor, params object[] arguments)
      {
         MethodInfo method = typeof(Editor).GetMethod("RunCommand",
            BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
         var instance = Expression.Parameter(typeof(Editor), "instance");
         var args = Expression.Parameter(typeof(object[]), "args");
         runCommand = Expression.Lambda<Func<Editor, object[], PromptStatus>>(
            Expression.Call(instance, method, args), instance, args)
               .Compile();
         return runCommand(editor, arguments);
      }
   }
}

 

 

Message 4 of 11

MrLi289
Explorer
Explorer

If DocumentCollection.IsApplicationContext == true, You can't use the command line or the Command() wrapper for the RunCommand method.What should I do.Is the execution order different from Command versions 15 and above

0 Likes
Message 5 of 11

ActivistInvestor
Mentor
Mentor

@MrLi289 wrote:

If DocumentCollection.IsApplicationContext == true, You can't use the command line or the Command() wrapper for the RunCommand method.What should I do.Is the execution order different from Command versions 15 and above


 

In AutoCAD 2016 or later, the DocumentCollection's ExecuteInCommandContextAsync() method can be used to solve that problem.

 

ExecuteInCommandContextAsync() is Autodesk's implementation of my original solution to that problem, that works in any AutoCAD release going back to AutoCAD 2006 or 2007 (although I never tested it with releases that old, as I originally wrote it in the AutoCAD 2012 time-frame).

 

That solution is discussed in this post, which also requires this file.

0 Likes
Message 6 of 11

MrLi289
Explorer
Explorer

15 以下版本如何使用同步命令发送

0 Likes
Message 7 of 11

MrLi289
Explorer
Explorer

I tried this method but it didn't work and the command sent was not executed

0 Likes
Message 8 of 11

ActivistInvestor
Mentor
Mentor

自己翻译成英文

0 Likes
Message 9 of 11

MrLi289
Explorer
Explorer

How to use synchronous command to send in versions below 15

0 Likes
Message 10 of 11

ActivistInvestor
Mentor
Mentor

@MrLi289 wrote:

I tried this method but it didn't work and the command sent was not executed


The code @_gile linked to, and the code that I posted in this thread all work, so if your attempt to use that code failed, it is an error on your part. 

 

If the code doesn't work for you, post your code that attempts to use it, otherwise we're not interested in nothing more than "didn't work".

Message 11 of 11

ActivistInvestor
Mentor
Mentor

@MrLi289 wrote:

How to use synchronous command to send in versions below 15


See the replies above, with code and links to more code.  The code in the above post adds the Command() method to the Editor in versions < 2015.  If you add that code to your project, you can use the Command() method in versions older than 2015 in the same way you would use the built-in Command() method in later versions.

 

I can't make it any simpler than that.

0 Likes