Setting an enumerate property.

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Setting an enumerate property.

Post by keithsloan52 »

Okay I have a Object with an enumerate property

Code: Select all

obj.addProperty("App::PropertyEnumeration","aunit","GDMLTube","aunit")
obj.aunit=['rad','deg']
obj.aunit=0
Which iniitalises the property to 'rad'.

Don't understand the double setting of obj.aunit, but went by something I found on the web and it seems to work,

But I would like to now set the value with some code. How can I do that?
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Setting an enumerate property.

Post by vocx »

keithsloan52 wrote: Fri Sep 13, 2019 4:19 pm Okay I have a Object with an enumerate property
...

But I would like to now set the value with some code. How can I do that?
What exactly is an enumeration? Like a list of strings? Then it's not a value. If you want to enter a value, you need to add a numeric property.

Code: Select all

obj.addProperty("App::PropertyVectorDistance", "Start", "Draft", QT_TRANSLATE_NOOP("App::Property", "The start point of this line"))
obj.addProperty("App::PropertyLength", "FilletRadius", "Draft", QT_TRANSLATE_NOOP("App::Property", "Radius to use to fillet the corners"))
For example.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: Setting an enumerate property.

Post by keithsloan52 »

vocx wrote: Fri Sep 13, 2019 4:28 pm
keithsloan52 wrote: Fri Sep 13, 2019 4:19 pm Okay I have a Object with an enumerate property
...

But I would like to now set the value with some code. How can I do that?
What exactly is an enumeration? Like a list of strings? Then it's not a value. If you want to enter a value, you need to add a numeric property.

Code: Select all

obj.addProperty("App::PropertyVectorDistance", "Start", "Draft", QT_TRANSLATE_NOOP("App::Property", "The start point of this line"))
obj.addProperty("App::PropertyLength", "FilletRadius", "Draft", QT_TRANSLATE_NOOP("App::Property", "Radius to use to fillet the corners"))
For example.
enumerate property is one from a list of values
By set a value I mean I want to set it to 'deg' where the default is 'rad' i.e. aunit=1
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: Setting an enumerate property.

Post by keithsloan52 »

Okay my class definition for a python object is

Code: Select all

class GDMLTube(GDMLcommon) :
   def __init__(self, obj, rmin, rmax, z, startphi, deltaphi, aunit,  \
                lunit, material):
      '''Add some custom properties to our Tube feature'''
      obj.addProperty("App::PropertyLength","rmin","GDMLTube","Inside Radius").rmin=rmin
      obj.addProperty("App::PropertyLength","rmax","GDMLTube","Outside Radius").rmax=rmax
      obj.addProperty("App::PropertyLength","z","GDMLTube","Length z").z=z
      obj.addProperty("App::PropertyFloat","startphi","GDMLTube","Start Angle").startphi=startphi
      obj.addProperty("App::PropertyFloat","deltaphi","GDMLTube","Delta Angle").deltaphi=deltaphi
      obj.addProperty("App::PropertyEnumeration","aunit","GDMLTube","aunit")
      obj.aunit=['rad','deg']
      obj.aunit=0
      obj.addProperty("App::PropertyString","lunit","GDMLTube","lunit").lunit=lunit
which as it stands defaults the object aunit property to rad.

What I would like to do is set the value based on the value of aunit passed to its __init__
function
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: Setting an enumerate property.

Post by TheMarkster »

Code: Select all

obj.aunit = ['rad','deg'].index(aunit)
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: Setting an enumerate property.

Post by keithsloan52 »

TheMarkster wrote: Fri Sep 13, 2019 9:28 pm

Code: Select all

obj.aunit = ['rad','deg'].index(aunit)
Thanks, found I actually had to code

Code: Select all

obj.aunit=['rad','deg']
obj.aunit=['rad','deg'].index(aunit)
Still a mystery to me as to why it requires a double reference
User avatar
Chris_G
Veteran
Posts: 2601
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: Setting an enumerate property.

Post by Chris_G »

keithsloan52 wrote: Sat Sep 14, 2019 4:51 am Still a mystery to me as to why it requires a double reference
I suppose that this property gets a special treatment internally :
- when it is set to a list of strings, the enum gets populated
- when it is set to an integer, the index in the EXISTING enum is set

Code: Select all

['rad','deg'].index(aunit) # This is an integer.

Code: Select all

obj.aunit = ['rad','deg'].index(aunit)
So this sets an index, but the enum list has not been populated first.
This would explain the need for :

Code: Select all

obj.aunit=['rad','deg'] # Populate the enum list
obj.aunit=['rad','deg'].index(aunit) # Set the index in the (not empty) list
keithsloan52
Veteran
Posts: 2764
Joined: Mon Feb 27, 2012 5:31 pm

Re: Setting an enumerate property.

Post by keithsloan52 »

Chris_G wrote: Sat Sep 14, 2019 7:57 am
keithsloan52 wrote: Sat Sep 14, 2019 4:51 am Still a mystery to me as to why it requires a double reference
I suppose that this property gets a special treatment internally :
- when it is set to a list of strings, the enum gets populated
- when it is set to an integer, the index in the EXISTING enum is set

Code: Select all

['rad','deg'].index(aunit) # This is an integer.

Code: Select all

obj.aunit = ['rad','deg'].index(aunit)
So this sets an index, but the enum list has not been populated first.
This would explain the need for :

Code: Select all

obj.aunit=['rad','deg'] # Populate the enum list
obj.aunit=['rad','deg'].index(aunit) # Set the index in the (not empty) list
You would have thought it easier to pass it a list and a number as a single list.
obj.aunit = [['rad','deg],num]
Post Reply