Salome HOME
Merge from V5_1_3_BR branch (07/12/09)
[modules/smesh.git] / src / DriverUNV / DriverUNV_R_SMDS_Mesh.cxx
1 //  Copyright (C) 2007-2008  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.
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 #include "DriverUNV_R_SMDS_Mesh.h"
23 #include "SMDS_Mesh.hxx"
24 #include "SMDS_MeshGroup.hxx"
25
26 #include "utilities.h"
27
28 #include "UNV2411_Structure.hxx"
29 #include "UNV2412_Structure.hxx"
30 #include "UNV2417_Structure.hxx"
31 #include "UNV_Utilities.hxx"
32
33 #include <Basics_Utils.hxx>
34
35 using namespace std;
36
37
38 #ifdef _DEBUG_
39 static int MYDEBUG = 0;
40 #else
41 static int MYDEBUG = 0;
42 #endif
43
44
45 DriverUNV_R_SMDS_Mesh::~DriverUNV_R_SMDS_Mesh()
46 {
47   if (myGroup != 0) 
48     delete myGroup;
49 }
50
51
52 Driver_Mesh::Status DriverUNV_R_SMDS_Mesh::Perform()
53 {
54   Kernel_Utils::Localizer loc;
55   Status aResult = DRS_OK;
56   std::ifstream in_stream(myFile.c_str());
57   try{
58     {
59       using namespace UNV2411;
60       TDataSet aDataSet2411;
61       UNV2411::Read(in_stream,aDataSet2411);
62       if(MYDEBUG) MESSAGE("Perform - aDataSet2411.size() = "<<aDataSet2411.size());
63       TDataSet::const_iterator anIter = aDataSet2411.begin();
64       for(; anIter != aDataSet2411.end(); anIter++){
65         const TNodeLab& aLabel = anIter->first;
66         const TRecord& aRec = anIter->second;
67         myMesh->AddNodeWithID(aRec.coord[0],aRec.coord[1],aRec.coord[2],aLabel);
68       }
69     }
70     {
71       using namespace UNV2412;
72       in_stream.seekg(0);
73       TDataSet aDataSet2412;
74       UNV2412::Read(in_stream,aDataSet2412);
75       TDataSet::const_iterator anIter = aDataSet2412.begin();
76       if(MYDEBUG) MESSAGE("Perform - aDataSet2412.size() = "<<aDataSet2412.size());
77       for(; anIter != aDataSet2412.end(); anIter++){
78         SMDS_MeshElement* anElement = NULL;
79         const TElementLab& aLabel = anIter->first;
80         const TRecord& aRec = anIter->second;
81         if(IsBeam(aRec.fe_descriptor_id)) {
82           switch ( aRec.node_labels.size() ) {
83           case 2: // edge with two nodes
84             anElement = myMesh->AddEdgeWithID(aRec.node_labels[0],
85                                               aRec.node_labels[1],
86                                               aLabel);
87             break;
88           case 3: // quadratic edge (with 3 nodes)
89             anElement = myMesh->AddEdgeWithID(aRec.node_labels[0],
90                                               aRec.node_labels[2],
91                                               aRec.node_labels[1],
92                                               aLabel);
93           }
94         }
95         else if(IsFace(aRec.fe_descriptor_id)) {
96           switch(aRec.fe_descriptor_id){
97           case 71: // TRI3
98           case 72:
99           case 74:
100             
101           case 41: // Plane Stress Linear Triangle - TRI3
102           case 91: // Thin Shell Linear Triangle - TRI3
103             anElement = myMesh->AddFaceWithID(aRec.node_labels[0],
104                                               aRec.node_labels[1],
105                                               aRec.node_labels[2],
106                                               aLabel);
107             break;
108             
109           case 42: // Plane Stress Quadratic Triangle - TRI6
110           case 92: // Thin Shell Quadratic Triangle - TRI6
111             anElement = myMesh->AddFaceWithID(aRec.node_labels[0],
112                                               aRec.node_labels[2],
113                                               aRec.node_labels[4],
114                                               aRec.node_labels[1],
115                                               aRec.node_labels[3],
116                                               aRec.node_labels[5],
117                                               aLabel);
118             break;
119             
120           case 44: // Plane Stress Linear Quadrilateral - QUAD4
121           case 94: // Thin Shell   Linear Quadrilateral -  QUAD4
122             anElement = myMesh->AddFaceWithID(aRec.node_labels[0],
123                                               aRec.node_labels[1],
124                                               aRec.node_labels[2],
125                                               aRec.node_labels[3],
126                                               aLabel);
127             break;
128             
129           case 45: // Plane Stress Quadratic Quadrilateral - QUAD8
130           case 95: // Thin Shell   Quadratic Quadrilateral - QUAD8
131             anElement = myMesh->AddFaceWithID(aRec.node_labels[0],
132                                               aRec.node_labels[2],
133                                               aRec.node_labels[4],
134                                               aRec.node_labels[6],
135                                               aRec.node_labels[1],
136                                               aRec.node_labels[3],
137                                               aRec.node_labels[5],
138                                               aRec.node_labels[7],
139                                               aLabel);
140             break;
141           }
142         }
143         else if(IsVolume(aRec.fe_descriptor_id)){
144           switch(aRec.fe_descriptor_id){
145             
146           case 111: // Solid Linear Tetrahedron - TET4
147             anElement = myMesh->AddVolumeWithID(aRec.node_labels[0],
148                                                 aRec.node_labels[2],
149                                                 aRec.node_labels[1],
150                                                 aRec.node_labels[3],
151                                                 aLabel);
152             break;
153
154           case 118: // Solid Quadratic Tetrahedron - TET10
155             anElement = myMesh->AddVolumeWithID(aRec.node_labels[0],
156                                                 aRec.node_labels[4],
157                                                 aRec.node_labels[2],
158
159                                                 aRec.node_labels[9],
160
161                                                 aRec.node_labels[5],
162                                                 aRec.node_labels[3],
163                                                 aRec.node_labels[1],
164
165                                                 aRec.node_labels[6],
166                                                 aRec.node_labels[8],
167                                                 aRec.node_labels[7],
168                                                 aLabel);
169             break;
170             
171           case 112: // Solid Linear Prism - PRISM6
172             anElement = myMesh->AddVolumeWithID(aRec.node_labels[0],
173                                                 aRec.node_labels[2],
174                                                 aRec.node_labels[1],
175                                                 aRec.node_labels[3],
176                                                 aRec.node_labels[5],
177                                                 aRec.node_labels[4],
178                                                 aLabel);
179             break;
180             
181           case 113: // Solid Quadratic Prism - PRISM15
182             anElement = myMesh->AddVolumeWithID(aRec.node_labels[0],
183                                                 aRec.node_labels[4],
184                                                 aRec.node_labels[2],
185
186                                                 aRec.node_labels[9],
187                                                 aRec.node_labels[13],
188                                                 aRec.node_labels[11],
189
190                                                 aRec.node_labels[5],
191                                                 aRec.node_labels[3],
192                                                 aRec.node_labels[1],
193
194                                                 aRec.node_labels[14],
195                                                 aRec.node_labels[12],
196                                                 aRec.node_labels[10],
197
198                                                 aRec.node_labels[6],
199                                                 aRec.node_labels[8],
200                                                 aRec.node_labels[7],
201                                                 aLabel);
202             break;
203             
204           case 115: // Solid Linear Brick - HEX8
205             anElement = myMesh->AddVolumeWithID(aRec.node_labels[0],
206                                                 aRec.node_labels[3],
207                                                 aRec.node_labels[2],
208                                                 aRec.node_labels[1],
209                                                 aRec.node_labels[4],
210                                                 aRec.node_labels[7],
211                                                 aRec.node_labels[6],
212                                                 aRec.node_labels[5],
213                                                 aLabel);
214             break;
215
216           case 116: // Solid Quadratic Brick - HEX20
217             anElement = myMesh->AddVolumeWithID(aRec.node_labels[0],
218                                                 aRec.node_labels[6],
219                                                 aRec.node_labels[4],
220                                                 aRec.node_labels[2],
221
222                                                 aRec.node_labels[12],
223                                                 aRec.node_labels[18],
224                                                 aRec.node_labels[16],
225                                                 aRec.node_labels[14],
226
227                                                 aRec.node_labels[7],
228                                                 aRec.node_labels[5],
229                                                 aRec.node_labels[3],
230                                                 aRec.node_labels[1],
231
232                                                 aRec.node_labels[19],
233                                                 aRec.node_labels[17],
234                                                 aRec.node_labels[15],
235                                                 aRec.node_labels[13],
236
237                                                 aRec.node_labels[8],
238                                                 aRec.node_labels[11],
239                                                 aRec.node_labels[10],
240                                                 aRec.node_labels[9],
241                                                 aLabel);
242             break;
243
244           case 114: // pyramid of 13 nodes (quadratic) - PIRA13
245             anElement = myMesh->AddVolumeWithID(aRec.node_labels[0],
246                                                 aRec.node_labels[6],
247                                                 aRec.node_labels[4],
248                                                 aRec.node_labels[2],
249                                                 aRec.node_labels[7],
250                                                 aRec.node_labels[5],
251                                                 aRec.node_labels[3],
252                                                 aRec.node_labels[1],
253
254                                                 aRec.node_labels[8],
255                                                 aRec.node_labels[11],
256                                                 aRec.node_labels[10],
257                                                 aRec.node_labels[9],
258                                                 aRec.node_labels[12],
259                                                 aLabel);
260             break;
261
262           }
263         }
264         //      if(!anElement)
265         //        MESSAGE("DriverUNV_R_SMDS_Mesh::Perform - can not add element with ID = "<<aLabel<<" and type = "<<aRec.fe_descriptor_id);
266       }
267     }
268     {
269       using namespace UNV2417;      
270       in_stream.seekg(0);
271       TDataSet aDataSet2417;
272       UNV2417::Read(in_stream,aDataSet2417);
273       if(MYDEBUG) MESSAGE("Perform - aDataSet2417.size() = "<<aDataSet2417.size());
274       if  (aDataSet2417.size() > 0) {
275         myGroup = new SMDS_MeshGroup(myMesh);
276         TDataSet::const_iterator anIter = aDataSet2417.begin();
277         for(; anIter != aDataSet2417.end(); anIter++){
278           const TGroupId& aLabel = anIter->first;
279           const TRecord& aRec = anIter->second;
280
281           int aNodesNb = aRec.NodeList.size();
282           int aElementsNb = aRec.ElementList.size();
283
284           bool useSuffix = ((aNodesNb > 0) && (aElementsNb > 0));
285           int i;
286           if (aNodesNb > 0) {
287             SMDS_MeshGroup* aNodesGroup = (SMDS_MeshGroup*) myGroup->AddSubGroup(SMDSAbs_Node);
288             std::string aGrName = (useSuffix) ? aRec.GroupName + "_Nodes" : aRec.GroupName;
289             int i = aGrName.find( "\r" );
290             if (i > 0)
291               aGrName.erase (i, 2);
292             myGroupNames.insert(TGroupNamesMap::value_type(aNodesGroup, aGrName));
293             myGroupId.insert(TGroupIdMap::value_type(aNodesGroup, aLabel));
294
295             for (i = 0; i < aNodesNb; i++) {
296               const SMDS_MeshNode* aNode = myMesh->FindNode(aRec.NodeList[i]);
297               if (aNode)
298                 aNodesGroup->Add(aNode);
299             }
300           }
301           if (aElementsNb > 0){
302             SMDS_MeshGroup* aEdgesGroup = 0;
303             SMDS_MeshGroup* aFacesGroup = 0;
304             SMDS_MeshGroup* aVolumeGroup = 0;
305             bool createdGroup = false;
306
307             for (i = 0; i < aElementsNb; i++) {
308               const SMDS_MeshElement* aElement = myMesh->FindElement(aRec.ElementList[i]);
309               if (aElement) {
310                 switch (aElement->GetType()) {
311                 case SMDSAbs_Edge:
312                   if (!aEdgesGroup) {
313                     aEdgesGroup = (SMDS_MeshGroup*) myGroup->AddSubGroup(SMDSAbs_Edge);
314                     if (!useSuffix && createdGroup) useSuffix = true;
315                     std::string aEdgesGrName = (useSuffix) ? aRec.GroupName + "_Edges" : aRec.GroupName;
316                     int i = aEdgesGrName.find( "\r" );
317                     if (i > 0)
318                       aEdgesGrName.erase (i, 2);
319                     myGroupNames.insert(TGroupNamesMap::value_type(aEdgesGroup, aEdgesGrName));
320                     myGroupId.insert(TGroupIdMap::value_type(aEdgesGroup, aLabel));
321                     createdGroup = true;
322                   }
323                   aEdgesGroup->Add(aElement);
324                   break;
325                 case SMDSAbs_Face:
326                   if (!aFacesGroup) {
327                     aFacesGroup = (SMDS_MeshGroup*) myGroup->AddSubGroup(SMDSAbs_Face);
328                     if (!useSuffix && createdGroup) useSuffix = true;
329                     std::string aFacesGrName = (useSuffix) ? aRec.GroupName + "_Faces" : aRec.GroupName;
330                     int i = aFacesGrName.find( "\r" );
331                     if (i > 0)
332                       aFacesGrName.erase (i, 2);
333                     myGroupNames.insert(TGroupNamesMap::value_type(aFacesGroup, aFacesGrName));
334                     myGroupId.insert(TGroupIdMap::value_type(aFacesGroup, aLabel));
335                     createdGroup = true;
336                   }
337                   aFacesGroup->Add(aElement);
338                   break;
339                 case SMDSAbs_Volume:
340                   if (!aVolumeGroup) {
341                     aVolumeGroup = (SMDS_MeshGroup*) myGroup->AddSubGroup(SMDSAbs_Volume);
342                     if (!useSuffix && createdGroup) useSuffix = true;
343                     std::string aVolumeGrName = (useSuffix) ? aRec.GroupName + "_Volumes" : aRec.GroupName;
344                     int i = aVolumeGrName.find( "\r" );
345                     if (i > 0)
346                       aVolumeGrName.erase (i, 2);
347                     myGroupNames.insert(TGroupNamesMap::value_type(aVolumeGroup, aVolumeGrName));
348                     myGroupId.insert(TGroupIdMap::value_type(aVolumeGroup, aLabel));
349                     createdGroup = true;
350                   }
351                   aVolumeGroup->Add(aElement);
352                   break;
353                 }
354               } 
355             }
356           }
357         }
358       }
359     } 
360   }
361   catch(const std::exception& exc){
362     INFOS("Follow exception was cought:\n\t"<<exc.what());
363   }
364   catch(...){
365     INFOS("Unknown exception was cought !!!");
366   }
367   return aResult;
368 }