error in FreeCAD units system: Density unit is not returned in Python

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
User avatar
bernd
Posts: 7301
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

error in FreeCAD units system: Density unit is not returned in Python

Postby bernd » Tue Feb 12, 2019 6:32 am

code:

Code: Select all

from FreeCAD import Units

Units.Unit('500 GPa')
Units.Pressure

Units.Unit('500 kg/m^3')
Units.Density

output:

Code: Select all

>>> 
>>> from FreeCAD import Units
>>> 
>>> Units.Unit('500 GPa')
Unit: kg/(mm*s^2) (-1,1,-2,0,0,0,0,0) [Pressure]
>>> Units.Pressure
Unit: kg/(mm*s^2) (-1,1,-2,0,0,0,0,0) [Pressure]
>>> 
>>> Units.Unit('500 kg/m^3')
Unit: kg/mm^3 (-3,1,0,0,0,0,0,0) [Density]
>>> Units.Density
Traceback (most recent call last):
  File "<input>", line 1, in <module>
AttributeError: module 'Units' has no attribute 'Density'
>>> 
>>> 

OS: Debian GNU/Linux buster/sid
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.15836 (Git)
Build type: Unknown
Branch: master
Hash: 51b32d75b411eaf5e75761be99e41a1b16f85943
Python version: 3.7.2+
Qt version: 5.11.3
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: German/Switzerland (de_CH)
User avatar
bernd
Posts: 7301
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: error in FreeCAD units system: Density unit is not returned in Python

Postby bernd » Tue Feb 12, 2019 6:56 am

a question in this regard ... Is it possible to return the Unit signature tuple by Python?

Code: Select all

from FreeCAD import Units
Units.Pressure
returns all unit informations.

Code: Select all

from FreeCAD import Units
Units.Pressure.Type
returns the name as string.

but I only need the signature tuple.

EDIT: I do not need this anymore. See next post. I could reduce this to much simpler expression.
User avatar
bernd
Posts: 7301
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: error in FreeCAD units system: Density unit is not returned in Python

Postby bernd » Tue Feb 12, 2019 7:48 am

I have the following string:

Code: Select all

'Pressure'
and I would like to have the return of:

Code: Select all

Units.Pressure
Does someone know how to do that with FreeCAD unit system?
User avatar
bernd
Posts: 7301
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: error in FreeCAD units system: Density unit is not returned in Python

Postby bernd » Tue Feb 12, 2019 7:57 am

bernd wrote:
Tue Feb 12, 2019 7:48 am
I have the following string:

Code: Select all

'Pressure'
and I would like to have the return of:

Code: Select all

Units.Pressure
Does someone know how to do that with FreeCAD unit system?

Found one myself, but it needs the FreeCADGui ...

Code: Select all

from FreeCAD import Units
myunittype = 'Pressure'
FreeCADGui.doCommand("myunit = Units." + myunittype)
myunit

output:

Code: Select all

>>>
>>> from FreeCAD import Units
>>> myunittype = 'Pressure'
>>> FreeCADGui.doCommand("myunit = Units." + myunittype)
>>> myunit = Units.Pressure
>>> myunit
Unit: kg/(mm*s^2) (-1,1,-2,0,0,0,0,0) [Pressure]
>>> 
without FreeCADGui would be way smarter ...
User avatar
bernd
Posts: 7301
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: error in FreeCAD units system: Density unit is not returned in Python

Postby bernd » Tue Feb 12, 2019 8:30 am

again found it myself ...

Code: Select all

getattr(FreeCAD.Units, "Pressure")

Code: Select all

>>> 
>>> getattr(FreeCAD.Units, "Pressure")
Unit: kg/(mm*s^2) (-1,1,-2,0,0,0,0,0) [Pressure]
>>> 
sorry for all the noise ...


The origin of the topic, the missing Density unit is still there ...
wmayer
Site Admin
Posts: 13910
Joined: Thu Feb 19, 2009 10:32 am

Re: error in FreeCAD units system: Density unit is not returned in Python

Postby wmayer » Tue Feb 12, 2019 2:13 pm

User avatar
bernd
Posts: 7301
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: error in FreeCAD units system: Density unit is not returned in Python

Postby bernd » Tue Feb 12, 2019 4:46 pm

:D