1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkXMLMultiBlockDataWriter.cxx
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14 =========================================================================*/
15 #include "vtkXMLMultiBlockDataWriter.h"
16
17 #include "vtkDataObjectTreeIterator.h"
18 #include "vtkInformation.h"
19 #include "vtkMultiBlockDataSet.h"
20 #include "vtkObjectFactory.h"
21 #include "vtkSmartPointer.h"
22 #include "vtkXMLDataElement.h"
23 #include "vtkInformation.h"
24
25 vtkStandardNewMacro(vtkXMLMultiBlockDataWriter);
26 //----------------------------------------------------------------------------
vtkXMLMultiBlockDataWriter()27 vtkXMLMultiBlockDataWriter::vtkXMLMultiBlockDataWriter()
28 {
29 }
30
31 //----------------------------------------------------------------------------
~vtkXMLMultiBlockDataWriter()32 vtkXMLMultiBlockDataWriter::~vtkXMLMultiBlockDataWriter()
33 {
34 }
35
36 //----------------------------------------------------------------------------
FillInputPortInformation(int vtkNotUsed (port),vtkInformation * info)37 int vtkXMLMultiBlockDataWriter::FillInputPortInformation(
38 int vtkNotUsed(port), vtkInformation* info)
39 {
40 info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkMultiBlockDataSet");
41 return 1;
42 }
43
44 //----------------------------------------------------------------------------
WriteComposite(vtkCompositeDataSet * compositeData,vtkXMLDataElement * parent,int & writerIdx)45 int vtkXMLMultiBlockDataWriter::WriteComposite(vtkCompositeDataSet* compositeData,
46 vtkXMLDataElement* parent, int &writerIdx)
47 {
48 if (! (compositeData->IsA("vtkMultiBlockDataSet")
49 ||compositeData->IsA("vtkMultiPieceDataSet")) )
50 {
51 vtkErrorMacro("Unsupported composite dataset type: "
52 << compositeData->GetClassName() << ".");
53 return 0;
54 }
55
56 // Write each input.
57 vtkSmartPointer<vtkDataObjectTreeIterator> iter;
58 iter.TakeReference(
59 vtkDataObjectTree::SafeDownCast(compositeData)->NewTreeIterator());
60 iter->VisitOnlyLeavesOff();
61 iter->TraverseSubTreeOff();
62 iter->SkipEmptyNodesOff();
63
64 int index = 0;
65 int RetVal = 0;
66 for (iter->InitTraversal(); !iter->IsDoneWithTraversal();
67 iter->GoToNextItem(), index++)
68 {
69 vtkDataObject* curDO = iter->GetCurrentDataObject();
70 const char *name = NULL;
71 if (iter->HasCurrentMetaData())
72 {
73 name = iter->GetCurrentMetaData()->Get(vtkCompositeDataSet::NAME());
74 }
75
76 if (curDO && curDO->IsA("vtkCompositeDataSet"))
77 // if node is a supported composite dataset
78 // note in structure file and recurse.
79 {
80 vtkXMLDataElement* tag = vtkXMLDataElement::New();
81 if (name)
82 {
83 tag->SetAttribute("name", name);
84 }
85
86 if (curDO->IsA("vtkMultiPieceDataSet"))
87 {
88 tag->SetName("Piece");
89 tag->SetIntAttribute("index", index);
90 }
91 else if (curDO->IsA("vtkMultiBlockDataSet"))
92 {
93 tag->SetName("Block");
94 tag->SetIntAttribute("index", index);
95 }
96 vtkCompositeDataSet* curCD
97 = vtkCompositeDataSet::SafeDownCast(curDO);
98 if (!this->WriteComposite(curCD, tag, writerIdx))
99 {
100 tag->Delete();
101 return 0;
102 }
103 RetVal = 1;
104 parent->AddNestedElement(tag);
105 tag->Delete();
106 }
107 else
108 // this node is not a composite data set.
109 {
110 vtkXMLDataElement* datasetXML = vtkXMLDataElement::New();
111 datasetXML->SetName("DataSet");
112 datasetXML->SetIntAttribute("index", index);
113 if (name)
114 {
115 datasetXML->SetAttribute("name", name);
116 }
117 vtkStdString fileName = this->CreatePieceFileName(writerIdx);
118 if (this->WriteNonCompositeData( curDO, datasetXML, writerIdx,
119 fileName.c_str()))
120 {
121 parent->AddNestedElement(datasetXML);
122 RetVal = 1;
123 }
124 datasetXML->Delete();
125 }
126 }
127 return RetVal;
128 }
129
130
131 //----------------------------------------------------------------------------
PrintSelf(ostream & os,vtkIndent indent)132 void vtkXMLMultiBlockDataWriter::PrintSelf(ostream& os, vtkIndent indent)
133 {
134 this->Superclass::PrintSelf(os, indent);
135 }
136
137