Is this a good Python-FreeCAD script?
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
-
- Posts: 26
- Joined: Mon Oct 03, 2022 9:29 am
Is this a good Python-FreeCAD script?
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.
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.
-
- Veteran
- Posts: 3107
- Joined: Thu Sep 24, 2020 10:31 pm
- Location: Hawaii
- Contact:
Re: Is this a good Python-FreeCAD script?
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.
-
- Posts: 26
- Joined: Mon Oct 03, 2022 9:29 am
Re: Is this a good Python-FreeCAD script?
Thanks, though I am surprised.Looks fine.
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.Or, if you would prefer to stay in PD, do a pocket to hollow the cylinder.
Re: Is this a good Python-FreeCAD script?
Hi
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
... i must stop my FreeCAD with gestionnaire des tâche
_____________________________
after correction ok
bon upgrade
mario
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
... 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.
My macros on Gist.github here complete macros Wiki and forum.
-
- Posts: 26
- Joined: Mon Oct 03, 2022 9:29 am
Re: Is this a good Python-FreeCAD script?
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.
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.
Re: Is this a good Python-FreeCAD script?
My philosophy for the code is simple: "Code is good, if working."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
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:
into something like this:
Code: Select all
extrude.Placement = App.Placement(App.Vector(0,0,wall-0.5),App.Rotation(App.Vector(1,0,0),0))
never use App, or qApp, rather not use "import something as sm", don't be so lazyCode: 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)
Thanks
Darek
github.com/dprojects
workbench for woodworking is available at: github.com/dprojects/Woodworking
Re: Is this a good Python-FreeCAD script?
Hi
now adapt this:
mario
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)
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.
My macros on Gist.github here complete macros Wiki and forum.
-
- Posts: 26
- Joined: Mon Oct 03, 2022 9:29 am
Re: Is this a good Python-FreeCAD script?
The code still works, so thanks.
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.
You are right, but I already use my real name everywhere.
Done (and I agree).dprojects wrote: ↑Sat Nov 26, 2022 8:26 pm [*] Make more modular, to achieve shorter lines: this:into something like this:Code: Select all
extrude.Placement = App.Placement(App.Vector(0,0,wall-0.5),App.Rotation(App.Vector(1,0,0),0))
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)
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!
-
- Posts: 26
- Joined: Mon Oct 03, 2022 9:29 am
Re: Is this a good Python-FreeCAD script?
Added the "import mesh". And thanks for this popup example, it works.mario52 wrote: ↑Sat Nov 26, 2022 9:43 pm Hi
yes
now adapt this:
marioCode: 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)
One of my future goals is to write a useful FreeCAD macro.
Re: Is this a good Python-FreeCAD script?
Your code at github not looks like newbie code You know a lot about FreeCAD and pythonPaulCobbaut wrote: ↑Sun Dec 04, 2022 12:30 pm I don't know what "import something as" does, still learning 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()
Thanks
Darek
github.com/dprojects
workbench for woodworking is available at: github.com/dprojects/Woodworking