# Populate scene # ----------------------------------------------------------------------------- # vehicle geometry vehicle_vtk = [] for filename in glob.glob(os.path.join(DATA_PATH, "vehicle") + "/*.vtp"): mesh = _load_vtp(filename, point_arrays=["U", "p"]) part_name = filename.split("/")[-1].replace(".vtp", "") child = dash_vtk.GeometryRepresentation( id=f"{part_name}-rep", colorMapPreset="erdc_rainbow_bright", colorDataRange=[0, 100], actor={"visibility": 1}, mapper={"scalarVisibility": False}, children=[dash_vtk.Mesh( id=f"{part_name}-mesh", state=mesh, )], ) vehicle_vtk.append(child) # isosurfaces isosurfs_vtk = [] for filename in glob.glob(os.path.join(DATA_PATH, "isosurfaces") + "/*.vtp"): mesh = _load_vtp(filename) surf_name = filename.split("/")[-1].replace(".vtp", "") child = dash_vtk.GeometryRepresentation( id=f"{surf_name}-rep", property={"color": [1, 0, 0]}, actor={"visibility": 0}, children=[dash_vtk.Mesh(
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP]) server = app.server viz = Viz(os.path.join(os.path.dirname(__file__), "data")) # ----------------------------------------------------------------------------- # 3D Viz # ----------------------------------------------------------------------------- vtk_view = dash_vtk.View( id="vtk-view", children=[ dash_vtk.GeometryRepresentation( id="bike-rep", children=[dash_vtk.Mesh( id="bike", state=viz.getBikeMesh(), )], ), dash_vtk.GeometryRepresentation( id="tubes-rep", colorMapPreset="erdc_rainbow_bright", colorDataRange=viz.getColorRange(), children=[ dash_vtk.Mesh( id="tubes-mesh", state=viz.getTubesMesh("p"), ) ], ), dash_vtk.GeometryRepresentation( id="seed-rep",
import dash_vtk from dash_vtk.utils import to_mesh_state from vtk.vtkImagingCore import vtkRTAnalyticSource # Use VTK to get some data data_source = vtkRTAnalyticSource() data_source.Update() # <= Execute source to produce an output dataset = data_source.GetOutput() # Use helper to get a mesh structure that can be passed as-is to a Mesh # RTData is the name of the field mesh_state = to_mesh_state(dataset) content = dash_vtk.View([ dash_vtk.GeometryRepresentation([dash_vtk.Mesh(state=mesh_state)]), ]) # Dash setup app = dash.Dash(__name__) server = app.server app.layout = html.Div( style={ "width": "100%", "height": "calc(100vh - 15px)" }, children=[content], ) if __name__ == "__main__":
import dash_html_components as html import vtk import dash_vtk from dash_vtk.utils import to_mesh_state reader = vtk.vtkXMLUnstructuredGridReader() reader.SetFileName("./data/flow_pyevtk.vtu") reader.Update() # Needed because of GetScalarRange output = reader.GetOutput() mesh_state = to_mesh_state(reader.GetOutput()) vtk_view = dash_vtk.View( id="vtk-view", children=[ dash_vtk.GeometryRepresentation( children=[dash_vtk.Mesh(state=mesh_state)], ) ], ) app = dash.Dash(__name__, external_stylesheets=[]) app.layout = html.Div( style={ "height": "calc(100vh - 16px)", "width": "100%" }, children=[html.Div(vtk_view, style={ "height": "100%", "width": "100%" })], )
reader = vtk.vtkXMLImageDataReader() reader.SetFileName(head_vti) reader.Update() # Extract iso-mesh from image contour = vtk.vtkContourFilter() contour.SetInputConnection(reader.GetOutputPort()) contour.SetNumberOfContours(1) contour.SetValue(0, 1500) contour.Update() # Get mesh to dash_vtk mesh_state = to_mesh_state(contour.GetOutput()) content = dash_vtk.View([ dash_vtk.GeometryRepresentation([ dash_vtk.Mesh(state=mesh_state) ]), ]) # Dash setup app = dash.Dash(__name__) server = app.server app.layout = html.Div( style={"width": "100%", "height": "calc(100vh - 15px)"}, children=[content], ) if __name__ == "__main__": app.run_server(debug=True)
def fea_vtk_upload(node_contents, elem_contents, elem_data_contents, cs_name, rng_slide, toggle_edges, is_cleared): ctx = dash.callback_context if not ctx.triggered: raise PreventUpdate trig_id = ctx.triggered[0]['prop_id'].split('.')[0] # scenario 1: nodes data, but elems wasnt cleared (clears elems) if trig_id == f'{APP_ID}_nodes_upload' and not is_cleared[0]: return [dash.no_update, [True], dash.no_update, None] # scenario 2: elems data, but nodes wasnt cleared (clears nodes) if trig_id == f'{APP_ID}_elems_upload' and not is_cleared[0]: return [dash.no_update, [True], None, dash.no_update] # scenario 3: nodes data, but no elems data if trig_id == f'{APP_ID}_nodes_upload' and elem_contents is None: raise PreventUpdate # scenario 4: elems data, but no nodes data if trig_id == f'{APP_ID}_elems_upload' and node_contents is None: raise PreventUpdate # scenario 5: data for both mesh, but no results if all([ node_contents is not None, elem_contents is not None, is_cleared[0], elem_data_contents is None ]): uGrid, _ = ns_export_to_uGrid(node_contents, elem_contents) mesh_state = to_mesh_state(uGrid) return [ dash_vtk.GeometryRepresentation( [dash_vtk.Mesh(state=mesh_state)], property={ "edgeVisibility": False, "opacity": 0.25, 'representation': 2 }), [False], dash.no_update, dash.no_update, ] # scenario 6: data for the whole shebang if all([ node_contents is not None, elem_contents is not None, elem_data_contents is not None ]): uGrid, rng = ns_export_to_uGrid(node_contents, elem_contents, elem_data_contents) mesh_state1 = to_mesh_state(uGrid) if rng_slide[0] == 0. and rng_slide[1] != 1.: # threshold to keep values below upper value of range slider thresh = uGrid.threshold_percent(rng_slide[1], invert=True) elif rng_slide[0] != 0. and rng_slide[1] == 1.: # threshold to keep values above lower value of range slider thresh = uGrid.threshold_percent(rng_slide[0]) elif rng_slide[0] == 0. and rng_slide[1] == 1.: thresh = uGrid.copy() else: # threshold to keep values in range thresh = uGrid.threshold_percent(rng_slide) mesh_state2 = to_mesh_state(thresh, field_to_keep='my_array') if 1 in toggle_edges: show_edges = True else: show_edges = False return [ [ # this is the ghost display to see the outline of the part dash_vtk.GeometryRepresentation( [dash_vtk.Mesh(state=mesh_state1)], property={ "edgeVisibility": False, "opacity": .15 }, ), # this is the threshold solid elements within the range slider values dash_vtk.GeometryRepresentation( [dash_vtk.Mesh(state=mesh_state2)], property={ "edgeVisibility": show_edges, "opacity": 1 }, colorMapPreset=cs_name, showCubeAxes=False, colorDataRange=rng), ], [False], dash.no_update, dash.no_update, ]