5

I want to convert a STEP file into an STL file format using Python. I have looked online and it looks like the best option is to either use FreeCAD or OpenCascade (OCC). However, I am a beginner and do not know where to start from. I did some search online and found this out (a code to convert STEP to OBJ file).

Are there any python examples from FreeCAD (based on OCC) to convert STEP files to STL? Where should I start?

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
Umar Dastgir
  • 688
  • 9
  • 25

1 Answers1

14

Here's a quick bit of code to start out:

import FreeCAD
import Part
import Mesh
shape = Part.Shape()
shape.read('my_shape.step')
doc = App.newDocument('Doc')
pf = doc.addObject("Part::Feature","MyShape")
pf.Shape = shape
Mesh.export([pf], 'my_shape.stl')

FreeCAD uses python extensively for user-facing functions. Basically, anything you do through the UI is done with python.

So it's useful to open up the UI, open up the Python console, and then do a function manually. You can often just copy the python directly from the console and edited it to serve your needs.

sliptonic
  • 460
  • 3
  • 11
  • Thanks for your response. I am a complete beginner in this stream. Is it possible to download the FreeCAD, Part and Mesh libraries using pip and run and IDLE script? How do I proceed on making a standalone python script for this? – Umar Dastgir Apr 19 '19 at 06:35
  • 1
    Not with Pip. The Mesh and Part libraries are installed with FreeCAD and should be in the system Path. So the above script should work as a standalone python script. See this page for more: https://www.freecadweb.org/wiki/Embedding_FreeCAD – sliptonic Apr 20 '19 at 18:30
  • I was able to convert STEP to STL file format. However, I am running into a problem converting Collada (.dae) to STL. I have noticed that FreeCAD software lets you import file in .dae and save it as .stl format. However, I can't find any code for that. Do you know of a way I can accomplish that? – Umar Dastgir Apr 22 '19 at 21:16
  • I tested by importing a .dae and exporting an .stl and it worked fine. I did have to manually install pycollada first. – sliptonic Apr 23 '19 at 22:49
  • In the above code you gave I tried to import .dae file by replacing shape.read('my_shape.step') by shape.read('my_shape.dae') (and I have a my_shape.dae file in the same directory). I have also installed pycollada ind imported that module (import collada). However, I get the error at shape.read('my_shape.dae') file that "shape.read('wolf.dae') Base.FreeCADError: {'sclassname': 'N4Base13FileExceptionE', 'sErrMsg': 'Unknown extension', 'sfile': '', 'iline': 0, 'sfunction': '', 'swhat': '', 'btranslatable': False, 'breported': True, 'filename': ''}" – Umar Dastgir Apr 24 '19 at 17:40
  • So basically the error tells me "Unknown extension". Can you let me know what environment you are using (version of FreeCAD, pycollada, python etc.). Are there any other modifications I need to make in the code above? Shape.read always gives an error of unsupported format. – Umar Dastgir Apr 24 '19 at 17:43
  • I have posted this question in the link given below. If you can answer it there, I can choose it as the accepted answer Link: https://stackoverflow.com/questions/55836264/freecad-shape-read-error-unsupported-format – Umar Dastgir Apr 24 '19 at 18:28
  • More information on FreeCAD: https://stackoverflow.com/questions/76665637/coding-library-to-convert-step-to-stl-without-large-output-file-sizes. In this post, a link is included to a set up and debugging on FreeCAD. – M. Nicol Jul 12 '23 at 16:56