Inventor 2010 Ribbons API Beispiel

Mo, 24.08.2009 - 19:25 -- Daniel Espendiller

Ab Inventor 2010 war es endlich soweit, dass auch dieses tolle Programm die Ribbon Oberfläche ähnlich des Office Paketes bekam. Somit wurde auch die Inventor API geändert. Es stehen Ribbons für die unterschiedlichen Dokumenttypen zur Verfügung:

  • ZeroDoc direkt nach dem Start, ohne geöffnete Datei
  • Part
  • Assembly
  • Drawing bei einer geöffneten Zeichnung (IDW)
  • Presentation
  • iFeatures
  • UnknownDocument

Die unterschiedlichen Ribbons werden auch genau über diese Namen über die API angesteurt. Neue Ribbons können logischerweise nicht angelegt werden (Wer braucht schon neue Dokumenttypen?). Die genannten können jedoch um weitere Tabs (oder auch Reiter genannt die Dinger oben) oder Buttons erweitert werden.
Organisiert werden die Buttons in den unterschiedlichen Ribbons in Panels. Diese können mit unterschiedlichen Buttontypen befüllt werden.
Genauer wird die Ribbon Oberfläche im Autodesk Developer Center beschrieben. Dort gibt es eine Datei Names inventor_2010_new_api.zip. (Siehe Verweise)

Beispiel

Ich habe ein kleines Beispiel über die Ansteuerung der Buttons geschrieben, welches alle wichtigen Befehle beinhaltet. Anlegen eines neue Tabs in einem bestimmten Ribbon (hier Drawing, also wird der Tab bei einer geöffneten IDW angezeigt), anlegen eines Panels und Erstellung unterschiedlicher Buttontypen.
Um das VB.NET 2003 Projekt zu nutzen, müssen die Verweise vorhanden sein. Dazu müssen die SDK Tools, befinden sich im Inventor Programmordner, installiert werden.

Folgende Buttons stehen beispielhaft zur Verfügung:

  • Erstellung einer PDF über das neue Inventor Translator-Addin (ab Inventor 2009). Es ist somit kein PDFCreator oder anderes Tool nötig, um eine PDF zu erstellen. Die PDF enthält übrigens auch die unterschiedliche Ebenen. Die Datei wird im gleichen Ordner wie die geöffnete Datei abgelegt.
  • Eine Form, die alle sämtliche Objekte der Ribbon Oberfläche ausliest und ausgiebt
  • und ein paar Buttons zum Spielen ohne weitere nennenwerte Funktion

