11#ifndef __vtkSlicerApplicationLogicRequests_h
12#define __vtkSlicerApplicationLogicRequests_h
46 ReadDataRequestFile(
const std::string& node,
const std::string& filename,
int displayData,
int deleteFile,
int uid = 0,
const std::string& cliNode = std::string())
62 vtkDebugWithObjectMacro(appLogic,
"ProcessReadNodeData: read data request node id = " << nd->
GetID());
64 vtkSmartPointer<vtkMRMLStorageNode> storageNode;
69 bool readFailed =
false;
70 std::string readError;
76 for (
int n = 0; n < numStorageNodes; n++)
81 if (useURI && testStorageNode->
GetURI() !=
nullptr)
86 vtkDebugWithObjectMacro(appLogic,
"ProcessReadNodeData: found a storage node with the right URI: " << testStorageNode->
GetURI());
87 storageNode = testStorageNode;
91 else if (testStorageNode->
GetFileName() !=
nullptr &&
95 vtkDebugWithObjectMacro(appLogic,
"ProcessReadNodeData: found a storage node with the right filename: " << testStorageNode->
GetFileName());
96 storageNode = testStorageNode;
103 bool createdNewStorageNode =
false;
104 if (storageNode.GetPointer() ==
nullptr)
107 if (itksys::SystemTools::FileExists(
m_Filename.c_str()))
112 createdNewStorageNode = (storageNode !=
nullptr);
117 if (storageNode.GetPointer() !=
nullptr)
121 vtkDebugWithObjectMacro(appLogic,
122 "ProcessReadNodeData: about to call read data, "
123 "storage node's read state is "
124 << storageNode->GetReadStateAsString());
128 storageNode->SetWriteStateIdle();
130 storageNode->GetUserMessages()->ClearMessages();
134 vtkDebugWithObjectMacro(appLogic,
"ProcessReadNodeData: calling ReadData on the storage node " << storageNode->GetID() <<
", uri = " << storageNode->GetURI());
135 if (!storageNode->ReadData(nd,
true))
137 readError = storageNode->GetUserMessages()->GetAllMessagesAsString();
140 if (createdNewStorageNode)
142 storageNode->SetURI(
nullptr);
148 vtkDebugWithObjectMacro(appLogic,
149 "ProcessReadNodeData: calling ReadData on the storage node " << storageNode->GetID() <<
", filename = " << storageNode->GetFileName());
150 if (!storageNode->ReadData(nd,
true))
152 readError = storageNode->GetUserMessages()->GetAllMessagesAsString();
155 if (createdNewStorageNode)
157 storageNode->SetFileName(
nullptr);
161 catch (itk::ExceptionObject& exc)
163 vtkErrorWithObjectMacro(appLogic,
"Exception while reading " <<
m_Filename <<
", " << exc);
164 readError = exc.GetDescription();
169 vtkErrorWithObjectMacro(appLogic,
"Unknown exception while reading " <<
m_Filename);
190 if (
m_Filename.find(
"slicer:") != std::string::npos)
196 removed =
static_cast<bool>(itksys::SystemTools::RemoveFile(
m_Filename.c_str()));
200 vtkGenericWarningMacro(
"Unable to delete temporary file " <<
m_Filename);
261 if (!errorText.empty() && errorText.back() !=
'\n')
265 errorText += std::string(
"Failed to load output into node ") + (targetNode->
GetName() ? targetNode->
GetName() :
"(unnamed)") +
" from file " +
m_Filename +
"\n";
266 errorText += readError;
284 const std::vector<std::string>& sourceNodes,
285 const std::string& filename,
313 removed =
static_cast<bool>(itksys::SystemTools::RemoveFile(
m_Filename.c_str()));
316 std::stringstream information;
317 information <<
"Unable to delete temporary file " <<
m_Filename << std::endl;
318 vtkGenericWarningMacro(<< information.str().c_str());
325 vtkNew<vtkMRMLScene> miniscene;
330 std::vector<std::string>::const_iterator tit;
331 std::vector<std::string>::const_iterator sit;
340 vtkMRMLNode* source = miniscene->GetNodeByID(sit->c_str());
343 if (source && target)
352 target->
Copy(source);
357 target->
Copy(source);
403 std::map<std::string, std::string> parentNodeIDMapper;
406 std::vector<vtkMRMLModelHierarchyNode*> childNodesThatNeedParentsIDsRemapped;
407 for (
int n = 0; n < miniscene->GetNumberOfNodesByClass(
"vtkMRMLModelHierarchyNode"); n++)
428 parentNodeIDMapper[std::string(mhnd->
GetID())] = std::string(tchild->
GetID());
432 std::string parentNodeID = std::string(tmhnd->
GetID());
436 std::map<std::string, std::string>::iterator pIt = parentNodeIDMapper.find(std::string(tcmhd->
GetParentNodeID()));
437 if (pIt != parentNodeIDMapper.end())
439 parentNodeID = pIt->second;
440 vtkDebugWithObjectMacro(appLogic,
"Remapped parent node id to " << parentNodeID.c_str());
444 childNodesThatNeedParentsIDsRemapped.push_back(tcmhd);
477 if (childNodesThatNeedParentsIDsRemapped.size() > 0)
480 for (
unsigned int i = 0; i < childNodesThatNeedParentsIDsRemapped.size(); i++)
482 std::map<std::string, std::string>::iterator pIt = parentNodeIDMapper.find(childNodesThatNeedParentsIDsRemapped[i]->GetParentNodeID());
483 if (pIt != parentNodeIDMapper.end())
485 vtkDebugWithObjectMacro(appLogic,
486 "Remapping child node " << childNodesThatNeedParentsIDsRemapped[i]->GetName() <<
" parent node id from "
487 << childNodesThatNeedParentsIDsRemapped[i]->GetParentNodeID() <<
" to " << pIt->second.c_str());
488 childNodesThatNeedParentsIDsRemapped[i]->SetParentNodeID(pIt->second.c_str());
496 std::stringstream information;
497 information <<
"Node " << (*sit) <<
" not found in scene file " <<
m_Filename << std::endl;
498 vtkGenericWarningMacro(<< information.str().c_str());
502 std::stringstream information;
503 information <<
"Node " << (*tit) <<
" not found in current scene." << std::endl;
504 vtkGenericWarningMacro(<< information.str().c_str());
517 removed =
static_cast<bool>(itksys::SystemTools::RemoveFile(
m_Filename.c_str()));
520 std::stringstream information;
521 information <<
"Unable to delete temporary file " <<
m_Filename << std::endl;
522 vtkGenericWarningMacro(<< information.str().c_str());
582 if (updatedNodeShItemID && siblingNodeShItemID)
584 vtkIdType parentItemID = shnd->
GetItemParent(siblingNodeShItemID);
613 if (referencingNode && referencedNode)
629 WriteDataRequestFile(
const std::string& vtkNotUsed(node),
const std::string& vtkNotUsed(filename),
int uid = 0)
643 WriteDataRequestScene(
const std::vector<std::string>& targetNodes,
const std::vector<std::string>& sourceNodes,
const std::string& filename,
int uid = 0)
virtual ~DataRequest()=default
virtual void Execute(vtkSlicerApplicationLogic *)
std::string m_ReferencingNode
void Execute(vtkSlicerApplicationLogic *appLogic) override
std::string m_ReferencedNode
ReadDataRequestAddNodeReference(const std::string &referencingNode, const std::string &referencedNode, const std::string &role, int uid=0)
void ReportReadErrorToCLINode(vtkSlicerApplicationLogic *appLogic, vtkMRMLNode *targetNode, const std::string &readError)
Add the error to the error text of the CLI node that requested reading (if any).
void Execute(vtkSlicerApplicationLogic *appLogic) override
ReadDataRequestFile(const std::string &node, const std::string &filename, int displayData, int deleteFile, int uid=0, const std::string &cliNode=std::string())
ReadDataRequestScene(const std::vector< std::string > &targetNodes, const std::vector< std::string > &sourceNodes, const std::string &filename, int displayData, int deleteFile, int uid=0)
std::vector< std::string > m_TargetNodes
std::vector< std::string > m_SourceNodes
void Execute(vtkSlicerApplicationLogic *appLogic) override
std::string m_SubjectHierarchySiblingNode
ReadDataRequestUpdateSubjectHierarchyLocation(const std::string &updatedNode, const std::string &siblingNode, int uid=0)
void Execute(vtkSlicerApplicationLogic *appLogic) override
std::string m_UpdatedNode
WriteDataRequestFile(const std::string &vtkNotUsed(node), const std::string &vtkNotUsed(filename), int uid=0)
WriteDataRequestScene(const std::vector< std::string > &targetNodes, const std::vector< std::string > &sourceNodes, const std::string &filename, int uid=0)
std::vector< std::string > m_TargetNodes
void Execute(vtkSlicerApplicationLogic *appLogic) override
std::vector< std::string > m_SourceNodes
virtual bool IsRemoteReference(const char *uri)
vtkMRMLScene * GetMRMLScene() const
Return a reference to the current MRML scene.
void PropagateTableSelection()
Propagate selected table in the SelectionNode to table view nodes.
void PropagateVolumeSelection(int fit=1)
vtkMRMLSelectionNode * GetSelectionNode() const
Get default Selection node.
MRML node for representing the parameters allowing to run a command line interface module (CLI)....
static vtkMRMLCommandLineModuleNode * SafeDownCast(vtkObject *o)
void SetErrorText(const std::string &text, bool modify=true)
std::string GetErrorText() const
bool ReadParameterFile(const std::string &filename)
Abstract class that contains graphical display properties for displayable nodes.
vtkMRMLDisplayNode * GetDisplayNode()
Get associated display MRML node.
void SetAndObserveDisplayNodeID(const char *DisplayNodeID)
static vtkMRMLDisplayableNode * SafeDownCast(vtkObject *o)
void SetAndObserveDisplayNodeID(const char *displayNodeID)
virtual void CreateDefaultDisplayNodes()
vtkMRMLDisplayNode * GetDisplayNode()
vtkMRMLHierarchyNode * GetTopParentNode()
Get the top parent node in the hierarchy.
virtual char * GetParentNodeID()
String ID of the parent hierarchy MRML node.
virtual void SetParentNodeID(const char *ref)
static vtkMRMLLabelMapVolumeNode * SafeDownCast(vtkObject *o)
MRML node to represent a hierarchy of models.
void SetModelNodeID(const char *id)
String ID of the model MRML node.
static vtkMRMLModelHierarchyNode * SafeDownCast(vtkObject *o)
vtkMRMLModelNode * GetModelNode()
Get associated model MRML node.
MRML node to represent a 3D surface model.
static vtkMRMLModelNode * SafeDownCast(vtkObject *o)
Abstract Superclass for all specific types of MRML nodes.
virtual char * GetID()
ID use by other nodes to reference this node in XML.
vtkMRMLNode * AddNodeReferenceID(const char *referenceRole, const char *referencedNodeID)
Convenience method that adds a referencedNodeID at the end of the list.
void Modified() override
Customized version of Modified() allowing to compress vtkCommand::ModifiedEvent.
virtual void Copy(vtkMRMLNode *node)
Copy node contents from another node of the same type. Does not copy node ID and Scene....
static vtkMRMLScalarVolumeNode * SafeDownCast(vtkObject *o)
A set of MRML Nodes that supports serialization and undo/redo.
void StartState(unsigned long state, int anticipatedMaxProgress=0)
Flag the scene as being in a state mode.
void EndState(unsigned long state)
Unflag the scene as being in a state mode.
int Commit(const char *url=nullptr, vtkMRMLMessageCollection *userMessages=nullptr)
virtual vtkCacheManager * GetCacheManager()
vtkMRMLNode * CopyNode(vtkMRMLNode *n)
Add a copy of a node to the scene.
vtkMRMLNode * GetNodeByID(const char *name)
Get node given a unique ID.
int Import(vtkMRMLMessageCollection *userMessages=nullptr)
Add the scene into the existing scene (no clear) from URL file or from.
void SetURL(const char *url)
Set URL (file name) of the scene.
void SetActiveTableID(const char *id)
void SetActiveVolumeID(const char *id)
void SetActiveLabelVolumeID(const char *id)
MRML node to represent a 3D surface model.
vtkMRMLStorageNode * GetNthStorageNode(int n)
Get associated display MRML node.
void SetAndObserveStorageNodeID(const char *storageNodeID)
String ID of the storage MRML node.
virtual bool AddDefaultStorageNode(const char *filename=nullptr)
const char * GetStorageNodeID()
vtkMRMLStorageNode * GetStorageNode()
static vtkMRMLStorableNode * SafeDownCast(vtkObject *o)
int GetNumberOfStorageNodes()
A superclass for other storage nodes.
virtual char * GetFileName()
MRML node to represent a complete subject hierarchy tree.
vtkIdType GetItemByDataNode(vtkMRMLNode *dataNode)
void SetItemLevel(vtkIdType itemID, std::string level)
Convenience function to set level attribute for a subject hierarchy item.
void SetItemParent(vtkIdType itemID, vtkIdType parentItemID, bool enableCircularCheck=true)
static vtkMRMLSubjectHierarchyNode * GetSubjectHierarchyNode(vtkMRMLScene *scene)
std::string GetItemLevel(vtkIdType itemID)
Convenience function to get level attribute for a subject hierarchy item.
vtkIdType GetItemParent(vtkIdType itemID)
static vtkMRMLTableNode * SafeDownCast(vtkObject *o)