如何将 VTK 渲染保存为 .vtk 或 .stl?

How to save a VTK render as a .vtk or .stl?

我创建了最初在 Networkx 中创建的 3D 网络的渲染图,但是现在我有了这个渲染图,我最终想将它导出为单个 .stl 文件。从下面的代码中,我将如何将字形、管、球组合到一个文件中。如果无法导出为 .stl,.vtk 也可以,因为它可以在 Paraview 中转换。

    np[n]=node_pos[n] 
nodePoints = vtk.vtkPoints() 

i=0 

for (x,y,z) in np.values(): 
    nodePoints.InsertPoint(i, x, y, z) 
    i=i+1 

# Create a polydata to be glyphed. 
inputData = vtk.vtkPolyData() 
inputData.SetPoints(nodePoints) 

# Use sphere as glyph source. 
balls = vtk.vtkSphereSource() 
balls.SetRadius(.1) 
balls.SetPhiResolution(20) 
balls.SetThetaResolution(20) 

glyphPoints = vtk.vtkGlyph3D() 
glyphPoints.SetInputData(inputData) 
glyphPoints.SetSourceData(balls.GetOutput()) 

glyphMapper = vtk.vtkPolyDataMapper() 
glyphMapper.SetInputData(glyphPoints.GetOutput()) 

glyph = vtk.vtkActor() 
glyph.SetMapper(glyphMapper) 
glyph.GetProperty().SetDiffuseColor(plum) 
glyph.GetProperty().SetSpecular(.3) 
glyph.GetProperty().SetSpecularPower(30) 

# Generate the polyline for the spline. 
points = vtk.vtkPoints() 
edgeData = vtk.vtkPolyData() 

# Edges 

lines = vtk.vtkCellArray() 
i=0 
for e in G.edges: 

    u=e[0] 
    v=e[1] 

    lines.InsertNextCell(2) 

    for n in (u,v): 
        (x,y,z)=node_pos[n] 
        points.InsertPoint(i, x, y, z) 
        lines.InsertCellPoint(i) 
        i=i+1 

edgeData.SetPoints(points) 
edgeData.SetLines(lines) 


Tubes = vtk.vtkTubeFilter() 
Tubes.SetNumberOfSides(16) 
Tubes.SetInputData(edgeData) 
Tubes.SetRadius(0.05)       # edge RADIUS 

profileMapper = vtk.vtkPolyDataMapper() 
profileMapper.SetInputData(Tubes.GetOutput()) 

balls.Update()

glyphPoints.Update()

Tubes.Update()    


profile = vtk.vtkActor() 
profile.SetMapper(profileMapper) 
profile.GetProperty().SetDiffuseColor(banana) 
profile.GetProperty().SetSpecular(.3) 
profile.GetProperty().SetSpecularPower(30) 


ren = vtk.vtkRenderer() 
renWin = vtk.vtkRenderWindow() 
renWin.AddRenderer(ren) 

iren = vtk.vtkRenderWindowInteractor() 
iren.SetRenderWindow(renWin) 

ren.AddActor(glyph) 
ren.AddActor(profile) 

renWin.SetSize(1000, 1000) 

iren.Initialize() 
renWin.Render() 
iren.Start() 

VTK 有导出器 类,您可以在这里看到: https://vtk.org/doc/nightly/html/classvtkExporter.html

其中,我认为 OBJ 是最接近 STL 的。您可以将场景导出为 OBJ,然后使用 MeshLab 将该 OBJ 转换为 STL。 VRML 也可以。