Salome HOME
Increment version: 8.5.0
[modules/smesh.git] / src / DriverUNV / UNV2417_Structure.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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 static string _group_labels[] = {"2417", "2429", "2430", "2432",
34                                  "2435", "2452", "2467", "2477"};
35 #define NBGROUP 8
36
37 static string _label_dataset = "2467";
38
39 void UNV2417::Read(std::ifstream& in_stream, TDataSet& theDataSet)
40 {
41   if(!in_stream.good())
42     EXCEPTION(runtime_error,"ERROR: Input file not good.");
43
44   std::string olds, news;
45   
46   while(true){
47     in_stream >> olds >> news;
48     /*
49      * a "-1" followed by a number means the beginning of a dataset
50      * stop combing at the end of the file
51      */
52     while( ((olds != "-1") || (news == "-1") ) && !in_stream.eof() ){     
53       olds = news;
54       in_stream >> news;
55     }
56     if(in_stream.eof())
57       return;
58     for (int i = 0; i < NBGROUP; i++) {
59       if (news == _group_labels[i]) {
60         ReadGroup(news, in_stream, theDataSet);
61       }
62     }
63   }
64 }
65
66
67
68 void UNV2417::ReadGroup(const std::string& myGroupLabel, std::ifstream& in_stream, TDataSet& theDataSet)
69 {
70   TGroupId aId;
71   for(; !in_stream.eof();){
72     in_stream >> aId ;
73     if(aId == -1){
74       // end of dataset is reached
75       break;
76     }
77
78     int n_nodes;
79     TRecord aRec;
80     int aTmp;
81     in_stream>>aTmp; // miss not necessary values
82     in_stream>>aTmp;
83     in_stream>>aTmp;
84     in_stream>>aTmp;
85     in_stream>>aTmp;
86     in_stream>>aTmp;
87     in_stream>>n_nodes;
88
89     std::getline(in_stream, aRec.GroupName, '\n'); // Finalise previous reading
90     std::getline(in_stream, aRec.GroupName, '\n');
91
92     int aElType;
93     int aElId;
94     int aNum;
95     for(int j=0; j < n_nodes; j++){
96       in_stream>>aElType;
97       in_stream>>aElId;
98       if ((myGroupLabel.compare("2435") == 0) ||
99           (myGroupLabel.compare("2452") == 0) ||
100           (myGroupLabel.compare("2467") == 0) ||
101           (myGroupLabel.compare("2477") == 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 }