C#二次开发CAD解决CAD一直需要重新 启动的问题

87 篇文章 17 订阅

//直接要调用的dll

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Reflection;

//用到了System,System.IO,System.Reflection
namespace LoadX
{
    public class LoadX
    {
        private Action cmd;
        [CommandMethod("DD")]
        public void ReloadX()
        {
            string dllName = "CadBasic.dll";//生成的需要调用的文件名
            string className = "CadBasic.cmd";//空间名.类名
            string methodName = "CmdSum";//method名

            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,"Tips");
            }
        }
        
    }
    public class CmdInfo
    { 
        public string DllName { get; set; }
        public string ClassName { get; set; }
        public string MethodName { get; set; }
    }
}

 //功能的dll

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CadBasic
{

    public class cmd
    {
        [CommandMethod("CmdHello")]
        public void CmdHello()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("Hello World From VS C#");
            System.Windows.Forms.MessageBox.Show("Hello World From VS C#(It's for Debugging)", "Info(Tips)");
        }
        [CommandMethod("CmdSum")]
        public void CmdSum()
        {
            int sum = 0;
            int max = 200;
            for (int i = 1; i <= max; i++)
            {
                sum += i;
            }
            double divisor = 13;
            double divide = sum / divisor;
            System.Windows.Forms.MessageBox.Show(sum.ToString() + "\n" + divide, "Info(Tips)");

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值