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