Skip to content

Commit

Permalink
feat: persist command panel status in a config on hard drive
Browse files Browse the repository at this point in the history
  • Loading branch information
Le0Michine committed Feb 19, 2023
1 parent 4d3b215 commit 0fa92f4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__pycache__
/.vscode
.env
.env
commandConfig
18 changes: 12 additions & 6 deletions commands/commandCreateBaseplate/entry.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import adsk.core, adsk.fusion, traceback
import os


from ...lib import configUtils
from ...lib import fusion360utils as futil
from ... import config
from ...lib.gridfinityUtils.const import BASE_TOTAL_HEIGHT, BIN_CORNER_FILLET_RADIUS, BIN_XY_TOLERANCE, DIMENSION_DEFAULT_WIDTH_UNIT
Expand Down Expand Up @@ -32,13 +32,17 @@
# Resource location for command icons, here we assume a sub folder in this directory named "resources".
ICON_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'resources', '')

CONFIG_FOLDER_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'commandConfig')

# Local list of event handlers used to maintain a reference so
# they are not released and garbage collected.
local_handlers = []


# Executed when add-in is run.
def start():
addinConfig = configUtils.readConfig(CONFIG_FOLDER_PATH)

# Create a command Definition.
cmd_def = ui.commandDefinitions.addButtonDefinition(CMD_ID, CMD_NAME, CMD_Description, ICON_FOLDER)

Expand All @@ -56,17 +60,22 @@ def start():
control = panel.controls.addCommand(cmd_def, COMMAND_BESIDE_ID, False)

# Specify if the command is promoted to the main toolbar.
control.isPromoted = IS_PROMOTED
control.isPromoted = addinConfig['UI'].getboolean('is_promoted')
# control.isPromoted = IS_PROMOTED


# Executed when add-in is stopped.
def stop():
# Get the various UI elements for this command
workspace = ui.workspaces.itemById(WORKSPACE_ID)
panel = workspace.toolbarPanels.itemById(PANEL_ID)
command_control = panel.controls.itemById(CMD_ID)
command_control: adsk.core.CommandControl = panel.controls.itemById(CMD_ID)
command_definition = ui.commandDefinitions.itemById(CMD_ID)

addinConfig = configUtils.readConfig(CONFIG_FOLDER_PATH)
addinConfig['UI']['is_promoted'] = 'yes' if command_control.isPromoted else 'no'
configUtils.writeConfig(addinConfig, CONFIG_FOLDER_PATH)

# Delete the button command control
if command_control:
command_control.deleteMe()
Expand Down Expand Up @@ -169,7 +178,6 @@ def command_execute(args: adsk.core.CommandEventArgs):
gridfinityBaseplateComponent,
)


# cut everything
toolBodies = adsk.core.ObjectCollection.create()
toolBodies.add(baseBody)
Expand All @@ -181,8 +189,6 @@ def command_execute(args: adsk.core.CommandEventArgs):
combineFeatures.add(combineFeatureInput)
gridfinityBaseplateComponent.bRepBodies.item(0).name = binName



except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Expand Down
20 changes: 17 additions & 3 deletions commands/commandCreateBin/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@




from ...lib import configUtils
from ...lib import fusion360utils as futil
from ... import config
from ...lib.gridfinityUtils.const import BIN_LIP_WALL_THICKNESS, BIN_WALL_THICKNESS, BIN_XY_TOLERANCE, DEFAULT_FILTER_TOLERANCE, DIMENSION_DEFAULT_HEIGHT_UNIT, DIMENSION_DEFAULT_WIDTH_UNIT
Expand Down Expand Up @@ -38,6 +40,8 @@
# Resource location for command icons, here we assume a sub folder in this directory named "resources".
ICON_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'resources', '')

CONFIG_FOLDER_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'commandConfig')

# Local list of event handlers used to maintain a reference so
# they are not released and garbage collected.
local_handlers = []
Expand All @@ -62,6 +66,8 @@

# Executed when add-in is run.
def start():
addinConfig = configUtils.readConfig(CONFIG_FOLDER_PATH)

# Create a command Definition.
cmd_def = ui.commandDefinitions.addButtonDefinition(CMD_ID, CMD_NAME, CMD_Description, ICON_FOLDER)

Expand All @@ -78,18 +84,26 @@ def start():
# Create the button command control in the UI after the specified existing command.
control = panel.controls.addCommand(cmd_def, COMMAND_BESIDE_ID, False)

# Specify if the command is promoted to the main toolbar.
control.isPromoted = IS_PROMOTED
# Specify if the command is promoted to the main toolbar.
control.isPromoted = addinConfig['UI'].getboolean('is_promoted')
# control.isPromoted = IS_PROMOTED




# Executed when add-in is stopped.
def stop():
# Get the various UI elements for this command
workspace = ui.workspaces.itemById(WORKSPACE_ID)
panel = workspace.toolbarPanels.itemById(PANEL_ID)
command_control = panel.controls.itemById(CMD_ID)
command_control: adsk.core.CommandControl = panel.controls.itemById(CMD_ID)
command_definition = ui.commandDefinitions.itemById(CMD_ID)

addinConfig = configUtils.readConfig(CONFIG_FOLDER_PATH)
addinConfig['UI']['is_promoted'] = 'yes' if command_control.isPromoted else 'no'
configUtils.writeConfig(addinConfig, CONFIG_FOLDER_PATH)


# Delete the button command control
if command_control:
command_control.deleteMe()
Expand Down
32 changes: 32 additions & 0 deletions lib/configUtils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import configparser
import os

CONFIG_FILE_NAME = 'config.ini'

def getDefaultConfig():
config = configparser.ConfigParser()
config['UI'] = {'IS_PROMOTED': 'yes'}
return config

def readConfig(path: str):
CONFIG_FILE_PATH = os.path.join(path, CONFIG_FILE_NAME)
config = configparser.ConfigParser()
try:
if os.path.exists(CONFIG_FILE_PATH):
config.read(CONFIG_FILE_PATH)
return config
return getDefaultConfig()
except:
return getDefaultConfig()

def writeConfig(config: configparser.ConfigParser, path: str):
try:
CONFIG_FILE_PATH = os.path.join(path, CONFIG_FILE_NAME)
if not os.path.exists(path):
os.mkdir(path)
with open(CONFIG_FILE_PATH, 'w') as configfile:
config.write(configfile)
return True
except:
return False

0 comments on commit 0fa92f4

Please sign in to comment.