[solved] how to obtain the global decimals for a script

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
uwestoehr
Veteran
Posts: 4961
Joined: Sun Jan 27, 2019 3:21 am
Location: Germany
Contact:

[solved] how to obtain the global decimals for a script

Post by uwestoehr »

For a script I want to get the number of decimals the user set in the FC preferences for the units.
I cannot find a method to get this info. Does anybody have a hint for me or is this at the moment not possible?
Last edited by uwestoehr on Sun Dec 08, 2019 12:58 am, edited 1 time in total.
User avatar
sgrogan
Veteran
Posts: 6499
Joined: Wed Oct 22, 2014 5:02 pm

Re: how to obtain the global decimals for a script

Post by sgrogan »

uwestoehr wrote: Sat Dec 07, 2019 5:24 pm For a script I want to get the number of decimals the user set in the FC preferences for the units.

Code: Select all

parms = App.ParamGet("User parameter:BaseApp/Preferences/Units")
parms.GetInt('UserSchema')
parms.GetInt('Decimals')
EDIT: Fixed second line, Schema gives the unit system, not needed in your case.
"fight the good fight"
User avatar
uwestoehr
Veteran
Posts: 4961
Joined: Sun Jan 27, 2019 3:21 am
Location: Germany
Contact:

Re: how to obtain the global decimals for a script

Post by uwestoehr »

sgrogan wrote: Sat Dec 07, 2019 5:40 pm

Code: Select all

parms = App.ParamGet("User parameter:BaseApp/Preferences/Units")
parms.GetInt('Decimals')
Many thanks! I only had to replace "App" by "FreeCAD".
To help others, I document this at the Wiki: https://www.freecadweb.org/wiki/Quantity#Precision
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: how to obtain the global decimals for a script

Post by vocx »

uwestoehr wrote: Sun Dec 08, 2019 12:58 am ...
Many thanks! I only had to replace "App" by "FreeCAD".
...
Just for reference, when FreeCAD starts it defines a couple of aliases which are accessible from the Python console.

Code: Select all

App = FreeCAD
Gui = FreeCADGui
This is the reason many examples in the wiki use the "App" namespace and not "FreeCAD".
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
sgrogan
Veteran
Posts: 6499
Joined: Wed Oct 22, 2014 5:02 pm

Re: [solved] how to obtain the global decimals for a script

Post by sgrogan »

vocx wrote: Sun Dec 08, 2019 5:45 pm This is the reason many examples in the wiki use the "App" namespace and not "FreeCAD".
I don't doubt you, but is this correct?
I thought the whole "branding" thing was to not have to use "FreeCAD"?
"fight the good fight"
User avatar
uwestoehr
Veteran
Posts: 4961
Joined: Sun Jan 27, 2019 3:21 am
Location: Germany
Contact:

Re: how to obtain the global decimals for a script

Post by uwestoehr »

vocx wrote: Sun Dec 08, 2019 5:45 pm Just for reference, when FreeCAD starts it defines a couple of aliases which are accessible from the Python console.
I see and changed it now. It did not work for me because I modified a WB script (part of the latest a2plus release).

May I ask 2 further things?

1. How to stop a script? In https://www.freecadweb.org/wiki/A2plus_ ... le_Example the animation is stopped by pressing the close button:

Code: Select all

if progressDialog.wasCanceled():
		sys.exit(0)
But then I get this python message:

Code: Select all

<string>(1): <unknown exception data>
So what is the correct way to stop the script?

2. What about pyside, pyside2 and PyQT? What should I use for new projects and what will be supported by FreeCAD regarding Python 3 and Qt5?
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: [solved] how to obtain the global decimals for a script

Post by vocx »

sgrogan wrote: Sun Dec 08, 2019 6:06 pm I thought the whole "branding" thing was to not have to use "FreeCAD"?
I asked this question before.

Code: Select all

Discussion: using FreeCAD or App namespaces, which is prefered
There doesn't seem to be a definite answer. It looks like it's a convention, but it is not official. It seems to me Jürgen defined these imports early on just to save typing the whole FreeCAD and FreeCADGui names.
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.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: how to obtain the global decimals for a script

