Here is my full Add-In code for reference. Please note to create Add-In instead of Script.
I have a model with user parameter which I could increase and decrease by 5 mm. Then if I change HeightQ paramter manually, then I have button to refresh certain sketch with text to have the dimension in it. For example parameter value is 15 mm, then sketch will have text in it "15 mm".
import os
import adsk.core, adsk.fusion
app = adsk.core.Application.get()
ui = app.userInterface
handlers = []
# Funktiot, joilla kasvatetaan ja pienennetään parametriarvoa ja päivitetään teksti
def change_height_param(increment):
try:
design = app.activeProduct
if not design:
ui.messageBox('Ei aktiivista suunnittelua')
return
userParams = design.userParameters
heightParam = userParams.itemByName('HeightQ')
if heightParam:
new_value = heightParam.value + (increment / 10) # Muutos senttimetreissä
heightParam.value = new_value
update_text_with_height_param() # Päivitetään luonnoksen teksti
else:
ui.messageBox('Parametri HeightQ ei löytynyt')
except Exception as e:
ui.messageBox(f'Virhe parametrin muuttamisessa: {str(e)}')
# Tekstin päivitystoiminto (sama kuin ennen)
def update_text_with_height_param():
try:
design = app.activeProduct
if not design:
ui.messageBox('Ei aktiivista suunnittelua')
return
rootComp = design.rootComponent
if not rootComp:
ui.messageBox('Ei komponenttia saatavilla')
return
sketches = rootComp.sketches
for sketch in sketches:
if sketch.name == 'HeightText':
userParams = design.userParameters
heightParam = userParams.itemByName('HeightQ')
if heightParam:
heightValue = int(heightParam.value * 10) # Muunna senttimetreistä millimetreiksi
sketchTexts = sketch.sketchTexts
if sketchTexts.count > 0:
textElement = sketchTexts.item(0)
textElement.text = str(heightValue) + ' mm'
else:
ui.messageBox('Ei tekstielementtejä luonnoksessa')
else:
ui.messageBox('Parametri HeightQ ei löytynyt')
except Exception as e:
ui.messageBox(f'Virhe tekstin päivityksessä: {str(e)}')
# Käsittelijät eri komennoille ilman ID-viittauksia
class IncreaseCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
command = eventArgs.command
onExecute = IncreaseCommandExecuteHandler()
command.execute.add(onExecute)
handlers.append(onExecute)
except Exception as e:
ui.messageBox(f'Virhe Increase-komennon luonnissa: {str(e)}')
class IncreaseCommandExecuteHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
change_height_param(5) # Kasvatetaan 5 mm
except Exception as e:
ui.messageBox(f'Virhe Increase-komennossa: {str(e)}')
class DecreaseCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
command = eventArgs.command
onExecute = DecreaseCommandExecuteHandler()
command.execute.add(onExecute)
handlers.append(onExecute)
except Exception as e:
ui.messageBox(f'Virhe Decrease-komennon luonnissa: {str(e)}')
class DecreaseCommandExecuteHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
change_height_param(-5) # Pienennetään 5 mm
except Exception as e:
ui.messageBox(f'Virhe Decrease-komennossa: {str(e)}')
class RefreshCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
command = eventArgs.command
onExecute = RefreshCommandExecuteHandler()
command.execute.add(onExecute)
handlers.append(onExecute)
except Exception as e:
ui.messageBox(f'Virhe Refresh-komennon luonnissa: {str(e)}')
class RefreshCommandExecuteHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
update_text_with_height_param() # Päivitä tekstielementti luonnoksessa
except Exception as e:
ui.messageBox(f'Virhe Refresh-komennossa: {str(e)}')
# Komennon luonti
def create_adjustment_buttons():
try:
# Selvitä Add-Inin hakemisto ja muodosta oikea polku kuvakkeille
addin_path = os.path.dirname(os.path.realpath(__file__))
resources_path = os.path.join(addin_path, 'Resources')
# Lisää Increase-nappula
increase_button = ui.commandDefinitions.itemById('increaseHeightCommand')
if not increase_button:
increase_button = ui.commandDefinitions.addButtonDefinition(
'increaseHeightCommand', 'Increase Height', 'Kasvattaa Height-parametrin arvoa 5 mm',
os.path.join(resources_path, 'Plus')
)
# Lisää Decrease-nappula
decrease_button = ui.commandDefinitions.itemById('decreaseHeightCommand')
if not decrease_button:
decrease_button = ui.commandDefinitions.addButtonDefinition(
'decreaseHeightCommand', 'Decrease Height', 'Pienentää Height-parametrin arvoa 5 mm',
os.path.join(resources_path, 'Minus')
)
# Lisää Refresh-nappula
refresh_button = ui.commandDefinitions.itemById('refreshHeightCommand')
if not refresh_button:
refresh_button = ui.commandDefinitions.addButtonDefinition(
'refreshHeightCommand', 'Refresh', 'Päivitä tekstielementti parametrin perusteella',
os.path.join(resources_path, 'Refresh')
)
# Rekisteröi Increase-komento
onIncreaseCreated = IncreaseCommandCreatedHandler()
increase_button.commandCreated.add(onIncreaseCreated)
handlers.append(onIncreaseCreated)
# Rekisteröi Decrease-komento
onDecreaseCreated = DecreaseCommandCreatedHandler()
decrease_button.commandCreated.add(onDecreaseCreated)
handlers.append(onDecreaseCreated)
# Rekisteröi Refresh-komento
onRefreshCreated = RefreshCommandCreatedHandler()
refresh_button.commandCreated.add(onRefreshCreated)
handlers.append(onRefreshCreated)
# Lisää nappulat Modify-paneeliin ja promoted-tilaan
workspace = ui.workspaces.itemById('FusionSolidEnvironment')
toolbar_tab = workspace.toolbarTabs.itemById('SolidTab')
modify_panel = toolbar_tab.toolbarPanels.itemById('SolidModifyPanel')
if modify_panel:
increase_control = modify_panel.controls.itemById('increaseHeightCommand')
if not increase_control:
increase_control = modify_panel.controls.addCommand(increase_button)
increase_control.isPromoted = True
decrease_control = modify_panel.controls.itemById('decreaseHeightCommand')
if not decrease_control:
decrease_control = modify_panel.controls.addCommand(decrease_button)
decrease_control.isPromoted = True
refresh_control = modify_panel.controls.itemById('refreshHeightCommand')
if not refresh_control:
refresh_control = modify_panel.controls.addCommand(refresh_button)
refresh_control.isPromoted = True
except Exception as e:
ui.messageBox(f'Virhe komennon luomisessa: {str(e)}')
# Komennon käynnistys
def run(context):
try:
create_adjustment_buttons() # Luo Increase, Decrease ja Refresh -nappulat käyttöliittymään
except Exception as e:
ui.messageBox(f'Virhe run-funktiossa: {str(e)}')
# Komennon lopetus
def stop(context):
try:
# Poista kaikki komennot ja tapahtumakäsittelijät
cmd_definitions = ui.commandDefinitions
# Poista Increase-nappula
increase_button = cmd_definitions.itemById('increaseHeightCommand')
if increase_button:
increase_button.deleteMe()
# Poista Decrease-nappula
decrease_button = cmd_definitions.itemById('decreaseHeightCommand')
if decrease_button:
decrease_button.deleteMe()
# Poista Refresh-nappula
refresh_button = cmd_definitions.itemById('refreshHeightCommand')
if refresh_button:
refresh_button.deleteMe()
workspace = ui.workspaces.itemById('FusionSolidEnvironment')
toolbar_tab = workspace.toolbarTabs.itemById('SolidTab')
modify_panel = toolbar_tab.toolbarPanels.itemById('SolidModifyPanel')
if modify_panel:
increase_control = modify_panel.controls.itemById('increaseHeightCommand')
if increase_control:
increase_control.deleteMe()
decrease_control = modify_panel.controls.itemById('decreaseHeightCommand')
if decrease_control:
decrease_control.deleteMe()
refresh_control = modify_panel.controls.itemById('refreshHeightCommand')
if refresh_control:
refresh_control.deleteMe()
global handlers
handlers = []
except Exception as e:
ui.messageBox(f'Virhe stop-funktiossa: {str(e)}')
I made this partly by help of ChatGPT.
Screenshot what kind of parameter model I have to update height and text on top surface of it.

