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