How can I visualize truss using VTK?

172 views Asked by At

I am trying to run the code below to visualize stresses on a truss but I am getting an error.I am using vtk 8.2.0 and after googling the error I found solutions to lower versions (below 8.2) so they couldnt work.The code is below.Please someone help me removing this error.

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)

I am getting the following error;Thank you in advance

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

There are 2 answers

0
mmusy On BEST ANSWER

This takes 3 lines of code in vtkplotter:

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

1
Dave Chen On

They changed the interface of VTK 6. SetInput was replaced with SetInputConnection and SetInputData. You can read about it here:

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

So you want to change your code to:

mapper.SetInputData(grid)