To get it all english, here is same code translated by ChatGPT (I cannot validate whether this is working or not, upper one is working for sure).
import os
import adsk.core, adsk.fusion
app = adsk.core.Application.get()
ui = app.userInterface
handlers = []
# Functions to increase and decrease the parameter value and update the text
def change_height_param(increment):
try:
design = app.activeProduct
if not design:
ui.messageBox('No active design')
return
userParams = design.userParameters
heightParam = userParams.itemByName('HeightQ')
if heightParam:
new_value = heightParam.value + (increment / 10) # Change in centimeters
heightParam.value = new_value
update_text_with_height_param() # Update the sketch text
else:
ui.messageBox('Parameter HeightQ not found')
except Exception as e:
ui.messageBox(f'Error changing parameter: {str(e)}')
# Text update function (same as before)
def update_text_with_height_param():
try:
design = app.activeProduct
if not design:
ui.messageBox('No active design')
return
rootComp = design.rootComponent
if not rootComp:
ui.messageBox('No component available')
return
sketches = rootComp.sketches
for sketch in sketches:
if sketch.name == 'HeightText':
userParams = design.userParameters
heightParam = userParams.itemByName('HeightQ')
if heightParam:
heightValue = int(heightParam.value * 10) # Convert from centimeters to millimeters
sketchTexts = sketch.sketchTexts
if sketchTexts.count > 0:
textElement = sketchTexts.item(0)
textElement.text = str(heightValue) + ' mm'
else:
ui.messageBox('No text elements in the sketch')
else:
ui.messageBox('Parameter HeightQ not found')
except Exception as e:
ui.messageBox(f'Error updating text: {str(e)}')
# Handlers for different commands without ID references
class IncreaseCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
command = eventArgs.command
onExecute = IncreaseCommandExecuteHandler()
command.execute.add(onExecute)
handlers.append(onExecute)
except Exception as e:
ui.messageBox(f'Error creating Increase command: {str(e)}')
class IncreaseCommandExecuteHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
change_height_param(5) # Increase by 5 mm
except Exception as e:
ui.messageBox(f'Error executing Increase command: {str(e)}')
class DecreaseCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
command = eventArgs.command
onExecute = DecreaseCommandExecuteHandler()
command.execute.add(onExecute)
handlers.append(onExecute)
except Exception as e:
ui.messageBox(f'Error creating Decrease command: {str(e)}')
class DecreaseCommandExecuteHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
change_height_param(-5) # Decrease by 5 mm
except Exception as e:
ui.messageBox(f'Error executing Decrease command: {str(e)}')
class RefreshCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
command = eventArgs.command
onExecute = RefreshCommandExecuteHandler()
command.execute.add(onExecute)
handlers.append(onExecute)
except Exception as e:
ui.messageBox(f'Error creating Refresh command: {str(e)}')
class RefreshCommandExecuteHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
update_text_with_height_param() # Update the text element in the sketch
except Exception as e:
ui.messageBox(f'Error executing Refresh command: {str(e)}')
# Command creation
def create_adjustment_buttons():
try:
# Determine Add-In directory and form the correct path for icons
addin_path = os.path.dirname(os.path.realpath(__file__))
resources_path = os.path.join(addin_path, 'Resources')
# Add Increase button
increase_button = ui.commandDefinitions.itemById('increaseHeightCommand')
if not increase_button:
increase_button = ui.commandDefinitions.addButtonDefinition(
'increaseHeightCommand', 'Increase Height', 'Increase Height parameter by 5 mm',
os.path.join(resources_path, 'Plus')
)
# Add Decrease button
decrease_button = ui.commandDefinitions.itemById('decreaseHeightCommand')
if not decrease_button:
decrease_button = ui.commandDefinitions.addButtonDefinition(
'decreaseHeightCommand', 'Decrease Height', 'Decrease Height parameter by 5 mm',
os.path.join(resources_path, 'Minus')
)
# Add Refresh button
refresh_button = ui.commandDefinitions.itemById('refreshHeightCommand')
if not refresh_button:
refresh_button = ui.commandDefinitions.addButtonDefinition(
'refreshHeightCommand', 'Refresh', 'Update text element based on parameter',
os.path.join(resources_path, 'Refresh')
)
# Register Increase command
onIncreaseCreated = IncreaseCommandCreatedHandler()
increase_button.commandCreated.add(onIncreaseCreated)
handlers.append(onIncreaseCreated)
# Register Decrease command
onDecreaseCreated = DecreaseCommandCreatedHandler()
decrease_button.commandCreated.add(onDecreaseCreated)
handlers.append(onDecreaseCreated)
# Register Refresh command
onRefreshCreated = RefreshCommandCreatedHandler()
refresh_button.commandCreated.add(onRefreshCreated)
handlers.append(onRefreshCreated)
# Add buttons to the Modify panel and promote them
workspace = ui.workspaces.itemById('FusionSolidEnvironment')
toolbar_tab = workspace.toolbarTabs.itemById('SolidTab')
modify_panel = toolbar_tab.toolbarPanels.itemById('SolidModifyPanel')
if modify_panel:
increase_control = modify_panel.controls.itemById('increaseHeightCommand')
if not increase_control:
increase_control = modify_panel.controls.addCommand(increase_button)
increase_control.isPromoted = True
decrease_control = modify_panel.controls.itemById('decreaseHeightCommand')
if not decrease_control:
decrease_control = modify_panel.controls.addCommand(decrease_button)
decrease_control.isPromoted = True
refresh_control = modify_panel.controls.itemById('refreshHeightCommand')
if not refresh_control:
refresh_control = modify_panel.controls.addCommand(refresh_button)
refresh_control.isPromoted = True
except Exception as e:
ui.messageBox(f'Error creating command: {str(e)}')
# Command start
def run(context):
try:
create_adjustment_buttons() # Create Increase, Decrease, and Refresh buttons in the UI
except Exception as e:
ui.messageBox(f'Error in run function: {str(e)}')
# Command stop
def stop(context):
try:
# Remove all commands and event handlers
cmd_definitions = ui.commandDefinitions
# Remove Increase button
increase_button = cmd_definitions.itemById('increaseHeightCommand')
if increase_button:
increase_button.deleteMe()
# Remove Decrease button
decrease_button = cmd_definitions.itemById('decreaseHeightCommand')
if decrease_button:
decrease_button.deleteMe()
# Remove Refresh button
refresh_button = cmd_definitions.itemById('refreshHeightCommand')
if refresh_button:
refresh_button.deleteMe()
workspace = ui.workspaces.itemById('FusionSolidEnvironment')
toolbar_tab = workspace.toolbarTabs.itemById('SolidTab')
modify_panel = toolbar_tab.toolbarPanels.itemById('SolidModifyPanel')
if modify_panel:
increase_control = modify_panel.controls.itemById('increaseHeightCommand')
if increase_control:
increase_control.deleteMe()
decrease_control = modify_panel.controls.itemById('decreaseHeightCommand')
if decrease_control:
decrease_control.deleteMe()
refresh_control = modify_panel.controls.itemById('refreshHeightCommand')
if refresh_control:
refresh_control.deleteMe()
global handlers
handlers = []
except Exception as e:
ui.messageBox(f'Error in stop function: {str(e)}')