问题
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