[solved] how to obtain the global decimals for a script
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
[solved] how to obtain the global decimals for a script
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?
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.
Re: how to obtain the global decimals for a script
Code: Select all
parms = App.ParamGet("User parameter:BaseApp/Preferences/Units")
parms.GetInt('UserSchema')
parms.GetInt('Decimals')
"fight the good fight"
Re: how to obtain the global decimals for a script
Many thanks! I only had to replace "App" by "FreeCAD".sgrogan wrote: ↑Sat Dec 07, 2019 5:40 pmCode: Select all
parms = App.ParamGet("User parameter:BaseApp/Preferences/Units") parms.GetInt('Decimals')
To help others, I document this at the Wiki: https://www.freecadweb.org/wiki/Quantity#Precision
Re: how to obtain the global decimals for a script
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
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.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
Re: [solved] how to obtain the global decimals for a script
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"
Re: how to obtain the global decimals for a script
I see and changed it now. It did not work for me because I modified a WB script (part of the latest a2plus release).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.
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)
Code: Select all
<string>(1): <unknown exception data>
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?
Re: [solved] how to obtain the global decimals for a script
I asked this question before.
Code: Select all
Discussion: using FreeCAD or App namespaces, which is prefered
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.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
Re: how to obtain the global decimals for a script
Don't use tabs to indent. Use 4 spaces. This agrees with Python conventions and the unofficial but de-facto FreeCAD style.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 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
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.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?
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
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.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
Re: how to obtain the global decimals for a script
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.
Indeed, stupid me!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.
Many thanks for this info. So I am already using pyside2 since I use Qt 5.import PySide
# in Qt4 it imports the real PySide
# in Qt5 it imports the fake PySide, which is the same as
# import PySide2
Re: how to obtain the global decimals for a 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.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.
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.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.