Salome HOME
c9dbdb411965af4cae63cf3fa3e6e7890203226b
[modules/visu.git] / src / CONVERTOR / VISU_Convertor.cxx
1 //  VISU OBJECT : interactive object for VISU entities implementation
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //  File:    VISU_Convertor.cxx
24 //  Author:  Alexey PETROV
25 //  Module : VISU
26
27 #include "VISU_Convertor.hxx"
28 #include "VISU_ConvertorUtils.hxx"
29
30 using namespace std;
31
32 #ifdef _DEBUG_
33 static int MYDEBUG = 0;
34 #else
35 static int MYDEBUG = 0;
36 #endif
37
38 extern "C" {
39   VISU_Convertor* CreateConvertor(const string& theFileName) throw(std::runtime_error&){
40     if(QFileInfo(theFileName.c_str()).extension(false) == "med")
41       return CreateMedConvertor(theFileName);
42     else
43       return CreateDatConvertor(theFileName);
44   }
45 }
46
47 namespace VISU{
48   inline int GetNbOfPoints(int theVTKCellType){
49     switch(theVTKCellType){
50     case VTK_VERTEX : return 1;
51     case VTK_LINE : return 2;
52     case VTK_TRIANGLE : return 3;
53     case VTK_QUAD : return 4;
54     case VTK_TETRA : return 4;
55     case VTK_HEXAHEDRON : return 8;
56     case VTK_WEDGE : return 6;
57     case VTK_PYRAMID : return 5;
58     default: return -1;
59     }
60   }
61
62   pair<int,int> TMeshOnEntity::GetCellsDims(const string& theFamilyName) const
63     throw(std::runtime_error&)
64   {
65     bool isFamilyPresent = (theFamilyName != "");
66     int aNbCells = 0, aCellsSize = 0;
67     if(!isFamilyPresent){
68       TCellsConn::const_iterator aCellsConnIter = myCellsConn.begin();
69       for(; aCellsConnIter != myCellsConn.end(); aCellsConnIter++){
70         const TConnForCellType& aConnForCellType = aCellsConnIter->second;
71         if(!aConnForCellType.empty()){
72           aNbCells += aConnForCellType.size();
73           aCellsSize += aConnForCellType.size()*(aConnForCellType[0].size()+1);
74         }
75       }
76     }else{
77       TFamilyMap::const_iterator aFamilyMapIter = myFamilyMap.find(theFamilyName);
78       if(aFamilyMapIter == myFamilyMap.end())
79         throw std::runtime_error("GetCellsDims >> There is no family on the mesh with entity !!!");
80       const TFamily& aFamily = aFamilyMapIter->second; 
81       const TFamily::TSubMesh& aSubMesh = aFamily.mySubMesh;
82       TFamily::TSubMesh::const_iterator aSubMeshIter = aSubMesh.begin();
83       for(; aSubMeshIter != aSubMesh.end(); aSubMeshIter++){
84         const TFamily::TSubMeshOnCellType& aSubMeshOnCellType = aSubMeshIter->second;
85         if(!aSubMeshOnCellType.empty()){
86           int tmp = aSubMeshOnCellType.size();
87           aNbCells += tmp;
88           int aVtkType = aSubMeshIter->first;
89           int aVtkSize = GetNbOfPoints(aVtkType);
90           aCellsSize += tmp*(aVtkSize+1);
91         }
92       }
93     }
94     return make_pair(aNbCells,aCellsSize);
95   }
96   
97   const TField* TMesh::GetField(const string& theFieldName) const {
98     TMeshOnEntityMap::const_iterator aMeshOnEntityMapIter = myMeshOnEntityMap.begin();
99     for(; aMeshOnEntityMapIter != myMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
100       const TFieldMap& aFieldMap = (aMeshOnEntityMapIter->second).myFieldMap;
101       TFieldMap::const_iterator aFieldMapIter = aFieldMap.begin();
102       for(; aFieldMapIter != aFieldMap.end(); aFieldMapIter++)
103         if(theFieldName == aFieldMapIter->first) return &(aFieldMapIter->second);
104     }
105     return NULL;
106   }
107
108   const TFamily* GetFamily(const VISU::TMeshOnEntity& theMeshOnEntity, const string& theFamilyName)
109     throw(std::runtime_error&) 
110   {
111     if(theFamilyName == "") return NULL;
112     const VISU::TFamilyMap& aFamilyMap = theMeshOnEntity.myFamilyMap;
113     VISU::TFamilyMap::const_iterator aFamilyMapIter = aFamilyMap.find(theFamilyName);
114     if(aFamilyMapIter == aFamilyMap.end())
115       throw std::runtime_error("GetFamily >> There is no family on the mesh with entity !!!");
116     const VISU::TFamily& aFamily = aFamilyMapIter->second;
117     return &aFamily;
118   }
119   
120   TFamily* GetFamily(VISU::TMeshOnEntity& theMeshOnEntity, const string& theFamilyName)
121     throw(std::runtime_error&) 
122   {
123     if(theFamilyName == "") return NULL;
124     VISU::TFamilyMap& aFamilyMap = theMeshOnEntity.myFamilyMap;
125     VISU::TFamilyMap::iterator aFamilyMapIter = aFamilyMap.find(theFamilyName);
126     if(aFamilyMapIter == aFamilyMap.end())
127       throw std::runtime_error("GetFamily >> There is no family on the mesh with entity !!!");
128     VISU::TFamily& aFamily = aFamilyMapIter->second;
129     return &aFamily;
130   }
131   
132   void TField::ShallowCopy(const TField& aField){
133     myEntity = aField.myEntity;
134     myMeshName = aField.myMeshName;
135     myNbComp = aField.myNbComp;
136     VISU::TField::TValField::const_iterator iter = aField.myValField.begin();
137     for(; iter != aField.myValField.end(); iter++)
138       myValField[iter->first];
139   }
140 }
141
142
143 const VISU::TMeshMap& VISU_Convertor::GetMeshMap() throw(std::runtime_error&){ 
144   if(!myIsDone) { myIsDone = true;  Build();}
145   return myMeshMap;
146 }
147
148
149 string VISU_Convertor::GenerateName(const VISU::TField::TTime& aTime){
150   static QString aName;
151   const string aUnits = aTime.second, tmp(aUnits.size(),' ');
152   if(aUnits == "" || aUnits == tmp)
153     aName.sprintf("%g, -",aTime.first);
154   else
155     aName.sprintf("%g, %s",aTime.first,aTime.second.c_str());
156   aName = aName.simplifyWhiteSpace();
157   return aName.latin1();
158 }
159
160 string VISU_Convertor::GenerateName(const string& theName, unsigned int theTimeId) {
161   static QString aName;
162   aName = QString(theName.c_str()).simplifyWhiteSpace();
163   int iEnd = strlen(aName);
164   static int VtkHighLevelLength = 12; //25
165   if(iEnd > VtkHighLevelLength) iEnd = VtkHighLevelLength;
166   char aNewName[iEnd+1];
167   aNewName[iEnd] = '\0';
168   strncpy(aNewName,aName,iEnd);
169   replace(aNewName,aNewName+iEnd,' ','_');
170   if(true || theTimeId == 0)
171     aName = aNewName;
172   else
173     aName.sprintf("%s_%d",aNewName,theTimeId);
174   return aName.latin1();
175 }