1

我对 Autocad 和 vb.net 之间的集成很感兴趣。任务是让autocad使用vb.net代码自动绘制。最终项目将允许我自动创建电缆绘图,从 tmeplates 和带有信息的 csv 文件(当然还有我的插件)开始。

“用户友好”是我的关键词。我不喜欢用户必须在 autocad 上键入命令来安装插件然后使用它的实现。出于这个原因,我有两个问题:

1)找到一种方法让我的用户轻松安装插件

2)开发一个易于使用的图形界面插件。

我找到了解决方案,我想与您分享。

编辑:@David wolfe 回答了在 autocad 2012 及更早版本中自动启动插件的解决方案,但我的工厂只有 autocad 2011 及更高版本的许可证。

4

2 回答 2

2

对于 2012 年及更高版本,请使用 2012 年引入的 AutoCAD 自动加载器格式。您的安装程序只需将其解压缩到一个文件夹,其余的由 Autocad 处理。

http://adndevblog.typepad.com/autocad/2013/01/autodesk-autoloader-white-paper.html

于 2013-02-02T13:37:19.327 回答
1

编辑:@david wolfe 为这个问题写了一个答案,以在 Autocad 2012 及更高版本中自动安装插件,但我什至处理 autocad 2011 和 autocad 2000,然后我找到了以下解决方案。

问题1 解决方法: vb.net开发的autocad插件是一个dll文件。要在 AutoCAD 启动时自动加载它,AutoCAD 需要修改系统注册表。为此,我更喜欢不使用AutoCAD的方法。我在 vb.net 中创建了一个新的“Windows 窗体应用程序”项目(称为 INSTALLER),在默认表单中我添加了 2 个按钮:一个用于注册插件,一个用于删除插件。注意:在这个示例中,我决定 dll 插件文件必须放在我们要创建的 exe 应用程序(安装程序)的同一文件夹中。这是我们的插件安装程序/删除程序的代码。首先创建一个模块并粘贴此代码:(代码中的一些评论和消息是意大利语的,您可以使用谷歌翻译轻松地将它们翻译成您的语言)

Imports Microsoft.Win32

Module GeneralFunctions


    Public Sub RegisterModule()

        Dim regKey As RegistryKey
        Dim PathToDll As String
        If MsgBox("Suggerimento: copia questo eseguibile,la dll e gli altri file, in una cartella a piacere prima di avviare la registrazione." & vbCrLf & "Se sposterai successivamente i file, sarà necessario registrare nuovamente il modulo" & vbCrLf & "Proseguire?", MsgBoxStyle.YesNo) <> MsgBoxResult.Yes Then
            Exit Sub
        End If

        PathToDll = System.Windows.Forms.Application.StartupPath & "\SupportoCavi.dll"

        If Not (FileIO.FileSystem.FileExists(PathToDll)) Then
            MsgBox("Il file SupportoCavi.dll non è stato trovato nella cartella")
            Exit Sub
        End If

        On Error GoTo PercorsoNonTrovato

        regKey = My.Computer.Registry.CurrentUser.OpenSubKey("Software\Autodesk\AutoCAD", True) 'HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\
        Dim keysList() As String = regKey.GetSubKeyNames

        regKey = regKey.OpenSubKey(keysList(0), True) 'HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R18.1\
        keysList = regKey.GetSubKeyNames()
        regKey = regKey.OpenSubKey(keysList(0), True) 'HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R18.1\ACAD-9001:410\
        regKey = regKey.OpenSubKey("Applications", True) 'HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R18.1\ACAD-9001:410\Applications

        On Error GoTo CreazioneChiaveFallita
        regKey = regKey.CreateSubKey("cavi") 'HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R18.1\ACAD-9001:410\Applications\cavi
        regKey.SetValue("DESCRIPTION", "Modulo per la creazione di cavi e cablaggi")
        regKey.SetValue("LOADCTRLS", 2)
        regKey.SetValue("MANAGED", 1)
        regKey.SetValue("LOADER", PathToDll)
        MsgBox("Modulo registrato con successo. Apri autocad per usare il nuovo set di strumenti")
        Exit Sub




