Esempio n. 1
0
def vtk_read_stl(path):
    from vtkplotlib.plots.polydata import PolyData
    from vtkplotlib.unicode_paths import PathHandler
    from vtkplotlib._vtk_errors import handler

    with PathHandler(path) as path_handler:
        reader = vtk.vtkSTLReader()
        handler.attach(reader)
        reader.SetFileName(path_handler.access_path)

        # Normally Reader doesn't do any reading until it's been plotted.
        # Update forces it to read.
        reader.Update()
        pd = PolyData(reader.GetOutput())

    # For some reason VTK just doesn't like some files. There are some vague
    # warnings in their docs - this could be what they're on about. If it
    # doesn't work ``reader.GetOutput()`` gives an empty polydata.
    if pd.vtk_polydata.GetNumberOfPoints() == 0:
        raise RuntimeError(
            "VTK's STLReader failed to read the STL file and no STL io backend "
            "is installed. VTK's STLReader is rather patchy. To read this file "
            "please ``pip install numpy-stl`` first.")

    return pd
Esempio n. 2
0
def path_str_to_vectors(path, ignore_numpystl=False):

    # Ideally let numpy-stl open the file if it is installed.
    if NUMPY_STL_AVAILABLE and not ignore_numpystl:
        return NumpyMesh.from_file(path).vectors

    # Otherwise try vtk's STL reader - however it's far from flawless.

    # A lot of redundancy here.
    # -Read the STL using vtk's STLReader
    # -Extract the polydata
    # -Extract the vectors from the polydata
    # This can then be given to mesh_plot which will convert it straight
    # back again to a polydata.

    from vtkplotlib.plots.polydata import PolyData
    from vtkplotlib.unicode_paths import PathHandler
    from vtkplotlib._vtk_errors import handler

    with PathHandler(path) as path_handler:
        reader = vtk.vtkSTLReader()
        handler.attach(reader)
        reader.SetFileName(path_handler.access_path)

        # vtk does a really good job of point merging but it's really not
        # worth the hassle to use it just to save a bt of RAM as everything
        # else in this script assumes an (n, 3, 3) table of vectors. Disable it
        # for speed.
        #        reader.SetMerging(False)

        # Normally Reader doesn't do any reading until it's been plotted.
        # Update forces it to read.
        reader.Update()
        pd = PolyData(reader.GetOutput())

    # For some reason VTK just doesn't like some files. There are some vague
    # warnings in their docs - this could be what they're on about. If it
    # doesn't work ``reader.GetOutput()`` gives an empty polydata.
    if pd.vtk_polydata.GetNumberOfPoints() == 0:
        raise RuntimeError(
            "VTK's STLReader failed to read the STL file and no STL io backend is installed. VTK's STLReader is rather patchy. To read this file please ``pip install numpy-stl`` first."
        )

    return normalise_mesh_type((pd.points, pd.polygons))
Esempio n. 3
0
 def __init__(self, fig="gcf"):
     super().__init__(fig)
     self.polydata = PolyData()
Esempio n. 4
0
 def __init__(self, fig="gcf"):
     super().__init__(fig)
     self.polydata = PolyData()
     self._freeze_scalar_range = False
Esempio n. 5
0
 def polydata(self):
     self.source.Update()
     return PolyData(self.source.GetOutput())