如何使用 Visual Studio 2015 在 AutoCAD 2017 中生成弹出按钮、工具栏和按钮

How to Generate Flyouts, Toolbars, and Buttons in AutoCAD 2017 using Visual Studio 2015

前段时间我在 AutoCAD 2010 中使用 Visual Studio 2010 编写了一些 DOT NET 例程,但现在我们正在升级到 AutoCAD 2017 / Visual Studio 2015。

我已成功将我的所有代码导入到新环境中,一切正常,除了弹出窗口、工具栏和工具栏按钮。在程序启动并且我的 DLL 被网络加载后,这些甚至根本不会出现在 AutoCAD 2017 中。 但是,所有命令在 AutoCAD 中仍然可用,但只能通过命令行使用。

我试图完成的是每次启动 AutoCAD 时弹出窗口自动出现在左上角。

下面是在程序初始化时运行的 myCommands.vb 文件,它应该会显示我在 AutoCAD 2017 中的弹出窗口,但不会:

Option Explicit On
Imports Microsoft.Win32
Imports System.Reflection
Imports System.Data.OleDb
Imports System.Runtime.CompilerServices
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Runtime
Imports acApp = Autodesk.AutoCAD.ApplicationServices.Application
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Customization
Imports acOp = Autodesk.AutoCAD.Interop
Imports acOpCom = Autodesk.AutoCAD.Interop.Common

