Beispiel #1
0
    def save(self, filename, binary=True):
        """Save this vtk object to file.

        Parameters
        ----------
        filename : str
         Filename of output file. Writer type is inferred from
         the extension of the filename.

        binary : bool, optional
         If True, write as binary, else ASCII.

        Notes
        -----
        Binary files write much faster than ASCII and have a smaller
        file size.

        """
        if self._WRITERS is None:
            raise NotImplementedError('{} writers are not specified, this should be a' \
                                      ' dict of (file extension: vtkWriter type)'
                                      .format(self.__class__.__name__))

        filename = os.path.abspath(os.path.expanduser(filename))
        file_ext = fileio.get_ext(filename)
        if file_ext not in self._WRITERS:
            raise ValueError('Invalid file extension for this data type. Must be one of: {}'.format(
                self._WRITERS.keys()))

        writer = self._WRITERS[file_ext]()
        fileio.set_vtkwriter_mode(vtk_writer=writer, use_binary=binary)
        writer.SetFileName(filename)
        writer.SetInputData(self)
        writer.Write()
Beispiel #2
0
    def _load_file(self, filename):
        """Generically load a vtk object from file.

        Parameters
        ----------
        filename : str
            Filename of object to be loaded.  File/reader type is inferred from the
            extension of the filename.

        Notes
        -----
        Binary files load much faster than ASCII.

        """
        if self._READERS is None:
            raise NotImplementedError('{} readers are not specified, this should be a' \
                                      ' dict of (file extension: vtkReader type)'
                                      .format(self.__class__.__name__))

        filename = os.path.abspath(os.path.expanduser(filename))
        if not os.path.isfile(filename):
            raise FileNotFoundError('File %s does not exist' % filename)

        file_ext = fileio.get_ext(filename)
        if file_ext not in self._READERS:
            keys_list = ', '.join(self._READERS.keys())
            raise ValueError('Invalid file extension for {}({}). Must be one of: {}'.format(
                self.__class__.__name__, file_ext, keys_list))

        reader = self._READERS[file_ext]()
        reader.SetFileName(filename)
        reader.Update()
        self.shallow_copy(reader.GetOutput())