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