Is this a good Python-FreeCAD script?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
PaulCobbaut
Posts: 26
Joined: Mon Oct 03, 2022 9:29 am

Is this a good Python-FreeCAD script?

Post by PaulCobbaut »

I just wonder in which ways my Python script can be improved.

https://github.com/paulcobbaut/FreeCAD-Cups

It generates simple measuring cups in FreeCAD and saves them as .stl files for 3D printing. It works, but I have little experience so it would be awesome to get feedback. For pictures https://www.printables.com/model/324896 ... uring-cups.
edwilliams16
Veteran
Posts: 3109
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Is this a good Python-FreeCAD script?

Post by edwilliams16 »

Looks fine. It might have been simpler to create two Part Cylinders and done a cut rather than creating the cylinders in the PD workbench and cutting them. Or, if you would prefer to stay in PD, do a pocket to hollow the cylinder.
PaulCobbaut
Posts: 26
Joined: Mon Oct 03, 2022 9:29 am

Re: Is this a good Python-FreeCAD script?

Post by PaulCobbaut »

Looks fine.
Thanks, though I am surprised.
Or, if you would prefer to stay in PD, do a pocket to hollow the cylinder.
I cannot get this to work (yet). (The GUI says "Profilebased Length: Could not extrude the sketch".) I need to learn more about Part and PartDesign to understand the errors in FreeCAD.
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: Is this a good Python-FreeCAD script?

Post by mario52 »

Hi
PaulCobbaut wrote: Sat Nov 26, 2022 9:53 am Thanks, though I am surprised.
back on earth

i have :

10:58:06 Part::Cut: Link(s) to object(s) 'cup_outer cup_inner' go out of the allowed scope 'cup'. Instead, the linked object(s) reside within 'Body Body Body'.
10:58:06 Traceback (most recent call last):
File "C:/Users/Mario/AppData/Roaming/FreeCAD/Macro/000x.FCMacro", line 123, in <module>
create_cup(ml , str(ml) + 'ml' , 'millilitre')
File "C:/Users/Mario/AppData/Roaming/FreeCAD/Macro/000x.FCMacro", line 62, in create_cup
ss=Draft.makeShapeString(String=ml,FontFile="/home/paul/FreeCAD models/cups_python/Vera.ttf",Size=(radius/2),Tracking=0.0)
File "C:\FreeCAD_weekly-builds-31097-2022-11-24-conda-Windows-x86_64-py310\Mod\Draft\draftmake\make_shapestring.py", line 57, in make_shapestring
obj.String = String
<class 'TypeError'>: type must be str or unicode, not int

____________________________
after correction:


11:13:15 Part::Cut: Link(s) to object(s) 'cup_outer cup_inner' go out of the allowed scope 'cup'. Instead, the linked object(s) reside within 'Body Body Body'.
11:13:16 Traceback (most recent call last):

File "C:/Users/xyz/AppData/Roaming/FreeCAD/Macro/000x.FCMacro", line 123, in <module>
create_cup(ml , str(ml) + 'ml' , 'millilitre')
File "C:/Users/Mario/AppData/Roaming/FreeCAD/Macro/000x.FCMacro", line 103, in create_cup
Mesh.export(export, u"/home/paul/FreeCAD models/cups_python/" + dir_name + "/sharp measuring cup " + cup_name + ".stl")
<class 'NameError'>: name 'Mesh' is not defined

_____________________________
after correction
MacroBegin00.png
MacroBegin00.png (10.64 KiB) Viewed 874 times
... i must stop my FreeCAD with gestionnaire des tâche
_____________________________
after correction ok

bon upgrade

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
PaulCobbaut
Posts: 26
Joined: Mon Oct 03, 2022 9:29 am

Re: Is this a good Python-FreeCAD script?

Post by PaulCobbaut »

mario52 wrote: Sat Nov 26, 2022 10:40 am back on earth
Yes!

Thanks for the string error (my version of FreeCAD did not complain, but I already had several scripts working in one version and failing in another...)

The hardcoding of directories was obviously a bad move, they are variables now at the start of the function.
mario52 wrote: Sat Nov 26, 2022 10:40 am <class 'NameError'>: name 'Mesh' is not defined
I don't get this error... should there be an "import mesh"?

