Python code for Units

Discussions about the wiki documentation of FreeCAD and its translation.
gomyhr
Posts: 2
Joined: Sun Jul 14, 2019 7:15 pm

Python code for Units

Postby gomyhr » Sun Jul 14, 2019 7:27 pm

On https://www.freecadweb.org/wiki/Quantit ... _scripting, the code examples start with
from Units import Unit
and
from Units import Unit
Now, in the python console in FreeCAD (0.18 16117 Appimage, on Ubuntu 14.04.6 LTS 64-bit) those lines do not work. The error message is:
Traceback (most recent call last):
File "<input>", line 1, in <module>
ModuleNotFoundError: No module named 'Units'
So, is the wiki code outdated (I see it had the same form back in 2014) or is something missing from the installation (I can import FreeCAD, numpy, pandas and other modules from the python console)?
User avatar
bernd
Posts: 8204
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: Python code for Units

Postby bernd » Mon Jul 15, 2019 5:38 am

I do not know, but here some working unit code ... https://github.com/FreeCAD/FreeCAD/blob ... #L775-L785

Code: Select all

# some unit code **********
from FreeCAD import Units
getattr(Units, 'Pressure')
Units.Pressure
Units.Quantity('25 MPa')
Units.Quantity('25 MPa').getValueAs('Pa')
Units.Quantity('25 MPa').getUserPreferred()[2]
Units.Quantity(25000, Units.Pressure)
Units.Quantity(25000, Units.Pressure).getValueAs('MPa')
Units.Unit('25 MPa')
Units.Unit(-1,1,-2,0,0,0,0,0)

output

Code: Select all

>>> 
>>> # some unit code **********
>>> from FreeCAD import Units
>>> getattr(Units, 'Pressure')
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.Quantity('25 MPa')
25000 kg/(mm*s^2)
>>> Units.Quantity('25 MPa').getValueAs('Pa')
2.5e+07 
>>> Units.Quantity('25 MPa').getUserPreferred()[2]
'MPa'
>>> Units.Quantity(25000, Units.Pressure)
25000 kg/(mm*s^2)
>>> Units.Quantity(25000, Units.Pressure).getValueAs('MPa')
25 
>>> Units.Unit('25 MPa')
Unit: kg/(mm*s^2) (-1,1,-2,0,0,0,0,0) [Pressure]
>>> Units.Unit(-1,1,-2,0,0,0,0,0)
Unit: kg/(mm*s^2) (-1,1,-2,0,0,0,0,0) [Pressure]
>>> 

on
OS: Debian GNU/Linux 10 (buster) (KDE/plasma)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.17360 (Git)
Build type: Unknown
Branch: master
Hash: 348160d4ea970f5f6d0850272c04800c0222c8ba
Python version: 3.7.3
Qt version: 5.11.3
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: German/Switzerland (de_CH)
gomyhr
Posts: 2
Joined: Sun Jul 14, 2019 7:15 pm

Re: Python code for Units

Postby gomyhr » Mon Jul 15, 2019 9:36 pm

Thank you for the working examples. It helped me understand a bit more how the namespace is structured, but I could not quite figure out how to make minimal changes to the wiki code that would work with recent versions (and preferably also work with older versions).

I checked with 0.17, and

Code: Select all

from Units import Unit
works there. I thought this might be related to changes in the python import system between python2 (0.17) and python3 (0.18), and that it could be modified to

Code: Select all

from FreeCAD.Units import Unit
which does not work in 0.17 and in 0.18 it gives:

Code: Select all

>>> from FreeCAD.Units import Unit
Traceback (most recent call last):
  File "<input>", line 1, in <module>
ModuleNotFoundError: No module named 'FreeCAD.Units'
I find it confusing that it says that there is no module named 'FreeCAD.Units', since

Code: Select all

>>> FreeCAD.Units
<module 'Units'>
I guess it all boils down to me not knowing how the FreeCAD enviroment is initialized (and how it has changed between 0.17 and 0.18).