Salome HOME
Fix on Bug PAL7927
[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 #include <qstring.h>
31
32 using namespace std;
33
34 namespace VISU{
35
36   inline int GetNbOfPoints(int theVTKCellType){
37     switch(theVTKCellType){
38     case VTK_VERTEX : return 1;
39     case VTK_LINE : return 2;
40     case VTK_TRIANGLE : return 3;
41     case VTK_QUAD : return 4;
42     case VTK_TETRA : return 4;
43     case VTK_HEXAHEDRON : return 8;
44     case VTK_WEDGE : return 6;
45     case VTK_PYRAMID : return 5;
46     default: return -1;
47     }
48   }
49
50   pair<int,int> TMeshOnEntity::GetCellsDims(const string& theFamilyName) const
51   {
52     if(theFamilyName == "")
53       return make_pair(myNbCells,myCellsSize);
54     TFamilyMap::const_iterator aFamilyMapIter = myFamilyMap.find(theFamilyName);
55     if(aFamilyMapIter == myFamilyMap.end())
56       throw std::runtime_error("GetCellsDims >> There is no family on the mesh with entity !!!");
57     const PFamily& aFamily = aFamilyMapIter->second; 
58     return make_pair(aFamily->myNbCells,aFamily->myCellsSize);
59   }
60   
61   const PField TMesh::GetField(const string& theFieldName) const {
62     TMeshOnEntityMap::const_iterator aMeshOnEntityMapIter = myMeshOnEntityMap.begin();
63     for(; aMeshOnEntityMapIter != myMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
64       const TFieldMap& aFieldMap = aMeshOnEntityMapIter->second->myFieldMap;
65       TFieldMap::const_iterator aFieldMapIter = aFieldMap.begin();
66       for(; aFieldMapIter != aFieldMap.end(); aFieldMapIter++)
67         if(theFieldName == aFieldMapIter->first) 
68           return aFieldMapIter->second;
69     }
70     return PField();
71   }
72
73
74   PFamily FindFamily(VISU::PMesh theMesh, const string& theFamilyName)
75   {
76     PFamily aFamily;
77     const TMeshOnEntityMap& aMeshOnEntityMap = theMesh->myMeshOnEntityMap;
78     TMeshOnEntityMap::const_iterator aMeshOnEntityMapIter = aMeshOnEntityMap.begin();
79     for(; aMeshOnEntityMapIter != aMeshOnEntityMap.end(); aMeshOnEntityMapIter++){
80       const PMeshOnEntity aMeshOnEntity = aMeshOnEntityMapIter->second;
81       aFamily = GetFamily(aMeshOnEntity,theFamilyName);
82       if(aFamily)
83         break;
84     }
85     return aFamily;
86   }
87   
88
89   PFamily GetFamily(VISU::PMeshOnEntity theMeshOnEntity, const string& theFamilyName)
90   {
91     PFamily aFamily;
92     if(theFamilyName != ""){
93       TFamilyMap& aFamilyMap = theMeshOnEntity->myFamilyMap;
94       TFamilyMap::iterator aFamilyMapIter = aFamilyMap.find(theFamilyName);
95       if(aFamilyMapIter != aFamilyMap.end())
96         aFamily = aFamilyMapIter->second;
97     }
98     return aFamily;
99   }
100 }
101
102
103 const VISU::TMeshMap& VISU_Convertor::GetMeshMap() { 
104   if(!myIsDone) { myIsDone = true;  Build();}
105   return myMeshMap;
106 }
107
108
109 string VISU_Convertor::GenerateName(const VISU::TTime& aTime){
110   static QString aName;
111   const string aUnits = aTime.second, tmp(aUnits.size(),' ');
112   if(aUnits == "" || aUnits == tmp)
113     aName.sprintf("%g, -",aTime.first);
114   else
115     aName.sprintf("%g, %s",aTime.first,aTime.second.c_str());
116   aName = aName.simplifyWhiteSpace();
117   return aName.latin1();
118 }
119
120 string VISU_Convertor::GenerateName(const string& theName, unsigned int theTimeId) {
121   static QString aName;
122   aName = QString(theName.c_str()).simplifyWhiteSpace();
123   int iEnd = strlen(aName);
124   static int VtkHighLevelLength = 12; //25
125   if(iEnd > VtkHighLevelLength) iEnd = VtkHighLevelLength;
126   char aNewName[iEnd+1];
127   aNewName[iEnd] = '\0';
128   strncpy(aNewName,aName,iEnd);
129   replace(aNewName,aNewName+iEnd,' ','_');
130   if(true || theTimeId == 0)
131     aName = aNewName;
132   else
133     aName.sprintf("%s_%d",aNewName,theTimeId);
134   return aName.latin1();
135 }