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?

1 Answer 1

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.

8
  • 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? Apr 19, 2019 at 6: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: freecadweb.org/wiki/Embedding_FreeCAD
    – sliptonic
    Apr 20, 2019 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? Apr 22, 2019 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, 2019 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': ''}" Apr 24, 2019 at 17:40

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.