Python code for Units

Discussions about the wiki documentation of FreeCAD and its translation.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
gomyhr
Posts: 2
Joined: Sun Jul 14, 2019 7:15 pm

Python code for Units

Post by gomyhr »

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
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Python code for Units

Post by bernd »

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

Post by gomyhr »

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
Veteran
Posts: 4961
Joined: Sun Jan 27, 2019 3:21 am
Location: Germany
Contact:

Re: Python code for Units

Post by uwestoehr »

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
Veteran
Posts: 1957
Joined: Sun Jul 07, 2013 12:08 pm

Re: Python code for Units

Post by ulrich1a »

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
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Python code for Units

Post by vocx »

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. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
User avatar
uwestoehr
Veteran
Posts: 4961
Joined: Sun Jan 27, 2019 3:21 am
Location: Germany
Contact:

Re: Python code for Units

Post by uwestoehr »

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)
josegegas
Posts: 241
Joined: Sat Feb 11, 2017 12:54 am
Location: New Zealand

Re: Python code for Units

Post by josegegas »

I am having the same problem. In FreeCAD 0.17 I was able to import

from Units import Unit,Quantity

Now in 0.18 and 0.19 it does not work any more.

I found a way to solve it though. I installed the units module to:

C:\Program Files\FreeCAD 0.18\Ext

Now I can import Unit and Quantity.

You can install units by

pip install units

and then just copy-paste the whole units folder from any location your local python instalation uses to where FreeCAD has its modules, in the case of Windows the path above.

Hope this helps.
User avatar
vanuan
Posts: 539
Joined: Wed Oct 24, 2018 9:49 pm

Re: Python code for Units

Post by vanuan »

It appears that previously, Units was a globally importable module.

Now it's nested inside the FreeCAD module as an object.

But you can't use "from FreeCAD.Units import Unit". Because FreeCAD is a module, not a package.

To make FreeCAD.Units import work, FreeCAD needs to be a package. But FreeCAD can't be a package because it's a native module (*.so/dll).

So there must be a "freecad" python package. Inside this package we can have the __init.py__ file:

```
from FreeCAD import *

__all__ = ['Units']
```

Then, you'd be able to do this

```
from freecad.Units import Unit
```

I haven't tested it though. But I think making each FreeCAD native module a proper python package is a step in the right direction.
Post Reply