.net 要同步执行的AutoCad NET发送字符串

.net 要同步执行的AutoCad NET发送字符串,.net,vb.net,autocad,.net,Vb.net,Autocad,在我的AutoCad插件中,我一直在使用Document.SendStringToExecute函数执行Lisp命令,手动将命令连接到字符串中。但是,我现在希望将我的插件应用于多个图形,这在使用异步命令时会变得很尴尬 现在我想使这些命令同步。它们都调用子LispCommand(),以便以后我可以更改该方法 Sub LispCommand(cmd as String) doc.SendStringToExecute(cmd, False, False, True) End Sub 我已尝试p/调

在我的AutoCad插件中,我一直在使用Document.SendStringToExecute函数执行Lisp命令,手动将命令连接到字符串中。但是,我现在希望将我的插件应用于多个图形,这在使用异步命令时会变得很尴尬

现在我想使这些命令同步。它们都调用子LispCommand(),以便以后我可以更改该方法

Sub LispCommand(cmd as String)
doc.SendStringToExecute(cmd, False, False, True)
End Sub
我已尝试p/调用acedCmd,但您似乎只能向其传递参数数组,我希望(如果可能)能够使用现有的完整字符串,例如“(c:ace_new_wiretype“RED”(列表)”)


是否有任何方法可以将连续字符串命令同步发送到AutoCad?

您可以尝试
应用程序。调用

 var rb = new ResultBuffer(new TypedValue[] {
   new TypedValue((int) LispDataType.Text, "c:ace_new_wiretype"),
   new TypedValue((int) LispDataType.Text, "RED"),  
   new TypedValue((int) LispDataType.ListBegin, null), 
   new TypedValue((int) LispDataType.ListEnd, null), 
   new TypedValue((int) LispDataType.Text, "")
 });
 Application.Invoke(rb);

(未测试)

您可以尝试
应用程序。调用

 var rb = new ResultBuffer(new TypedValue[] {
   new TypedValue((int) LispDataType.Text, "c:ace_new_wiretype"),
   new TypedValue((int) LispDataType.Text, "RED"),  
   new TypedValue((int) LispDataType.ListBegin, null), 
   new TypedValue((int) LispDataType.ListEnd, null), 
   new TypedValue((int) LispDataType.Text, "")
 });
 Application.Invoke(rb);

(未测试)

试试这个。我在AutoCAD 2012和2013上使用它。请尝试命令方法和ExecuteSingoverInvoke方法。如果愿意,请卸下记录器

using System;
using System.Security;
using System.Runtime.InteropServices;
using System.Collections.Generic;

using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
using Castle.Core.Logging;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;


namespace YourCAD.Utilities
{
    using System.Reflection;

    using Autodesk.AutoCAD.ApplicationServices;
    using Autodesk.AutoCAD.EditorInput;

    [SuppressUnmanagedCodeSecurity]
    public static class CommandLineHelper
    {
        private const string ACAD_EXE = "acad.exe";

        private const short RTSTR = 5005;

        private const short RTNORM = 5100;

        private const short RTNONE = 5000;

        private const short RTREAL = 5001;

        private const short RT3DPOINT = 5009;

        private const short RTLONG = 5010;

        private const short RTSHORT = 5003;

        private const short RTENAME = 5006;

        private const short RTPOINT = 5002; /*2D point X and Y only */

        private static Dictionary<Type, short> resTypes = new Dictionary<Type, short>();

        private static ILogger _logger = NullLogger.Instance;

        public static ILogger Logger
        {
            get
            {
                return _logger;
            }
            set
            {
                _logger = value;
            }
        }

        static CommandLineHelper()
        {
            resTypes[typeof(string)] = RTSTR;
            resTypes[typeof(double)] = RTREAL;
            resTypes[typeof(Point3d)] = RT3DPOINT;
            resTypes[typeof(ObjectId)] = RTENAME;
            resTypes[typeof(Int32)] = RTLONG;
            resTypes[typeof(Int16)] = RTSHORT;
            resTypes[typeof(Point2d)] = RTPOINT;
        }

        private static TypedValue TypedValueFromObject(Object val)
        {
            if (val == null) throw new ArgumentException("null not permitted as command argument");
            short code = -1;

            if (resTypes.TryGetValue(val.GetType(), out code) && code > 0)
            {
                return new TypedValue(code, val);
            }
            throw new InvalidOperationException("Unsupported type in Command() method");
        }

