1

我正在尝试运行下面的代码以可视化桁架上的压力,但出现错误。我使用的是 vtk 8.2.0,在谷歌搜索错误后,我找到了较低版本(低于 8.2)的解决方案,因此它们无法工作。代码在下面。请有人帮我消除此错误。

import vtk
import numpy as np


def displayTruss(elemNodes, nodeCords, stress, name="Quantity"):
    pts = vtk.vtkPoints()

    for x, y in nodeCords:
        pts.InsertNextPoint(x, y, 0.0)

    lines = vtk.vtkCellArray()
    for ii, jj in elemNodes:
        lines.InsertNextCell(2)
        lines.InsertCellPoint(ii)
        lines.InsertCellPoint(jj)

    stdata = vtk.vtkDoubleArray()
    stdata.SetName(name)
    for val in stress:
        stdata.InsertNextValue(val)

    grid = vtk.vtkPolyData()
    grid.SetPoints(pts)
    grid.SetLines(lines)

    grid.GetCellData().SetScalars(stdata)

    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInput(grid)
    mapper.SetScalarRange(np.min(stress), np.max(stress))

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)

    sbar = vtk.vtkScalarBarActor()
    sbar.SetLookupTable(mapper.GetLookupTable())
    sbar.SetTitle(name)

    ren = vtk.vtkRenderer()

    ren.AddActor2D(sbar)
    ren.AddActor(actor)

    renwin = vtk.vtkRenderWindow()
    renwin.AddRenderer(ren)
    renwin.SetSize(900, 500)

    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow(renwin)

    iren.Initialize()
    renwin.Render()
    iren.Start()


# example
elemNodes = np.array([[0, 1], [0, 2], [1, 2], [1, 3],
                      [0, 3], [2, 3], [2, 5], [3, 4], [3, 5], [2, 4], [4, 5]])

nodeCords = np.array([
    [0.0, 0.0], [0.0, 3000.0],
    [3000.0, 0.0], [3000.0, 3000.0],
    [6000.0, 0.0], [6000.0, 3000.0]
])

stress = np.array([-210.902, 122.432, 62.558, -44.235, -173.145, -88.47, 62.558, -173.145, -44.235, 122.432, -210.902])

displayTruss(elemNodes, nodeCords, stress)

我收到以下错误;提前谢谢你

line 29, in displayTruss
    mapper.SetInput(grid)
AttributeError: 'vtkRenderingOpenGL2Python.vtkOpenGLPolyDataMapper' object has no attribute 'SetInput'
4

2 回答 2

0

他们更改了 VTK 6 的接口。SetInput 被替换为 SetInputConnection 和 SetInputData。你可以在这里读到它:

https://vtk.org/Wiki/VTK/VTK_6_Migration/Replacement_of_SetInput

因此,您想将代码更改为:

mapper.SetInputData(grid)
于 2020-03-25T16:45:44.000 回答
0

这需要vtkplotter中的 3 行代码:

from vtkplotter import Lines, show
import numpy as np

elemNodes = np.array([[0, 1], [0, 2], [1, 2], [1, 3],
                      [0, 3], [2, 3], [2, 5], [3, 4], [3, 5], [2, 4], [4, 5]])

nodeCords = np.array([[0.0, 0.0, 0],    [0.0, 3000.0, 0],
                      [3000.0, 0.0, 0], [3000.0, 3000.0, 0],
                      [6000.0, 0.0, 0], [6000.0, 3000.0, 0]])

stress = np.array([-210.902, 122.432, 62.558, -44.235, -173.145, -88.47, 62.558,
                   -173.145, -44.235, 122.432, -210.902])

truss = Lines(nodeCords[elemNodes])
truss.cellColors(stress, cmap="jet").lineWidth(4).addScalarBar(title='Quantity')

show(truss, axes=1, bg="k")

在此处输入图像描述

于 2020-03-26T01:07:07.660 回答