Salome HOME
Merge with OCC-V2_1_0_deb
[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) {
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   {
64     bool isFamilyPresent = (theFamilyName != "");
65     int aNbCells = 0, aCellsSize = 0;
66     if(!isFamilyPresent){
67       TCellsConn::const_iterator aCellsConnIter = myCellsConn.begin();
68       for(; aCellsConnIter != myCellsConn.end(); aCellsConnIter++){
69         const TConnForCellType& aConnForCellType = aCellsConnIter->second;
70         if(!aConnForCellType.empty()){
71           aNbCells += aConnForCellType.size();
72           aCellsSize += aConnForCellType.size()*(aConnForCellType[0].size()+1);
73         }
74       }
75     }else{
76       TFamilyMap::const_iterator aFamilyMapIter = myFamilyMap.find(theFamilyName);
77       if(aFamilyMapIter == myFamilyMap.end())
78         throw std::runtime_error("GetCellsDims >> There is no family on the mesh with entity !!!");
79       const TFamily& aFamily = aFamilyMapIter->second; 
80       const TFamily::TSubMesh& aSubMesh = aFamily.mySubMesh;
81       TFamily::TSubMesh::const_iterator aSubMeshIter = aSubMesh.begin();
82       for(; aSubMeshIter != aSubMesh.end(); aSubMeshIter++){
83         const TFamily::TSubMeshOnCellType& aSubMeshOnCellType = aSubMeshIter->second;
84         if(!aSubMeshOnCellType.empty()){
85           int tmp = aSubMeshOnCellType.size();
86           aNbCells += tmp;
87           int aVtkType = aSubMeshIter->first;
88           int aVtkSize = GetNbOfPoints(aVtkType);
89           aCellsSize += tmp*(aVtkSize+1);
90         }
91       }
92     }
93     return make_pair(aNbCells,aCellsSize);
94   }
95   
96   const TField* TMesh::GetField(const string& theFieldName) const {
97     TMeshOnEntityMap::const_iterator aMeshOnEntityMapIter = myMeshOnEntityMap.begin();
98     for(; aMeshOnEntityMapIter != myMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
99       const TFieldMap& aFieldMap = (aMeshOnEntityMapIter->second).myFieldMap;
100       TFieldMap::const_iterator aFieldMapIter = aFieldMap.begin();
101       for(; aFieldMapIter != aFieldMap.end(); aFieldMapIter++)
102         if(theFieldName == aFieldMapIter->first) return &(aFieldMapIter->second);
103     }
104     return NULL;
105   }
106
107   const TFamily* GetFamily(const VISU::TMeshOnEntity& theMeshOnEntity, const string& theFamilyName)
108   {
109     if(theFamilyName == "") return NULL;
110     const VISU::TFamilyMap& aFamilyMap = theMeshOnEntity.myFamilyMap;
111     VISU::TFamilyMap::const_iterator aFamilyMapIter = aFamilyMap.find(theFamilyName);
112     if(aFamilyMapIter == aFamilyMap.end())
113       throw std::runtime_error("GetFamily >> There is no family on the mesh with entity !!!");
114     const VISU::TFamily& aFamily = aFamilyMapIter->second;
115     return &aFamily;
116   }
117   
118   TFamily* GetFamily(VISU::TMeshOnEntity& theMeshOnEntity, const string& theFamilyName)
119   {
120     if(theFamilyName == "") return NULL;
121     VISU::TFamilyMap& aFamilyMap = theMeshOnEntity.myFamilyMap;
122     VISU::TFamilyMap::iterator aFamilyMapIter = aFamilyMap.find(theFamilyName);
123     if(aFamilyMapIter == aFamilyMap.end())
124       throw std::runtime_error("GetFamily >> There is no family on the mesh with entity !!!");
125     VISU::TFamily& aFamily = aFamilyMapIter->second;
126     return &aFamily;
127   }
128   
129   void TField::ShallowCopy(const TField& aField){
130     myEntity = aField.myEntity;
131     myMeshName = aField.myMeshName;
132     myNbComp = aField.myNbComp;
133     VISU::TField::TValField::const_iterator iter = aField.myValField.begin();
134     for(; iter != aField.myValField.end(); iter++)
135       myValField[iter->first];
136   }
137 }
138
139
140 const VISU::TMeshMap& VISU_Convertor::GetMeshMap() { 
141   if(!myIsDone) { myIsDone = true;  Build();}
142   return myMeshMap;
143 }
144
145
146 string VISU_Convertor::GenerateName(const VISU::TField::TTime& aTime){
147   static QString aName;
148   const string aUnits = aTime.second, tmp(aUnits.size(),' ');
149   if(aUnits == "" || aUnits == tmp)
150     aName.sprintf("%g, -",aTime.first);
151   else
152     aName.sprintf("%g, %s",aTime.first,aTime.second.c_str());
153   aName = aName.simplifyWhiteSpace();
154   return aName.latin1();
155 }
156
157 string VISU_Convertor::GenerateName(const string& theName, unsigned int theTimeId) {
158   static QString aName;
159   aName = QString(theName.c_str()).simplifyWhiteSpace();
160   int iEnd = strlen(aName);
161   static int VtkHighLevelLength = 12; //25
162   if(iEnd > VtkHighLevelLength) iEnd = VtkHighLevelLength;
163   char aNewName[iEnd+1];
164   aNewName[iEnd] = '\0';
165   strncpy(aNewName,aName,iEnd);
166   replace(aNewName,aNewName+iEnd,' ','_');
167   if(true || theTimeId == 0)
168     aName = aNewName;
169   else
170     aName.sprintf("%s_%d",aNewName,theTimeId);
171   return aName.latin1();
172 }