Convert STL object to VTK geometry in python

廉价感情. 提交于 2020-12-15 04:33:08

问题


I am wanting to do Boolean operations on STL files with geometric primitives from the VTK library.

My problem is converting the STL geometry to something that the VTK Boolean objects will except.

I tried the following...

import vtk

filename = 'gyroid.stl' 
reader = vtk.vtkSTLReader()
reader.SetFileName(filename)
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(reader.GetOutputPort())
gyroid = vtk.vtkActor()
gyroid.SetMapper(mapper)

sphere = vtk.vtkSphere()
sphere.SetRadius(30)
sphere.SetCenter(0, 0, 0)

boolean = vtk.vtkImplicitBoolean()
boolean.SetOperationTypeToIntersection()
boolean.AddFunction(gyroid)
boolean.AddFunction(sphere)

But get the following error...

File "D:\Python codes\VTK\untitled8.py", line 29, in <module>
    boolean.AddFunction(gyroid)

TypeError: AddFunction argument %Id: %V

It throws the same error if I replace gyroid with mapper

How do I convert the STL mesh into something useable by VTK? Or cant I do this & need to look elsewhere?


回答1:


The problem is not about converting STL in VTK but about how to use VTK API :)

vtkImplicitBoolean works on implicits function, i.e. classes that can generate data, such as the vtkSphere. See here for doc and here for usage

As you have a loaded dataset, you cannot use this. Instead, use vtkBooleanOperationPolyDataFilter and generate a sphere with the vtkSphereSource. Here and here for examples.

Example

sphere = vtk.vtkSphereSource()
booleanOperation = vtk.vtkBooleanOperationPolyDataFilter()
booleanOperation.SetOperationToIntersection()
booleanOperation.SetInputConnection(0, reader.GetOutputPort())
booleanOperation.SetInputConnection(1, sphere.GetOutputPort())


来源:https://stackoverflow.com/questions/65112987/convert-stl-object-to-vtk-geometry-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!