CAD二次開發入門:WPF類庫

人生为卒發表於2024-08-14

參考學習影片:https://www.bilibili.com/video/BV16Y411v7kr/?spm_id_from=333.337.search-card.all.click&vd_source=fbb64ea20b269b753497bf6c2499fc29

第一步:建立WPF類庫,並寫CAD呼叫方法

修改輸出型別為:類庫

新增CAD開發需要的類庫

main頁面新增以下內容:

    <Grid>
        <Button Height="50" Click="Button_Click" Margin="285,184,157,185" Content="開啟程式" Tag=""/>
        <TextBox x:Name="txtPath" HorizontalAlignment="Left" Height="23" Margin="245,110,0,0" TextWrapping="Wrap" Text="D:\Program Files (x86)\Tencent\WeChat\WeChat.exe" VerticalAlignment="Top" Width="450"/>
    </Grid>

main後臺新增以下內容:

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string Path = "";
            try
            {
                Path = @"D:\Program Files (x86)\Tencent\WeChat\WeChat.exe";
                if (!string.IsNullOrEmpty(txtPath.Text))
                {
                    Path = txtPath.Text;
                }
                System.Diagnostics.Process.Start(Path);
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.CurrentDocument.Editor.WriteMessage("啟動成功:" + Path);
            }
            catch (Exception ex)
            {

                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.CurrentDocument.Editor.WriteMessage("路徑:" + Path + ",啟動異常," + ex.Message);
            }
        }

新增一個類,寫CAD可執行方法,OpenExe :

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

namespace WpfApp4
{
     public class Class1
    {
       [CommandMethod("OpenExe")] 
       public void OpenExe()
        {
            MainWindow main = new MainWindow();
            Application.ShowModelessWindow(main);
        }
    }
}

程式開發完成,生成動態庫 dll:

第二步:開啟CAD,引用生成的動態庫,並呼叫方法開啟畫面

CAD引用dll,使用指令【NETLOAD】,之後選擇dll:

CAD呼叫wpf動態庫的方法:

命令輸入方法名:openexe

執行命令後,CAD自動開啟WPF的畫面

相關文章