One of the greatest features of PythonOCC is the ease to create a CAD file that permanently stores the shape you created. PythonOCC easily writes and reads the following formats:
- STL (the de-facto standard for 3D printing)
- IGES (in versions 5.1 and 5.3)
- BREP
- STEP (AP 203 and AP214 variants – if you are wondering what the differences are, read here)
Before we move on to more complex shapes than the simple box of the previous post, let’s see how we could save this box to 3D print it.
from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox from OCC.StlAPI import StlAPI_Writer # Create a simple box using the primitives API box_shape = BRepPrimAPI_MakeBox(50,50,50).Shape() # Export to STL stl_writer = StlAPI_Writer() stl_writer.SetASCIIMode(True) stl_writer.Write(box_shape,"box.stl")
It is as simple a that !
The call to .SetASCIIMode(True) is not necessary as ASCII mode is the default to write STL files.
Using .SetASCIIMode(False) would create a binary STL file that is smaller in size than its ASCII counterpart.
You can now try to slice this STL file in Cura, Slic3r or Skeinforge to feed the 3D printer with GCode.
If, in the future, you want to export more than 1 shape to a file, the following rules apply:
- STL and BREP writers can only deal with 1 shape (e.g. 1 box), so if you have many shapes, you have to fuse them so that they can go into a single STL or BREP file
- IGES and STEP writers can deal with multiple shapes (e.g. 1 box + 1 cylinder + 1 freeform shape)
Thank you for your post! Is there an easy way to specify the output resolution of the STL file? I would like to control how fine/course of a tessellation is outputted.
Yes, see the SetRelativeMode(), SetDeflection() and SetCoefficient() methods. If relative mode is enabled, the coefficient (relative to the shape dimensions) is used for meshing/tesselation. Otherwise, the (absolute) deflection is used. You may have a look at StlAPI.py in Lib/site-packages/OCC of your python installation/env.