Salome HOME
Merge from V6_main 01/04/2013
[modules/paravis.git] / src / Plugins / MedReader / IO / vtkMedFile.cxx
1 // Copyright (C) 2010-2013  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "vtkMedFile.h"
21
22 #include "vtkObjectFactory.h"
23 #include "vtkDataArraySelection.h"
24 #include "vtkSmartPointer.h"
25
26 #include "vtkMedMesh.h"
27 #include "vtkMedField.h"
28 #include "vtkMedProfile.h"
29 #include "vtkMedLocalization.h"
30 #include "vtkMedUtilities.h"
31 #include "vtkMedLink.h"
32 #include "vtkMedDriver.h"
33 #include "vtkMedFactory.h"
34 #include "vtkMedStructElement.h"
35
36 vtkCxxGetObjectVectorMacro(vtkMedFile, Mesh, vtkMedMesh);
37 vtkCxxSetObjectVectorMacro(vtkMedFile, Mesh, vtkMedMesh);
38
39 vtkCxxGetObjectVectorMacro(vtkMedFile, Field, vtkMedField);
40 vtkCxxSetObjectVectorMacro(vtkMedFile, Field, vtkMedField);
41
42 vtkCxxGetObjectVectorMacro(vtkMedFile, Profile, vtkMedProfile);
43 vtkCxxSetObjectVectorMacro(vtkMedFile, Profile, vtkMedProfile);
44
45 vtkCxxGetObjectVectorMacro(vtkMedFile, Localization, vtkMedLocalization);
46 vtkCxxSetObjectVectorMacro(vtkMedFile, Localization, vtkMedLocalization);
47
48 vtkCxxGetObjectVectorMacro(vtkMedFile, Link, vtkMedLink);
49 vtkCxxSetObjectVectorMacro(vtkMedFile, Link, vtkMedLink);
50
51 vtkCxxGetObjectVectorMacro(vtkMedFile, StructElement, vtkMedStructElement);
52 vtkCxxSetObjectVectorMacro(vtkMedFile, StructElement, vtkMedStructElement);
53
54 vtkCxxGetObjectVectorMacro(vtkMedFile, SupportMesh, vtkMedMesh);
55 vtkCxxSetObjectVectorMacro(vtkMedFile, SupportMesh, vtkMedMesh);
56
57 vtkCxxSetObjectMacro(vtkMedFile, MedDriver, vtkMedDriver);
58
59 //vtkCxxRevisionMacro(vtkMedFile, "$Revision$")
60 vtkStandardNewMacro(vtkMedFile)
61
62 vtkMedFile::vtkMedFile()
63 {
64   this->Comment = NULL;
65   this->Mesh = new vtkObjectVector<vtkMedMesh> ();
66   this->Field = new vtkObjectVector<vtkMedField> ();
67   this->Profile = new vtkObjectVector<vtkMedProfile> ();
68   this->Localization = new vtkObjectVector<vtkMedLocalization> ();
69   this->Link = new vtkObjectVector<vtkMedLink> ();
70   this->StructElement = new vtkObjectVector<vtkMedStructElement>();
71   this->SupportMesh = new vtkObjectVector<vtkMedMesh>();
72   this->FileName = NULL;
73   this->MedDriver = NULL;
74   this->VersionMajor = -1;
75   this->VersionMinor = -1;
76   this->VersionRelease = -1;
77 }
78
79 vtkMedFile::~vtkMedFile()
80 {
81   this->SetComment(NULL);
82   delete this->Mesh;
83   delete this->Field;
84   delete this->Profile;
85   delete this->Localization;
86   delete this->Link;
87   delete this->StructElement;
88   delete this->SupportMesh;
89   this->SetFileName(NULL);
90   this->SetMedDriver(NULL);
91 }
92
93 int vtkMedFile::CreateDriver()
94 {
95   int major, minor, release;
96   vtkMedDriver* driver=vtkMedDriver::New();
97   driver->SetMedFile(this);
98   bool canRead=driver->CanReadFile();
99   if(!canRead)
100     {
101     driver->Delete();
102     this->SetMedDriver(NULL);
103     return 0;
104     }
105   driver->ReadFileVersion(&major, &minor, &release);
106   driver->Delete();
107   vtkMedFactory* factory=vtkMedFactory::New();
108   driver=factory->NewMedDriver(major, minor, release);
109   factory->Delete();
110   this->SetMedDriver(driver);
111   if (driver)
112     {
113     driver->SetMedFile(this);
114     return 1;
115     }
116   return 0;
117 }
118
119 void  vtkMedFile::ReadInformation()
120 {
121   if(this->MedDriver == NULL)
122     {
123     if(!this->CreateDriver())
124       return;
125     }
126
127   // at this point, we know that we have a valid driver.
128   this->MedDriver->ReadFileInformation(this);
129 }
130
131 vtkMedMesh* vtkMedFile::GetMesh(const char* str)
132 {
133   for (int m = 0; m < this->Mesh->size(); m++)
134     {
135     vtkMedMesh* mesh = this->Mesh->at(m);
136     if (strcmp(mesh->GetName(), str) == 0)
137       {
138       return mesh;
139       }
140     }
141   return NULL;
142 }
143
144 vtkMedProfile* vtkMedFile::GetProfile(const char* str)
145 {
146   for (int profId = 0; profId < this->Profile->size(); profId++)
147     {
148     vtkMedProfile* profile = this->Profile->at(profId);
149     if (strcmp(profile->GetName(), str) == 0)
150       {
151       return profile;
152       }
153     }
154   return NULL;
155
156 }
157
158 vtkMedLocalization* vtkMedFile::GetLocalization(const char* str)
159 {
160   for (int quadId = 0; quadId < this->Localization->size(); quadId++)
161     {
162     vtkMedLocalization* loc = this->Localization->at(quadId);
163     if (strcmp(loc->GetName(), str) == 0)
164       {
165       return loc;
166       }
167     }
168   return NULL;
169 }
170
171 vtkMedStructElement* vtkMedFile::GetStructElement(const vtkMedEntity& entity)
172 {
173   if(entity.EntityType != MED_STRUCT_ELEMENT)
174     return NULL;
175
176   for(int selemit = 0; selemit < this->GetNumberOfStructElement(); selemit++)
177     {
178     vtkMedStructElement* structelem = this->GetStructElement(selemit);
179     if(structelem->GetGeometryType() == entity.GeometryType)
180       return structelem;
181     }
182   return NULL;
183 }
184
185 void vtkMedFile::PrintSelf(ostream& os, vtkIndent indent)
186 {
187   this->Superclass::PrintSelf(os, indent);
188
189   PRINT_OBJECT_VECTOR(os, indent, Mesh);
190   PRINT_OBJECT_VECTOR(os, indent, Field);
191   PRINT_OBJECT_VECTOR(os, indent, Profile);
192   PRINT_OBJECT_VECTOR(os, indent, Localization);
193   PRINT_OBJECT_VECTOR(os, indent, Link);
194 }