CAD二次开发,启动加载界面,并在点击Ribbon按钮时热加载功能DLL,并提供打包的工程框架

本文介绍了如何使用LSP在AutoCAD中实现自动加载功能,包括启动时加载指定的界面DLL,以及通过按钮绑定CAD内部任务或使用反射动态加载DLL中的方法。重点展示了AcadNetApp.cs中的关键代码和LoadDllCommandHandler类的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

整个思路为启动时运行lsp,lsp操作仅加载一个界面DLL,其中按钮绑定CAD内部任务,或者使用反射绑定到指定的DLL文件;

假设这里已经完成了一个功能,并编译为  CAD.Demo.dll

先写个lsp文件,很简单 就是加载Ribbon主程DLL

AutoLoad.lsp

;;; AutoLoad.lsp 
;;; 用于启动自动加载 
#| 
该文件AutoLoad.lsp可以复制在任意位置,功能安装完毕后,首次打开CAD进行需要设置
设置方法如下:

	输入命令appload 
	弹窗
	启动组=>内容 选择当前AutoLoad.lsp文件所在的路径,此时就已经永久加载了 ;
	如要卸载 把这个文件路径删除就可以  ;
|#
 


(setq CadToolsPath "D:\\WORKSPACE\\CODE_CAD\\CAD.Demo\\bin\\Debug\\")		;; 设定工具集DLL目录	
(command "NETLOAD" (strcat CadToolsPath "CAD.Ribbon.Template.dll"))	;; 加载界面DLL文件,即为功能界面的入口
 


; (command "NETLOAD" (strcat CadToolsPath "其他功能界面的DLL.dll"))	 

此时,建立工程 CAD.Template.Ribbon

主程文件   AcadNetApp.cs

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


[assembly: ExtensionApplication(typeof(CAD.Template.Ribbon.AcadNetApp))]
namespace  CAD.Template.Ribbon
{
    public class AcadNetApp : IExtensionApplication
    {
        public static string assemblyPath = Assembly.GetExecutingAssembly().Location;
        public static string assemblyDir = Path.GetDirectoryName(assemblyPath) + "\\";

        /// <summary>
        /// 启动动作
        /// </summary>
        public void Initialize()
        {
            Application.Idle += OnIdle;
            //需要添加事件,监听工作空间有没有变化,如果有变化,需要重新生成面板
            Application.SystemVariableChanged += new SystemVariableChangedEventHandler(Application_SystemVariableChanged);

        }

        /// <summary>
        /// 关闭动作
        /// </summary>
        public void Terminate()
        {
            // do somehing to cleanup resource 
            Application.Idle -= OnIdle;
            //卸载
            Application.SystemVariableChanged -= new SystemVariableChangedEventHandler(Application_SystemVariableChanged);

        }

        /// <summary>
        /// 启动时行为
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnIdle(object sender, EventArgs e)
        {
            //无论如何 刷新一下工作空间
            var wsCurrent = Application.GetSystemVariable("WSCURRENT");
            Application.SetSystemVariable("WSCURRENT", wsCurrent);

            Application.Idle -= OnIdle;
        }

        /// <summary>
        /// 监听动作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Application_SystemVariableChanged(object sender, SystemVariableChangedEventArgs e)
        {
            //Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            //ed.WriteMessage("\n-Sys Var Changed: " + e.Name);

            if (e.Name.ToLower() == "ws
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值