AutoCAD二次开发(.Net)之读写LSP变量

        //LSP变量的写入
        [System.Security.SuppressUnmanagedCodeSecurity]
        [DllImport("accore.dll", EntryPoint = "acedPutSym",CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
        extern static private int acedPutSym(string args, IntPtr result);
        //32位的CAD的动态链接库和64位的有分别,下面是32位的方法
        [System.Security.SuppressUnmanagedCodeSecurity]
        [DllImport("accore.dll", EntryPoint = "_acedPutSym",CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
        extern static private int acedPutSym32(string args, IntPtr result);
        /// 
        /// Set a LISP variable value.
        /// 
        /// The variable name.
        /// The variable value
        public static void SetLispSym(string name, ResultBuffer rb)
        {
            if (Environment.Is64BitOperatingSystem)//判断系统是32位还是64位
            {
                acedPutSym(name, rb.UnmanagedObject);
            }
            else
            {
                acedPutSym32(name, rb.UnmanagedObject);
            }
        }
        //LSP变量的读取
        [System.Security.SuppressUnmanagedCodeSecurity]
        [DllImport("accore.dll", EntryPoint = "acedGetSym",CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
        extern static private int acedGetSym(string args, out IntPtr result);

        [System.Security.SuppressUnmanagedCodeSecurity]
        [DllImport("accore.dll", EntryPoint = "_acedGetSym",CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
        extern static private int acedGetSym32(string args, out IntPtr result);
        /// 
        /// Get a LISP variable value.
        /// 
        /// The variable name.
        /// The variable value or null if failed.
        public static ResultBuffer GetLispSym(string name)
        {
            IntPtr ip = IntPtr.Zero;
            if (Environment.Is64BitOperatingSystem)
            {
                int status = acedGetSym(name, out ip);
                if (status == (int)PromptStatus.OK && ip != IntPtr.Zero)
                {
                    return ResultBuffer.Create(ip, true);
                }
            }
            else
            {
                int status = acedGetSym32(name, out ip);
                if (status == (int)PromptStatus.OK && ip != IntPtr.Zero)
                {
                    return ResultBuffer.Create(ip, true);
                }
            }
            return null;
        }
        /// 
        /// LSP变量赋值
        /// 
        /// 
        private static void SetLSPVar(string varName, string volLevel)
        {
            Autodesk.AutoCAD.DatabaseServices.ResultBuffer rb = new Autodesk.AutoCAD.DatabaseServices.ResultBuffer();
            rb.Add(new Autodesk.AutoCAD.DatabaseServices.TypedValue(5005, volLevel));
            CADHelperClass.SetLispSym(varName, rb);
        } 
        //读取LSP变量 
        private static string ReadLSPVar(string varName)
        {
            string name = "";
            ResultBuffer rb = GetLispSym(varName);
            foreach (TypedValue val in (System.Collections.IEnumerable)rb)
            {
                name = val.Value.ToString();
            }
            return name;
        }

 

 

 

你可能感兴趣的:(C#,AutoCAD)