Slicer 5.13
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
Loading...
Searching...
No Matches
vtkMRMLFileIOManager.h
Go to the documentation of this file.
1/*==============================================================================
2
3 Program: 3D Slicer
4
5 Copyright (c) Kitware Inc.
6
7 See COPYRIGHT.txt
8 or http://www.slicer.org/copyright/copyright.txt for details.
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16==============================================================================*/
17
18#ifndef __vtkMRMLFileIOManager_h
19#define __vtkMRMLFileIOManager_h
20
21#include "vtkMRMLLogicExport.h"
22
23#include <vtkObject.h>
24#include <vtkSmartPointer.h>
25#include <vtkWeakPointer.h>
26
27#include <string>
28#include <vector>
29
30class vtkCollection;
31class vtkImageData;
33class vtkMRMLNode;
34class vtkMRMLScene;
44
50class VTK_MRML_LOGIC_EXPORT vtkMRMLFileIOManager : public vtkObject
51{
52public:
54 vtkTypeMacro(vtkMRMLFileIOManager, vtkObject);
55 void PrintSelf(ostream& os, vtkIndent indent) override;
56
57 enum
58 {
75 };
76
78 virtual void SetScene(vtkMRMLScene* scene);
80
85
87
97 vtkMRMLNodeWriter* RegisterNodeWriter(const std::string& description,
98 const std::string& fileType,
99 const std::vector<std::string>& nodeClassNames,
100 bool supportUseCompression = true);
104
106
107 vtkMRMLFileReader* GetReaderByClassName(const std::string& className) const;
108 vtkMRMLFileWriter* GetWriterByClassName(const std::string& className) const;
110
112
118
120
121
124 std::vector<vtkSmartPointer<vtkMRMLFileReader>> GetReadersForFile(const std::string& filePath);
126 vtkMRMLFileReader* GetReaderForFile(const std::string& filePath);
128 std::string GetFileTypeForFile(const std::string& filePath);
130 std::vector<std::string> GetFileTypesForFile(const std::string& filePath);
132 std::vector<std::string> GetFileDescriptionsForFile(const std::string& filePath);
133
135 vtkMRMLFileReader* GetReaderByDescription(const std::string& description);
137 std::string GetFileTypeFromDescription(const std::string& description);
139 std::vector<std::string> GetFileDescriptionsByType(const std::string& fileType);
140
151 vtkMRMLFileReader* ExamineFileList(std::vector<std::string>& fileList, vtkMRMLIOProperties* ioProperties);
153
155
156
158 std::vector<vtkSmartPointer<vtkMRMLFileWriter>> GetWritersForObject(vtkObject* object);
164 vtkMRMLFileWriter* GetWriterForObject(vtkObject* object, const std::string& format = std::string());
166 std::string GetFileWriterFileType(vtkObject* object, const std::string& format = std::string());
168 std::string GetFileTypeForObject(vtkObject* object);
170 std::vector<std::string> GetFileWriterDescriptions(const std::string& fileType);
172 std::vector<std::string> GetFileWriterExtensions(vtkObject* object);
174
176
177 std::vector<std::string> GetReaderFileTypes();
178 std::vector<std::string> GetWriterFileTypes();
180 std::vector<std::string> GetExtensionsForFileType(const std::string& fileType, bool writers);
182 std::vector<std::string> GetNameFiltersForFileType(const std::string& fileType, bool writers);
184 std::string GetDescriptionForFileType(const std::string& fileType, bool writers);
186 std::vector<vtkSmartPointer<vtkMRMLFileReader>> GetReadersForFileType(const std::string& fileType);
188 std::vector<vtkSmartPointer<vtkMRMLFileWriter>> GetWritersForFileType(const std::string& fileType);
190
192
193
195 std::vector<std::string> GetAllWritableFileExtensions();
197 std::vector<std::string> GetAllReadableFileExtensions();
198
203
207
209 static std::string ForceFileNameValidCharacters(const std::string& filename);
210
215 std::string ForceFileNameMaxLength(const std::string& filename, int extensionLength, int maxLength = -1);
216
221
226 std::string ExtractKnownExtension(const std::string& fileName, vtkObject* object);
227
232 std::string StripKnownExtension(const std::string& fileName, vtkObject* object);
234
236
237
247 virtual bool LoadNodes(const std::string& fileType, //
248 vtkMRMLIOProperties* properties,
249 vtkCollection* loadedNodes = nullptr,
250 vtkMRMLMessageCollection* userMessages = nullptr);
251
255 virtual bool LoadNodesFromList(vtkCollection* propertiesList, vtkCollection* loadedNodes = nullptr, vtkMRMLMessageCollection* userMessages = nullptr);
256
258 vtkMRMLNode* LoadNodesAndGetFirst(const std::string& fileType, vtkMRMLIOProperties* properties, vtkMRMLMessageCollection* userMessages = nullptr);
259
261 bool LoadScene(const std::string& fileName, bool clear = true, vtkMRMLMessageCollection* userMessages = nullptr);
262
264 bool LoadFile(const std::string& fileName, vtkMRMLMessageCollection* userMessages = nullptr);
266
268
269
275 bool SaveNodes(const std::string& fileType, vtkMRMLIOProperties* properties, vtkMRMLMessageCollection* userMessages = nullptr, vtkMRMLScene* scene = nullptr);
276
286 bool ExportNodes(vtkCollection* propertiesList, bool hardenTransforms, vtkMRMLMessageCollection* userMessages = nullptr);
287
289 bool ExportNodes(const std::vector<std::string>& nodeIDs,
290 const std::vector<std::string>& fileNames,
291 vtkMRMLIOProperties* commonProperties,
292 bool hardenTransforms,
293 vtkMRMLMessageCollection* userMessages = nullptr);
294
297 bool SaveScene(const std::string& fileName, vtkImageData* screenShot = nullptr, vtkMRMLMessageCollection* userMessages = nullptr);
298
302
306
310 void InvokeFileSavedEvent(vtkMRMLIOProperties* savedFileProperties);
311
317
321 std::string GetDefaultSceneFileType() const;
322 void SetDefaultSceneFileType(const std::string& fileType);
323
324protected:
327
330 template <typename HandlerType>
331 static std::vector<vtkSmartPointer<HandlerType>> SortedByConfidence(const std::vector<vtkSmartPointer<HandlerType>>& handlers, //
332 const std::vector<double>& confidences);
333
336 std::vector<vtkSmartPointer<vtkMRMLFileWriter>> GetWritersForFile(const std::string& fileType, vtkMRMLIOProperties* properties, vtkMRMLScene* scene);
337
338 vtkWeakPointer<vtkMRMLScene> Scene;
339 vtkWeakPointer<vtkMRMLApplicationLogic> ApplicationLogic;
340 std::vector<vtkSmartPointer<vtkMRMLFileReader>> Readers;
341 std::vector<vtkSmartPointer<vtkMRMLFileWriter>> Writers;
342
345
349 bool StartOperationProgress(const std::string& title, int numberOfFiles);
352 void EndOperationProgress(bool operationStarted, bool aborted = false);
355 {
357 bool OperationStarted{ false };
361 double RangeMinimum{ 0.0 };
362 double RangeMaximum{ 1.0 };
364 bool Aborted{ false };
365 };
366
369 ListProgress StartListProgress(const std::string& title, int numberOfFiles);
371 void StartListItemProgress(ListProgress& listProgress, int fileIndex);
374 bool EndListItemProgress(ListProgress& listProgress, int fileIndex);
377 void EndListProgress(ListProgress& listProgress, vtkMRMLMessageCollection* userMessages, const std::string& abortedMessageFormat);
378
392 bool ExportNode(vtkMRMLIOProperties* properties, vtkMRMLScene* temporaryScene, bool hardenTransforms, vtkMRMLMessageCollection* userMessages);
393
396 void ReportFileProgress(double fileProgress, const std::string& text = std::string());
398 static void OnSceneProgress(vtkObject* caller, unsigned long eid, void* clientData, void* callData);
399
402 void InvokeProgressEvent(bool force = false);
403
408 bool ProgressEventPending{ false };
409
411 vtkSmartPointer<vtkMRMLOperationProgress> OperationProgress;
414
415private:
417 void operator=(const vtkMRMLFileIOManager&) = delete;
418};
419
420#endif
bool LoadFile(const std::string &fileName, vtkMRMLMessageCollection *userMessages=nullptr)
Load a file. All the options (e.g., file type) are chosen by default.
std::vector< vtkSmartPointer< vtkMRMLFileReader > > GetReadersForFileType(const std::string &fileType)
The readers of a file type, in the order they were registered.
vtkWeakPointer< vtkMRMLApplicationLogic > ApplicationLogic
void PrintSelf(ostream &os, vtkIndent indent) override
std::string GetFileTypeForObject(vtkObject *object)
Same as GetFileWriterFileType(object).
@ OperationEndEvent
Loading or saving of files is completed. Call data: vtkMRMLOperationProgress.
@ HandlerRegisteredEvent
A reader or writer is registered. Call data: the reader or writer (vtkMRMLFileIOHandler).
@ OperationStartEvent
Loading or saving of files is started. Call data: vtkMRMLOperationProgress.
@ HandlerUnregisteredEvent
A reader or writer is unregistered. Call data: the reader or writer (vtkMRMLFileIOHandler).
@ FileSavedEvent
A file is saved. Call data: vtkMRMLIOProperties of the saved file.
ListProgress StartListProgress(const std::string &title, int numberOfFiles)
std::vector< vtkSmartPointer< vtkMRMLFileWriter > > Writers
std::vector< std::string > GetNameFiltersForFileType(const std::string &fileType, bool writers)
The name filters of every reader (or writer) of a file type: "Mimics project (*.mcs)".
virtual bool LoadNodes(const std::string &fileType, vtkMRMLIOProperties *properties, vtkCollection *loadedNodes=nullptr, vtkMRMLMessageCollection *userMessages=nullptr)
Loading.
std::string GetFileWriterFileType(vtkObject *object, const std::string &format=std::string())
The file type of the best file writer for this object, or an empty string.
bool ExportNodes(const std::vector< std::string > &nodeIDs, const std::vector< std::string > &fileNames, vtkMRMLIOProperties *commonProperties, bool hardenTransforms, vtkMRMLMessageCollection *userMessages=nullptr)
Export nodes, with the same properties for all nodes.
std::vector< std::string > GetExtensionsForFileType(const std::string &fileType, bool writers)
The extensions of every reader (or writer) of a file type: ".mcs", ".mcs.gz".
vtkWeakPointer< vtkMRMLScene > Scene
std::vector< vtkSmartPointer< vtkMRMLFileReader > > Readers
std::vector< std::string > GetAllWritableFileExtensions()
File extensions and names.
vtkMRMLFileWriter * GetWriterByClassName(const std::string &className) const
static std::string ForceFileNameValidCharacters(const std::string &filename)
Remove characters that are likely to cause problems in a filename.
std::string GetDefaultSceneFileType() const
static void OnSceneProgress(vtkObject *caller, unsigned long eid, void *clientData, void *callData)
Forwards scene import or save progress (percent) as file progress.
std::vector< std::string > GetFileTypesForFile(const std::string &filePath)
All the file types that can be used for reading the file, most likely first.
void InvokeFileSavedEvent(vtkMRMLIOProperties *savedFileProperties)
Invoke FileSavedEvent. It may be used by writers that save files without the manager.
void RegisterReader(vtkMRMLFileReader *reader)
vtkMRMLNodeWriter * RegisterNodeWriter(const std::string &description, const std::string &fileType, const std::vector< std::string > &nodeClassNames, bool supportUseCompression=true)
~vtkMRMLFileIOManager() override
static vtkMRMLStorageNode * CreateAndAddDefaultStorageNode(vtkMRMLStorableNode *node)
Create and add default storage node.
double LastProgressEventTime
Time of the last invoked progress event (universal time in seconds)
vtkMRMLFileWriter * GetWriterForObject(vtkObject *object, const std::string &format=std::string())
std::string StripKnownExtension(const std::string &fileName, vtkObject *object)
std::vector< std::string > GetFileDescriptionsByType(const std::string &fileType)
Descriptions of all the readers of a file type. Usually there is only one reader for a file type.
std::vector< std::string > GetReaderFileTypes()
The file types that are registered, and what belongs to each of them.
void UnregisterHandler(vtkMRMLFileIOHandler *handler)
Remove a reader or a writer. Whoever registered a handler is responsible for unregistering it.
void EndOperationProgress(bool operationStarted, bool aborted=false)
bool LoadScene(const std::string &fileName, bool clear=true, vtkMRMLMessageCollection *userMessages=nullptr)
Load or import a scene.
virtual void SetScene(vtkMRMLScene *scene)
The scene the handlers read into and write from.
void SetDefaultSceneFileType(const std::string &fileType)
static vtkMRMLFileIOManager * New()
void StartListItemProgress(ListProgress &listProgress, int fileIndex)
Report that processing of the file at fileIndex starts.
void ReportFileProgress(double fileProgress, const std::string &text=std::string())
std::vector< vtkSmartPointer< vtkMRMLFileReader > > GetReadersForFile(const std::string &filePath)
Readers.
static std::vector< vtkSmartPointer< HandlerType > > SortedByConfidence(const std::vector< vtkSmartPointer< HandlerType > > &handlers, const std::vector< double > &confidences)
int GetNumberOfWriters() const
bool ExportNode(vtkMRMLIOProperties *properties, vtkMRMLScene *temporaryScene, bool hardenTransforms, vtkMRMLMessageCollection *userMessages)
void RegisterWriter(vtkMRMLFileWriter *writer)
bool SaveScene(const std::string &fileName, vtkImageData *screenShot=nullptr, vtkMRMLMessageCollection *userMessages=nullptr)
vtkMRMLFileReader * GetReaderByDescription(const std::string &description)
The reader that has the specified description, or nullptr.
std::vector< vtkSmartPointer< vtkMRMLFileWriter > > GetWritersForFile(const std::string &fileType, vtkMRMLIOProperties *properties, vtkMRMLScene *scene)
int GetNumberOfReaders() const
What is registered.
std::vector< vtkSmartPointer< vtkMRMLFileWriter > > GetWritersForObject(vtkObject *object)
Writers.
std::vector< std::string > GetFileWriterDescriptions(const std::string &fileType)
Descriptions of all writers of a file type.
vtkMRMLApplicationLogic * GetApplicationLogic() const
vtkMRMLFileReader * GetNthReader(int index) const
std::string GetFileTypeFromDescription(const std::string &description)
The file type of the reader that has the specified description, or an empty string.
vtkSmartPointer< vtkMRMLOperationProgress > OperationProgress
Progress of the loading or saving operation that is in progress (nullptr if no operation is in progre...
virtual bool LoadNodesFromList(vtkCollection *propertiesList, vtkCollection *loadedNodes=nullptr, vtkMRMLMessageCollection *userMessages=nullptr)
void InvokeNewFileLoadedEvent(vtkMRMLIOProperties *loadedFileProperties)
Invoke NewFileLoadedEvent. It may be used by readers that load files without the manager.
vtkMRMLFileWriter * GetNthWriter(int index) const
void EndListProgress(ListProgress &listProgress, vtkMRMLMessageCollection *userMessages, const std::string &abortedMessageFormat)
std::string ForceFileNameMaxLength(const std::string &filename, int extensionLength, int maxLength=-1)
bool SaveNodes(const std::string &fileType, vtkMRMLIOProperties *properties, vtkMRMLMessageCollection *userMessages=nullptr, vtkMRMLScene *scene=nullptr)
Saving.
void SetApplicationLogic(vtkMRMLApplicationLogic *applicationLogic)
vtkMRMLScene * GetScene() const
vtkMRMLNode * LoadNodesAndGetFirst(const std::string &fileType, vtkMRMLIOProperties *properties, vtkMRMLMessageCollection *userMessages=nullptr)
Load nodes and return the first loaded node.
std::vector< vtkSmartPointer< vtkMRMLFileWriter > > GetWritersForFileType(const std::string &fileType)
The writers of a file type, in the order they were registered.
bool StartOperationProgress(const std::string &title, int numberOfFiles)
std::vector< std::string > GetWriterFileTypes()
bool ProgressEventPending
True if progress changed since the last progress event.
static std::string GetFileNameValidCharactersPattern()
bool ExportNodes(vtkCollection *propertiesList, bool hardenTransforms, vtkMRMLMessageCollection *userMessages=nullptr)
std::string GetFileTypeForFile(const std::string &filePath)
The file type of the reader that is surest it can read this file ("VolumeFile"), or an empty string.
std::string ExtractKnownExtension(const std::string &fileName, vtkObject *object)
std::vector< std::string > GetAllReadableFileExtensions()
All the readable file extensions for all registered types of storage nodes. Includes the leading dot.
vtkMRMLFileReader * GetReaderForFile(const std::string &filePath)
The reader that is surest it can read this file, or nullptr.
void RegisterHandler(vtkMRMLFileIOHandler *handler)
Add a reader or a writer (calls RegisterReader() or RegisterWriter(), depending on the type of the ha...
std::string GetDescriptionForFileType(const std::string &fileType, bool writers)
What a file type is called ("Volume"), from the first handler that has a description.
bool EndListItemProgress(ListProgress &listProgress, int fileIndex)
vtkMRMLFileReader * ExamineFileList(std::vector< std::string > &fileList, vtkMRMLIOProperties *ioProperties)
bool SceneProgressObserved
True while the scene is observed for reporting its import progress.
std::vector< std::string > GetFileWriterExtensions(vtkObject *object)
Name filters of all writers that can write the object, the most confident writer's first.
std::vector< std::string > GetFileDescriptionsForFile(const std::string &filePath)
The descriptions ("Volume", "Segmentation", ...) of the readers that can read the file,...
std::string GetCompleteSlicerWritableFileNameSuffix(vtkMRMLStorableNode *node)
void InvokeProgressEvent(bool force=false)
vtkMRMLFileReader * GetReaderByClassName(const std::string &className) const
Get a registered reader or writer by its class name (exact match). Returns nullptr if not found.
Abstract Superclass for all specific types of MRML nodes.
Progress of a long operation, such as loading files.
A set of MRML Nodes that supports serialization and undo/redo.
MRML node to represent a 3D surface model.
A superclass for other storage nodes.
State of processing (loading or saving) a list of files.
bool Aborted
True if processing of the list was aborted (some files were not processed)
double RangeMinimum
Part of the overall progress range that processing of the list covers.
bool OperationStarted
True if processing of the list started an operation (it is not part of another operation)