整个思路为启动时运行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