Salome HOME
6856037fae4ef3ecfa6024b9ff7c40fcb2e4f6ec
[modules/smesh.git] / src / DriverUNV / DriverUNV_R_SMDS_Mesh.cxx
1 // Copyright (C) 2007-2013  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 "UNV164_Structure.hxx"
30 #include "UNV2411_Structure.hxx"
31 #include "UNV2412_Structure.hxx"
32 #include "UNV2417_Structure.hxx"
33 #include "UNV2420_Structure.hxx"
34 #include "UNV_Utilities.hxx"
35
36 #include <Basics_Utils.hxx>
37
38 using namespace std;
39
40
41 #ifdef _DEBUG_
42 static int MYDEBUG = 1;
43 #else
44 static int MYDEBUG = 0;
45 #endif
46
47 namespace
48 {
49   /*!
50    * \brief Move node coordinates to the global Cartesian CS
51    */
52   void transformNodes( UNV2411::TDataSet::const_iterator fromNode,
53                        UNV2411::TDataSet::const_iterator endNode,
54                        const UNV2420::TRecord &          csRecord )
55   {
56     const int csLabel = fromNode->exp_coord_sys_num;
57
58     UNV2411::TDataSet::const_iterator nodeIt;
59
60     // apply Transformation Matrix
61     if ( !csRecord.isIdentityMatrix() )
62     {
63       for ( nodeIt = fromNode; nodeIt != endNode; ++nodeIt )
64       {
65         const UNV2411::TRecord& nodeRec = *nodeIt;
66         if ( nodeRec.exp_coord_sys_num == csLabel )
67           csRecord.ApplyMatrix( (double*) nodeRec.coord );
68       }
69     }
70
71     // transform from Cylindrical CS
72     if ( csRecord.coord_sys_type == UNV2420::Cylindrical )
73     {
74       for ( nodeIt = fromNode; nodeIt != endNode; ++nodeIt )
75       {
76         const UNV2411::TRecord& nodeRec = *nodeIt;
77         if ( nodeRec.exp_coord_sys_num == csLabel )
78           csRecord.FromCylindricalCS( (double*) nodeRec.coord );
79       }
80     }
81     // transform from Spherical CS
82     else if ( csRecord.coord_sys_type == UNV2420::Spherical )
83     {
84       for ( nodeIt = fromNode; nodeIt != endNode; ++nodeIt )
85       {
86         const UNV2411::TRecord& nodeRec = *nodeIt;
87         if ( nodeRec.exp_coord_sys_num == csLabel )
88           csRecord.FromSphericalCS( (double*) nodeRec.coord );
89       }
90     }
91   }
92 }
93
94 DriverUNV_R_SMDS_Mesh::~DriverUNV_R_SMDS_Mesh()
95 {
96   if (myGroup != 0) 
97     delete myGroup;
98 }
99
100
101 Driver_Mesh::Status DriverUNV_R_SMDS_Mesh::Perform()
102 {
103   Kernel_Utils::Localizer loc;
104   Status aResult = DRS_OK;
105   std::ifstream in_stream(myFile.c_str());
106   try
107   {
108     {
109       // Read Units
110       UNV164::TRecord aUnitsRecord;
111       UNV164::Read( in_stream, aUnitsRecord );
112
113       // Read Coordinate systems
114       UNV2420::TDataSet aCoordSysDataSet;
115       UNV2420::Read(in_stream, myMeshName, aCoordSysDataSet);
116
117       // Read nodes
118       using namespace UNV2411;
119       TDataSet aDataSet2411;
120       UNV2411::Read(in_stream,aDataSet2411);
121       if(MYDEBUG) MESSAGE("Perform - aDataSet2411.size() = "<<aDataSet2411.size());
122
123       // Move nodes in a global CS
124       if ( !aCoordSysDataSet.empty() )
125       {
126         UNV2420::TDataSet::const_iterator csIter = aCoordSysDataSet.begin();
127         for ( ; csIter != aCoordSysDataSet.end(); ++csIter )
128         {
129           // find any node in this CS
130           TDataSet::const_iterator nodeIter = aDataSet2411.begin();
131           for (; nodeIter != aDataSet2411.end(); nodeIter++)
132             if ( nodeIter->exp_coord_sys_num == csIter->coord_sys_label )
133             {
134               transformNodes( nodeIter, aDataSet2411.end(), *csIter );
135               break;
136             }
137         }
138       }
139       // Move nodes to SI unit system
140       const double lenFactor = aUnitsRecord.factors[ UNV164::LENGTH_FACTOR ]; 
141       if ( lenFactor != 1. )
142       {
143         TDataSet::iterator nodeIter = aDataSet2411.begin(), nodeEnd;
144         for ( nodeEnd = aDataSet2411.end(); nodeIter != nodeEnd; nodeIter++)
145         {
146           UNV2411::TRecord& nodeRec = *nodeIter;
147           nodeRec.coord[0] *= lenFactor;
148           nodeRec.coord[1] *= lenFactor;
149           nodeRec.coord[2] *= lenFactor;
150         }
151       }
152
153       // Create nodes in the mesh
154       TDataSet::const_iterator anIter = aDataSet2411.begin();
155       for(; anIter != aDataSet2411.end(); anIter++)
156       {
157         const TRecord& aRec = *anIter;
158         myMesh->AddNodeWithID(aRec.coord[0],aRec.coord[1],aRec.coord[2],aRec.label);
159       }
160     }
161     {
162       using namespace UNV2412;
163       TDataSet aDataSet2412;
164       UNV2412::Read(in_stream,aDataSet2412);
165       TDataSet::const_iterator anIter = aDataSet2412.begin();
166       if(MYDEBUG) MESSAGE("Perform - aDataSet2412.size() = "<<aDataSet2412.size());
167       for(; anIter != aDataSet2412.end(); anIter++)
168       {
169         SMDS_MeshElement* anElement = NULL;
170         const TRecord& aRec = *anIter;
171         if(IsBeam(aRec.fe_descriptor_id)) {
172           switch ( aRec.node_labels.size() ) {
173           case 2: // edge with two nodes
174             //MESSAGE("add edge " << aLabel << " " << aRec.node_labels[0] << " " << aRec.node_labels[1]);
175             anElement = myMesh->AddEdgeWithID(aRec.node_labels[0],
176                                               aRec.node_labels[1],
177                                               aRec.label);
178             break;
179           case 3: // quadratic edge (with 3 nodes)
180             //MESSAGE("add edge " << aRec.label << " " << aRec.node_labels[0] << " " << aRec.node_labels[1] << " " << aRec.node_labels[2]);
181             anElement = myMesh->AddEdgeWithID(aRec.node_labels[0],
182                                               aRec.node_labels[2],
183                                               aRec.node_labels[1],
184                                               aRec.label);
185           }
186         }
187         else if(IsFace(aRec.fe_descriptor_id)) {
188           //MESSAGE("add face " << aRec.label);
189           switch(aRec.fe_descriptor_id){
190           case 41: // Plane Stress Linear Triangle      
191           case 51: // Plane Strain Linear Triangle      
192           case 61: // Plate Linear Triangle             
193           case 74: // Membrane Linear Triangle          
194           case 81: // Axisymetric Solid Linear Triangle 
195           case 91: // Thin Shell Linear Triangle        
196             anElement = myMesh->AddFaceWithID(aRec.node_labels[0],
197                                               aRec.node_labels[1],
198                                               aRec.node_labels[2],
199                                               aRec.label);
200             break;
201             
202           case 42: //  Plane Stress Parabolic Triangle       
203           case 52: //  Plane Strain Parabolic Triangle       
204           case 62: //  Plate Parabolic Triangle              
205           case 72: //  Membrane Parabolic Triangle           
206           case 82: //  Axisymetric Solid Parabolic Triangle  
207           case 92: //  Thin Shell Parabolic Triangle         
208             //MESSAGE("add face " << aRec.label << " " << aRec.node_labels[0] << " " << aRec.node_labels[1] << " " << aRec.node_labels[2] << " " << aRec.node_labels[3] << " " << aRec.node_labels[4] << " " << aRec.node_labels[5]);
209             anElement = myMesh->AddFaceWithID(aRec.node_labels[0],
210                                               aRec.node_labels[2],
211                                               aRec.node_labels[4],
212                                               aRec.node_labels[1],
213                                               aRec.node_labels[3],
214                                               aRec.node_labels[5],
215                                               aRec.label);
216             break;
217             
218           case 44: // Plane Stress Linear Quadrilateral     
219           case 54: // Plane Strain Linear Quadrilateral     
220           case 64: // Plate Linear Quadrilateral            
221           case 71: // Membrane Linear Quadrilateral         
222           case 84: // Axisymetric Solid Linear Quadrilateral
223           case 94: // Thin Shell Linear Quadrilateral       
224             anElement = myMesh->AddFaceWithID(aRec.node_labels[0],
225                                               aRec.node_labels[1],
226                                               aRec.node_labels[2],
227                                               aRec.node_labels[3],
228                                               aRec.label);
229             break;
230             
231           case 45: // Plane Stress Parabolic Quadrilateral      
232           case 55: // Plane Strain Parabolic Quadrilateral      
233           case 65: // Plate Parabolic Quadrilateral             
234           case 75: // Membrane Parabolic Quadrilateral          
235           case 85: // Axisymetric Solid Parabolic Quadrilateral 
236           case 95: // Thin Shell Parabolic Quadrilateral        
237             anElement = myMesh->AddFaceWithID(aRec.node_labels[0],
238                                               aRec.node_labels[2],
239                                               aRec.node_labels[4],
240                                               aRec.node_labels[6],
241                                               aRec.node_labels[1],
242                                               aRec.node_labels[3],
243                                               aRec.node_labels[5],
244                                               aRec.node_labels[7],
245                                               aRec.label);
246             break;
247           }
248         }
249         else if(IsVolume(aRec.fe_descriptor_id)){
250           //MESSAGE("add volume " << aRec.label);
251           switch(aRec.fe_descriptor_id){
252             
253           case 111: // Solid Linear Tetrahedron - TET4
254             anElement = myMesh->AddVolumeWithID(aRec.node_labels[0],
255                                                 aRec.node_labels[2],
256                                                 aRec.node_labels[1],
257                                                 aRec.node_labels[3],
258                                                 aRec.label);
259             break;
260
261           case 118: // Solid Quadratic Tetrahedron - TET10
262             anElement = myMesh->AddVolumeWithID(aRec.node_labels[0],
263                                                 aRec.node_labels[4],
264                                                 aRec.node_labels[2],
265
266                                                 aRec.node_labels[9],
267
268                                                 aRec.node_labels[5],
269                                                 aRec.node_labels[3],
270                                                 aRec.node_labels[1],
271
272                                                 aRec.node_labels[6],
273                                                 aRec.node_labels[8],
274                                                 aRec.node_labels[7],
275                                                 aRec.label);
276             break;
277             
278           case 112: // Solid Linear Prism - PRISM6
279             anElement = myMesh->AddVolumeWithID(aRec.node_labels[0],
280                                                 aRec.node_labels[2],
281                                                 aRec.node_labels[1],
282                                                 aRec.node_labels[3],
283                                                 aRec.node_labels[5],
284                                                 aRec.node_labels[4],
285                                                 aRec.label);
286             break;
287             
288           case 113: // Solid Quadratic Prism - PRISM15
289             anElement = myMesh->AddVolumeWithID(aRec.node_labels[0],
290                                                 aRec.node_labels[4],
291                                                 aRec.node_labels[2],
292
293                                                 aRec.node_labels[9],
294                                                 aRec.node_labels[13],
295                                                 aRec.node_labels[11],
296
297                                                 aRec.node_labels[5],
298                                                 aRec.node_labels[3],
299                                                 aRec.node_labels[1],
300
301                                                 aRec.node_labels[14],
302                                                 aRec.node_labels[12],
303                                                 aRec.node_labels[10],
304
305                                                 aRec.node_labels[6],
306                                                 aRec.node_labels[8],
307                                                 aRec.node_labels[7],
308                                                 aRec.label);
309             break;
310             
311           case 115: // Solid Linear Brick - HEX8
312             anElement = myMesh->AddVolumeWithID(aRec.node_labels[0],
313                                                 aRec.node_labels[3],
314                                                 aRec.node_labels[2],
315                                                 aRec.node_labels[1],
316                                                 aRec.node_labels[4],
317                                                 aRec.node_labels[7],
318                                                 aRec.node_labels[6],
319                                                 aRec.node_labels[5],
320                                                 aRec.label);
321             break;
322
323           case 116: // Solid Quadratic Brick - HEX20
324             anElement = myMesh->AddVolumeWithID(aRec.node_labels[0],
325                                                 aRec.node_labels[6],
326                                                 aRec.node_labels[4],
327                                                 aRec.node_labels[2],
328
329                                                 aRec.node_labels[12],
330                                                 aRec.node_labels[18],
331                                                 aRec.node_labels[16],
332                                                 aRec.node_labels[14],
333
334                                                 aRec.node_labels[7],
335                                                 aRec.node_labels[5],
336                                                 aRec.node_labels[3],
337                                                 aRec.node_labels[1],
338
339                                                 aRec.node_labels[19],
340                                                 aRec.node_labels[17],
341                                                 aRec.node_labels[15],
342                                                 aRec.node_labels[13],
343
344                                                 aRec.node_labels[8],
345                                                 aRec.node_labels[11],
346                                                 aRec.node_labels[10],
347                                                 aRec.node_labels[9],
348                                                 aRec.label);
349             break;
350
351           case 114: // pyramid of 13 nodes (quadratic) - PIRA13
352             anElement = myMesh->AddVolumeWithID(aRec.node_labels[0],
353                                                 aRec.node_labels[6],
354                                                 aRec.node_labels[4],
355                                                 aRec.node_labels[2],
356                                                 aRec.node_labels[7],
357                                                 aRec.node_labels[5],
358                                                 aRec.node_labels[3],
359                                                 aRec.node_labels[1],
360
361                                                 aRec.node_labels[8],
362                                                 aRec.node_labels[11],
363                                                 aRec.node_labels[10],
364                                                 aRec.node_labels[9],
365                                                 aRec.node_labels[12],
366                                                 aRec.label);
367             break;
368
369           }
370         }
371         if(!anElement)
372           MESSAGE("DriverUNV_R_SMDS_Mesh::Perform - can not add element with ID = "<<aRec.label<<" and type = "<<aRec.fe_descriptor_id);
373       }
374     }
375     {
376       using namespace UNV2417;      
377       TDataSet aDataSet2417;
378       UNV2417::Read(in_stream,aDataSet2417);
379       if(MYDEBUG) MESSAGE("Perform - aDataSet2417.size() = "<<aDataSet2417.size());
380       if  (aDataSet2417.size() > 0) {
381         myGroup = new SMDS_MeshGroup(myMesh);
382         TDataSet::const_iterator anIter = aDataSet2417.begin();
383         for(; anIter != aDataSet2417.end(); anIter++){
384           const TGroupId& aLabel = anIter->first;
385           const TRecord& aRec = anIter->second;
386
387           int aNodesNb = aRec.NodeList.size();
388           int aElementsNb = aRec.ElementList.size();
389
390           bool useSuffix = ((aNodesNb > 0) && (aElementsNb > 0));
391           int i;
392           if (aNodesNb > 0) {
393             SMDS_MeshGroup* aNodesGroup = (SMDS_MeshGroup*) myGroup->AddSubGroup(SMDSAbs_Node);
394             std::string aGrName = (useSuffix) ? aRec.GroupName + "_Nodes" : aRec.GroupName;
395             int i = aGrName.find( "\r" );
396             if (i > 0)
397               aGrName.erase (i, 2);
398             myGroupNames.insert(TGroupNamesMap::value_type(aNodesGroup, aGrName));
399             myGroupId.insert(TGroupIdMap::value_type(aNodesGroup, aLabel));
400
401             for (i = 0; i < aNodesNb; i++) {
402               const SMDS_MeshNode* aNode = myMesh->FindNode(aRec.NodeList[i]);
403               if (aNode)
404                 aNodesGroup->Add(aNode);
405             }
406           }
407           if (aElementsNb > 0){
408             SMDS_MeshGroup* aEdgesGroup = 0;
409             SMDS_MeshGroup* aFacesGroup = 0;
410             SMDS_MeshGroup* aVolumeGroup = 0;
411             bool createdGroup = false;
412
413             for (i = 0; i < aElementsNb; i++) {
414               const SMDS_MeshElement* aElement = myMesh->FindElement(aRec.ElementList[i]);
415               if (aElement) {
416                 switch (aElement->GetType()) {
417                 case SMDSAbs_Edge:
418                   if (!aEdgesGroup) {
419                     aEdgesGroup = (SMDS_MeshGroup*) myGroup->AddSubGroup(SMDSAbs_Edge);
420                     if (!useSuffix && createdGroup) useSuffix = true;
421                     std::string aEdgesGrName = (useSuffix) ? aRec.GroupName + "_Edges" : aRec.GroupName;
422                     int i = aEdgesGrName.find( "\r" );
423                     if (i > 0)
424                       aEdgesGrName.erase (i, 2);
425                     myGroupNames.insert(TGroupNamesMap::value_type(aEdgesGroup, aEdgesGrName));
426                     myGroupId.insert(TGroupIdMap::value_type(aEdgesGroup, aLabel));
427                     createdGroup = true;
428                   }
429                   aEdgesGroup->Add(aElement);
430                   break;
431                 case SMDSAbs_Face:
432                   if (!aFacesGroup) {
433                     aFacesGroup = (SMDS_MeshGroup*) myGroup->AddSubGroup(SMDSAbs_Face);
434                     if (!useSuffix && createdGroup) useSuffix = true;
435                     std::string aFacesGrName = (useSuffix) ? aRec.GroupName + "_Faces" : aRec.GroupName;
436                     int i = aFacesGrName.find( "\r" );
437                     if (i > 0)
438                       aFacesGrName.erase (i, 2);
439                     myGroupNames.insert(TGroupNamesMap::value_type(aFacesGroup, aFacesGrName));
440                     myGroupId.insert(TGroupIdMap::value_type(aFacesGroup, aLabel));
441                     createdGroup = true;
442                   }
443                   aFacesGroup->Add(aElement);
444                   break;
445                 case SMDSAbs_Volume:
446                   if (!aVolumeGroup) {
447                     aVolumeGroup = (SMDS_MeshGroup*) myGroup->AddSubGroup(SMDSAbs_Volume);
448                     if (!useSuffix && createdGroup) useSuffix = true;
449                     std::string aVolumeGrName = (useSuffix) ? aRec.GroupName + "_Volumes" : aRec.GroupName;
450                     int i = aVolumeGrName.find( "\r" );
451                     if (i > 0)
452                       aVolumeGrName.erase (i, 2);
453                     myGroupNames.insert(TGroupNamesMap::value_type(aVolumeGroup, aVolumeGrName));
454                     myGroupId.insert(TGroupIdMap::value_type(aVolumeGroup, aLabel));
455                     createdGroup = true;
456                   }
457                   aVolumeGroup->Add(aElement);
458                   break;
459                 }
460               } 
461             }
462           }
463         }
464       }
465     } 
466   }
467   catch(const std::exception& exc){
468     INFOS("Follow exception was cought:\n\t"<<exc.what());
469   }
470   catch(...){
471     INFOS("Unknown exception was cought !!!");
472   }
473   if (myMesh)
474     myMesh->compactMesh();
475   return aResult;
476 }