Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[modules/smesh.git] / src / DriverUNV / UNV2417_Structure.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
8 //
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 #include "UNV2417_Structure.hxx"
21 #include "UNV_Utilities.hxx"
22
23 #include <fstream>      
24 #include <iomanip>
25
26 using namespace std;
27 using namespace UNV;
28 using namespace UNV2417;
29
30 #ifdef _DEBUG_
31 static int MYDEBUG = 0;
32 #else
33 static int MYDEBUG = 0;
34 #endif
35
36
37 static string _group_labels[] = {"2417", "2429", "2430", "2432", "2435", "2452", "2467"};
38 #define NBGROUP 7
39
40 static string _label_dataset = "2467";
41
42 void UNV2417::Read(std::ifstream& in_stream, TDataSet& theDataSet)
43 {
44   if(!in_stream.good())
45     EXCEPTION(runtime_error,"ERROR: Input file not good.");
46
47   std::string olds, news;
48   
49   while(true){
50     in_stream >> olds >> news;
51     /*
52      * a "-1" followed by a number means the beginning of a dataset
53      * stop combing at the end of the file
54      */
55     while( ((olds != "-1") || (news == "-1") ) && !in_stream.eof() ){     
56       olds = news;
57       in_stream >> news;
58     }
59     if(in_stream.eof())
60       return;
61     for (int i = 0; i < NBGROUP; i++) {
62       if (news == _group_labels[i]) {
63         ReadGroup(news, in_stream, theDataSet);
64       }
65     }
66   }
67 }
68
69
70
71 void UNV2417::ReadGroup(const std::string& myGroupLabel, std::ifstream& in_stream, TDataSet& theDataSet)
72 {
73   TGroupId aId;
74   for(; !in_stream.eof();){
75     in_stream >> aId ;
76     if(aId == -1){
77       // end of dataset is reached
78       break;
79     }
80
81     int n_nodes;
82     TRecord aRec;
83     int aTmp;
84     in_stream>>aTmp; // miss not necessary values
85     in_stream>>aTmp;
86     in_stream>>aTmp;
87     in_stream>>aTmp;
88     in_stream>>aTmp;
89     in_stream>>aTmp;
90     in_stream>>n_nodes;
91
92     std::getline(in_stream, aRec.GroupName, '\n'); // Finalise previous reading
93     std::getline(in_stream, aRec.GroupName, '\n');
94     
95     int aElType;
96     int aElId;
97     int aNum;
98     for(int j=0; j < n_nodes; j++){
99       in_stream>>aElType;
100       in_stream>>aElId;
101       if ((myGroupLabel.compare("2435") == 0) || (myGroupLabel.compare("2452") == 0) || (myGroupLabel.compare("2467") == 0)) {
102         in_stream>>aTmp;
103         in_stream>>aTmp;
104       }
105       switch (aElType) {
106       case 7: // Nodes
107         aNum = aRec.NodeList.size();
108         aRec.NodeList.resize(aNum + 1);
109         aRec.NodeList[aNum] = aElId;
110         break;
111       case 8: // Elements
112         aNum = aRec.ElementList.size();
113         aRec.ElementList.resize(aNum + 1);
114         aRec.ElementList[aNum] = aElId;
115         break;
116       }
117     }
118     theDataSet.insert(TDataSet::value_type(aId,aRec));
119   }
120
121 }
122
123
124 void UNV2417::Write(std::ofstream& out_stream, const TDataSet& theDataSet)
125 {
126   if(!out_stream.good())
127     EXCEPTION(runtime_error,"ERROR: Output file not good.");
128   
129   /*
130    * Write beginning of dataset
131    */
132   out_stream<<"    -1\n";
133   out_stream<<"  "<<_label_dataset<<"\n";
134
135   TDataSet::const_iterator anIter = theDataSet.begin();
136   for(; anIter != theDataSet.end(); anIter++){
137     const TGroupId& aLabel = anIter->first;
138     const TRecord& aRec = anIter->second;
139     int aNbNodes = aRec.NodeList.size();
140     int aNbElements = aRec.ElementList.size();
141     int aNbRecords = aNbNodes + aNbElements;
142
143     out_stream<<std::setw(10)<<aLabel;  /* group ID */
144     out_stream<<std::setw(10)<<0;  
145     out_stream<<std::setw(10)<<0;
146     out_stream<<std::setw(10)<<0;
147     out_stream<<std::setw(10)<<0;
148     out_stream<<std::setw(10)<<0;
149     out_stream<<std::setw(10)<<0;
150     out_stream<<std::setw(10)<<aNbRecords<<std::endl; 
151
152     out_stream<<aRec.GroupName<<std::endl;
153     int aRow = 0;
154     int i;
155     for (i = 0; i < aNbNodes; i++) {
156       if (aRow == 2) {
157         out_stream<<std::endl; 
158         aRow = 0;
159       }
160       out_stream<<std::setw(10)<<7;
161       out_stream<<std::setw(10)<<aRec.NodeList[i];
162       out_stream<<std::setw(10)<<0;
163       out_stream<<std::setw(10)<<0;
164       aRow++;
165     }
166     for (i = 0; i < aNbElements; i++) {
167       if (aRow == 2) {
168         out_stream<<std::endl; 
169         aRow = 0;
170       }
171       out_stream<<std::setw(10)<<8;
172       out_stream<<std::setw(10)<<aRec.ElementList[i];
173       out_stream<<std::setw(10)<<0;
174       out_stream<<std::setw(10)<<0;
175       aRow++;
176     }
177     out_stream<<std::endl; 
178   }
179
180   /*
181    * Write end of dataset
182    */
183   out_stream<<"    -1\n";
184 }