如何使用VTK可视化桁架?

问题描述 投票:0回答:2

我正在尝试运行下面的代码以可视化桁架上的压力,但出现错误。我使用的是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'
vtk truss
2个回答
0
投票

他们更改了VTK 6的界面。SetInput被SetInputConnection和SetInputData取代。您可以在这里阅读:

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

所以您想将代码更改为:

mapper.SetInputData(grid)

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")

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.