简体   繁体   English

plot vtk 使用 python

[英]plot vtk using python

I'm using python to plot a VTK data.我正在使用 python 到 plot 一个 VTK 数据。 I'm following this example: http://www.tfd.chalmers.se/~hani/kurser/OS_CFD_2007/presentation_rasmus.pdf and I'm using the same OpenFOAM tutorial elbow, with exactly the same archive elbow_200.vtk.我正在关注这个示例: http://www.tfd.chalmers.se/~hani/kurser/OS_CFD_2007/presentation_rasmus.pdf ,我使用的是相同的 OpenFOAM 教程肘部,具有完全相同的存档。

If I copy the code as it is, I got the error: AttributeError: 'vtkmodules.vtkFiltersCore.vtkFieldDataToAttributeD' object has no attribute 'SetInput'如果我按原样复制代码,则会收到错误消息:AttributeError: 'vtkmodules.vtkFiltersCore.vtkFieldDataToAttributeD' object has no attribute 'SetInput'

Then I see that: https://vtk.org/doc/release/5.0/html/a01389.html#z1879_2 and realized that I have only SetInputFieldToDataObjectField.然后我看到: https://vtk.org/doc/release/5.0/html/a01389.html#z1879_2并意识到我只有 SetInputFieldToDataObjectField。

I tried...我试过了...

import os
from vtk import *
from vtk import vtkUnstructuredGridReader

#set the fileName for the current case
myFileName='elbow_200.vtk'

#Need a reader for unstructured grids
reader = vtkUnstructuredGridReader()

reader.SetFileName(myFileName)

reader.Update()

#In OpenFOAM all results are Field-data.
#This has no concept of cells or nodes.
#Need to filter to cells.


toCellFilter = vtkFieldDataToAttributeDataFilter()
toCellFilter.SetInputFieldToDataObjectField()
toCellFilter.SetInputFieldToCellDataField() #celulas
toCellFilter.SetOutputAttributeDataToCellData() 

#Assign here which field
#we are interested in.
toCellFilter.SetScalarComponent(0, 'alpha.air' ,0)
#This is all we need to do do calculations.
#To get 3D image, need some more components.
#First a window

renWin = vtkRenderWindow()

ren1 = vtkRenderer()
#Add renderer to window
renWin.AddRenderer(ren1)
#Add pressure data to the renderer.

#Mapping assigns data to colors and geometry.
mapper = vtkDataSetMapper()
mapper.SetInputData(toCellFilter.GetOutput())


#The object is assigned to an actor.
actor = vtkActor()
actor.SetMapper(mapper)
#Add actor to renderer.
ren1.AddActor(actor)
#Finally render image
renWin.Render()

Using the code above, I got this error:使用上面的代码,我得到了这个错误:

2022-08-23 13:32:32.842 ( 831.111s) [ 8238F740] vtkExecutive.cxx:752 ERR| 2022-08-23 13:32:32.842 (831.111s) [8238F740] vtkExecutive.cxx:752 ERR| vtkCompositeDataPipeline (0x560cb23f6a40): Algorithm vtkFieldDataToAttributeDataFilter(0x560cb22945b0) returned failure for request: vtkInformation (0x560cb23f50b0) Debug: Off Modified Time: 1445 Reference Count: 1 Registered Events: (none) Request: REQUEST_DATA_OBJECT ALGORITHM_AFTER_FORWARD: 1 FORWARD_DIRECTION: 0 vtkCompositeDataPipeline (0x560cb23f6a40):算法 vtkFieldDataToAttributeDataFilter(0x560cb22945b0) 返回请求失败:vtkInformation (0x560cb23f50b0) 调试:关闭修改时间:1445 引用计数:1 注册事件:(无)请求:REQUEST_DATA_OBJECT ALGORITHMD

2022-08-23 13:32:32.920 ( 831.189s) [ 8238F740]vtkDemandDrivenPipeline:666 ERR| 2022-08-23 13:32:32.920 (831.189s) [8238F740]vtkDemandDrivenPipeline:666 ERR| vtkCompositeDataPipeline (0x560cb23fe960): Input port 0 of algorithm vtkDataSetMapper(0x560cb2368110) has 0 connections but is not optional. vtkCompositeDataPipeline (0x560cb23fe960):算法 vtkDataSetMapper(0x560cb2368110) 的输入端口 0 有 0 个连接,但不是可选的。 2022-08-23 13:32:32.920 ( 831.189s) [ 8238F740]vtkDemandDrivenPipeline:666 ERR| 2022-08-23 13:32:32.920 (831.189s) [8238F740]vtkDemandDrivenPipeline:666 ERR| vtkCompositeDataPipeline (0x560cb23fe960): Input port 0 of algorithm vtkDataSetMapper(0x560cb2368110) has 0 connections but is not optional. vtkCompositeDataPipeline (0x560cb23fe960):算法 vtkDataSetMapper(0x560cb2368110) 的输入端口 0 有 0 个连接,但不是可选的。

I tried this Plot vtk file using Python with no success..使用 Python 尝试了这个 Plot vtk 文件,但没有成功。

The PDF presentation is of a very old version of VTK, where functions like SetInput() still existed. PDF 演示文稿是一个非常旧的 VTK 版本,其中像SetInput()这样的函数仍然存在。 In the meantime, a lot has changed regarding how you create the VTK pipeline, see here for example.与此同时,关于创建 VTK 管道的方式发生了很多变化,例如,请参见此处 I guess you are using a more recent version of VTK.我猜你正在使用更新版本的 VTK。

My guess is that you have to change the line (from the original code from the presentation)我的猜测是您必须更改行(来自演示文稿的原始代码)

toCellFilter.SetInput(reader.GetOutput())

to the line到线

toCellFilter.SetInputData(reader.GetOutput())

and it should work.它应该可以工作。

Your code right now simply cannot work, because you don't give an input to your vtkFieldDataToAttributeDataFilter .您的代码现在根本无法工作,因为您没有向vtkFieldDataToAttributeDataFilter提供输入。 That's also what the error output is trying to tell you: Algorithm vtkFieldDataToAttributeDataFilter(0x560cb22945b0) returned failure for request: vtkInformation .这也是错误 output 试图告诉你的: Algorithm vtkFieldDataToAttributeDataFilter(0x560cb22945b0) returned failure for request: vtkInformation This means that the filter can't find its input, it doesn't get the vtkInformation object from the input.这意味着过滤器找不到它的输入,它没有从输入中获取vtkInformation object。

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

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