I am trying to save a cube into a vtk and stl files! this is the task my question is I read the docs and save it into .ply file this is the stl extension, so how can I save it as a vtk file with python ! i know the question is not clear but this is who I told can someone explain me anything ? I get confused,thanks in advance
I’m assuming that you have point locations and then the point locations that make up each triangle. I am assuming you are using triangles. You might be able to do it sort of like this (in Python) - I’m not an expert, so here goes:
import vtk
pts = vtk.vtkPoints()
pts.InsertNextPoint(0, 0, 0)
pts.InsertNextPoint(0, 0, 1)
pts.InsertNextPoint(0, 1, 0)
pts.InsertNextPoint(0, 1, 1)
pts.InsertNextPoint(1, 0, 0)
pts.InsertNextPoint(1, 0, 1)
pts.InsertNextPoint(1, 1, 0)
pts.InsertNextPoint(1, 1, 1)
u = vtk.vtkUnstructuredGrid()
u.SetPoints(pts)
u.InsertNextCell(vtk.VTK_HEXAHEDRON, 8, [0, 1, 2, 3, 4, 5, 6, 7])
w = vtk.vtkUnstructuredGridWriter()
w.SetFileName('cube.vtu')
w.SetInputData(u)
w.Write()
c = vtk.vtkDataSetSurfaceFilter()
c.SetInputData(u)
c.Update()
w = vtk.vtkSTLWriter()
w.SetFileName('cube.stl')
w.SetInputConnection(c.GetOutputPort())
w.Write()
# Or, just make a vtkPolyData instead of a vtkUnstructuredGrid.
1 Like
thanks, I understand the part about converting to stl but the vtk part didn’t get it yet! I just started learning vtk yesterday so a lot of things are still not clear! how I can now save the files to .vtu extensions? is vtu the vtk extensions they are the same?
#!~ How to write it into vtk then?
colors = vtkNamedColors()
# filename = get_program_parameters()
cube = vtk.vtkCubeSource()
cube.Update()
# Write the stl file to disk
stlWriter = vtkSTLWriter()
stlWriter.SetFileName('cube.stl')
stlWriter.SetInputConnection(cube.GetOutputPort())
stlWriter.Write()
You are likely looking for vtkXMLUnstructuredGridWriter, just change the writer object constructor and the filename.
The newer format is the XML format. The text format that I had in the script is called “legacy” format.