<Assembly: CommandClass(GetType(CatScript17.CatScriptCommands))> 
Namespace CatScript17
    ' Manages all CatScript commands for this software. Also contains all the autocad flyout/toolbar commands.
    Public Class CatScriptCommands
        Implements IExtensionApplication
        Public Shared strAppName As String = "CatScript17"
        Public Shared cs As CustomizationSection
        Public Shared restoreDefaultMacro As Boolean = False

        Public Sub Initialize() Implements IExtensionApplication.Initialize
            ' Autoload the catscript17 flyouts and toolbars into AutoCAD 2017 upon each startup
            CatScriptToolbar.addToolbar()
        End Sub

        Public Sub Terminate() Implements IExtensionApplication.Terminate
            Throw New NotImplementedException()
        End Sub

        ' Displaying the Air Conveyor Corner form
        <CommandMethod("accorner", CommandFlags.UsePickSet)>
        Public Shared Sub acCorner()
        End Sub

        ' Displaying the Air Conveyor Side Elevation form
        <CommandMethod("acelevation", CommandFlags.UsePickSet)>
        Public Shared Sub acElevation()
        End Sub

        ' Displaying the Air Conveyor Straight form
        <CommandMethod("acstraight", CommandFlags.UsePickSet)>
        Public Shared Sub acStraight()
        End Sub

        ' Displaying the Case Straight Combo form
        <CommandMethod("cccombo", CommandFlags.UsePickSet)>
        Public Shared Sub ccCombo()
        End Sub

        ' Displaying the Case Corner Conveyor form
        <CommandMethod("cccorner", CommandFlags.UsePickSet)>
        Public Shared Sub ccCorner()
        End Sub

        ' Displaying the Case Conveyor Side Elevation form
        <CommandMethod("ccelevation", CommandFlags.UsePickSet)>
        Public Shared Sub ccElevation()
        End Sub

        ' Displaying the Case Straight Conveyor form
        <CommandMethod("ccstraight", CommandFlags.UsePickSet)>
        Public Shared Sub ccStraight()
        End Sub

        ' Displaying the Case Turner Conveyor form
        <CommandMethod("ccturner", CommandFlags.UsePickSet)>
        Public Shared Sub ccTurner()
        End Sub

        ' Displaying the Case Zero Pressure Conveyor form
        <CommandMethod("cczeropressure", CommandFlags.UsePickSet)>
        Public Shared Sub ccZeroPressure()
        End Sub

        ' Displaying the table top Bi-Di form
        <CommandMethod("ttbidi", CommandFlags.UsePickSet)>
        Public Shared Sub ttBiDi()
        End Sub

        ' Displaying the table top Combiner form
        <CommandMethod("ttcombiner", CommandFlags.UsePickSet)>
        Public Shared Sub ttCombiner()
        End Sub

        ' Displaying the table top Corner form
        <CommandMethod("ttcorner", CommandFlags.UsePickSet)>
        Public Shared Sub ttCorner()
        End Sub

        ' Displaying the table top Decel form
        <CommandMethod("ttdecel", CommandFlags.UsePickSet)>
        Public Shared Sub ttDecel()
        End Sub

        ' Displaying the table top Divert form
        <CommandMethod("ttdivert", CommandFlags.UsePickSet)>
        Public Shared Sub ttDivert()
        End Sub

        ' Displaying the table top Dynamic form
        <CommandMethod("ttdynamic", CommandFlags.UsePickSet)>
        Public Shared Sub ttDynamic()
        End Sub

        ' Displaying the Table Top Side Elevation form
        <CommandMethod("ttelevation", CommandFlags.UsePickSet)>
        Public Shared Sub ttElevation()
        End Sub

        ' Displaying the Mass Merge Conveyor form
        <CommandMethod("ttmerge", CommandFlags.UsePickSet)>
        Public Shared Sub ttMerge()
        End Sub

        ' Displaying the Mass Parallel Conveyor form
        <CommandMethod("ttparallel", CommandFlags.UsePickSet)>
        Public Shared Sub ttParallel()
        End Sub

        ' Displaying the Mass Straight Conveyor form
        <CommandMethod("ttstraight", CommandFlags.UsePickSet)>
        Public Shared Sub ttStraight()
        End Sub
    End Class

    Public Class CatScriptToolbar
        '-----------
        ' Properties
        '-----------
        Private Shared groups As acOp.AcadMenuGroups
        Private Shared group As acOp.AcadMenuGroup
        Private Shared toolbars As acOp.IAcadToolbars
        Private Shared toolbar As acOp.AcadToolbar
        Private Shared flyout As acOp.AcadToolbarItem
        '--------
        ' Methods
        '--------
        ' Component for adding toolbars and flyouts
        Public Shared Sub addToolbar()
            Try
                Dim app As acOp.AcadApplication = _
                DirectCast(Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication,  _
                acOp.AcadApplication)

                ' Define the toolbars group
                groups = app.MenuGroups
                group = groups.Item(0)
                toolbars = group.Toolbars

                ' Generate the flyout toolbar
                toolbar = toolbars.Add("CatScript Tools")

                ' Generate the flyouts
                flyoutForTableTop(0)
                flyoutForCaseConveyor(1)
                flyoutForAirConveyor(2)

                ' Dock the newly created toolbar on the left
                toolbar.Dock(acOpCom.AcToolbarDockStatus.acToolbarDockLeft)

            Catch ex As System.Exception
            End Try
        End Sub

        ' Component for generating the flyout for air conveyor, as well as buttons for each routine
        Private Shared Sub flyoutForAirConveyor(ByVal toolbarIndex As Long)
            ' Create an air conveyor toolbar
            Dim str As String = "Air"
            Dim tool As acOp.AcadToolbar = toolbars.Add(str & " Conveyor")
            Dim titles(), commands(), descs() As String

            titles = {"Straight", "Corner", "Elevation"}
            commands = {"straight", "corner", "elevation"}

            descs = {"Generates the straight sections for air conveyor applications. " & _
                     "Allows for section dimensioning",
                     "Generates the curved sections and corners for air conveyor applications " & _
                     "(in angles of 30, 45, 60, and 90 degrees). User also has the option of generating " & _
                     "infinite curves (with legs or hangers) for the air conveyor corner",
                     "Generates the side and plan views of elevation changes for air conveyors. " & _
                     "This is accomplished using infeed and discharge NRE, incline/decline angle or " & _
                     "specified/picked distance, and infeed/discharge section lengths. This routine " & _
                     "allows for section dimensioning (if generating ONLY plan view)"}

            Dim button As acOp.AcadToolbarItem
            For i As Long = 0 To titles.Length - 1
                ' Add the button to temporary toolbar
                button = tool.AddToolbarButton(i, str & " " & titles(i), _
                descs(i), ChrW(27) & ChrW(27) & "_ac" & commands(i) & vbLf, False)

                ' Set the images for the button. cat.root is C:\catscript\
                button.SetBitmaps(cat.root & "AC " & titles(i) & ".bmp", _
                                  cat.root & "AC " & titles(i) & ".bmp")
            Next

            'Create the Group flyout
            flyout = toolbar.AddToolbarButton(toolbarIndex, _
                "Air Conveyor", "Groups the Air Conveyors", _
                ChrW(27) & ChrW(27) & "_acstraight" & vbLf, True)
            flyout.SetBitmaps(cat.root & "AC Straight.bmp", cat.root & "AC Straight.bmp")

            ' Attach the group toolbar to the flyout just created
            flyout.AttachToolbarToFlyout(group.Name.ToString(), str & " Conveyor")
            tool.Visible = False
        End Sub

        ' Component for generating the flyout for case conveyor, as well as buttons for each routine
        Private Shared Sub flyoutForCaseConveyor(ByVal toolbarIndex As Long)
            ' Create a case conveyor toolbar
            Dim tool As acOp.AcadToolbar = toolbars.Add("Case Conveyor")
            Dim titles(), commands(), descs() As String

            titles = {"Straight", "Corner", "Turner", "Zero Pressure", "Elevation", "Combo"}
            commands = {"straight", "corner", "turner", "zeropressure", "elevation", "combo"}

            descs = {"Generates the straight sections for case conveyor applications. " & _
                     "Sections generated are Straight Idler, Pass Through, Drive, and Stand Alone, " & _
                     "using varying infeed and discharge styles. This routine allows " & _
                     "for section dimensioning",
                     "Generates the curved sections and corners in case conveyor applications",
                     "Generates the Straight Stand-Alone, and Multiple Chain sections of case " & _
                     "conveyor for product orientation change. This routine allows for section dimensioning",
                     "Generates straight sections of Zero-Pressure Accumulation in case " & _
                     "conveyor applications. This routine allows for section dimensioning",
                     "Generates the side and plan views of elevation changes for case conveyors. " & _
                     "This is accomplished using infeed and discharge TOC, incline/decline angle or " & _
                     "specified/picked distance, and infeed/discharge section lengths. This routine " & _
                     "allows for section dimensioning (if generating ONLY plan view)", _
                     "Combines two or three case straight sections into one case straight section"}

            ' Add the button to temporary toolbar
            Dim button As acOp.AcadToolbarItem
            For i As Long = 0 To titles.Length - 1
                button = tool.AddToolbarButton(i, "Case " & titles(i), _
                descs(i), ChrW(27) & ChrW(27) & "_cc" & commands(i) & vbLf, False)

                ' Set the images for the button
                button.SetBitmaps(cat.root & "CC " & titles(i) & ".bmp", _
                                  cat.root & "CC " & titles(i) & ".bmp")
            Next

            'Create the Group flyout
            flyout = toolbar.AddToolbarButton(toolbarIndex, _
                "Case Conveyor", "Groups the Case conveyors", _
                ChrW(27) & ChrW(27) & "_ccstraight" & vbLf, True)
            flyout.SetBitmaps(cat.root & "CC Straight.bmp", cat.root & "CC Straight.bmp")

            ' Attach the group toolbar to the flyout just created
            flyout.AttachToolbarToFlyout(group.Name.ToString(), "Case Conveyor")
            tool.Visible = False
        End Sub

        ' Component for generating the flyout for table top, as well as buttons for each routine
        Private Shared Sub flyoutForTableTop(ByVal toolbarIndex As Long)
            ' Create a table top toolbar
            Dim tool As acOp.AcadToolbar = toolbars.Add("Table Top")
            Dim titles(), commands(), descs() As String

            titles = {"Straight", "Corner", "Dynamic Transfer", "Parallel Transfer", _
                       "Merge", "Divert", "Combiner", "Decel"}
            commands = {"straight", "corner", "dynamic", "parallel", _
                        "merge", "divert", "combiner", "decel"}

            descs = {"Generates the straight sections for table top applications. " & _
                     "Sections generated are Straight Idler, Pass Through, Drive, and " & _
                     "Stand Alone. User has the option of specifying rail configuration, " & _
                     "using varying infeed and discharge styles. This routine allows " & _
                     "for section dimensioning",
                     "Generates the curved sections and corners in table top applications.",
                     "Generates the dynamic transfer sections in table top applications. " & _
                     "User has the option of specifying rail configuration. " & _
                     "This routine allows for section dimensioning",
                     "Generates the parallel transfers in table top applications. User has the " & _
                     "option of specifying rail configuration. This routine allows for section dimensioning",
                     "Generates merge tables in table top applications. User has the option of " & _
                     "specifying rail configuration. This routine allows for section dimensioning",
                     "Generates divert tables in table top applications. User has the option of " & _
                     "specifying rail configuration. This routine allows for section dimensioning",
                     "Generates combiner tables in table top applications. " & _
                     "This routine allows for section dimensioning",
                     "Generates deceleration tables in table top applications. " & _
                     "This routine allows for section dimensioning"}

            ' Generate the buttons
            Dim button As acOp.AcadToolbarItem

            For i As Long = 0 To titles.Length - 1
                ' Add the button to temporary toolbar
                button = tool.AddToolbarButton(i, "Table Top " & titles(i), _
                descs(i), ChrW(27) & ChrW(27) & "_tt" & commands(i) & vbLf, False)
                ' Set the images for the button
                button.SetBitmaps(cat.root & "TT " & titles(i) & ".bmp", _
                                  cat.root & "TT " & titles(i) & ".bmp")
            Next

            'Create the Group flyout
            flyout = toolbar.AddToolbarButton(toolbarIndex, _
                "Table Top Conveyor", "Groups the Table Top conveyors", _
                ChrW(27) & ChrW(27) & "_ttstraight" & vbLf, True)
            flyout.SetBitmaps(cat.root & "TT Straight.bmp", cat.root & "TT Straight.bmp")

            ' Attach the group toolbar to the flyout just created
            flyout.AttachToolbarToFlyout(group.Name.ToString(), "Table Top")
            tool.Visible = False
        End Sub
    End Class
End Namespace

此项目引用了来自 ObjectARX 2017 的最新 AutoCAD API DLL。

关于如何获取此代码以显示这 3 个弹出窗口及其工具栏的任何想法? AutoCAD 是否更改了在 AutoCAD 2017 VS2015 中生成弹出按钮和工具栏的方式? 如果是这样,关于如何在程序启动时在 AutoCAD 2017 中自动加载我的弹出窗口和工具栏有什么指示吗?

谢谢。

此问题已解决。工具栏在每个 AutoCAD 会话中都不会更改,因此创建了一个局部 cui 菜单文件。现在,工具栏和弹出按钮通过一次性 cuiload 命令在 AutoCAD 2017 中成功加载。

经验教训:当您可以简单地手动完成时,不要尝试通过复杂的编程代码来完成它。