[Finished] Part Design - Scripting "Basic Part Design Tutorial" - Comments welcomed

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

[Finished] Part Design - Scripting "Basic Part Design Tutorial" - Comments welcomed

Post by onekk »

Hello to all.

This is my trying to reproduce "Basic Part Design Tutorial" found at:


https://wiki.freecadweb.org/Basic_Part_Design_Tutorial


Starting from this drawing (same as in the linked page above):

Tutorial_Drawing_Sheet.png
Tutorial_Drawing_Sheet.png (96.05 KiB) Viewed 1680 times

Here the results.

20221013_pdtut_ew3.py
(9.75 KiB) Downloaded 45 times

The code is modified thanks to @edwilliams16 help, see images of the "proof of concept" sketches.

https://forum.freecadweb.org/viewtopic. ... 70#p632170

See:

https://forum.freecadweb.org/viewtopic.php?f=21&t=72267

for the entire discussion about making a new "more modern" PD Basic tutorial.



Here the image you will obtain:

20221004-pd_tut_def.png
20221004-pd_tut_def.png (7.39 KiB) Viewed 1707 times

Produced file is:
pd_tut_ew.FCStd
(48.63 KiB) Downloaded 33 times

EDIT: A more modern tutorial is done old files were deleted from this post, old discussion text is retained as quoted text
And I wonder if there is a way to avoid to use selecting "Face" or "Vertex" to avoid TNP.


Any help to improve code, suggestions or comments will be appreciated.
I have one minor problem with an error about:


PositionBySupport: AttachEngine3D: Part has null shape


That is appearing, and I can't find from were it originate.


EDIT: It was a missing recompute.
Any help to improve code, suggestions or comments will be appreciated.

Regards

Carlo D.
Last edited by onekk on Thu Oct 13, 2022 6:41 am, edited 14 times in total.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: [Finished] Part Design - Scripting "Basic Part Design Tutorial" - Comments welcomed

Post by onekk »

Ok I have finished the work and cleaned the page, as nobody has replied, I prefer to show a "decent working code" rather than a list of "request for help" that has lead to nothing useful.


@Kunda1 Could I ask you some help to maybe make this a "Scripting Part Design WB" wiki page?


Sorry for any inconvenience occured during the editing and deleting of the "in progress" posts.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: [Finished] Part Design - Scripting "Basic Part Design Tutorial" - Comments welcomed

Post by Roy_043 »

I have had a quick look at the code. It looks pretty good to me. There is at least one point where you deviate from the tutorial: the first sketch should have an equal constraint.

There are some minor cosmetic issues (names, quoting style, zero written as 0/0.0/0.000000, VEC0 and ROT0 are not used).

I would not use doc.getObject(bd_name) when I already have the object.

To change the sign of a variable that holds a numerical value you use l2 * -1, you can also use -l2.

I would prefer to perform all recomputes in the main function. AFAIK newBody.recompute() right before doc.recompute() does not make sense.

Your code relies on fixed face and vertex indices and therefore may suffer from TNP issues in future versions.

Strangely, although the file is saved by the code, I still have to save it before closing.
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: [Finished] Part Design - Scripting "Basic Part Design Tutorial" - Comments welcomed

Post by onekk »

Roy_043 wrote: Thu Oct 06, 2022 7:57 pm ...
Hello, many thanks for reviewing.

I agree that name, squoting style and other thing are not perfect.

for the VEC0 and ROT0 I have used this convention for a couple of reasons:

- shorten writing many Placements, were Vector(0,0,0) will be more long that VEC0, and for rotation is even worse.
- immediately see that this values is left to zero ()

Some other thing are some remnants of the original "Python Console" echo, but some are intended to be as is as example in:

Code: Select all

def make_sk1(doc, bd_name, sk_name):
    """Make sketch 1."""
    sk = doc.getObject(bd_name).newObject('Sketcher::SketchObject', sk_name)
you could pass an instance of the document (that could even be different from the active document) and the method is working.

I'm interested in a way to avoid "as much as possible TNP" do you have some hints?

For the file things, I suppose that is related to the setview() that maybe coudl trigger the Touched flag in the file.