        public static int Command(params object[] args)
        {
            if (AcadApp.DocumentManager.IsApplicationContext) throw new InvalidCastException("Invalid execution context");
            int stat = 0;
            int cnt = 0;
            using (ResultBuffer buffer = new ResultBuffer())
            {
                foreach (object o in args)
                {
                    buffer.Add(TypedValueFromObject(o));
                    ++cnt;
                }
                if (cnt > 0)
                {
#if acad2012
                    stat = acedCmd2012(buffer.UnmanagedObject);
#endif
#if acad2013
                    stat = acedCmd2013( buffer.UnmanagedObject );
#endif

                }
            }
            return stat;
        }


        //[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, EntryPoint = "?acedSetCurrentVPort@@YA?AW4ErrorStatus@Acad@@PBVAcDbViewport@@@Z")]
        //extern static private int acedCmd2013(IntPtr acDbVport);

        //[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, EntryPoint = "?acedSetCurrentVPort@@YA?AW4ErrorStatus@Acad@@PBVAcDbViewport@@@Z")]
        //extern static private int acedCmd2008(IntPtr acDbVport);

        //[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, EntryPoint = "?acedSetCurrentVPort@@YA?AW4ErrorStatus@Acad@@PEBVAcDbViewport@@@Z")]
        //extern static int acedCmd2011(IntPtr resbuf);

        [System.Security.SuppressUnmanagedCodeSecurity]
        [DllImport("accore.dll", EntryPoint = "acedCmd", CallingConvention = CallingConvention.Cdecl,
            CharSet = CharSet.Auto)]
        private static extern int acedCmd2013(IntPtr resbuf);

        //[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl, EntryPoint = "acedCmd")]
        [System.Security.SuppressUnmanagedCodeSecurity]
        [DllImport("acad.exe", EntryPoint = "acedCmd", CallingConvention = CallingConvention.Cdecl,
            CharSet = CharSet.Auto)]
        private static extern int acedCmd2012(IntPtr resbuf);

        public static void ExecuteStringOverInvoke(string command)
        {
            try
            {
#if acad2012
                object activeDocument =
                    Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.AcadDocument;
#endif

#if acad2013
                object activeDocument = Autodesk.AutoCAD.ApplicationServices.DocumentExtension.GetAcadDocument(
                    Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument);
#endif
                object[] data = { command };
                activeDocument.GetType()
                              .InvokeMember(
                                  "SendCommand", System.Reflection.BindingFlags.InvokeMethod, null, activeDocument, data);
            }
            catch (Autodesk.AutoCAD.Runtime.Exception exception)
            {
                Logger.Error("Command line class error.", exception);
            }
        }

        static MethodInfo runCommand = typeof(Editor).GetMethod(
            "RunCommand", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

        public static PromptStatus Command(this Editor ed, params object[] args)
        {
            if (Application.DocumentManager.IsApplicationContext)
            {
                throw new InvalidOperationException("Invalid execution context for Command()");
            }
            if (ed.Document != Application.DocumentManager.MdiActiveDocument)
            {
                throw new InvalidOperationException("Document is not active");
            }
            return (PromptStatus)runCommand.Invoke(ed, new object[] { args });
        }
    }

    public class CommandLineShortCuts
    {
        // Sample member functions that use the Command() method.

        public static int ZoomExtents()
        {
            return CommandLineHelper.Command("._ZOOM", "_E");
        }

        public static int ZoomCenter(Point3d center, double height)
        {
            return CommandLineHelper.Command("._ZOOM", "_C", center, height);
        }

        public static int ZoomWindow(Point3d corner1, Point3d corner2)
        {
            return CommandLineHelper.Command("._ZOOM", "_W", corner1, corner2);
        }

        public static int SetFilletRadius(double filletRadius)
        {
            return CommandLineHelper.Command("._FILLET", "_R", filletRadius);
        }

