[Frage] Programatically creating/changing parameters [Gelöst]


Hello!

What I want to do:

  • Let the user import an Excel file
  • Extract the available worksheets
  • List them in the Python part, where the user can select the worksheets that he wants to work with (each worksheet has a checkbox)

My problem: I am successfully allowing the user to import the file and retrieve the worksheets, but I am not able to list all the worksheets.
Right now I am trying to do it like this:
Creating the parameter:

    <Parameter>
        <Name>SelectedWorksheets</Name>
        <Text>Worksheet1,Worksheet2</Text>
        <Value>False|False</Value>
        <ValueType>namedtuple(CheckBox,CheckBox)</ValueType>
        <NamedTuple>
            <TypeName>WorksheetSelection</TypeName>
            <FieldNames>Worksheet1,Worksheet2</FieldNames>
        </NamedTuple>
    </Parameter>

Modifying the parameter:
        props = ctrl_prop_util.control_props
        # Assigning worksheet names to props[':SelectedWorksheets'][0].text
        props[':SelectedWorksheets'][0].text = '|'.join(worksheet_names)

But when I try to change the value string, I get the error that value_str does not have any setters. This is how I am trying to change it:
        props[':SelectedWorksheets'][0].value_str = '|'.join(['False'] * len(worksheet_names))

Is there another way to do this?

Another option would be to dynamically create the parameter (creating a checkbox parameter for each worksheet), but I did not manage to do this and I also could not find anything in the manual...

Lösung anzeigen Lösung verbergen

I have now found a solution, but it still uses some 'private' methods, so I guess it is not the best way to do this:

from typing import List
from BuildingElement import BuildingElement
from ControlProperties import ControlProperties

def appendToNamedTuple(id_name: str,
            selectedWorksheetsProps: ControlProperties,
            worksheet_names:  List[str],
            type:   str,
            build_ele:        BuildingElement):
        # assign the names of the worksheet to the text and divide them with a |, so that each checkbox has a their 'title'
        selectedWorksheetsProps._ControlProperties__text = '|'.join(worksheet_names)
        # for each worksheet append the name with an id to the named tuple definition (in my case worksheet0, worksheet1, etc.)
        for idx in enumerate(worksheet_names):
            name = id_name
            name += str(idx)
            build_ele.SelectedWorksheets.named_tuple_def.field_names.append(name)
            # assign the init value. in my case  0 for false
            build_ele.SelectedWorksheets._ParameterProperty__value += (0,)
        # define what type the thing has, in my case it is CheckBox, but can be Integer, ComboBox, ...
        initial_value = 'namedtuple('
        checkboxes = (',' + type) * len(worksheet_names)
        checkboxes = checkboxes[1:]
        build_ele.SelectedWorksheets.value_type = initial_value + checkboxes + ')'

Hi,

Have you try with "_replace" ?

Example :

<Parameter>
	<Name>StirrupList</Name>
	<Text>,Espacement,Distance</Text>
	<Value>[images\ParamB.png|120|450;
			images\ParamC.png|120|450]
	</Value>
	<ValueType>namedtuple(Picture,Length,Length,Separator)</ValueType>
	<NamedTuple>
		<TypeName>Stirrup</TypeName>
		<FieldNames>Picture,Spacing,Length,Separator</FieldNames>
	</NamedTuple>
	<MinValue>,50,,</MinValue>
	<MaxValue>,360,,</MaxValue>
	<Enable>,,False</Enable>
</Parameter>

stirrup_list    = build_ele.StirrupList.value
stirrup_list[0] = stirrup_list[0]._replace(Spacing = B_stirrup_spacing)

Best
Christophe

I have now found a solution, but it still uses some 'private' methods, so I guess it is not the best way to do this:

from typing import List
from BuildingElement import BuildingElement
from ControlProperties import ControlProperties

def appendToNamedTuple(id_name: str,
            selectedWorksheetsProps: ControlProperties,
            worksheet_names:  List[str],
            type:   str,
            build_ele:        BuildingElement):
        # assign the names of the worksheet to the text and divide them with a |, so that each checkbox has a their 'title'
        selectedWorksheetsProps._ControlProperties__text = '|'.join(worksheet_names)
        # for each worksheet append the name with an id to the named tuple definition (in my case worksheet0, worksheet1, etc.)
        for idx in enumerate(worksheet_names):
            name = id_name
            name += str(idx)
            build_ele.SelectedWorksheets.named_tuple_def.field_names.append(name)
            # assign the init value. in my case  0 for false
            build_ele.SelectedWorksheets._ParameterProperty__value += (0,)
        # define what type the thing has, in my case it is CheckBox, but can be Integer, ComboBox, ...
        initial_value = 'namedtuple('
        checkboxes = (',' + type) * len(worksheet_names)
        checkboxes = checkboxes[1:]
        build_ele.SelectedWorksheets.value_type = initial_value + checkboxes + ')'