multi-sketch sweep scripting

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
lelegossalas
Posts: 11
Joined: Wed Mar 18, 2020 1:59 pm

multi-sketch sweep scripting

Post by lelegossalas »

Hi,

I need to create a solid using a lot of sections (currently 30+, making scripting essential).
All the sections have been created using python script. For prototyping, the sections are just made with the outter box of the shape. Constraints dimensions/position are set in spreadsheet page.

I have manually tried both the PartDesign workbench tool (AdditiveLoft/Pipe) and the Part workbench sweep tools, but I got 2 problems:
- 1st one is that the python console do not display the commands to add sections. So I don't know how to script that
- 2nd one is that the smoothing generates some very strange curves. I did not find how to generate a linear result using brken lines instead of smooth curves.

Thanks for help.

PS:
I'm currently using freecad 0.18 (16110) on windows10, but I can update the version if required.
Attachments
CaptureLissageSection.PNG
CaptureLissageSection.PNG (40.55 KiB) Viewed 633 times
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: multi-sketch sweep scripting

Post by openBrain »

Welcome. Please attach file and/or current script, and please post your full FreeCAD info as well. ;)
lelegossalas
Posts: 11
Joined: Wed Mar 18, 2020 1:59 pm

Re: multi-sketch sweep scripting

Post by lelegossalas »

OS: Windows 10
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.16110 (Git)
Build type: Release
Branch: (HEAD detached at upstream/releases/FreeCAD-0-18)
Hash: f7dccfaa909e5b9da26bf50c4a22ccca9bb10c40
Python version: 3.6.6
Qt version: 5.6.2
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: French/France (fr_FR)

I have concatenated my several script files in a single one.
Just run it from a <no-opened-project> state in freecad, the script will create the project, the part body, the data sheet and all the sketches.
Attachments
help_section_script.py
(10.47 KiB) Downloaded 29 times
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: multi-sketch sweep scripting

Post by openBrain »

Attached your macro with ... some corrections :) ... and the loft. ;)
I'll comment a bit later on. Feel free to ask if further needed. ;)
Attachments
help_section_script.py
(10.33 KiB) Downloaded 33 times
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: multi-sketch sweep scripting

Post by openBrain »

OK, so some bulk remarks :
  • Better use 4-space indentation. This is standard. ;)
  • When coding a script, you basically don't need to reproduce all actions you do when using the GUI, such as activating objects or workbenches, recomputing, ... I commented all this line out in the macro I posted above.
  • You should notice that (almost) all functions that create a new object return a reference to this object. So you can take advantage of that to store it and use it in subsequent operations. So you're sure which object you're addressing. I extensively use this in the macro above.
  • I uses 'len(DataList)' (which represents the length of data table) in the loops rather than a separate variable. So if you're data table grows, it will adapt automatically.
  • I uses string formatting (old style) to get 'iteratorString'
  • I uses a list to store sketches when they are created so I can then use it to create the loft
  • Loft creation is quite simple. 'Profile' is the base section. 'Sections' holds the other sections in order they have to be processed. 'Ruled' tells if it shall be "smoothed" or not. 'Closed' tells if the shape shall be closed on the ends.
Hope that helps
lelegossalas
Posts: 11
Joined: Wed Mar 18, 2020 1:59 pm

Re: multi-sketch sweep scripting

Post by lelegossalas »

Great, thanks a lot.
Looks so simple...

Thanks too for corrections and advices. I'm not really as familiar yet with python than with C/C++ and many other langages.
lelegossalas
Posts: 11
Joined: Wed Mar 18, 2020 1:59 pm

Re: multi-sketch sweep scripting

Post by lelegossalas »

Hi,

I'm still having trouble with my script.
In fact, the script looks good, but the call to loft function call generate error when the 1st section is included.
The error says that the number of segments is not the same (I can't remember how I get this information, most of time I only get the "ERROR" status information)

Since the beginning of the topic, the loft shape has been improved : one corner of the rectangle now uses ellipse.
All shapes are still same (iterative creation), only dimensions change.
The particularity of the 1st section is that the ellips is very flat compared to other section. I have tried to change the parameters to make it more circular, but error remains.
Small axis of ellipse may be either horizontal or vertical, but that does not seem to be a problem when loft is restricted (exclusion of a couple sections for each side)

Using smooth (myLoft.Ruled = False) works to create objet, but I don't need smoothing, which make more complex objets and generates some strange shapes around first and/or last sections.

I don't know how I can debug this :(
Is there a way to know how many segments are used for each section ?

Thanks for help.

Regards.


OS: Windows 10
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.4 (GitTag)
Build type: Release
Branch: releases/FreeCAD-0-18
Hash: 980bf9060e28555fecd9e3462f68ca74007b70f8
Python version: 3.6.6
Qt version: 5.6.2
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: French/France (fr_FR)
Attachments
temp_help.py
(18.31 KiB) Downloaded 48 times
Post Reply