PercorsoNonTrovato:
        On Error Resume Next
        Dim more As String = ""
        If Not (IsNothing(regKey)) Then
            more = "Errore su chiave " & regKey.Name
        End If
        MsgBox("Percorso non trovato nel registro di sistema," & vbCrLf & "comunica la tua versione di autocad e questo messaggio di errore a me@me.com  " & vbCrLf & more)
        Exit Sub

CreazioneChiaveFallita:
        On Error Resume Next

        If Not (IsNothing(regKey)) Then
            more = "Errore su chiave " & regKey.Name
        End If
        MsgBox("Errore durante la registrazione del modulo," & vbCrLf & "comunica la tua versione di autocad e questo messaggio di errore a me@me.com  " & vbCrLf & more)
        Exit Sub



    End Sub

    Public Sub UnregisterModule()

        Dim regKey As RegistryKey

        On Error GoTo PercorsoNonTrovato

        regKey = My.Computer.Registry.CurrentUser.OpenSubKey("Software\Autodesk\AutoCAD", True) 'HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\
        Dim keysList() As String = regKey.GetSubKeyNames

        regKey = regKey.OpenSubKey(keysList(0), True) 'HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R18.1\
        keysList = regKey.GetSubKeyNames()
        regKey = regKey.OpenSubKey(keysList(0), True) 'HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R18.1\ACAD-9001:410\
        regKey = regKey.OpenSubKey("Applications", True) 'HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R18.1\ACAD-9001:410\Applications

        On Error GoTo EliminazioneChiaveFallita
        regKey.DeleteSubKeyTree("cavi") 'HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R18.1\ACAD-9001:410\Applications\cavi
        MsgBox("Modulo rimosso con successo dal registro di sistema.")
        Exit Sub




PercorsoNonTrovato:
        On Error Resume Next
        Dim more As String = ""
        If Not (IsNothing(regKey)) Then
            more = "Errore su chiave " & regKey.Name
        End If
        MsgBox("Percorso non trovato nel registro di sistema," & vbCrLf & "comunica la tua versione di autocad e questo messaggio di errore a emanuele.vacca@selex-si.com  " & vbCrLf & more)
        Exit Sub

EliminazioneChiaveFallita:
        On Error Resume Next

        If Not (IsNothing(regKey)) Then
            more = "Errore su chiave " & regKey.Name
        End If
        MsgBox("Errore durante la rimozione del modulo," & vbCrLf & "comunica la tua versione di autocad e questo messaggio di errore a emanuele.vacca@selex-si.com  " & vbCrLf & more)
        Exit Sub



    End Sub
End Module

现在您只需调用 RegisterModule() 和 UnregisterModule() 子例程即可注册或注销我们的插件。这是我的安装程序 GUI:

安装程序

问题 2 解决方案: 要创建一个易于使用的插件,我认为最好是使用图形用户界面。出于这个原因,我创建了一个带有按钮、下拉菜单和图片的调色板。每次打开 AutoCAD 时,我们的插件都会自动加载,并创建我们的调色板。以下代码是一个简单的实现,您可以使用您的按钮和代码来个性化调色板。首先创建一个新项目(创建一个Windows控件库)并添加对以下组件的引用:

(根据您的 AutoCAD 版本,此路径可能会更改)

C:\Program Files\Autodesk\AutoCAD 2011\acdbmgd.dll

C:\Program Files\Autodesk\AutoCAD 2011\acmgd.dll

然后创建一个类并粘贴此代码(注意:有一些我的实现代码,如结构,但您可以根据需要轻松清除它。loadDatabase,例如是我的自定义例程,您可能不需要):

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports System.IO

Imports System.Reflection

<Assembly: ExtensionApplication(GetType(adskClass))> 

