龙空技术网

VS2017 C# CAD二次开发

Python甘泉GH 111

前言:

今天朋友们对“pythonautocad开发”可能比较珍视,各位老铁们都需要了解一些“pythonautocad开发”的相关内容。那么小编也在网摘上收集了一些对于“pythonautocad开发””的相关知识,希望咱们能喜欢,小伙伴们一起来学习一下吧!

现在学习VS2017 C# CAD2014,开始C# CAD二次开发第一步!!

(此处已添加书籍卡片,请到头条号平台查看)

编辑

添加图片注释,不超过 140 字(可选)

1.exp1 => Class1.cs 代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Autodesk.AutoCAD.ApplicationServices;using Autodesk.AutoCAD.DatabaseServices;using Autodesk.AutoCAD.EditorInput;using Autodesk.AutoCAD.Runtime;using Autodesk.AutoCAD.Colors;namespace exp1{    public class cSum    {        public void Sum()        {//System.Windows.Forms.messagebox.show("Hello World From VS C#(It's for Degbugging)","提示");            long s = 0;            for (int i = 1; i <= 101; i++)            {                s += i;            }            System.Windows.Forms.MessageBox.Show(s.ToString(), "提示");        }    }}

这个是使用cmd.json的文件的类,修改cmd.json文件的内容不必要重新编译类文件!

2.loadx => Class1.cs 代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Autodesk.AutoCAD.ApplicationServices;using Autodesk.AutoCAD.DatabaseServices;using Autodesk.AutoCAD.EditorInput;using Autodesk.AutoCAD.Runtime;using Autodesk.AutoCAD.Colors;using System.Reflection;using System.IO;using Newtonsoft.Json;using Newtonsoft.Json.Linq;namespace Loadx{    public class loadx    {        private Action cmd;        [CommandMethod("DD")]        public void Reloadx()        {            #region CmdSum            CmdInfo cmdInfo = new CmdInfo();            string jsonFile = @"E:\VS\exp1\cmd.json";            using (System.IO.StreamReader file = System.IO.File.OpenText(jsonFile))            {                using (JsonTextReader reader = new JsonTextReader(file))                {                    JObject o = (JObject)JToken.ReadFrom(reader);                    cmdInfo.DllName = o["DllName"].ToString();                    cmdInfo.ClassName = o["ClassName"].ToString();                    cmdInfo.MethodName = o["MethodName"].ToString();                }            }            string DllName = cmdInfo.DllName;            string ClassName = cmdInfo.ClassName;            string MethodName = cmdInfo.MethodName;                //string DllName = "exp1.dll";                //string ClassName = "exp1.cSum";                //string MethodName = "Sum";            #endregion            #region Don't                var adapterfileInfo = new FileInfo(Assembly.GetExecutingAssembly().Location);            var targetFilePath = Path.Combine(adapterfileInfo.DirectoryName, DllName);            var targetAssembly = Assembly.Load(File.ReadAllBytes(targetFilePath));            var targetType = targetAssembly.GetType(ClassName);            var targetMethod = targetType.GetMethod(MethodName);            var targetObject = Activator.CreateInstance(targetType);            cmd = () => targetMethod.Invoke(targetObject, null);            try            {                cmd?.Invoke();            }            catch (System.Exception ex)            {            System.Windows.Forms.MessageBox.Show(ex.Message, "Tips1");            };            #endregion        }    }    public class CmdInfo    {        public string DllName { get; set; }        public string ClassName { get; set; }        public string MethodName { get; set; }    }}

3.cmd.json 代码如下:

编辑

添加图片注释,不超过 140 字(可选)

{"DllName":"exp1.dll","ClassName":"exp1.cSum","MethodName":"Sum"}

这个类是不用cmd.json文件的类!

4.loadx => Class2.cs 代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Autodesk.AutoCAD.ApplicationServices;using Autodesk.AutoCAD.DatabaseServices;using Autodesk.AutoCAD.EditorInput;using Autodesk.AutoCAD.Runtime;using Autodesk.AutoCAD.Colors;using System.Reflection;using System.IO;using Newtonsoft.Json;using Newtonsoft.Json.Linq;namespace Loadx{    public class loadx1    {        private Action cmd1;        [CommandMethod("DD1")]        public void Reloadx()        {            #region CmdSum            string DllName = "exp1.dll";            string ClassName = "exp1.cSum";            string MethodName = "Sum";            #endregion            #region Don't            var adapterfileInfo = new FileInfo(Assembly.GetExecutingAssembly().Location);            var targetFilePath = Path.Combine(adapterfileInfo.DirectoryName, DllName);            var targetAssembly = Assembly.Load(File.ReadAllBytes(targetFilePath));            var targetType = targetAssembly.GetType(ClassName);            var targetMethod = targetType.GetMethod(MethodName);            var targetObject = Activator.CreateInstance(targetType);            cmd1 = () => targetMethod.Invoke(targetObject, null);            try            {                cmd1?.Invoke();            }            catch (System.Exception ex)            {            System.Windows.Forms.MessageBox.Show(ex.Message, "Tips1");            };            #endregion        }  }    public class CmdInfo1    {        public string DllName { get; set; }        public string ClassName { get; set; }        public string MethodName { get; set; }    }}

效果:

打开CAD2014,在命令行输入:netload命令回车!选择如图中的动态链接库文件!

编辑切换为居中

添加图片注释,不超过 140 字(可选)

输入命令:DD命令回车!

编辑

添加图片注释,不超过 140 字(可选)

提示:.exp1 => Class1.cs

修改后只要重新编译exp1即可,不用重新关闭重启CAD。

如果要修改启动的类,可以直接修改cmd.json文件对应的项即可。

这里再做一点补充!每次都要使用netload加载动态链接库文件也挺烦的。

编辑切换为居中

添加图片注释,不超过 140 字(可选)

在系统命令行输入regedit,打开注册表编辑器,在对应的键值中添加如图所示的项。

在CAD的选项中添加如下的受信任位置:

编辑切换为居中

添加图片注释,不超过 140 字(可选)

启动时就不会再提示如下所示的警告!!!

编辑

添加图片注释,不超过 140 字(可选)

通过上面的处理后,只要打开CAD,就可以使用自己开发的命令了。

标签: #pythonautocad开发 #vs2017使用方法实例 #vs2017怎么运行c语言代码 #vs2017怎么运行 #vs2017怎样运行程序代码