总结.NET CAD各种命令发送方式

.NET API提供了各种的调用命令的方式,有异步与同步的发送命令,本文章总结了各种命令发送的方法与方式。

在最后调用命令的方式

此方式会在当前整个命令结束后才会调用,并且支持文档的切换与锁文档

 /// 
 /// 命令结束后调用命令
 /// 
 /// 命令
 public static void SendComandFinally(string command)
 {
     Document acdDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
     acdDoc.SendStringToExecute(command, true, false, false);
 }

同步发送命令

此方法为调用Autodesk.AutoCAD.Interop.dll中的AcadApplication进行发送命令

/// 
/// 同步发送命令
/// 
/// 命令
public static void SynchronizeSendCommand(string command)
{
    AcadApplication app = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication as AcadApplication;

    app.ActiveDocument.SendCommand(command);
}

带参数的命令发送

此方法为带参数的命令发送,并且为在程序运行过程中发送,在CAD2015之前,.NET API并没有封装公开此方法,但是可以通过非托管代码调用,下面是在CAD不同版本中调用Command的方法以及在CAD2015版本以下的非托管代码加载调用方式

        /// 
        /// 带参数的发送命令(程序过程中发送)
        /// 
        /// 
        public static void ParameterCommand(params object[] args)
        {
#if CAD2012 || CAD2013 || CAD2014
            Command(args);
#else
            Application.DocumentManager.MdiActiveDocument.Editor.Command(args);
#endif
        }


#if CAD2012
        [DllImport("acad.exe", EntryPoint = "acedCmd", CallingConvention = CallingConvention.Cdecl,
            CharSet = CharSet.Auto)]
        private static extern int acedCmd(IntPtr resbuf);
#elif CAD2013 || CAD2014
        [DllImport("accore.dll", EntryPoint = "acedCmd", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        private extern static int acedCmd(IntPtr resbuf);
#else
        [DllImport("accore.dll", EntryPoint = "acedCmdS", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
        private extern static int acedCmd(IntPtr resbuf);
#endif

        /// 
        /// 低版本命令调用
        /// 
        /// 命令名称后跟命令输入
        public static void Command(params object[] args)
        {
            ResultBuffer resbuf = new ResultBuffer();
            foreach (object obj in args)
            {
                switch (obj.GetType().Name)
                {
                    case "String":
                        resbuf.Add(new TypedValue((int)LispDataType.Text, obj)); break;
                    case "Int16":
                        resbuf.Add(new TypedValue((int)LispDataType.Int16, obj)); break;
                    case "Int32":
                        resbuf.Add(new TypedValue((int)LispDataType.Int32, obj)); break;
                    case "Double":
                        resbuf.Add(new TypedValue((int)LispDataType.Double, obj)); break;
                    case "Point2d":
                        resbuf.Add(new TypedValue((int)LispDataType.Point2d, obj)); break;
                    case "Point3d":
                        resbuf.Add(new TypedValue((int)LispDataType.Point3d, obj)); break;
                    case "ObjectId":
                        resbuf.Add(new TypedValue((int)LispDataType.ObjectId, obj)); break;
                    case "ObjectId[]":
                        foreach (ObjectId id in (ObjectId[])obj)
                            resbuf.Add(new TypedValue((int)LispDataType.ObjectId, id));
                        break;
                    case "ObjectIdCollection":
                        foreach (ObjectId id in (ObjectIdCollection)obj)
                            resbuf.Add(new TypedValue((int)LispDataType.ObjectId, id));
                        break;
                    case "SelectionSetFullyMarshalled":
                        resbuf.Add(new TypedValue((int)LispDataType.SelectionSet, obj)); break;
                    default:
                        throw new InvalidOperationException("Unsupported type in Command() method");
                }
            }
            acedCmd(resbuf.UnmanagedObject);
        }

你可能感兴趣的:(.net,cad,CAD二次开发,.net,算法,c#)