StandardAddInServer.vb

        ' Some example Buttons
        Dim WithEvents m_button1Def As ButtonDefinition
        Dim WithEvents m_button2Def As ButtonDefinition
        Dim WithEvents m_button3Def As ButtonDefinition
 
        '[...]
 
        Public Sub Activate(ByVal addInSiteObject As Inventor.ApplicationAddInSite, ByVal firstTime As Boolean) Implements Inventor.ApplicationAddInServer.Activate
 
            ' This method is called by Inventor when it loads the AddIn.
            ' The AddInSiteObject provides access to the Inventor Application object.
            ' The FirstTime flag indicates if the AddIn is loaded for the first time.
 
            ' Initialize AddIn members.
            m_inventorApplication = addInSiteObject.Application
 
            ' TODO:  Add ApplicationAddInServer.Activate implementation.
            ' e.g. event initialization, command creation etc.
 
            ' we need the code of our addin for buttons
            Dim AddInCLSID As GuidAttribute = CType(System.Attribute.GetCustomAttribute(GetType(StandardAddInServer), GetType(GuidAttribute)), GuidAttribute)
            Dim AddInCLSIDString As String = "{" & AddInCLSID.Value & "}"
 
            ' Create the control definitions.  This is the same for ribbon and classic. (not really Autodesk some method are missing!)
            Dim controlDefs As ControlDefinitions = m_inventorApplication.CommandManager.ControlDefinitions
            m_button1Def = controlDefs.AddButtonDefinition("Print PDF", "UIRibbonSampleOne", CommandTypesEnum.kFilePropertyEditCmdType)
            m_button2Def = controlDefs.AddButtonDefinition("Show All Ribbons internals", "UIRibbonSampleTwo", CommandTypesEnum.kFilePropertyEditCmdType)
 
            ' the same with a gif image as icon
            ' Create the control definitions for button with icon
            Dim StdIconIPictureDisp As Object = System.Type.Missing
            StdIconIPictureDisp = Microsoft.VisualBasic.Compatibility.VB6.Support.IconToIPicture(System.Drawing.Icon.FromHandle(My.Resources.defaultprinter.GetHicon()))
            m_button3Def = controlDefs.AddButtonDefinition("Show Hello", "UIRibbonSampleMessage", CommandTypesEnum.kFilePropertyEditCmdType, Nothing, Nothing, Nothing, StdIconIPictureDisp, StdIconIPictureDisp)
 
            ' Example for using addhandler as event; if you need dynamic creation of buttons
            ' the handler will return the ButtonDefinition of the pressed button. So you can use
            ' InternalName,DisplayName and all the other stuff
            Dim k As New RibbonButton(controlDefs.AddButtonDefinition("Show asdasdHello", "UIRibbonSampleMasdasdasdessage", CommandTypesEnum.kFilePropertyEditCmdType))
            AddHandler k.pressed, AddressOf Me.ButtonEvent
 
            ' Check if ribbon interface und classic interface is activated
            Dim uiManager As Inventor.UserInterfaceManager = m_inventorApplication.UserInterfaceManager
            If uiManager.InterfaceStyle = Inventor.InterfaceStyleEnum.kRibbonInterface Then
                ' Get the assembly ribbon and the "Drawing" tab.
 
                'other available ribbons
                'ZeroDoc, Part, Assembly, Drawing, Presentation, iFeatures, UnknownDocument
 
                Dim assemblyRibbon As Inventor.Ribbon = uiManager.Ribbons.Item("Drawing")
 
                'create or get ribbontab
                Dim assemblyTab As Inventor.RibbonTab = AddRibbonTab(assemblyRibbon, "my_test", "my_test", AddInCLSIDString)
 
                'create or get panel
                Dim panel As Inventor.RibbonPanel = AddRibbonPanel(assemblyTab, "Ribbon UI Demo", "RibbonUIDemoPanel", "{3ff095f0-8eff-4cd0-b6a3-ac06556c3777}")
 
                ' Add the buttons to the tab with the first one being large and the rest small.
                panel.CommandControls.AddButton(m_button1Def, True)
                panel.CommandControls.AddButton(m_button2Def)
                panel.CommandControls.AddButton(m_button3Def)
                panel.CommandControls.AddButton(k.ButtonDefinition)
 
            Else
                MsgBox("no ribbon interface")
                ' Get the command bar used for the assembly panel in the “Classic” user interface.
                Dim assemblyBar As Inventor.CommandBar = uiManager.CommandBars.Item("AMxAssemblyPanelCmdBar")
                ' Add the buttons to the command bar.
                assemblyBar.Controls.AddButton(m_button1Def)
                assemblyBar.Controls.AddButton(m_button2Def)
            End If
 
        End Sub
        ''' <summary>
        ''' Example EventHandler for dynamic Inventor Buttons; only in Inventor 2010
        ''' Generate it with Class RibbonButton
        ''' </summary>
        ''' <param name="pressedButton">ButtonDefinition of the pressed Button</param>
        ''' <remarks></remarks>
        Sub ButtonEvent(ByVal pressedButton As ButtonDefinition)
            MsgBox(pressedButton.InternalName)
        End Sub
 
        '[...]
 
        Private Sub m_button1Def_OnExecute(ByVal Context As Inventor.NameValueMap) Handles m_button1Def.OnExecute
            If m_inventorApplication.ActiveDocument.FullFileName.Length = 0 Then
                MsgBox("no filename found; save it first?")
                Exit Sub
            End If
 
            SaveAsPDF(m_inventorApplication, m_inventorApplication.ActiveDocument.FullFileName & ".pdf")
            MsgBox(m_inventorApplication, m_inventorApplication.ActiveDocument.FullFileName & ".pdf")
        End Sub
 
        Private Sub m_button2Def_OnExecute(ByVal Context As Inventor.NameValueMap) Handles m_button2Def.OnExecute
            PrintRibbon(m_inventorApplication)
        End Sub
 
        Private Sub m_button3Def_OnExecute(ByVal Context As Inventor.NameValueMap) Handles m_button3Def.OnExecute
            MsgBox("Hello world")
        End Sub