I have a corrected version of the file with some improvement, but I don't want to flood this thread, if you permit I will PM you to work on some modification to make a better example script.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: [Finished] Part Design - Scripting "Basic Part Design Tutorial" - Comments welcomed

Post by mario52 »

Hi onekk

picolo problema

wikiExample00.png
wikiExample00.png (8.32 KiB) Viewed 1463 times

Code: Select all

OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.21.30492 (Git)
Build type: Release
Branch: master
Hash: b4578fb3eae842eb510a1bc055d07d369a67ce94
Python 3.8.13, Qt 5.15.4, Coin 4.0.0, Vtk 9.1.0, OCC 7.6.3

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.
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: [Finished] Part Design - Scripting "Basic Part Design Tutorial" - Comments welcomed

Post by Roy_043 »

Regarding TNP:
Thinking about this some more it is probably unwise to invest time in this. You would do that to handle new versions of OCCT that might decide to create the sub elements of an object in a different order from the current version. But the final model itself would still be sensitive to the TNP problem.
It would make more sense to create a version of the model that avoids the TNP problem all together.

EDIT: Mario's post suggests that looking at this may be needed after all.

onekk wrote: Fri Oct 07, 2022 7:55 am you could pass an instance of the document (that could even be different from the active document) and the method is working.
That is correct. But I would prefer this signature for the function: make_sk1(sk_object).

Feel free to PM a new version of the code, I'll try to look at it next week. But keep in mind that I am not a Python specialist. :mrgreen: And keeping the discussion in the forum may be more fruitful.

EDIT: 20221004-PDtut_v6.py works fine here:

Code: Select all

OS: Windows 8.1 Version 6.3 (Build 9600)
Word size of FreeCAD: 64-bit
Version: 0.21.30492 (Git)
Build type: Release
Branch: master
Hash: b4578fb3eae842eb510a1bc055d07d369a67ce94
Python 3.10.6, Qt 5.15.4, Coin 4.0.0, Vtk 9.1.0, OCC 7.6.3
Locale: C/Default (C) [ OS: Dutch/Netherlands (nl_NL) ]
Installed mods: 
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: [Finished] Part Design - Scripting "Basic Part Design Tutorial" - Comments welcomed

Post by onekk »

Roy_043 wrote: Sat Oct 08, 2022 10:25 am ...
Thanks, I'm working on a more compact version of the code, so probably tomorrow, I will post here the new iteration.

My concerns are not regarding Python, but the underlying structure of the document, Attachments, datum planes and such things, eventually the way to transpose the GUI work in Python, but this could be an interesting challenge.

Thanks again and regards.

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: [Finished] Part Design - Scripting "Basic Part Design Tutorial" - Comments welcomed

Post by edwilliams16 »

@onekk
It has bothered me that the Basic Part Design Tutorial freely attaches sketches to faces, which is recommended against in the forum.
It also seems simpler to me to make the central pocket symmetrically from the YZ_Plane referencing the first sketch, rather than using the projection onto the rear face.

Here's two methods that use only sketches on the cardinal planes together with external references and expressions connecting them. In the first case sketch dimensions reference pad lengths, in the latter a "master sketch" provides these.

Here's the sketch network:
Screen Shot 2022-10-10 at 10.18.35 AM.png
Screen Shot 2022-10-10 at 10.18.35 AM.png (21.5 KiB) Viewed 1325 times
Attachments
PDtutorialEW.FCStd
(111.53 KiB) Downloaded 35 times
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: [Finished] Part Design - Scripting "Basic Part Design Tutorial" - Comments welcomed

Post by onekk »

edwilliams16 wrote: Mon Oct 10, 2022 8:24 pm ...
Many thanks, I will study and try to reproduce it with scripting.

It may be take some time, but don't fear :D I will bug you if I have problems.

Many thanks again and Best Regards.

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: [Finished] Part Design - Scripting "Basic Part Design Tutorial" - Comments welcomed

Post by Roy_043 »

Related discussion in the Wiki forum:
https://forum.freecadweb.org/viewtopic.php?f=21&t=72267
Post Reply