Salome HOME
IPAL 19788 4.x: REGRESSION - groups are lost after import UNV file.
[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     int aElType;
97     int aElId;
98     int aNum;
99     for(int j=0; j < n_nodes; j++){
100       in_stream>>aElType;
101       in_stream>>aElId;
102       if ((myGroupLabel.compare("2435") == 0) ||
103           (myGroupLabel.compare("2452") == 0) ||
104           (myGroupLabel.compare("2467") == 0) ||
105           (myGroupLabel.compare("2477") == 0)) {
106         in_stream>>aTmp;
107         in_stream>>aTmp;
108       }
109       switch (aElType) {
110       case 7: // Nodes
111         aNum = aRec.NodeList.size();
112         aRec.NodeList.resize(aNum + 1);
113         aRec.NodeList[aNum] = aElId;
114         break;
115       case 8: // Elements
116         aNum = aRec.ElementList.size();
117         aRec.ElementList.resize(aNum + 1);
118         aRec.ElementList[aNum] = aElId;
119         break;
120       }
121     }
122     theDataSet.insert(TDataSet::value_type(aId,aRec));
123   }
124
125 }
126
127
128 void UNV2417::Write(std::ofstream& out_stream, const TDataSet& theDataSet)
129 {
130   if(!out_stream.good())
131     EXCEPTION(runtime_error,"ERROR: Output file not good.");
132   
133   /*
134    * Write beginning of dataset
135    */
136   out_stream<<"    -1\n";
137   out_stream<<"  "<<_label_dataset<<"\n";
138
139   TDataSet::const_iterator anIter = theDataSet.begin();
140   for(; anIter != theDataSet.end(); anIter++){
141     const TGroupId& aLabel = anIter->first;
142     const TRecord& aRec = anIter->second;
143     int aNbNodes = aRec.NodeList.size();
144     int aNbElements = aRec.ElementList.size();
145     int aNbRecords = aNbNodes + aNbElements;
146
147     out_stream<<std::setw(10)<<aLabel;  /* group ID */
148     out_stream<<std::setw(10)<<0;  
149     out_stream<<std::setw(10)<<0;
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)<<aNbRecords<<std::endl; 
155
156     out_stream<<aRec.GroupName<<std::endl;
157     int aRow = 0;
158     int i;
159     for (i = 0; i < aNbNodes; i++) {
160       if (aRow == 2) {
161         out_stream<<std::endl; 
162         aRow = 0;
163       }
164       out_stream<<std::setw(10)<<7;
165       out_stream<<std::setw(10)<<aRec.NodeList[i];
166       out_stream<<std::setw(10)<<0;
167       out_stream<<std::setw(10)<<0;
168       aRow++;
169     }
170     for (i = 0; i < aNbElements; i++) {
171       if (aRow == 2) {
172         out_stream<<std::endl; 
173         aRow = 0;
174       }
175       out_stream<<std::setw(10)<<8;
176       out_stream<<std::setw(10)<<aRec.ElementList[i];
177       out_stream<<std::setw(10)<<0;
178       out_stream<<std::setw(10)<<0;
179       aRow++;
180     }
181     out_stream<<std::endl; 
182   }
183
184   /*
185    * Write end of dataset
186    */
187   out_stream<<"    -1\n";
188 }