Paraview–批量导入vtk文件–Method one

发布于 2022-10-21  652 次阅读


1. Python Shell

import polykriging as pk
vtkpath=r'C:\Users\AQ84510\Nutstore\1\Canada_Yuwei\École de technologie supérieure\2022_Sandwich flow\Articles_FYW\A2\CVFEM Code\Modified Code'
#此处的文件名不能直接用到paraview中,因为包含非英语字符
files = pk.filenames(vtkpath,condition="vtk")
#files:所有后缀为vtk的文件名做成的N×1的列表
files.sort(key=lambda x : int(x[:-4])) #如果文件名为如:1.vtk
# files.sort(key=lambda x : int(x.split('_')[1][:-4])) #如果文件名为如:f_1.vtk
paraviewreadfile=[vtkpath+'\\'+i for i in files] #directory path+file name
#import pandas as pd;se=(vtkpath+pd.Series(files)).tolist() # %timeit spends more
np.save('H:\\AA.npy',paraviewreadfile) #存储位置必须是全英文
PVvtk=np.load('H:\\AA.npy')

2.1 Pvpython shell

在spyder里直接导入pvpython以及paraview,渲染速度比较慢,建议直接使用paraview端

2.2 ParaView自带python shell:

a. Open paraview, here is ParaView 5.10.1. View \Rightarrow Python Shell

版本不一样,可能会有错误

b. 批量导入一系列vtk文件并显示

# trace generated using paraview version 5.10.1
#import paraview
#paraview.compatibility.major = 5
#paraview.compatibility.minor = 10

#### import the simple module from the paraview
from paraview.simple import *
import numpy as np
#### disable automatic camera reset on 'Show'
paraview.simple._DisableFirstRenderCameraReset()

###############################################################################################
'''   Sandwich Parameter file name as input in batch,here is fillingfactor       '''
paraviewreadfile=(np.load('H:\\AA.npy')).tolist()
# create a new 'Legacy VTK Reader'
fillingfactor = LegacyVTKReader(registrationName='fillingfactor*', FileNames=paraviewreadfile)
###############################################################################################
# get active source.
fillingfactor = GetActiveSource()
# set active source
SetActiveSource(fillingfactor)
# get active view
renderView1 = GetActiveViewOrCreate('RenderView')
# show data in view
fillingfactorDisplay = Show(fillingfactor, renderView1, 'UnstructuredGridRepresentation') 
# trace defaults for the display properties.
fillingfactorDisplay.Representation ='Surface With Edges' # 'Surface' etc.

效果如下:

2.3 Macros in ParaView

如果想知道每一步操作的代码,可以Tools \Rightarrow Start trace,结束完感兴趣的操作后stop trace。然后查看生成的py文件。以此作为参考。

批量导入时可以直接使用录制好修改的宏。

届ける言葉を今は育ててる
最后更新于 2022-10-21