Public Class adskClass

    Implements IExtensionApplication

    Public currentPath As String

    Public templates() As template

    Public Structure template
        Dim title As String
        Dim description As String
        Dim previewPath As String
        Dim templatePath As String
    End Structure



    Public Sub Initialize() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Initialize
        'get current path
        Dim myAssy As [Assembly]
        myAssy = [Assembly].GetExecutingAssembly
        currentPath = myAssy.Location
        currentPath = System.IO.Path.GetDirectoryName(currentPath)

        loadPalette()
        loadDatabase()
    End Sub

    Public Sub Terminate() Implements Autodesk.AutoCAD.Runtime.IExtensionApplication.Terminate
        myPaletteSet.Remove(0)
    End Sub

    ' declare a paletteset object, this will only be created once
    Public myPaletteSet As Autodesk.AutoCAD.Windows.PaletteSet
    ' we need a palette which will be housed by the paletteSet
    Public myPalette As UserControl1

    Public Sub loadPalette()
        ' check to see if it is valid
        If (myPaletteSet = Nothing) Then
            ' create a new palette set, with a unique guid
            myPaletteSet = New Autodesk.AutoCAD.Windows.PaletteSet("SUPPORTO CAVI") ', New Guid("D61D0875-A507-4b73-8B5F-9266BEACD596"))
            ' now create a palette inside, this has our tree control
            myPalette = New UserControl1(Me)

            ' now add the palette to the paletteset
            myPaletteSet.Add("Supporto Cavi", myPalette)
        End If

        ' now display the paletteset
        myPaletteSet.Visible = True





    End Sub



    Public Sub loadDatabase()

        Dim databaseFile As String = currentPath & "\modelli\database.csv"
        If Not (FileIO.FileSystem.FileExists(databaseFile)) Then
            MsgBox("Non ho trovato il file database.csv nella cartella modelli per il modulo SUPPORTO CAVI" & vbCrLf & databaseFile)
            Exit Sub
        End If

        'carica file database
        On Error GoTo erroreDuranteCaricamentoDatabase
        Dim tmpstream As StreamReader = File.OpenText(databaseFile)
        Dim strlines() As String
        Dim strline() As String

        strlines = tmpstream.ReadToEnd().Split(Environment.NewLine)
        ReDim templates(UBound(strlines))

        For i As Integer = 0 To UBound(templates)
            strline = strlines(i).Split(",")
            templates(i).title = strline(0)
            Dim tmpFileName = strline(1)
            templates(i).previewPath = currentPath & "\modelli\" & tmpFileName & ".png"
            templates(i).templatePath = currentPath & "\modelli\" & tmpFileName & ".dwg"
            templates(i).description = strline(2)
            myPalette.cableTemplate.Items.Add(templates(i).title)
        Next

        Exit Sub
erroreDuranteCaricamentoDatabase:
        MsgBox("Errore durante il caricamento del database per il modulo SUPPORTO CAVI." & vbCrLf & Err.Description)


    End Sub

    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub




End Class

然后在我们的 userControl 中,从图形视图切换到代码视图并粘贴以下代码:

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.DatabaseServices

'Imports Autodesk.AutoCAD.Windows
Imports Autodesk.AutoCAD.Runtime


Public Class UserControl1

    Public parentClass As adskClass


    Public Sub New(ByVal parent As adskClass)
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        parentClass = parent
    End Sub

    Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    End Class

'Now we create a class that will help us if we have objects that we want to drag and drop from the palette to the autocad draw area. this class detects when the object is "dropped" in the AutoCAD editor. It Inherits from Autodesk.AutoCAD.Windows.DropTarget. 

Public Class MyDropTarget
    Inherits Autodesk.AutoCAD.Windows.DropTarget

    Public Overrides Sub OnDrop(ByVal e As System.Windows.Forms.DragEventArgs)
        Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor

        Try
            Using docLock As DocumentLock = Application.DocumentManager.MdiActiveDocument.LockDocument()
                'Run the AddAnEnt procedure if needed
                'adskClass.AddAnEnt()
            End Using

        Catch ex As System.Exception
            ed.WriteMessage("Error Handling OnDrop: " + ex.Message)
        End Try

    End Sub
End Class

编译所有内容,将生成的dll文件复制到之前创建的安装程序路径中并进行测试。

现在您已准备好在您的调色板上添加图形按钮、代码以及任何您想用它做的事情。

这是我在 AutoCAD 上的调色板的 GUI: 调色板

如何分发:我将安装程序 exe、dll 插件、自述文件以及自定义插件所需的模板或其他文件放在一个文件夹中。然后我压缩这个文件夹并通过电子邮件发送给我的用户。这样,用户只需解压硬盘中的文件夹(非临时路径),打开exe并按下“REGISTER PLUGIN”按钮即可。结束! 现在用户可以打开 AutoCAD 并使用该插件。

于 2013-02-01T17:02:49.653 回答