Convert STEP file type to STL
Asked Answered
A

1

6

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?

Anchylose answered 16/4, 2019 at 18:49 Comment(0)
B
16

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.

Beaudoin answered 17/4, 2019 at 17:20 Comment(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?Anchylose
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_FreeCADBeaudoin
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?Anchylose
I tested by importing a .dae and exporting an .stl and it worked fine. I did have to manually install pycollada first.Beaudoin
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': ''}"Anchylose
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.Anchylose
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: #55836764Anchylose
More information on FreeCAD: #76666137. In this post, a link is included to a set up and debugging on FreeCAD.Nada

© 2022 - 2024 — McMap. All rights reserved.