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