FreeCAD & Python | Using the API for automation
Dr Pi
Introduction to FreeCAD Python API. Automate the creation of parts from a CSV (or database) - In this video I use Pandas to read a CSV and send the values as variables into FreeCAD.
The FreeCAD code is programmed mainly in C++, but relies heavily on Python The FreeCAD source code is fully multi-platform
FreeCAD can be compiled and run without its Graphical User Interface, the code in App is independent of any GUI-related library
-solid core functionality is programmed in C++ and end-user tools are written in Python -To get started we will look at creating parts from a CSV, without using the GUI -This could be useful for 3d printing, eg export files as .stl
Example of what can be made with FreeCAD : š https://forum.freecadweb.org/viewtopic.php?f=24&t=59466
FreeCAD API official documentation š https://wiki.freecadweb.org/FreeCAD_API
TL;DW conda install PyQt š https://anaconda.org/anaconda/pyqt
Install FreeCAD: š https://forum.freecadweb.org/viewtopic.php?f=4&t=56744 š https://anaconda.org/freecad/freecad
git clone https://github.com/FreeCAD/FreeCAD.git freecad-source (get the dependencies using your method of choice) mkdir freecad-build cd freecad-build cmake ../freecad-source make sudo make install
or
sudo add-apt-repository -y ppa:freecad-maintainers/freecad-stable sudo apt-get update sudo apt-get install freecad
Download Macro: š https://wiki.freecadweb.org/Macro_Pyramid
Run from command line - no GUI!
Example Code: file is called fc.py
#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ #|r|e|d|a|n|d|g|r|e|e|n|.|c|o|.|u|k| #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
import FreeCAD as App #from PySide import QtGui
import pandas as pd
df = pd.read_csv("c.csv") # (read from SQL eventually)
j = len(df)
for i in range(0,j):
fn =(df.iloc[i]['name']) lw =(df.iloc[i]['width']) lh =(df.iloc[i]['height'])
Make new document for each part - per line of CSV
doc = App.newDocument() doc.FileName = fn + ".FCStd"
App.ActiveDocument.addObject("Part::Box","Box") App.ActiveDocument.ActiveObject.Label = "Cube" App.ActiveDocument.recompute()
#Gui.SendMsgToActiveView("ViewFit")
Uses object called by internal name 'Box'
my_box = FreeCAD.ActiveDocument.getObject('Box') my_box.Height = float(lw) my_box.Width = float(lh)
doc.save()
Run with example data file : "c.csv"
name,height,width a1,10,33 a2,3,44 a3,8,28 a4,14,38
From cli type:
[rag@~]$ freecadcmd fc.py
It will make 4 parts, with sizes as per your csv.
Visit redandgreen blog for more Tutorials
105515338 Bytes