Generate a library of parts using python

Have some feature requests, feedback, cool stuff to share, or want to know where FreeCAD is going? This is the place.
Forum rules
Be nice to others! Read the FreeCAD code of conduct!
Post Reply
asharsid45
Posts: 2
Joined: Sat Aug 06, 2022 6:21 pm

Generate a library of parts using python

Post by asharsid45 »

Hello All!

Problem statement -- I want to create a library of different springs. I have linked all the dimensions of the spring with the spreadsheet, So I have to change the 4 variables(length, wire diameter, inner radius, pitch) to create another spring, and save it in three formats (i.e .FCstd, .STEP, .IGES) I have all the dimensions in an excel file. To do this task manually is taking a lot of time, is there any way I can generate the whole library using automation?



thank you for your time !!
Peace
Last edited by asharsid45 on Tue Aug 16, 2022 9:20 am, edited 1 time in total.
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: Generate a library of parts using python

Post by adrianinsaval »

This can definitively be done through python, share more details and someone might come up with a script.

Also consider using Spreadsheet_Workbench#Configuration_tables
asharsid45
Posts: 2
Joined: Sat Aug 06, 2022 6:21 pm

Re: Generate a library of parts using python

Post by asharsid45 »

Hello, Thanks for your reply,

I apologise for the late reply.

I have tried to simplify the task, Can someone build a script that can automatically generate any number of instances for the below geometry on the basis of two parameters i.e radius and pad!

The dimensions are stored in the excel file and the generated models should be saved in FCstd, IGES and STEP formats according to the names in the excel file.

Image

All the relevant files can be accessed using this link.
https://drive.google.com/drive/folders/ ... sp=sharing
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: Generate a library of parts using python

Post by adrianinsaval »

As first step I recommend using a configuration table, then make a macro that iterates on all the configuration name values, recompute and save as + export. Assuming you make a configuration table as in the attached file you would do this:

Code: Select all

doc=App.activeDocument()
import ImportGui
for i in range(0,14):
  doc.Body.Configuration=i
  doc.recompute()
  doc.saveAs(f"/some/path/{doc.Body.Configuration}.FCStd")
  ImportGui.export([doc.Body],f"/some/path/{doc.Body.Configuration}.step")
  ImportGui.export([doc.Body],f"/some/path/{doc.Body.Configuration}.iges")
  
there might be some way to check how many items the Configuration enum has and use that instead of a hardcoded range so the macro auto adapts, but I don't know how to do that. @realthunder do you know how I can iterate through all the values in a configuration table in python?

You could also make some sort of GUI that ask for a path to save the file if you want to. As a side note, you used a reference to a cell number for your pad length, you should use the assigned alias instead to make sure the reference doesn't break if you move the cell.
Attachments
test.FCStd
(12.64 KiB) Downloaded 4 times
Post Reply