带有vb.net的AUTOCAD插件调色板 - 自动启动 - 易于安装 - 易于使用

问题描述 投票:1回答:2

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

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

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

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

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

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

.net vb.net vb.net-2010 autocad autocad-plugin
2个回答
2
投票

对于2012及更高版本,使用2012年推出的AutoCAD自动加载器格式。您的安装程序只需将其解压缩到一个文件夹,Autocad将负责其余部分。

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


1
投票

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

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

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 [email protected]  " & 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 [email protected]  " & 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 [email protected]  " & 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 [email protected]  " & 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并使用该插件。

© www.soinside.com 2019 - 2024. All rights reserved.