Convert TopoDS_Shape from opencascade.js to vtkPolyData from vtk.js (Triangulation failed at cellOffset)

Hi everyone,

New guy here trying to make some shit :]
I’m trying to use opencascade.js and vtk.js together and basically my goal is to open some .STEP file with opencascade.js and render it with vtk.js. So I get TopoDS_Shape instance from opencascade.js and now I don’t really know how to convert TopoDS_Shape into vtkPolyData so I can render it?

Thanks in advance!

Update 2:This is where I get so far…

      function generateVtkPolyData(vertices, faces) {
        const vtkPolyData = vtk.Common.DataModel.vtkPolyData;
        const vtkPoints = vtk.Common.Core.vtkPoints;
        const vtkCellArray = vtk.Common.Core.vtkCellArray;
        const polyData = vtkPolyData.newInstance();
        const points = vtkPoints.newInstance();
        points.setNumberOfPoints(vertices.length);
        vertices.forEach((vertex, index) => {
          points.setPoint(index, vertex);
        });
        const polys = vtkCellArray.newInstance();
        faces.forEach((face) => {
          polys.insertNextCell([face.length, ...face]);
        });
        polyData.setPoints(points);
        polyData.setPolys(polys);

        return polyData;
      }

      initOpenCascade().then(openCascade => {
        document.getElementById("step-file").addEventListener('input', async (event) => { 
          const shape = await loadSTEPorIGES(openCascade, event.srcElement.files[0]); 
          const fullScreenRenderer = vtk.Rendering.Misc.vtkFullScreenRenderWindow.newInstance();
          const actor = vtk.Rendering.Core.vtkActor.newInstance();
          const mapper = vtk.Rendering.Core.vtkMapper.newInstance();
          const vtkTriangleFilter = vtk.Filters.General.vtkTriangleFilter;
          /* shape vertices and faces for simple box 10x10x10:
          shape.vertices = [
            [-5, -5, -5],
            [-5, -5, 5],
            [-5, 5, -5],
            [-5, 5, 5],
            [5, -5, -5],
            [5, -5, 5],
            [5, 5, -5],
            [5, 5, 5],
          ]
          shape.faces = [
            [0, 1, 2, 3],
            [4, 5, 6, 7],
            [0, 1, 4, 5],
            [2, 3, 6, 7],
            [0, 2, 4, 6],
            [1, 3, 5, 7],
          ]
          */
          const polyData = generateVtkPolyData(shape.vertices, shape.faces)
          const triangleFilter = vtkTriangleFilter.newInstance(); 
          triangleFilter.setInputData(polyData);
          triangleFilter.update();

          actor.setMapper(mapper);
          mapper.setInputData(triangleFilter.getOutputData());
          const renderer = fullScreenRenderer.getRenderer();
          renderer.addActor(actor);
          renderer.resetCamera();

          const renderWindow = fullScreenRenderer.getRenderWindow();
          renderWindow.render();
        });
      });

The problem that I have now is with vtkTriangleFilter:
Triangulation failed at cellOffset 0
Triangulation failed at cellOffset 6
Triangulation failed at cellOffset 12
Triangulation failed at cellOffset 18
Triangulation failed at cellOffset 24
Triangulation failed at cellOffset 30

What is wrong here?

Remove the vtkTriangleFilter it is not needed.

Also it might be easier and more efficient to create typed array directly and set them to the points/cells.

const points = new Float32Array(vertices.flat());
const polys = new Uint16Array(faces.map((f) => [f.length, ...f]).flat());
polydata.getPoints().setData(points, 3);
polydata.getPolys().setData(polys);

mapper.setInputData(polydata);

Hey thanks for the response!

It almost work, this is what i get as output: