Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Set IsReadOnly for StringValueCommandInput

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
-pezi-
323 Views, 8 Replies

Set IsReadOnly for StringValueCommandInput

It is possible set IsReadOnly for StringValueCommandInput when adding new row in TableCommandInput? Because this option works for me only if I doing settings in CommandCreatedEventHandler.

8 REPLIES 8
Message 2 of 9
kandennti
in reply to: -pezi-

Hi @-pezi- .

 

I made and tried something like this.

# 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:
            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

            global _tblIpt, _tblAddBtnIpt
            tblStyle = adsk.core.TablePresentationStyles
            _tblIpt = inputs.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] # OK
    _tblIpt.addCommandInput(txtIpt, rowCount, 0)
    # txtIpt.isReadOnly = info[1] # NG

 

Is "IsReadOnly = True" set before addingCommandInput?

Message 3 of 9
-pezi-
in reply to: -pezi-

Hi @kandennti 

Thank you for the quick reply. Your example works well. But I'm creating a TableCommandInput on the second TabCommandInput and here's its behavior, as I wrote earlier. I tried to edit the example http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-e5c4dbe8-ee48-11e4-9823-f8b156d7cd97

 

 

	// Create three new command inputs.
	Ptr<CommandInput> childTableValueInput = cmdInputs->addValueInput("TableInput_value" + rowId.str(), "Value", "cm", ValueInput::createByReal(rowNumber));
	Ptr<StringValueCommandInput> childTableStringInput = cmdInputs->addStringValueInput("TableInput_string" + rowId.str(), "String", rowId.str());
	Ptr<CommandInput> childTableSpinnerInput = cmdInputs->addIntegerSpinnerCommandInput("spinnerInt" + rowId.str(), "Integer Spinner", 0, 100, 2, rowNumber);
	childTableStringInput->isReadOnly(true); // This is my edit

 

 


but the result is the same as in my code (when adding a line, the readOnly setting is not respected).

Message 4 of 9
kandennti
in reply to: -pezi-

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.

Message 5 of 9
nnikbin
in reply to: kandennti

Hi @-pezi- 

In 2019, I reported almost the same problem here.

Is the text that you need in your table rows always readonly?

Message 6 of 9
-pezi-
in reply to: nnikbin

Hi, @nnikbin 

thanks for your reply. And yes my text in a row is always readonly. I want to create CommandInputs with two TabCommandInput. On the first Tab will be TableCommandInput when show columns which are possible configuration second TabCommandInput. Because count of columns can be variable, on switching from configuration's tab on a view (first tab), I doing deleting all rows, and again adds rows with columns by configuration on the second tab.

Message 7 of 9
nnikbin
in reply to: -pezi-

@-pezi- 

I had the same problem in my add-in. Finally I found a good solution. I used BoolValueCommandInput (Button) and made it readonly. By setting its text property (without icon) it acts like a readonly StringValueCommandInput.

 

In following picture all numbers are BoolValueCommandInput.

 

19.png

 

Message 8 of 9
-pezi-
in reply to: nnikbin

Hi @nnikbin 

thank you for your tip it is a good solution for me.

Message 9 of 9
nnikbin
in reply to: -pezi-

You are welcome @-pezi- 

I am glad it helped. I hope Autodesk solve the root issue soon.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report