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