|
| 1 | +import adsk.core |
| 2 | +import adsk.fusion |
| 3 | +import traceback |
| 4 | + |
| 5 | +from .fusion_api import Handlers |
| 6 | +from .fusion_api.Handlers import handlers |
| 7 | + |
| 8 | + |
| 9 | +def create_tab(workspace, tab_name): |
| 10 | + # Get all the tabs for the workspace: |
| 11 | + allTabs = workspace.toolbarTabs |
| 12 | + tabId = tab_name + 'TabId' |
| 13 | + |
| 14 | + # check if tab exists |
| 15 | + newTab = allTabs.itemById(tabId) |
| 16 | + |
| 17 | + if not newTab: |
| 18 | + # Add a new tab to the workspace: |
| 19 | + newTab = allTabs.add(tabId, tab_name) |
| 20 | + return newTab |
| 21 | + |
| 22 | + |
| 23 | +def create_panel(workspace, tab, panel_name): |
| 24 | + # Get all of the toolbar panels for the NewCam tab: |
| 25 | + allTabPanels = tab.toolbarPanels |
| 26 | + |
| 27 | + # Activate the Cam Workspace before activating the newly added Tab |
| 28 | + workspace.activate() |
| 29 | + |
| 30 | + panel = None |
| 31 | + panel = allTabPanels.itemById(panel_name + 'PanelId') |
| 32 | + if panel is None: |
| 33 | + # Add setup panel |
| 34 | + panel = allTabPanels.add(panel_name + 'PanelId', panel_name) |
| 35 | + return panel |
| 36 | + |
| 37 | + |
| 38 | +def create_button(workspace, tab, panel, button_name, CreatedEventHandler, tooltip=None, resources=''): |
| 39 | + # We want this panel to be visible: |
| 40 | + workspace.activate() |
| 41 | + panel.isVisible = True |
| 42 | + |
| 43 | + app = adsk.core.Application.get() |
| 44 | + ui = app.userInterface |
| 45 | + cmdDefinitions = ui.commandDefinitions |
| 46 | + |
| 47 | + # Check if Setup Button exists |
| 48 | + buttonId = button_name.replace(' ', '') + 'Id' |
| 49 | + button = cmdDefinitions.itemById(buttonId) |
| 50 | + if not tooltip: |
| 51 | + tooltip = button_name |
| 52 | + |
| 53 | + if not button: |
| 54 | + # Create a button command definition. |
| 55 | + button = cmdDefinitions.addButtonDefinition( |
| 56 | + buttonId, button_name, tooltip, resources) |
| 57 | + |
| 58 | + # Connect to the command created event. |
| 59 | + newcommandCreated = CreatedEventHandler() |
| 60 | + button.commandCreated.add(newcommandCreated) |
| 61 | + handlers.append(newcommandCreated) |
| 62 | + |
| 63 | + # Add setup button to ASMBL setup panel |
| 64 | + panelControls = panel.controls |
| 65 | + buttonControl = panelControls.itemById(buttonId) |
| 66 | + if not buttonControl: |
| 67 | + buttonControl = panelControls.addCommand(button) |
| 68 | + return buttonControl |
| 69 | + |
| 70 | + |
| 71 | +def remove_pannel(tab, panel_name): |
| 72 | + # Get all the tabs for the workspace: |
| 73 | + panelId = panel_name + 'PanelId' |
| 74 | + |
| 75 | + # check if tab exists |
| 76 | + panel = tab.toolbarPanels.itemById(panelId) |
| 77 | + |
| 78 | + # Remove the controls we added to our panel |
| 79 | + for control in panel.controls: |
| 80 | + if control.isValid: |
| 81 | + control.deleteMe() |
| 82 | + |
| 83 | + # Remove our panel |
| 84 | + panel.deleteMe() |
| 85 | + |
| 86 | + |
| 87 | +def run(context): |
| 88 | + ui = None |
| 89 | + try: |
| 90 | + app = adsk.core.Application.get() |
| 91 | + ui = app.userInterface |
| 92 | + |
| 93 | + cmdDefinitions = ui.commandDefinitions |
| 94 | + |
| 95 | + # Get all workspaces: |
| 96 | + allWorkspaces = ui.workspaces |
| 97 | + |
| 98 | + # Get the CAM workspace: |
| 99 | + camWorkspace = allWorkspaces.itemById('CAMEnvironment') |
| 100 | + |
| 101 | + AsmblTab = create_tab(camWorkspace, 'Asmbl') |
| 102 | + # asmblSetupPanel = create_panel(camWorkspace, AsmblTab, 'Setup') |
| 103 | + |
| 104 | + asmblActionsPanel = create_panel(camWorkspace, AsmblTab, 'Actions') |
| 105 | + asmblPostProcessControl = create_button(camWorkspace, AsmblTab, asmblActionsPanel, |
| 106 | + 'Post Process', Handlers.PostProcessCreatedEventHandler, |
| 107 | + tooltip='Generate combined gcode file for ASMBL', |
| 108 | + resources='./resources/PostProcess') |
| 109 | + asmblPostProcessControl.isPromotedByDefault = True |
| 110 | + asmblPostProcessControl.isPromoted = True |
| 111 | + asmblPostProcessControl.commandDefinition.tooltipDescription = '\ |
| 112 | + <br>Requires an FFF setup and a milling setup</br>\ |
| 113 | + <br>Will not work if there are any more/fewer setups</br>' |
| 114 | + asmblPostProcessControl.commandDefinition.toolClipFilename = 'resources/GenerateAsmbl/tooltip.png' |
| 115 | + |
| 116 | + |
| 117 | + pass |
| 118 | + |
| 119 | + except: |
| 120 | + if ui: |
| 121 | + ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) |
| 122 | + |
| 123 | + |
| 124 | +# When the addin stops we need to clean up the ui |
| 125 | +def stop(context): |
| 126 | + app = adsk.core.Application.get() |
| 127 | + ui = app.userInterface |
| 128 | + |
| 129 | + try: |
| 130 | + # Get all workspaces: |
| 131 | + allWorkspaces = ui.workspaces |
| 132 | + |
| 133 | + # Get the CAM workspace: |
| 134 | + camWorkspace = allWorkspaces.itemById('CAMEnvironment') |
| 135 | + |
| 136 | + allTabs = camWorkspace.toolbarTabs |
| 137 | + |
| 138 | + # check if tab exists |
| 139 | + tab = allTabs.itemById('AsmblTabId') |
| 140 | + |
| 141 | + # remove_pannel(tab, 'Setup') |
| 142 | + remove_pannel(tab, 'Actions') |
| 143 | + |
| 144 | + # Remove our render tab from the UI |
| 145 | + if tab.isValid: |
| 146 | + tab.deleteMe() |
| 147 | + except: |
| 148 | + if ui: |
| 149 | + ui.messageBox('Failed:\n{}'.format(traceback.format_exc())) |
0 commit comments