title: Converting a STEP file into an STL
body: <p>I am looking for suggestions on how to tackle the following problem:</p>
  
  <blockquote>
    <p>To convert a STEP (ISO 10303, AP 203/214) into a triangular mesh, i.e. into STL</p>
  </blockquote>
  
  <p>Obviously, STEP supports various exact representations of some smooth curves, for instance NURBS, so the two are not isomorphic, hence there is no 'translating' one into another.</p>
  
  <p>The research I have done so far suggested that the way to go would be to use some kind of CAD software, which I did - I wrote the following simple script using <code>PythonOCC</code>:</p>
  
  <pre><code>def read_step(filename):
      from OCC.STEPControl import STEPControl_Reader
      from OCC.IFSelect import IFSelect_RetDone, IFSelect_ItemsByEntity
  
      step_reader = STEPControl_Reader()
      status = step_reader.ReadFile(filename)
      if status == IFSelect_RetDone:
          failsonly = False
          step_reader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity)
          step_reader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity) 
  
          ok = step_reader.TransferRoot(1)
          _nbs = step_reader.NbShapes()
          return step_reader.Shape(1)
      else:
          raise ValueError('Cannot read the file')
  
  def write_stl(shape, filename, def=0.1):
      from OCC.StlAPI import StlAPI_Writer
      import os
  
      directory = os.path.split(__name__)[0]
      stl_output_dir = os.path.abspath(directory)
      assert os.path.isdir(stl_output_dir)
  
      stl_file = os.path.join(stl_output_dir, filename)
  
      stl_writer = StlAPI_Writer()
      stl_writer.SetASCIIMode(False)
  
      from OCC.BRepMesh import BRepMesh_IncrementalMesh
      mesh = BRepMesh_IncrementalMesh(shape, def)
      mesh.Perform()
      assert mesh.IsDone()
  
      stl_writer.Write(shape, stl_file)
      assert os.path.isfile(stl_file)
  
  ...
  </code></pre>
  
  <p>Those functions are capable of doing the job. However, the result isn't always very satisfactory, for instance:</p>
  
  <p><a href="https://i.stack.imgur.com/MhPFd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MhPFd.png" alt="The original STEP file - 76K"></a>
  The original STEP file - 76K</p>
  
  <p><a href="https://i.stack.imgur.com/w8OiI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/w8OiI.png" alt="Resulting STL file with def=0.9 - 16K"></a>
  Resulting STL file with def=0.9 - 16K</p>
  
  <p><a href="https://i.stack.imgur.com/XMasu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XMasu.png" alt="Resulting STL file with def=0.1 - 116K"></a>
  Resulting STL file with def=0.1 - 116K</p>
  
  <p>Obviously, there is little reason to believe a generic converter is achievable, but I'm here to ask for some more insight. I would appreciate tips on resources that might be useful to help me take on this problem.</p>
  
  <p>I would like to try and achieve the following results:</p>
  
  <ul>
  <li>Hopefully make it work for as many models as possible</li>
  <li>Not gain too much weight on the files</li>
  </ul>
  
  <p>I would appreciate any hints and suggestions on where to look!</p>