/// Example use of OVERKILL command
/// (AutoCAD 2012 or later only)
/// from managed code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
namespace Namespace1
{
public static class OverkillExample
{
[CommandMethod( "MYOVERKILL" )]
public static void MyOverkill()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
// Note the leading "-" on the OVERKILL command, which
// forces it to use the command line:
doc.Editor.Command( "._-OVERKILL", "_ALL", "", "" );
}
}
}
/// EditorUtils.cs - Non-reflection-based access to the
/// Editor's non-public RunCommand() method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Reflection;
using Autodesk.AutoCAD.ApplicationServices;
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 = GenerateRunCommand();
static Func<Editor, object[], PromptStatus> GenerateRunCommand()
{
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" ); return Expression.Lambda<Func<Editor, object[], PromptStatus>>(
Expression.Call( instance, method, args ), instance, args )
.Compile();
}
}
}