Fixed the table to be set in a tab.
# Table Cell ReadOnly Test
import adsk.core, adsk.fusion, traceback
_commandId = 'Table Cell ReadOnly Test'
_tblIpt = adsk.core.TableCommandInput.cast(None)
_tblAddBtnIpt = adsk.core.BoolValueCommandInput.cast(None)
_handlers = []
_app = None
_ui = None
class CommandInputChangedHandler(adsk.core.InputChangedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
global _tblAddBtnIpt
if args.input != _tblAddBtnIpt:
return
addRowToTable()
except:
pass
# if _ui:
# _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class CommandDestroyHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
adsk.terminate()
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class CommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
cmd = args.command
# event
onDestroy = CommandDestroyHandler()
cmd.destroy.add(onDestroy)
_handlers.append(onDestroy)
onInputChanged = CommandInputChangedHandler()
cmd.inputChanged.add(onInputChanged)
_handlers.append(onInputChanged)
# inputs
inputs = cmd.commandInputs
tab1 = inputs.addTabCommandInput('tab_1', 'Tab 1')
tab1ChildInputs = tab1.children
tab2 = inputs.addTabCommandInput('tab_2', 'Tab 2')
tab2ChildInputs = tab2.children
global _tblIpt, _tblAddBtnIpt
tblStyle = adsk.core.TablePresentationStyles
_tblIpt = tab2ChildInputs.addTableCommandInput('table', 'Table', 5, '1')
_tblIpt.hasGrid = False
_tblIpt.tablePresentationStyle = tblStyle.itemBorderTablePresentationStyle
_tblAddBtnIpt = inputs.addBoolValueInput('tableAdd', 'Add', False, '', True)
_tblIpt.addToolbarCommandInput(_tblAddBtnIpt)
# Add 2 lines in advance
addRowToTable()
addRowToTable()
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def run(context):
try:
global _app, _ui
_app = adsk.core.Application.get()
_ui = _app.userInterface
cmdDefs = _ui.commandDefinitions
cmdDef = cmdDefs.itemById(_commandId)
if cmdDef:
cmdDef.deleteMe()
cmdDef = cmdDefs.addButtonDefinition(_commandId, _commandId, _commandId)
onCmdCreated = CommandCreatedHandler()
cmdDef.commandCreated.add(onCmdCreated)
_handlers.append(onCmdCreated)
cmdDef.execute()
adsk.autoTerminate(False)
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
# Add row to table
# Even number:readOnly
# Odd number:read/write
def addRowToTable():
global _tblIpt
inputs = adsk.core.CommandInputs.cast(_tblIpt.commandInputs)
rowCount = _tblIpt.rowCount
if rowCount % 2 == 0:
info = ('readOnly', True)
else:
info = ('read/write', False)
txtIpt = inputs.addStringValueInput(
'txt{}'.format(rowCount), info[0], info[0])
txtIpt.isReadOnly = info[1]
_tblIpt.addCommandInput(txtIpt, rowCount, 0)
It is true that "IsReadOnly = True" doesn't seem to be working outside of the CommandCreatedEvent.
Perhaps adding a row to the table outside of a CommandCreatedEvent is not supposed to be done.