forked from Slicer/SlicerGitSVNArchive
-
Notifications
You must be signed in to change notification settings - Fork 2
/
vtkMRMLParser.cxx
247 lines (222 loc) · 8.16 KB
/
vtkMRMLParser.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*=auto=========================================================================
Portions (c) Copyright 2005 Brigham and Women's Hospital (BWH) All Rights Reserved.
See COPYRIGHT.txt
or http://www.slicer.org/copyright/copyright.txt for details.
Program: 3D Slicer
Module: $RCSfile: vtkMRMLParser.cxx,v $
Date: $Date: 2006/03/11 19:51:14 $
Version: $Revision: 1.8 $
=========================================================================auto=*/
// MRML includes
#include "vtkMRMLParser.h"
#include "vtkMRMLScene.h"
#include "vtkMRMLStorageNode.h"
#include "vtkMRMLNode.h"
#include "vtkMRMLSubjectHierarchyNode.h"
#include "vtkMRMLSubjectHierarchyLegacyNode.h"
#include "vtkMRMLSceneViewNode.h"
#include "vtkTagTable.h"
// VTK includes
#include <vtkCollection.h>
#include <vtkObjectFactory.h>
#include <vtkStdString.h>
// STD includes
#include <sstream>
//------------------------------------------------------------------------------
vtkStandardNewMacro(vtkMRMLParser);
//------------------------------------------------------------------------------
void vtkMRMLParser::StartElement(const char* tagName, const char** atts)
{
if (!strcmp(tagName, "MRML"))
{
//--- BEGIN test of user tags
//--- pull out any tags describing the scene and fill the scene's tagtable.
const char* attName;
const char* attValue;
while (*atts != nullptr)
{
attName = *(atts++);
attValue = *(atts++);
if (!strcmp(attName, "version"))
{
this->GetMRMLScene()->SetLastLoadedVersion(attValue);
}
else if (!strcmp(attName, "userTags"))
{
if ( this->MRMLScene->GetUserTagTable() == nullptr )
{
//--- null table, no tags are read.
return;
}
std::stringstream ss(attValue);
std::string kwd = "";
std::string val = "";
std::string::size_type i;
while (!ss.eof())
{
std::string tags;
ss >> tags;
//--- now pull apart individual tags
if ( tags.c_str() != nullptr )
{
i = tags.find("=");
if ( i != std::string::npos)
{
kwd = tags.substr(0, i);
val = tags.substr(i+1, std::string::npos );
if ( kwd.c_str() != nullptr && val.c_str() != nullptr )
{
this->MRMLScene->GetUserTagTable()->AddOrUpdateTag ( kwd.c_str(), val.c_str(), 0 );
}
}
}
}
} //--- END test of user tags.
} // while
return;
} // MRML
// SubjectHierarchyItem tag means the element belongs to a subject hierarchy item, not a MRML node
//TODO: This special case can be resolved by a more generic mechanism that passes the non-node child
// elements to the containing node for parsing
if (!strcmp(tagName, "SubjectHierarchyItem"))
{
// Have the recently loaded subject hierarchy node parse the item and add it as unresolved.
// Getting the last loaded node safely returns the subject hierarchy nodes, as only it can have
// children items named SubjectHierarchyItem.
// Another possibility is that it's part of a scene view, in which case we need to access the
// last node in the scene view's snapshot scene
vtkMRMLSubjectHierarchyNode* subjectHierarchyNode = vtkMRMLSubjectHierarchyNode::SafeDownCast(
this->NodeCollection->GetItemAsObject(this->NodeCollection->GetNumberOfItems()-1) );
if (!subjectHierarchyNode)
{
vtkMRMLSceneViewNode* sceneViewNode = vtkMRMLSceneViewNode::SafeDownCast(
this->NodeCollection->GetItemAsObject(this->NodeCollection->GetNumberOfItems()-1) );
if (!sceneViewNode)
{
vtkWarningMacro("Invalid parent node element for SubjectHierarchyItem");
return;
}
vtkCollection* shNodeCollection = sceneViewNode->GetStoredScene()->GetNodesByClass("vtkMRMLSubjectHierarchyNode");
subjectHierarchyNode = vtkMRMLSubjectHierarchyNode::SafeDownCast(shNodeCollection->GetItemAsObject(0));
shNodeCollection->Delete();
}
subjectHierarchyNode->ReadItemFromXML(atts);
return;
}
const char* tmp = this->MRMLScene->GetClassNameByTag(tagName);
std::string className = tmp ? tmp : "";
// CreateNodeByClass should have a chance to instantiate non-registered node
if (className.empty())
{
className = "vtkMRML";
className += tagName;
// Append 'Node' prefix only if required
if (className.find("Node") != className.size() - 4)
{
className += "Node";
}
}
vtkMRMLNode* node = this->MRMLScene->CreateNodeByClass( className.c_str() );
if (!node)
{
vtkWarningMacro(<< "Failed to CreateNodeByClass: " << className);
return;
}
// It is needed to have the scene set before ReadXMLAttributes is
// called on storage nodes.
if (vtkMRMLStorageNode::SafeDownCast(node) != nullptr)
{
node->SetScene(this->GetMRMLScene());
}
node->ReadXMLAttributes(atts);
// Slicer3 snap shot nodes were hidden by default, show them so that
// they show up in the tree views
#if MRML_SUPPORT_VERSION < 0x040000
if (strcmp(tagName, "SceneSnapshot") == 0)
{
node->HideFromEditorsOff();
}
#endif
// Replace old-style label map nodes (vtkMRMLScalarVolumeNode with LabelMap custom attribute)
// with new-style vtkMRMLLabelMapVolumeNode
if (node->IsA("vtkMRMLScalarVolumeNode"))
{
const char* labelMapAttr = node->GetAttribute("LabelMap");
bool isLabelMap = labelMapAttr ? (atoi(labelMapAttr)!=0) : false;
if (isLabelMap)
{
// create a copy of the node of the correct class
vtkMRMLNode* newTypeLabelMapNode = this->MRMLScene->CreateNodeByClass( "vtkMRMLLabelMapVolumeNode" );
newTypeLabelMapNode->CopyWithScene(node); // copy all contents, including MRML node ID
newTypeLabelMapNode->RemoveAttribute("LabelMap"); // this attribute is obsolete
// replace the current node with the new one
node->Delete();
node=newTypeLabelMapNode;
}
}
// Replace old-style subject hierarchy nodes (vtkMRMLSubjectHierarchy nodes without the version
// attribute) with legacy node type that is handled by the hierarchy
if (node->IsA("vtkMRMLSubjectHierarchyNode"))
{
const char* shVersionAttr = node->GetAttribute(
vtkMRMLSubjectHierarchyNode::SUBJECTHIERARCHY_VERSION_ATTRIBUTE_NAME.c_str() );
bool isOldShNode = shVersionAttr ? (atoi(shVersionAttr)<2) : true;
if (isOldShNode)
{
// create a copy of the node of the correct class
vtkMRMLSubjectHierarchyLegacyNode* legacyShNode = vtkMRMLSubjectHierarchyLegacyNode::New(); // Type is not registered
// Set scene and read attributes manually, because CopyWithScene does not work due to vtkMRMLSubjectHierarchy node not
// being child class of vtkMRMLHierarchyNode, and copying non-existent node references results in invalid memory access
legacyShNode->SetScene(this->GetMRMLScene());
legacyShNode->ReadXMLAttributes(atts);
legacyShNode->HideFromEditorsOff(); // disable hide from editors so that the nodes can be added to subject hierarchy
// replace the current node with the new one
node->Delete();
node=legacyShNode;
}
}
if (!this->NodeStack.empty())
{
vtkMRMLNode* parentNode = this->NodeStack.top();
parentNode->ProcessChildNode(node);
}
this->NodeStack.push(node);
if (this->NodeCollection)
{
if (node->GetAddToScene())
{
this->NodeCollection->vtkCollection::AddItem((vtkObject *)node);
}
}
else
{
this->MRMLScene->AddNode(node);
}
node->Delete();
}
//-----------------------------------------------------------------------------
void vtkMRMLParser::EndElement(const char *name)
{
if ( !strcmp(name, "MRML") || this->NodeStack.empty() )
{
return;
}
const char* className = this->MRMLScene->GetClassNameByTag(name);
if (className == nullptr)
{
// check for a renamed node
if (strcmp(name, "SceneSnapshot") == 0)
{
className = this->MRMLScene->GetClassNameByTag("SceneView");
if (className == nullptr)
{
return;
}
}
else
{
return;
}
}
this->NodeStack.pop();
}