We can add ButtonDefinition from a macro, but to make the button definition work in VBA from clicking a button that would introduce some issue to you, like the VBA may be held because you need to monitor the button click so you can't run any other VBA macros, and users may stop the command carelessly. I recommend to just create an addin to do the export DXF job instead of macro/iLogic rule if you want to place the button onto the File Browser menu. But I can show you how to create the button from VBA&iLogic code here, you can make your decision:
1. Copy the below VBA code to your "Modul1" module in ApplicationProject.
Sub CreateExportDXFCommand()
Dim oFileButtons As Inventor.CommandControls
Set oFileButtons = ThisApplication.UserInterfaceManager.FileBrowserControls
Dim oExportButton As Inventor.CommandControl
Set oExportButton = oFileButtons.Item("Export")
Dim oExportDXFDef As Inventor.ButtonDefinition
On Error Resume Next
Set oExportDXFDef = ThisApplication.CommandManager.ControlDefinitions("CustomAutoExportDXFCmd")
If Err Then
Err.Clear
On Error GoTo 0
Set oExportDXFDef = ThisApplication.CommandManager.ControlDefinitions.AddButtonDefinition("Auto Export to DXF", "CustomAutoExportDXFCmd", kFileOperationsCmdType, "Custom commands", "Auto Export active part/assembly document to DXF", "Using this command to auto export your current document to DXF")
End If
oExportDXFDef.ProgressiveToolTip.Title = "DXF Auto-Export"
oExportDXFDef.ProgressiveToolTip.Description = "Exports DXFs for the active Assembly (or a single active Part) to the Project folder."
Dim oButton As Inventor.CommandControl
Dim oCheckButton As CommandControl
For Each oCheckButton In oExportButton.ChildControls
If oCheckButton.ControlDefinition Is oExportDXFDef Then
Set oButton = oCheckButton
Exit For
End If
Next
'~~~~~Create button if missing~~~~~
If oButton Is Nothing Then
Set oButton = oExportButton.ChildControls.AddButton(oExportDXFDef, True, True, "AppExportDwgCmd", False)
Dim ocls As New Class1
ocls.init
Else
'Un-comment the following line for testing. Running this rule will then toggle the button on and off.
oButton.Delete
End If
End Sub
2. Copy below VBA code to your "Class1" class module in ApplicationProject.
Private WithEvents oButtonDef As ButtonDefinition
Public Sub init()
Set oButtonDef = ThisApplication.CommandManager.ControlDefinitions("CustomAutoExportDXFCmd")
Do
ThisApplication.UserInterfaceManager.DoEvents
Loop Until bStop
End Sub
Private Sub oButtonDef_OnExecute(ByVal Context As NameValueMap)
' here add your code to export the active document to DXF
MsgBox "I am triggered."
End Sub
3. Add below iLogic code to an iLogic rule, and you can run the rule.
Sub Main()
Dim m_inventorApp As Inventor.Application
m_inventorApp = ThisApplication
On Error Resume Next
' get the application VBA project
Dim oVBAProj As InventorVBAProject
Dim oAppVBAProj As InventorVBAProject
For Each oVBAProj In m_inventorApp.VBAProjects
If oVBAProj.ProjectType = VBAProjectTypeEnum.kApplicationVBAProject Then
oAppVBAProj = oVBAProj
Exit For
End If
Next
' get the VBA module
Dim oVBAComp As InventorVBAComponent
oVBAComp = oVBAProj.InventorVBAComponents("Module1")
Dim oVBASub As InventorVBAMember
oVBASub = oVBAComp.InventorVBAMembers("CreateExportDXFCommand")
oVBASub.Execute()
End Sub
4. Now from the File Browser menu, you can find the button like below, click it a message is popped up.
Hope this helps.
If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.
Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.