Skip to content

Constants

In some cases, it's better to use named constants instead of magic numbers inside the .pyp and .py file. Syntax like RED or SET_LENGTH is much more readable than 6 or 1001.

Inside the .pyp file the named constants can be defined in the <Constants> section. Define this section between <Script> and <Page> sections.

</Script>

<Constants>
    <Constant>
        <Name>RED</Name>
        <Value>6</Value>
        <ValueType>Integer</ValueType>
    </Constant>
</Constants>

<Page>

Usage in the .pyp file

The named constants can be used in the .pyp file for the tags <Value>, <ValueList> and <EventId>.

tag example
<Value> <Value>GREEN</Value>
<ValueList> <ValueList>LENGTH_1|LENGTH_2|LENGTH_3</ValueList>
<EventId> <EventId>SET_LENGTH</EventId>

Usage in the script

The constants are, just like parameters, part of the BuildingElement object and can be accessed inside the script with their names like this:

if value == build_ele.LENGTH_1:
   build_ele.Width.value = 1000.0

...

if event_id == build_ele.SET_LENGTH:
   build_ele.Length.value = 1000.0

Example

For a complete usage of the constants see the examples:

  • ...\etc\Examples\PythonParts\PaletteExamples\ ButtonList.pyp
  • ...\etc\PythonPartsExampleScripts\PaletteExamples\ ButtonList.py
  • ...\etc\Examples\PythonParts\PaletteExamples\ Buttons.pyp
  • ...\etc\PythonPartsExampleScripts\PaletteExamples\ Buttons.py
  • ...\etc\Examples\PythonParts\PaletteExamples\ ComboBox.pyp
  • ...\etc\PythonPartsExampleScripts\PaletteExamples\ ComboBox.py
  • ...\etc\Examples\PythonParts\PaletteExamples\ RadioButtons.pyp
  • ...\etc\PythonPartsExampleScripts\PaletteExamples\ RadioButtons.py