        public static int FilletPolyline(ObjectId polylineId)
        {
            return CommandLineHelper.Command("._FILLET", "_P", polylineId);
        }
    }

}
使用系统;
使用系统安全;
使用System.Runtime.InteropServices;
使用System.Collections.Generic;
使用Autodesk.AutoCAD.Geometry;
使用Autodesk.AutoCAD.DatabaseServices;
使用Castle.Core.Logging;
使用AcadApp=Autodesk.AutoCAD.ApplicationServices.Application;
名称空间YourCAD.Utilities
{
运用系统反思;
使用Autodesk.AutoCAD.ApplicationServices;
使用Autodesk.AutoCAD.EditorInput;
[SuppressUnmanagedCodeSecurity]
公共静态类CommandLineHelper
{
私有常量字符串ACAD_EXE=“ACAD.EXE”;
私有常量短RTSTR=5005;
私有常量短RTNORM=5100;
专用const short RTNONE=5000;
私有常量短RTREAL=5001;
专用常量短RT3DPOINT=5009;
private const short RTLONG=5010;
专用常量短RTSHORT=5003;
private const short RTENAME=5006;
专用常量短RTPOINT=5002;/*仅限2D点X和Y*/
private static Dictionary resTypes=new Dictionary();
私有静态ILogger _logger=NullLogger.Instance;
公共静态ILogger记录器
{
得到
{
返回记录器;
}
设置
{
_记录器=值;
}
}
静态命令行助手()
{
resTypes[typeof(string)]=RTSTR;
重新类型[typeof(double)]=RTREAL;
重新类型[typeof(Point3d)]=RT3DPOINT;
重新类型[typeof(ObjectId)]=RTENAME;
resTypes[typeof(Int32)]=RTLONG;
resTypes[typeof(Int16)]=RTSHORT;
重新类型[typeof(Point2d)]=RTPOINT;
}
私有静态类型值类型值fromObject(Object val)
{
如果(val==null)抛出新的ArgumentException(“不允许null作为命令参数”);
短代码=-1;
if(resTypes.TryGetValue(val.GetType(),out代码)&&code>0)
{
返回新的TypedValue(代码,val);
}
抛出新的InvalidOperationException(“Command()方法中不支持的类型”);
}
公共静态int命令(params对象[]args)
{
如果(AcadApp.DocumentManager.IsApplicationContext)抛出新的InvalidCastException(“无效执行上下文”);
int stat=0;
int-cnt=0;
使用(ResultBuffer buffer=new ResultBuffer())
{
foreach(args中的对象o)
{
Add(TypedValueFromObject(o));
++碳纳米管;
}
如果(cnt>0)
{
#如果acad2012
stat=acedCmd2012(buffer.UnmanagedObject);
#恩迪夫
#如果acad2013
stat=acedCmd2013(buffer.UnmanagedObject);
#恩迪夫
}
}
返回统计;
}
//[DllImport(“acad.exe”,CallingConvention=CallingConvention.Cdecl,EntryPoint=“?acedSetCurrentVPort@@YA?”?AW4ErrorStatus@Acad@@PBVAcDbViewport@@@Z”)]
//外部静态专用int acedCmd2013(IntPtr acDbVport);
//[DllImport(“acad.exe”,CallingConvention=CallingConvention.Cdecl,EntryPoint=“?acedSetCurrentVPort@@YA?”?AW4ErrorStatus@Acad@@PBVAcDbViewport@@@Z”)]
//外部静态专用int acedCmd2008(IntPtr acDbVport);
//[DllImport(“acad.exe”,CallingConvention=CallingConvention.Cdecl,EntryPoint=“?acedSetCurrentVPort@@YA?”?AW4ErrorStatus@Acad@@PEBVAcDbViewport@@@Z”)]
//外部静态int acedCmd2011(IntPtr resbuf);
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport(“accore.dll”,EntryPoint=“acedCmd”,CallingConvention=CallingConvention.Cdecl,
CharSet=CharSet.Auto)]
私有静态外部int acedCmd2013(IntPtr resbuf);
//[DllImport(“acad.exe”,CallingConvention=CallingConvention.Cdecl,EntryPoint=“acedCmd”)]
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport(“acad.exe”,EntryPoint=“acedCmd”,CallingConvention=CallingConvention.Cdecl,
CharSet=CharSet.Auto)]
私有静态外部int acedCmd2012(IntPtr resbuf);
公共静态void executestringverinvoke(字符串命令)
{
尝试
{
#如果acad2012
对象动态文档=
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.AcadDocument;
#恩迪夫
#如果acad2013
对象activeDocument=Autodesk.AutoCAD.ApplicationServices.DocumentExtension.GetAcadDocument(
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument);
#恩迪夫
对象[]数据={command};
activeDocument.GetType()
.InvokeMember(
“SendCommand”,System.Reflection.BindingFlag