cad自动加载dll

 

环境:cad2013、vs2019

方法一:bundle文件的使用(操作简单,适合多个离散dll文件,不需要netload命令)

步骤:

在如图所示目录中创建文件夹xx.bundle

   2.在xx.bundle文件夹中继续创建如下文件及文件夹

   3.debug文件夹下保存使用的dll文件

   4.PackageContents.xml文件内容如下

复制代码
<?xml version="1.0" encoding="utf-8"?>
<ApplicationPackage
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    Name="HelloAutoCAD"      // 插件名称 
    Description="HelloAutoCAD"
    Author="WangYuna">       // 插件作者 

    <Components>  
        // 目标操作系统 // 目标AutoCAD或基于AutoCAD的产品 // 定义该组组件支持的最小AutoCAD版本号(可以在CAD中输入AcadVer来查看)
        <RuntimeRequirements OS="Win32|Win64" Platform="AutoCAD" SeriesMin="R19.0" SeriesMax="R22" />
        <ComponentEntry
            ModuleName=".\debug\19dialog.dll" //编译的DLL文件(需要改变)
            LoadOnAutoCADStartup="true"
            LoadOnCommandInvocation="false"
            AppDescription="dialogDemo by Wang Yuna"
            AppName="dialogDemo"
            AppType=".NET">
            <Commands GroupName="TestCmds">
                <Command Local="DialogDemo" Global="DialogDemo" /> //命令名称(CommandMethod)需要改变
            </Commands>
        </ComponentEntry>

        <ComponentEntry
            ModuleName=".\debug\17a.dll"
            LoadOnAutoCADStartup="true"
            LoadOnCommandInvocation="false"
            AppDescription="RibbonDemo by Wang Yuna"
            AppName="RibbonDemo"
            AppType=".NET">

            <Commands GroupName="Cmds">
                <Command Local="RibbonDemo" Global="RibbonDemo" StartupCommand="True" />
            </Commands>
                //StartupCommand="True" 表示加载完成后自动运行该命令
        </ComponentEntry>

    </Components>
</ApplicationPackage>
复制代码

  5.直接运行cad程序可正常实现自动加载功能

 

方法二:IExtensionApplication接口

背景

在用.net做CAD经典模式菜单栏功能的时候想要实现打开CAD自动加载,于是经过查阅相关文档和代码运行测试,发现可以通过实现IExtensionApplication接口的方式达到这个目的。

相关文档说明

当AutoCAD装载一个托管程序时,它查询程序的装配件(assembly)是否有ExtensionApplication自定义特性性。如果它找到这个特性,AutoCAD把这个特性所联系的类型作为程序的入口点。如果没有找到这个特性,AutoCAD查找所有实现IExtensionApplication接口的输出类。如果没有找到相关的接口实现,AutoCAD就会跳过程序的初始化步骤。 ExtensionApplication特性只能被附加到一个类型。这个被附加的类型必须实现IExtensionApplication接口

  • 引用需要添加 Autodesk.AutoCAD.Interop.dll CAD安装目录内
  • 菜单栏和菜单栏加载都放在了一个MainClass.cs文件:
复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Collections.Specialized;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;

using Autodesk.AutoCAD.Interop;  //引用需要添加 Autodesk.AutoCAD.Interop.dll  CAD安装目录内
using System.IO;


[assembly: ExtensionApplication(typeof(MyMenuTools.AcadNetApp))] //启动时加载工具栏,注意typeof括号里的类库名

namespace MyMenuTools
{
    //添加项目类引用
    public class AcadNetApp : Autodesk.AutoCAD.Runtime.IExtensionApplication
    {
        //重写初始化
        public void Initialize()
        {
            //加载后初始化的程序放在这里 这样程序一加载DLL文件就会执行
            Document doc = Application.DocumentManager.MdiActiveDocument;
            doc.Editor.WriteMessage("\n加载程序中...........\n");
            //加载菜单栏需要
            AddMenu();
        }

        //重写结束
        public void Terminate()
        {
            // do somehing to cleanup resource
        }

        //加载菜单栏
        public void AddMenu()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            doc.Editor.WriteMessage("添加菜单栏成功!>>>>>>>>>>>>>>");
            AcadApplication acadApp = Application.AcadApplication as AcadApplication;

            //创建建菜单栏的对象
            AcadPopupMenu myMenu = null;  

            // 创建菜单
            if (myMenu == null)
            {
                // 菜单名称
                myMenu = acadApp.MenuGroups.Item(0).Menus.Add("个人专用菜单工具(2020.07)");

                myMenu.AddMenuItem(myMenu.Count, "文字批量替换", "TBR "); //每个命令后面有一个空格,相当于咱们输入命令按空格
                myMenu.AddMenuItem(myMenu.Count, "文字批量对齐", "TBA ");
                myMenu.AddMenuItem(myMenu.Count, "输出文本", "CTE ");
                //开始加子菜单栏
                AcadPopupMenu subMenu = myMenu.AddSubMenu(myMenu.Count, "CADLinkToEXcel");  //子菜单对象
                subMenu.AddMenuItem(myMenu.Count, "Excel表格导入", "CTES ");
                subMenu.AddMenuItem(myMenu.Count, "由Excel数据更新CAD表格数据", "UCTFE ");
                subMenu.AddMenuItem(myMenu.Count, "由CAD表格数据更新Excel表格数据", "UEFCT ");
                myMenu.AddSeparator(myMenu.Count); //加入分割符号
                //结束加子菜单栏

            }

