在python中将STL对象转换为VTK几何 [英] Convert STL object to VTK geometry in python

查看:69
本文介绍了在python中将STL对象转换为VTK几何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对来自VTK库中具有几何图元的STL文件执行布尔运算.

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

我的问题是将STL几何体转换为VTK布尔对象将除外的东西.

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

如果我将 gyroid 替换为 mapper

如何将STL网格转换为VTK可以使用的东西?或者我不能这样做&需要去别的地方找

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

推荐答案

问题不在于在VTK中转换STL,而在于如何使用VTK API:)

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

vtkImplicitBoolean 适用于隐式函数,即可以生成数据的类,例如vtkSphere.在此处查看文档此处用于用法

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

由于已加载数据集,因此无法使用它.而是使用 vtkBooleanOperationPolyDataFilter 并使用 vtkSphereSource 生成一个球体.此处

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.

示例

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

这篇关于在python中将STL对象转换为VTK几何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