Salome HOME
6e6170d9793db28e1a1220d96425588ae89b4f9f
[modules/visu.git] / src / CONVERTOR / VISUConvertor.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  VISU OBJECT : interactive object for VISU entities implementation
23 //  File:    VISUConvertor.cxx
24 //  Author:  Alexey PETROV
25 //  Module : VISU
26 //
27 #include "VISU_Convertor.hxx"
28 #include "VISU_ConvertorUtils.hxx"
29
30 #include "VISU_Convertor_impl.hxx"
31 #include "VISU_TableReader.hxx"
32
33 #include <fstream>      
34 #include <strstream>
35 #include <memory>       
36
37 #include <vtkCellType.h>
38 #include <vtkUnstructuredGrid.h>
39
40 #include <QDir>
41 #include <QFileInfo>
42 #include <QStringList>
43
44
45 #include "utilities.h"
46
47 using namespace std;
48
49 #ifdef DEBUG
50 static int MYDEBUG = 1;
51 #else
52 static int MYDEBUG = 0;
53 #endif
54
55 //#define _DEBUG_ID_MAPPING_
56 #define _DEXCEPT_
57
58 typedef vtkUnstructuredGrid TOutput;
59
60 void ParseMEDFile(const char* theFileName) 
61 {
62 #ifndef _DEXCEPT_
63   try{
64 #endif
65     MSG(MYDEBUG,"'"<<theFileName<<"'...");
66     //theFileName = "Apointe.med";
67     auto_ptr<VISU_Convertor> aCon(CreateConvertor(theFileName));
68     //aCon->GetSize();
69     //return;
70     aCon->BuildEntities();
71     aCon->BuildGroups();
72     aCon->BuildFields();
73     aCon->BuildMinMax();
74     const VISU::TMeshMap& aMeshMap = aCon->GetMeshMap();
75     //return;
76     VISU::TMeshMap::const_iterator aMeshMapIter = aMeshMap.begin();
77     for(; aMeshMapIter != aMeshMap.end(); aMeshMapIter++){
78       //continue;
79
80       const string& aMeshName = aMeshMapIter->first;
81       const VISU::PMesh& aMesh = aMeshMapIter->second;
82       const VISU::TMeshOnEntityMap& aMeshOnEntityMap = aMesh->myMeshOnEntityMap;
83       VISU::TMeshOnEntityMap::const_iterator aMeshOnEntityMapIter;
84
85       //Import fields
86       aMeshOnEntityMapIter = aMeshOnEntityMap.begin();
87       for(; aMeshOnEntityMapIter != aMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
88         const VISU::TEntity& anEntity = aMeshOnEntityMapIter->first;
89         const VISU::PMeshOnEntity& aMeshOnEntity = aMeshOnEntityMapIter->second;
90         const VISU::TFieldMap& aFieldMap = aMeshOnEntity->myFieldMap;
91         VISU::TFieldMap::const_reverse_iterator aFieldMapIter = aFieldMap.rbegin();
92         for(; aFieldMapIter != aFieldMap.rend(); aFieldMapIter++){
93           const string& aFieldName = aFieldMapIter->first;
94           const VISU::PField& aField = aFieldMapIter->second;
95           const VISU::TValField& aValField = aField->myValField;
96           VISU::TValField::const_iterator aValFieldIter = aValField.begin();
97           for(; aValFieldIter != aValField.end(); aValFieldIter++){
98             int aTimeStamp = aValFieldIter->first;
99
100             if(anEntity != VISU::NODE_ENTITY){
101               VISU::PGaussPtsIDMapper aGaussMesh = 
102                 aCon->GetTimeStampOnGaussPts(aMeshName,anEntity,aFieldName,aTimeStamp);
103 #ifdef _DEBUG_ID_MAPPING_
104               vtkDataSet* aDataSet = aGaussMesh->GetOutput();
105               aDataSet->Update();
106               int aNbCells = aDataSet->GetNumberOfCells();
107               MESSAGE("aNbCells = "<<aNbCells);
108               for(int anCellId = 0; anCellId < aNbCells; anCellId++){
109                 VISU::TGaussPointID anObjID = aGaussMesh->GetObjID(anCellId);
110                 cout<<anObjID.first<<"; "<<anObjID.second<<"; "<<aGaussMesh->GetNodeVTKID(anObjID.first)<<endl;
111                 vtkFloatingPointType* aCoord = aGaussMesh->GetNodeCoord(anCellId);
112                 cout<<aCoord[0]<<"; "<<aCoord[1]<<"; "<<aCoord[2]<<endl;
113               }
114 #endif
115             }else{
116               //continue;
117               VISU::PIDMapper anIDMapper = 
118                 aCon->GetTimeStampOnMesh(aMeshName,anEntity,aFieldName,aTimeStamp);
119 #ifdef _DEBUG_ID_MAPPING_
120               vtkDataSet* aDataSet = anIDMapper->GetOutput();
121               aDataSet->Update();
122               int aNbCells = aDataSet->GetNumberOfCells();
123               for(int anCellId = 0; anCellId < aNbCells; anCellId++){
124                 int anObjID = anIDMapper->GetElemObjID(anCellId);
125                 int aVTKID  = anIDMapper->GetElemVTKID(anObjID);
126                 cout<<anObjID<<"; "<<aVTKID<<endl;
127               }
128 #endif
129             }
130             //goto OK;
131           }
132         }
133       }
134
135       //Importing groups
136       const VISU::TGroupMap& aGroupMap = aMesh->myGroupMap;
137       VISU::TGroupMap::const_iterator aGroupMapIter = aGroupMap.begin();
138       for(; aGroupMapIter != aGroupMap.end(); aGroupMapIter++){
139         const string& aGroupName = aGroupMapIter->first;
140         aCon->GetMeshOnGroup(aMeshName,aGroupName);
141       }
142
143       //continue;
144
145       //Import mesh on entity
146       aMeshOnEntityMapIter = aMeshOnEntityMap.begin();
147       for(; aMeshOnEntityMapIter != aMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
148         const VISU::TEntity& anEntity = aMeshOnEntityMapIter->first;
149         VISU::PIDMapper anIDMapper = aCon->GetMeshOnEntity(aMeshName,anEntity);
150 #ifdef _DEBUG_ID_MAPPING_
151         vtkDataSet* aDataSet = anIDMapper->GetOutput();
152         int aNbCells, anCellId, anObjID, aVTKID;
153         aNbCells = aDataSet->GetNumberOfCells();
154         for(anCellId = 0; anCellId < aNbCells; anCellId++){
155           anObjID = anIDMapper->GetElemObjID(anCellId);
156           aVTKID  = anIDMapper->GetElemVTKID(anObjID);
157           MESSAGE(anObjID<<"; "<<aVTKID);
158         }
159 #endif
160       }
161
162       //Import families
163       aMeshOnEntityMapIter = aMeshOnEntityMap.begin();
164       for(; aMeshOnEntityMapIter != aMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
165         const VISU::TEntity& anEntity = aMeshOnEntityMapIter->first;
166         const VISU::PMeshOnEntity& aMeshOnEntity = aMeshOnEntityMapIter->second;
167         //aCon->GetMeshOnEntity(aMeshName,anEntity);
168         const VISU::TFamilyMap& aFamilyMap = aMeshOnEntity->myFamilyMap;
169         VISU::TFamilyMap::const_iterator aFamilyMapIter = aFamilyMap.begin();
170         for(; aFamilyMapIter != aFamilyMap.end(); aFamilyMapIter++){
171           const string& aFamilyName = aFamilyMapIter->first;
172           aCon->GetFamilyOnEntity(aMeshName,anEntity,aFamilyName);
173         }
174       }
175     }
176   OK:
177     MSG(MYDEBUG,"OK");
178 #ifndef _DEXCEPT_
179   }catch(std::exception& exc){
180     MSG(MYDEBUG,"Follow exception was occured in file:"<<theFileName<<"\n"<<exc.what());
181   }catch(...){
182     MSG(MYDEBUG,"Unknown exception was occured in VISU_Convertor_impl in file:"<<theFileName);
183   } 
184 #endif
185 }
186
187 void ParseXLSFile(const char* theFileName) 
188 {
189   VISU::TTableContainer aContainer;
190   VISU::ImportTables( theFileName, aContainer );
191 }
192
193 int
194 main( int argc, char** argv )
195
196   if ( argc > 1 ) {
197     QFileInfo aFileInfo( argv[1] );
198     for ( int i = 0; i < 1; i++ ) {
199       if ( aFileInfo.exists() ) {
200         if ( aFileInfo.isDir() ) {
201           QDir aDir( aFileInfo.absoluteFilePath() );
202           QStringList wildcards; wildcards << "*.med";
203           QStringList aStringList = aDir.entryList( wildcards, QDir::Files );
204           int jEnd = aStringList.count();
205           for ( int j = 0; j < jEnd; j++ ) {
206             ParseMEDFile( aDir.filePath( aStringList[j] ).toLatin1().data() );
207           }
208         } else {
209           QString anExtension = aFileInfo.suffix();
210           if ( anExtension == "med" )
211             ParseMEDFile( argv[1] );
212           else if ( anExtension == "xls" || anExtension == "txt" )
213             ParseXLSFile( argv[1] );
214         }
215       }
216     }
217     return 0;
218   }
219   return 1;
220 }