简体   繁体   English

如何使用 python vtk 将剪辑的 object 保存到 stl

[英]How to save clipped object to stl with python vtk

I was following clipDataSetWithPolyData我在关注clipDataSetWithPolyData

I want to save the clipped square into stl file.我想将剪切后的正方形保存到 stl 文件中。 裁剪图像

It seems that if I use the stl writter to write it out好像如果我用 stl writer 写出来

clipperOutsideMapper = vtkDataSetMapper()
clipperOutsideMapper.SetInputConnection(clipper.GetOutputPort(1))
clipperOutsideMapper.ScalarVisibilityOff()
# use stl writer to write clipped cube into stl file
stlWriter = vtkSTLWriter()
stlWriter.SetFileName("clippedSqure.stl")
stlWriter.SetInputConnection(clipper.GetOutputPort(1))
stlWriter.Write()

It always returns me this error.它总是给我返回这个错误。 I mean it can render the clipped cube in the rendered window but why it cannot be saved?我的意思是它可以在渲染的 window 中渲染裁剪的立方体,但为什么不能保存?

2023-01-29 17:43:08.985 (   5.569s) [          22AD7D]vtkDemandDrivenPipeline:760    ERR| vtkCompositeDataPipeline (0x7fad6c4bb220): Input for connection index 0 on input port index 0 for algorithm vtkSTLWriter(0x7fad6c4bac80) is of type vtkUnstructuredGrid, but a vtkPolyData is required.
2023-01-29 17:43:08.985 (   5.569s) [          22AD7D]vtkDemandDrivenPipeline:760    ERR| vtkCompositeDataPipeline (0x7fad6c4bb220): Input for connection index 0 on input port index 0 for algorithm vtkSTLWriter(0x7fad6c4bac80) is of type vtkUnstructuredGrid, but a vtkPolyData is required.

I figure it out myself.我自己想办法。 The issue was stl writter was asking for a vtkPolyData , however only vtkUnstructuredGrid was provided.问题是 stl 作者要求vtkPolyData ,但只提供了vtkUnstructuredGrid Thus, we need to transfer from vtkUnstructuredGrid to vtkPolyData by using the following code wrote by myself.因此,我们需要使用自己编写的以下代码从vtkUnstructuredGrid转移到vtkPolyData

stlFilter = vtkDataSetSurfaceFilter()
stlFilter.SetInputConnection(clipper.GetOutputPort(1))
stlFilter.Update()

Then you just write stl file now:然后你现在就写stl文件:

stlWriter = vtkSTLWriter()
stlWriter.SetFileName("clippedSqure.stl")
stlWriter.SetInputConnection(stlFilter.GetOutputPort())
stlWriter.Write()

Hope it helps everybody who has similar problems.希望对遇到类似问题的大家有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM