[Solved] Sketcher, (PartDesign sketcher) how to script constraints, in an efficient way?

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

[Solved] Sketcher, (PartDesign sketcher) how to script constraints, in an efficient way?

Post by onekk »

Hello, I'm trying to script your asm4_tut1 in:

https://github.com/Zolko-123/FreeCAD_Ex ... Tutorial1/

I have this sketch to reproduce with code:
Sketch_Axis.png
Sketch_Axis.png (19.47 KiB) Viewed 564 times

I have put together this code:
20220922-asm4_tut1.py
(5.01 KiB) Downloaded 20 times
But It will fail adding the two constraints, that are intended to be for the first (ln1) and third line (ln3).

Tested on:

Code: Select all

OS: Artix Linux (openbox)
Word size of FreeCAD: 64-bit
Version: 0.20.1.29410 (Git) AppImage
Build type: Release
Branch: (HEAD detached at 0.20.1)
Hash: f5d13554ecc7a456fb6e970568ae5c74ba727563
Python 3.10.5, Qt 5.15.4, Coin 4.0.0, Vtk 9.1.0, OCC 7.6.2
Locale: Italian/Italy (it_IT)
Installed mods: 
  * Assembly4 0.12.4
  * toSketch 1.0.1
  * Curves 0.5.8
Any help will be appreciated.

TIA and Regards.

Carlo D.
Last edited by onekk on Sun Sep 25, 2022 7:52 am, edited 1 time 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: Sketcher, (PartDesign sketcher) how to script constraints, in an efficient way?

Post by onekk »

Partially solved:

I have to make all points coincidents.

Code: Select all

print(sk.GeometryCount)

for l_idx in range(len(lines) -1):
    sk.addConstraint(Sketcher.Constraint('Coincident',l_idx,2,l_idx + 1,1))

# last line is not sequential
sk.addConstraint(Sketcher.Constraint('Coincident',len(lines) -1,2,0,1))
And then assign 'Horizontal' constraints,

Code: Select all

for line_n in (0, 2, 4, 6, 8, 10):
    sk.addConstraint(Sketcher.Constraint('Horizontal', line_n))

Now improving things.

However any help is appreciated, it is always interesting to see how other will code things.

I will update my progress here.

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
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: [Solved] Sketcher, (PartDesign sketcher) how to script constraints, in an efficient way?

Post by onekk »

Totally solved, solution in:

https://forum.freecadweb.org/viewtopic. ... 26#p628526

Regards and Thanks to all.

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/
Post Reply