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: 9862
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).
User avatar
uwestoehr
Posts: 1447
Joined: Sun Jan 27, 2019 3:21 am

Re: Python code for Units

Postby uwestoehr » Sat Nov 30, 2019 6:27 pm

gomyhr wrote:
Sun Jul 14, 2019 7:27 pm
On https://www.freecadweb.org/wiki/Quantit ... _scripting, ...
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)?
I see the same. It is no longer possible to use this Python code:

Code: Select all

from Units import Unit,Quantity
If somebody could at least give me a pointer where the function Quantity() is no defined, I will update the Wiki page accordingly.
ulrich1a
Posts: 1925
Joined: Sun Jul 07, 2013 12:08 pm

Re: Python code for Units

Postby ulrich1a » Sat Nov 30, 2019 8:21 pm

uwestoehr wrote:
Sat Nov 30, 2019 6:27 pm
If somebody could at least give me a pointer where the function Quantity() is now defined, I will update the Wiki page accordingly.

Code: Select all

>>> import FreeCAD
>>> FreeCAD.Units
<module 'Units'>
>>> FreeCAD.Units.Unit
<class 'Base.Unit'>
>>> FreeCAD.Units.Quantity
<class 'Base.Quantity'>
The source files are located in src/Base/ beginning with Quantity.

Ulrich
vocx
Posts: 4101
Joined: Thu Oct 18, 2018 9:18 pm

Re: Python code for Units

Postby vocx » Sun Dec 01, 2019 6:13 am

uwestoehr wrote:
Sat Nov 30, 2019 6:27 pm
...
If somebody could at least give me a pointer where the function Quantity() is no defined, I will update the Wiki page accordingly.
The base modules in FreeCAD are a bit confusing sometimes because some modules are defined in C++ code, but in the Python interpreter they are made available from another namespace.

You have to check the source code occasionally and the Doxygen documentation to see these relationships.
Always add the important information to your posts if you need help.
To support the documentation effort, and code development, your donation is appreciated: paypal.
User avatar
uwestoehr
Posts: 1447
Joined: Sun Jan 27, 2019 3:21 am

Re: Python code for Units

Postby uwestoehr » Sun Dec 01, 2019 6:47 pm

ulrich1a wrote:
Sat Nov 30, 2019 8:21 pm

Code: Select all

>>> import FreeCAD
>>> FreeCAD.Units
<module 'Units'>
>>> FreeCAD.Units.Unit
<class 'Base.Unit'>
>>> FreeCAD.Units.Quantity
<class 'Base.Quantity'>
Many thanks! So Quantity and Unit are now classes and no longer modules. I updated the Wiki Quantity page accordingly (and tested all expressions from here)