Salome HOME
Merge from V5_1_main 14/05/2010
[modules/smesh.git] / src / DriverUNV / UNV2417_Structure.cxx
1 //  Copyright (C) 2007-2010  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
23 #include "UNV2417_Structure.hxx"
24 #include "UNV_Utilities.hxx"
25
26 #include <fstream>      
27 #include <iomanip>
28
29 using namespace std;
30 using namespace UNV;
31 using namespace UNV2417;
32
33 #ifdef _DEBUG_
34 static int MYDEBUG = 0;
35 #else
36 static int MYDEBUG = 0;
37 #endif
38
39
40 static string _group_labels[] = {"2417", "2429", "2430", "2432",
41                                  "2435", "2452", "2467", "2477"};
42 #define NBGROUP 8
43
44 static string _label_dataset = "2467";
45
46 void UNV2417::Read(std::ifstream& in_stream, TDataSet& theDataSet)
47 {
48   if(!in_stream.good())
49     EXCEPTION(runtime_error,"ERROR: Input file not good.");
50
51   std::string olds, news;
52   
53   while(true){
54     in_stream >> olds >> news;
55     /*
56      * a "-1" followed by a number means the beginning of a dataset
57      * stop combing at the end of the file
58      */
59     while( ((olds != "-1") || (news == "-1") ) && !in_stream.eof() ){     
60       olds = news;
61       in_stream >> news;
62     }
63     if(in_stream.eof())
64       return;
65     for (int i = 0; i < NBGROUP; i++) {
66       if (news == _group_labels[i]) {
67         ReadGroup(news, in_stream, theDataSet);
68       }
69     }
70   }
71 }
72
73
74
75 void UNV2417::ReadGroup(const std::string& myGroupLabel, std::ifstream& in_stream, TDataSet& theDataSet)
76 {
77   TGroupId aId;
78   for(; !in_stream.eof();){
79     in_stream >> aId ;
80     if(aId == -1){
81       // end of dataset is reached
82       break;
83     }
84
85     int n_nodes;
86     TRecord aRec;
87     int aTmp;
88     in_stream>>aTmp; // miss not necessary values
89     in_stream>>aTmp;
90     in_stream>>aTmp;
91     in_stream>>aTmp;
92     in_stream>>aTmp;
93     in_stream>>aTmp;
94     in_stream>>n_nodes;
95
96     std::getline(in_stream, aRec.GroupName, '\n'); // Finalise previous reading
97     std::getline(in_stream, aRec.GroupName, '\n');
98
99     int aElType;
100     int aElId;
101     int aNum;
102     for(int j=0; j < n_nodes; j++){
103       in_stream>>aElType;
104       in_stream>>aElId;
105       if ((myGroupLabel.compare("2435") == 0) ||
106           (myGroupLabel.compare("2452") == 0) ||
107           (myGroupLabel.compare("2467") == 0) ||
108           (myGroupLabel.compare("2477") == 0)) {
109         in_stream>>aTmp;
110         in_stream>>aTmp;
111       }
112       switch (aElType) {
113       case 7: // Nodes
114         aNum = aRec.NodeList.size();
115         aRec.NodeList.resize(aNum + 1);
116         aRec.NodeList[aNum] = aElId;
117         break;
118       case 8: // Elements
119         aNum = aRec.ElementList.size();
120         aRec.ElementList.resize(aNum + 1);
121         aRec.ElementList[aNum] = aElId;
122         break;
123       }
124     }
125     theDataSet.insert(TDataSet::value_type(aId,aRec));
126   }
127
128 }
129
130
131 void UNV2417::Write(std::ofstream& out_stream, const TDataSet& theDataSet)
132 {
133   if(!out_stream.good())
134     EXCEPTION(runtime_error,"ERROR: Output file not good.");
135   
136   /*
137    * Write beginning of dataset
138    */
139   out_stream<<"    -1\n";
140   out_stream<<"  "<<_label_dataset<<"\n";
141
142   TDataSet::const_iterator anIter = theDataSet.begin();
143   for(; anIter != theDataSet.end(); anIter++){
144     const TGroupId& aLabel = anIter->first;
145     const TRecord& aRec = anIter->second;
146     int aNbNodes = aRec.NodeList.size();
147     int aNbElements = aRec.ElementList.size();
148     int aNbRecords = aNbNodes + aNbElements;
149
150     out_stream<<std::setw(10)<<aLabel;  /* group ID */
151     out_stream<<std::setw(10)<<0;  
152     out_stream<<std::setw(10)<<0;
153     out_stream<<std::setw(10)<<0;
154     out_stream<<std::setw(10)<<0;
155     out_stream<<std::setw(10)<<0;
156     out_stream<<std::setw(10)<<0;
157     out_stream<<std::setw(10)<<aNbRecords<<std::endl; 
158
159     out_stream<<aRec.GroupName<<std::endl;
160     int aRow = 0;
161     int i;
162     for (i = 0; i < aNbNodes; i++) {
163       if (aRow == 2) {
164         out_stream<<std::endl; 
165         aRow = 0;
166       }
167       out_stream<<std::setw(10)<<7;
168       out_stream<<std::setw(10)<<aRec.NodeList[i];
169       out_stream<<std::setw(10)<<0;
170       out_stream<<std::setw(10)<<0;
171       aRow++;
172     }
173     for (i = 0; i < aNbElements; i++) {
174       if (aRow == 2) {
175         out_stream<<std::endl; 
176         aRow = 0;
177       }
178       out_stream<<std::setw(10)<<8;
179       out_stream<<std::setw(10)<<aRec.ElementList[i];
180       out_stream<<std::setw(10)<<0;
181       out_stream<<std::setw(10)<<0;
182       aRow++;
183     }
184     out_stream<<std::endl; 
185   }
186
187   /*
188    * Write end of dataset
189    */
190   out_stream<<"    -1\n";
191 }