            // 菜单是否显示  看看已经显示的菜单栏里面有没有这一栏
            bool isShowed = false;  //初始化没有显示
            foreach (AcadPopupMenu menu in acadApp.MenuBar)  //遍历现有所有菜单栏
            {
                if (menu == myMenu)
                {
                    isShowed = true;
                    break;
                }
            }

            // 显示菜单 加载自定义的菜单栏
            if (!isShowed)
            {
                myMenu.InsertInMenuBar(acadApp.MenuBar.Count);
            }

        }        
    }    
}
复制代码
  • 将命令程序单独放置在myCommands.cs中
复制代码
// (C) Copyright 2020 by  
//
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using System.Collections.Generic;
using Exception = Autodesk.AutoCAD.Runtime.Exception;
using System.Windows.Forms;
using Application = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.Windows;
using OpenFileDialog = Autodesk.AutoCAD.Windows.OpenFileDialog;

using System.IO;

using Table = Autodesk.AutoCAD.DatabaseServices.Table;

using System.Runtime.InteropServices;

[assembly: CommandClass(typeof(MyMenuTools.MyCommands))]

namespace MyMenuTools
{

    public class MyCommands
    {

        //初始化  窗口焦点切换功能
        [DllImport("user32.dll", EntryPoint = "SetFocus")]
        public static extern int SetFocus(IntPtr hWnd);

        // 创建个人类OtherToosClass对象 用来调用里面的方法
        OtherToolsClass otl = new OtherToolsClass();

        [CommandMethod("TBA")]
        public void TextBatchAlign()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.WriteMessage("\n 欢迎使用文字批量对齐程序!!!");
            // 选择一个基准点
            Point3d point = otl.SelectPoint("\n>>>>请选择基准点!!");
            // 创建一个 TypedValue 数组来定义过滤器条件
            //TypedValue[] typeValue = new TypedValue[1];
            TypedValue[] typeValue = new TypedValue[2];
            // 过滤条件 只选择单行文本
            typeValue.SetValue(new TypedValue(0, "TEXT"), 0);
            // 文本内容
            //typeValue.SetValue(new TypedValue((int)DxfCode.Text, "数据智能笔记A"), 1);
            // 选择集 区域手动选择方式
            SelectionSet sSet = otl.SelectSsGet("GetSelection", null, typeValue);
            // 判断选择集是否为空
            if (sSet != null)
            {
                // 如果选择集不为空 遍历选择图元对象 
                foreach (SelectedObject sSetObj in sSet)
                {
                    // 开启事务处理
                    using (Transaction trans = db.TransactionManager.StartTransaction())
                    {
                        // 单行文本对象 打开方式为写
                        DBText dbText = trans.GetObject(sSetObj.ObjectId, OpenMode.ForWrite) as DBText;
                        // 垂直方向左对齐
                        dbText.HorizontalMode = TextHorizontalMode.TextLeft;
                        // 判断对齐方式是否是左对齐 
                        if (dbText.HorizontalMode != TextHorizontalMode.TextLeft)
                        {
                            // 对齐点
                            Point3d aliPoint = dbText.AlignmentPoint;
                            ed.WriteMessage("\n" + aliPoint.ToString());
                            // 位置点
                            Point3d position = dbText.Position;
                            dbText.AlignmentPoint = new Point3d(point.X, position.Y, 0);
                        }
                        // 如果是左对齐只需要调整插入点
                        else
                        {
                            Point3d position = dbText.Position;
                            dbText.Position = new Point3d(point.X, position.Y, 0);
                        }
                        trans.Commit();
                    }
                }
            }
        }

        [CommandMethod("TBR")]
        public void TextBatchReplace()
        {

          ....

        }

        //输出文本到Excel和Txt 打开窗口

        [CommandMethod("CTE")] //CAD启动界面
        public void CADTextExport()
        {
            ...
        }
        [CommandMethod("CTES")]
        static public void CADTablebyExcelSheet()
        {
            ...
        }

        /// <summary>
        /// 由Excel表格中数据来更新CAD中的table
        /// 也就是说 excel数据变化后保存 然后运行该命令来更新CAD中的表格数据
        /// </summary>
        [CommandMethod("UCTFE")]
        static public void UpdateCADTableFromExcel()
        {
            ...
        }

        /// <summary>
        /// 由CAD表格数据更新Excle中的数据
        /// </summary>

        [CommandMethod("UEFCT")]
        static public void UpdateExcelFromCADTable()
        {
            ...
        }

}
复制代码
  • 使用bundle文件加载
复制代码
 <ComponentEntry
            ModuleName=".\debug\Debug\MyMenuTools.dll"
            LoadOnAutoCADStartup="true"
            LoadOnCommandInvocation="false"
            AppDescription="MyMenuTools by Wang Yuna"
            AppName="MyMenuTools"
            AppType=".NET">

        </ComponentEntry>
复制代码

 

 方法三:添加netload至cad2013.lsp(只能添加一个dll)

 文件最后加上一句:

 

 

posted @   wangyuna7723  阅读(342)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示