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