Post by vocx »

uwestoehr wrote: Sun Dec 08, 2019 10:32 pm ...
1. How to stop a script? In https://www.freecadweb.org/wiki/A2plus_ ... le_Example the animation is stopped by pressing the close button:
Don't use tabs to indent. Use 4 spaces. This agrees with Python conventions and the unofficial but de-facto FreeCAD style.

Don't use sys.exit, that is used when you want to terminate the entire Python interpreter, like if you wanted to close the entire FreeCAD application.

Since your animation is controlled with a while loop and a condition, when you press the button, you should go to that condition so the condition is satisfied and the loop finishes.

Code: Select all

while angle < 360: # run this loop until we have one full turn (360 degrees)
    if progressDialog.wasCanceled():
        angle = 360

Code: Select all

stop = False
while angle < 360 and not stop:
    if progressDialog.wasCanceled():
        stop = True
2. What about pyside, pyside2 and PyQT? What should I use for new projects and what will be supported by FreeCAD regarding Python 3 and Qt5?
New projects should use Python 3 and Qt5 (PySide2). As long as you are using a version of FreeCAD compiled for Python 3 and Qt5, importing PySide will internally import PySide2. That is, the PySide module is a fake module that loads the appropriate PySide2 modules. This was done to maintain Qt4 compatibility with a single import.

If FreeCAD is compiled against Qt4, then importing PySide will import the real module and not a fake one.

Code: Select all

import PySide
# in Qt4 it imports the real PySide
# in Qt5 it imports the fake PySide, which is the same as
# import PySide2
Werner mentioned that in the future we could drop Qt4 (PySide) completely, and then we would have to use PySide2 explicitly. Then the alias would be done in reverse. That is, if using Qt4 when importing PySide2, it would import the PySide modules.

PyQt is no longer used. It was used in the past, but it seems FreeCAD moved to PySide a long time ago, around 2013 or so.

Maybe it makes sense to investigate QtPy or Qt.py, which should be able to handle PySide, PySide2, PyQt4, and PyQt5 transparently. But for the time being, PySide2 and Python 3 is the way to go. That is, import PySide, but use PySide2 documentation to know how the classes and functions work, https://doc.qt.io/qtforpython/modules.html
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: how to obtain the global decimals for a script

Post by uwestoehr »

vocx wrote: Sun Dec 08, 2019 11:17 pm Don't use tabs to indent. Use 4 spaces. This agrees with Python conventions and the unofficial but de-facto FreeCAD style.
I wrote the script within FreeCAD. This editor does not automatically add spaces and after a dozen times with indenting errors I used tabs. I am no Pythionist and therefore don't know the rules. However, after googling it seems it is not a mistake to use tabs. One must only use the consistently in the script.

Since your animation is controlled with a while loop and a condition, when you press the button, you should go to that condition so the condition is satisfied and the loop finishes.
Indeed, stupid me!

import PySide
# in Qt4 it imports the real PySide
# in Qt5 it imports the fake PySide, which is the same as
# import PySide2
Many thanks for this info. So I am already using pyside2 since I use Qt 5.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: how to obtain the global decimals for a script

Post by vocx »

uwestoehr wrote: Mon Dec 09, 2019 12:18 am I wrote the script within FreeCAD. This editor does not automatically add spaces and after a dozen times with indenting errors I used tabs. I am no Pythionist and therefore don't know the rules. However, after googling it seems it is not a mistake to use tabs. One must only use the consistently in the script.
It is not an error to use Tabs, but if we want to comply with PEP8, which is the de facto standard to format Python code, we should use 4 space indentation. See PEP8.

There is no formal coding standard set by Werner or Yorik, the bosses of FreeCAD, but we essentially follow PEP8. Bernd, the boss of FEM, has codified the standard for that workbench, which is essentially PEP8.

As for the Tabs in the editor see Preferences_Editor#Editor.
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.
Post Reply