Skip to content

Commit

Permalink
Fixed and improved the STL exporter example, see issue #296
Browse files Browse the repository at this point in the history
  • Loading branch information
tpaviot committed Jun 10, 2017
1 parent a784419 commit bb13189
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions examples/core_export_stl.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,38 @@

import os
from OCC.StlAPI import StlAPI_Writer
from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox
from OCC.BRepPrimAPI import BRepPrimAPI_MakeTorus
from OCC.BRepMesh import BRepMesh_IncrementalMesh

my_box = BRepPrimAPI_MakeBox(10., 20., 30.).Shape()
# first, create the shape
my_torus = BRepPrimAPI_MakeTorus(20., 10.).Shape()
# then mesh it. This mesh is used by the STL exporter
# Note : if the mesh is not performed, the STL exporter
# won't do nothing
mesh = BRepMesh_IncrementalMesh(my_torus, 0.9)
mesh.Perform()
assert mesh.IsDone()

# set the directory where to output the
directory = os.path.split(__name__)[0]
stl_output_dir = os.path.abspath(os.path.join(directory, "models"))

# make sure the path exists otherwise OCE get confused
assert os.path.isdir(stl_output_dir)
stl_output_file = os.path.join(stl_output_dir, "box.stl")

stl_low_resolution_file = os.path.join(stl_output_dir, "torus_low_resolution.stl")
stl_exporter = StlAPI_Writer()
stl_exporter.SetASCIIMode(True) # change to False if you need binary export
stl_exporter.Write(my_box, stl_output_file)
stl_exporter.Write(my_torus, stl_low_resolution_file)
# make sure the program was created
assert os.path.isfile(stl_low_resolution_file)

# then we change the mesh resolution
#
mesh.SetDeflection(0.05)
mesh.Perform()
assert mesh.IsDone()
stl_high_resolution_file = os.path.join(stl_output_dir, "torus_high_resolution.stl")
# we set the format to binary
stl_exporter.SetASCIIMode(False)
stl_exporter.Write(my_torus, stl_high_resolution_file)
assert os.path.isfile(stl_high_resolution_file)

0 comments on commit bb13189

Please sign in to comment.