Anyway, thanks for landing me! Github (https://github.com/paulcobbaut/FreeCAD- ... 9a4223622b) and Printables are updated.
User avatar
dprojects
Posts: 721
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: Is this a good Python-FreeCAD script?

Post by dprojects »

PaulCobbaut wrote: Sat Nov 26, 2022 8:31 am I just wonder in which ways my Python script can be improved.
https://github.com/paulcobbaut/FreeCAD-Cups
My philosophy for the code is simple: "Code is good, if working."

The code looks clear, there are comments what is good. However, what I would change:
  • Personally I use gVariable for global variables name. It is some kind of safeguard for overwriting global variable by local. Python has something like "global" for functions but if you add code at the end of the file, outside function this will overwrite.
  • I prefer "variable = something" not "variable=something", it is more readable.
  • Personally I don't use underscores: "variable_variable" or "variable__" or "__val__"
  • Never hardcode path to local HDD: my_font_file = '/home/paul/FreeCAD models/cups_python/Vera.ttf' it is security issue "/home/paul/" aka hacker ;-) but if you forget about it this will cause errors...
  • Make more modular, to achieve shorter lines: this:

    Code: Select all

    extrude.Placement = App.Placement(App.Vector(0,0,wall-0.5),App.Rotation(App.Vector(1,0,0),0))
    
    into something like this:

    Code: Select all

    pos = FreeCAD.Vector(0, 0, wall-0.5)
    rot = FreeCAD.Rotation(FreeCAD.Vector(1, 0, 0), 0)
    extrude.Placement = FreeCAD.Placement(pos, rot)
    
    never use App, or qApp, rather not use "import something as sm", don't be so lazy ;-)

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: Is this a good Python-FreeCAD script?

Post by mario52 »

Hi
PaulCobbaut wrote: Sat Nov 26, 2022 2:16 pm should there be an "import mesh"?
yes

now adapt this:

Code: Select all

import PySide2
from PySide2 import QtGui , QtCore, QtWidgets
from PySide2.QtWidgets import *

pathFileStl = PySide2.QtWidgets.QFileDialog.getExistingDirectory(None,"Select your folder")
print(pathFileStl)

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
PaulCobbaut
Posts: 26
Joined: Mon Oct 03, 2022 9:29 am

Re: Is this a good Python-FreeCAD script?

Post by PaulCobbaut »

dprojects wrote: Sat Nov 26, 2022 8:26 pm My philosophy for the code is simple: "Code is good, if working."
The code still works, so thanks.
dprojects wrote: Sat Nov 26, 2022 8:26 pm [*] I prefer "variable = something" not "variable=something", it is more readable.
[*] Personally I don't use underscores: "variable_variable" or "variable__" or "__val__"
The missing spaces all came from code copied from the Python Console, all fixed now.
Snake case is recommended in Python no? Anyway, I find it more readable. The double underscores again were copied from the Python Console and are replaced with a proper variable name.
dprojects wrote: Sat Nov 26, 2022 8:26 pm [*] Never hardcode path to local HDD: my_font_file = '/home/paul/FreeCAD models/cups_python/Vera.ttf' it is security issue "/home/paul/" aka hacker ;-) but if you forget about it this will cause errors...
You are right, but I already use my real name everywhere.
dprojects wrote: Sat Nov 26, 2022 8:26 pm [*] Make more modular, to achieve shorter lines: this:

Code: Select all

extrude.Placement = App.Placement(App.Vector(0,0,wall-0.5),App.Rotation(App.Vector(1,0,0),0))
into something like this:

Code: Select all

pos = FreeCAD.Vector(0, 0, wall-0.5)
rot = FreeCAD.Rotation(FreeCAD.Vector(1, 0, 0), 0)
extrude.Placement = FreeCAD.Placement(pos, rot)
Done (and I agree).
dprojects wrote: Sat Nov 26, 2022 8:26 pm never use App, or qApp, rather not use "import something as sm", don't be so lazy ;-)
Again copied from Python Console, I try to not use App. All fixed now.
I don't know what "import something as" does, still learning Python.

Thanks for your advice, it is really appreciated!
PaulCobbaut
Posts: 26
Joined: Mon Oct 03, 2022 9:29 am

Re: Is this a good Python-FreeCAD script?

Post by PaulCobbaut »

mario52 wrote: Sat Nov 26, 2022 9:43 pm Hi
PaulCobbaut wrote: Sat Nov 26, 2022 2:16 pm should there be an "import mesh"?
yes

now adapt this:

Code: Select all

import PySide2
from PySide2 import QtGui , QtCore, QtWidgets
from PySide2.QtWidgets import *

pathFileStl = PySide2.QtWidgets.QFileDialog.getExistingDirectory(None,"Select your folder")
print(pathFileStl)

mario
Added the "import mesh". And thanks for this popup example, it works.
One of my future goals is to write a useful FreeCAD macro.
User avatar
dprojects
Posts: 721
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: Is this a good Python-FreeCAD script?

Post by dprojects »

PaulCobbaut wrote: Sun Dec 04, 2022 12:30 pm I don't know what "import something as" does, still learning Python.
Your code at github not looks like newbie code ;-) You know a lot about FreeCAD and python ;-)
It looks like you are good developer but you want to test us, if we will see all the "mistakes" or not, for me this is a little funny, because I see you are good developer ;-)

Personally, I would change the big-mambo-jumbo create_cup function into several smaller ones to get better control over the code and make the function shorter and clear, but you probably know about it ;-)

Also at the end there is beauty test flower ;-)

Code: Select all

FreeCADGui.ActiveDocument.ActiveView.fitAll()
without importing FreeCADGui ;-) this will be working as macro at editor maybe but if you move the code as tool "icon" at workbench this will not be working without FreeCADGui import. At FreeCAD it is better to import everything what you use, so the code will be more flexible, mobile, you save your time later, for example if you want to move it as workbench tool.

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
Post Reply