other functions

    ''' <summary>
    ''' Add a RibbonTab to Inventor Ribbon interface and return the ribbon
    ''' if exists it return only the ribbon tab
    ''' 
    ''' http://discussion.autodesk.com/forums/thread.jspa?messageID=6225731
    ''' </summary>
    ''' <param name="objEnvironment"></param>
    ''' <param name="strDisplayName">DisplayName of the Ribbon</param>
    ''' <param name="strInternalName">InternalName must be unique</param>
    ''' <param name="strCLSID">a Guid String; should be Addin Guid</param>
    ''' <returns>RibbonTag object</returns>
    ''' <remarks></remarks>
    Function AddRibbonTab(ByVal objEnvironment As Inventor.Ribbon, ByVal strDisplayName As String, ByVal strInternalName As String, ByVal strCLSID As String) As RibbonTab
        Dim objRibbonTab As RibbonTab = Nothing
        Try
            Try
                objRibbonTab = objEnvironment.RibbonTabs.Add(strDisplayName, strInternalName, strCLSID)
            Catch ex As Exception
                objRibbonTab = objEnvironment.RibbonTabs.Item(strInternalName)
            End Try
        Catch ex As Exception
            'Debug.WriteLine(ex)
            MsgBox(ex.Message)
        End Try
        Return objRibbonTab
    End Function
    ''' <summary>
    ''' Add a RibbonPannel to a InventorRibbonTab and return the tab
    ''' if exists it return only the tab
    ''' 
    ''' http://discussion.autodesk.com/forums/thread.jspa?messageID=6225731
    ''' </summary>
    ''' <param name="objRibbonTab">RibbonTab the Panel should be shown on</param>
    ''' <param name="strDisplayName">DisplayName of the Tab</param>
    ''' <param name="strInternalName">InternalName must be unique</param>
    ''' <param name="strCLSID">a Guid String; should be Addin Guid</param>
    ''' <returns>RibbonPanel object</returns>
    ''' <remarks></remarks>
    Function AddRibbonPanel(ByVal objRibbonTab As RibbonTab, ByVal strDisplayName As String, ByVal strInternalName As String, ByVal strCLSID As String) As RibbonPanel
        Dim objRibbonPanel As RibbonPanel = Nothing
        Try
            Try
                objRibbonPanel = objRibbonTab.RibbonPanels.Add(strDisplayName, strInternalName, strCLSID)
            Catch ex As Exception
                objRibbonPanel = objRibbonTab.RibbonPanels.Item(strInternalName)
            End Try
        Catch ex As Exception
            'Debug.WriteLine(ex)
            MsgBox(ex.Message)
        End Try
        Return objRibbonPanel
    End Function
    ''' <summary>
    ''' Use the Inventor translator Add-In to generate a PDF without any further tools
    ''' 
    ''' http://modthemachine.typepad.com/my_weblog/2009/01/translating-files-with-the-api.html
    ''' </summary>
    ''' <param name="ThisApplication">Inventor Application object; a Drawing must be current Document!</param>
    ''' <param name="OutFile">FullName with complete Path for PDF-File</param>
    ''' <remarks></remarks>
    Public Sub SaveAsPDF(ByVal ThisApplication As Inventor.Application, ByVal OutFile As String)
        ' Get the PDF translator Add-In. 
        Dim oPDFTrans As TranslatorAddIn
        oPDFTrans = ThisApplication.ApplicationAddIns.ItemById( _
                                "{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
        If oPDFTrans Is Nothing Then
            MsgBox("Could not access PDF translator.")
            Exit Sub
        End If
 
        ' Create some objects that are used to pass information to  the translator Add-In.  
        Dim oContext As TranslationContext
        oContext = ThisApplication.TransientObjects.CreateTranslationContext
        Dim oOptions As NameValueMap
        oOptions = ThisApplication.TransientObjects.CreateNameValueMap
        If oPDFTrans.HasSaveCopyAsOptions(ThisApplication.ActiveDocument, _
                                                  oContext, oOptions) Then
            ' Set to print all sheets.  This can also have the value 
            ' kPrintCurrentSheet or kPrintSheetRange. If kPrintSheetRange 
            ' is used then you must also use the CustomBeginSheet and 
            ' Custom_End_Sheet to define the sheet range. 
            oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
 
            ' Other possible options... 
            'oOptions.Value("Custom_Begin_Sheet") = 1 
            'oOptions.Value("Custom_End_Sheet") = 5 
            oOptions.Value("All_Color_AS_Black") = True
            'oOptions.Value("Remove_Line_Weights") = True 
            'oOptions.Value("Vector_Resolution") = 200
 
            ' Define various settings and input to provide the translator. 
            oContext.Type = Inventor.IOMechanismEnum.kFileBrowseIOMechanism
            Dim oData As DataMedium
            oData = ThisApplication.TransientObjects.CreateDataMedium
            oData.FileName = OutFile
 
            ' Call the translator. 
            Call oPDFTrans.SaveCopyAs(ThisApplication.ActiveDocument, _
                                              oContext, oOptions, oData)
        End If
    End Sub
    ''' <summary>
    ''' Opens a Form and print all Ribbon/Tabs/Buttons Information
    ''' with InternalName,Visibility,... of the Current Inventor Window
    ''' 
    ''' found in inventor_2010_new_api.zip / Inventor 2010 New API.pptx
    ''' </summary>
    ''' <param name="m_inventorApplication">Inventor Application object</param>
    ''' <remarks></remarks>
 
    Public Sub PrintRibbon(ByVal m_inventorApplication As Inventor.Application)
        Dim txt As String = ""
 
        Dim oControl As CommandControl
        Dim oRibbon As Ribbon
        For Each oRibbon In m_inventorApplication.UserInterfaceManager.Ribbons
            txt &= "Int:" & oRibbon.InternalName & vbNewLine
 
            Dim oTab As RibbonTab
            For Each oTab In oRibbon.RibbonTabs
                txt &= "    Tab: " & oTab.DisplayName & ", " & oTab.InternalName & ", Visible: " & oTab.Visible & vbNewLine
 
                Dim oPanel As RibbonPanel
                For Each oPanel In oTab.RibbonPanels
                    txt &= "       Panel: " & oPanel.DisplayName & ", " & oPanel.InternalName & ", Visible: " & oPanel.Visible & vbNewLine
 
                    For Each oControl In oPanel.CommandControls
                        txt &= "           Control: " & oControl.DisplayName & ", " & oControl.InternalName & ", Visible: " & oControl.Visible & vbNewLine
                    Next
                Next
            Next
        Next
        Dim j As New Form1
        j.SetText(txt)
        j.Show()
 
    End Sub

Disqus - noscript

Super, Danke!