Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select element on plotter and save it as it's own vtk file #51

Closed
laserman781 opened this issue Aug 15, 2019 · 13 comments
Closed

Select element on plotter and save it as it's own vtk file #51

laserman781 opened this issue Aug 15, 2019 · 13 comments
Labels
picking/selecting Topics around using interactive picking and selecting in renderers plotting General plotting/rendering topics

Comments

@laserman781
Copy link

I was wondering if it was possible to select elements on a plot interactively (ex.: a collection of holes) and save them as their own vtk file or perhaps print their table data?

Thank you!

@banesullivan
Copy link
Member

Cell selection is currently possible - check out the .enable_cell_picking method on the plotter: https://docs.pyvista.org/plotting/plotting.html#pyvista.BasePlotter.enable_cell_picking

You could pass a custom callback function to save out the mesh or grab the saved mesh after closing the plotter from the .picked_cells attribute

Otherwise, I'm not sure what you mean by selecting elements/holes...

@banesullivan banesullivan added picking/selecting Topics around using interactive picking and selecting in renderers plotting General plotting/rendering topics labels Aug 16, 2019
@laserman781
Copy link
Author

What kind of function would I need to create to do this? I simply want to be able to select a mesh from the plot and export it as it's own vtk file using the save function.

@laserman781
Copy link
Author

Or more specifically selecting a vtk cell

@laserman781
Copy link
Author

The goal is to try and get something similar to extract selection from ParaView.

@banesullivan
Copy link
Member

After making a selection with the plotter's .enable_cell_picking (which recently had some enhancements that aren't released yet), you can grab the mesh from the .picked_cells attribute on that plotter - then you could save that mesh if desired.

Here's an example

import pyvista as pv
from pyvista import examples

mesh = examples.download_dragon()

p = pv.Plotter(notebook=False)
p.add_mesh(mesh)
p.enable_cell_picking(mesh)
p.show()

2019-08-19 12 02 23

selection = p.picked_cells
selection.plot(color='white')

download

# Save it
selection.save('my_selection.vtk')

@laserman781
Copy link
Author

This works great! I am getting the option "Press P to pick a single cell under the mouse"

Is this coming in the next release?

@banesullivan
Copy link
Member

Yes, a few new features with cell selection are coming in the next release! Along with many other point selection tools and widgets.

@laserman781
Copy link
Author

Awesome! Final question... I mentioned above being able to select from a table. In ParaView there is the option to have a spreadsheet view side by side with the plot and be able to select the cells by their rows (see screenshot in email). from there I can apply the extract cellls filter. Is something like this possible in PyVista as well?

@banesullivan
Copy link
Member

banesullivan commented Aug 19, 2019

In short, not really as PyVista isn't designed to be GUI application like ParaView.

But if you know the cell IDs (the rows in the spreadsheet view) then you can use PyVista's .extract_cells filter to extract cells by their IDs which is exactly what ParaView's extract selection filter is doing.

Coming in the next release of PyVista is support for vtkTable objects in a new Table class which can go back and forth with Pandas DataFrame objects. Perhaps there are good tools for interactively making selections from Pandas data frames that would be well suited for this? I image we could make a tool that would display the cell data (or point data) as a pandas DataFrame and perhaps leverage some sort of interactive DataFrame tool to make selections and extract cells in a similar fashion.

Unfortunately it is out of scope for us to deisgn any sort of interactive selection tool based on a table representation.

@banesullivan
Copy link
Member

Or if you are feeling really adventurous, you could make a plugin to PyVista using PyQt to show these tables and allow selections to callback to the extract filters. Making the GUI would be the hard part... all the extraction and PyVista stuff is already implemented

@laserman781
Copy link
Author

Okay, I will give this a try. Thank you!

@banesullivan
Copy link
Member

banesullivan commented Aug 23, 2019

@banesullivan You could make a plugin to PyVista using PyQt to show these tables and allow selections to callback to the extract filters. Making the GUI would be the hard part... all the extraction and PyVista stuff is already implemented".

@laserman781 Do you have any ideas on how I could do this? Also, how do I get the mesh data (point data and or cell data) in a table for this?

On the lastest version of PyVista, the Table class was introduced - a wrapped implementation of vtkTable which can also go back and forth to a Pandas DataFrame. I'd start by making tables of the mesh's point/cell data by passing those dictionaries to the Table constructor:

import pyvista as pv
from pyvista import examples

# Any mesh
mesh = examples.download_st_helens().warp_by_scalar()

# Make a table
point_table = pv.Table(mesh.point_arrays)

Also, now you have a Table which can be converted to a Pandas DataFrame:

df = point_table.to_pandas()

I'd recommend starting there and then taking a look at the Qt Table Widget and this SO post about making Qt Tables from data frames. Then you'd want to display that widget in an app (maybe link it to PyVista's BackgroundPlotter) and enable row selection in the qt widget which would call back to function that extracts the cell/point indices (row indices from the table) using either extract_cells or extract_points

@banesullivan
Copy link
Member

FYI... there are some custom Qt widgets implemented in VTK for tables but I'm not sure if they are available on the Python distribution: https://vtk.org/doc/nightly/html/classvtkQtTableView.html

It might require a custom build of VTK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
picking/selecting Topics around using interactive picking and selecting in renderers plotting General plotting/rendering topics
Projects
None yet
Development

No branches or pull requests

2 participants