Salome HOME
bos #20256: [CEA 18523] Porting SMESH to int 64 bits
[modules/smesh.git] / src / DriverUNV / DriverUNV_W_SMDS_Mesh.cxx
1 // Copyright (C) 2007-2021  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 <algorithm>
24
25 #include "DriverUNV_W_SMDS_Mesh.h"
26
27 #include "SMDS_Mesh.hxx"
28 #include "SMESHDS_GroupBase.hxx"
29
30 #include "utilities.h"
31
32 #include "UNV164_Structure.hxx"
33 #include "UNV2411_Structure.hxx"
34 #include "UNV2412_Structure.hxx"
35 #include "UNV2417_Structure.hxx"
36 #include "UNV2420_Structure.hxx"
37 #include "UNV_Utilities.hxx"
38
39 #include <Basics_Utils.hxx>
40
41 using namespace std;
42 using namespace UNV;
43
44 Driver_Mesh::Status DriverUNV_W_SMDS_Mesh::Perform()
45 {
46   Kernel_Utils::Localizer loc;
47   Status aResult = DRS_OK;
48
49   if ( Driver_Mesh::IsMeshTooLarge< int >( myMesh, /*checkIDs =*/ false))
50     return DRS_TOO_LARGE_MESH;
51
52 #if defined(WIN32) && defined(UNICODE)
53   std::wstring aFile = Kernel_Utils::utf8_decode_s(myFile);
54   std::ofstream out_stream(aFile.c_str());
55 #else
56   std::ofstream out_stream(myFile.c_str());
57 #endif
58   try{
59
60     UNV164::Write( out_stream ); // unit system
61     UNV2420::Write( out_stream, myMeshName ); // Coordinate system
62
63     std::vector< size_t > nodeLabelByID;
64     if ( myMesh->HasNumerationHoles() )
65       nodeLabelByID.resize( myMesh->MaxNodeID() + 1 );
66
67     {
68       using namespace UNV2411;
69       TDataSet aDataSet2411;
70       // -----------------------------------
71       // Storing SMDS nodes to the UNV file
72       // -----------------------------------
73       SMDS_NodeIteratorPtr aNodesIter = myMesh->nodesIterator();
74       TRecord aRec;
75       for ( aRec.label = 1; aNodesIter->more(); ++aRec.label )
76       {
77         const SMDS_MeshNode* aNode = aNodesIter->next();
78         // aRec.label    = aNode->GetID(); -- IPAL54452
79         if ( !nodeLabelByID.empty() )
80           nodeLabelByID[ aNode->GetID() ] = aRec.label;
81         aRec.coord[0] = aNode->X();
82         aRec.coord[1] = aNode->Y();
83         aRec.coord[2] = aNode->Z();
84         aDataSet2411.push_back( aRec );
85       }
86       UNV2411::Write(out_stream,aDataSet2411);
87     }
88
89     std::vector< size_t > elemLabelByID;
90     if ( !myGroups.empty() )
91       elemLabelByID.resize( myMesh->MaxElementID() + 1 );
92
93     {
94       using namespace UNV2412;
95       TDataSet aDataSet2412;
96       TRecord aRec;
97       aRec.label = 0;
98
99       // -------------------
100       // Storing SMDS Edges
101       // -------------------
102       if ( myMesh->NbEdges() )
103       {
104         SMDS_EdgeIteratorPtr anIter = myMesh->edgesIterator();
105         while ( anIter->more() )
106         {
107           const SMDS_MeshEdge* anElem = anIter->next();
108           // aRec.label = anElem->GetID();  -- IPAL54452
109           ++aRec.label;
110           if ( !elemLabelByID.empty() )
111             elemLabelByID[ anElem->GetID() ] = aRec.label;
112
113           aRec.fe_descriptor_id = anElem->IsQuadratic() ? 22 : 11;
114
115           SMDS_NodeIteratorPtr aNodesIter = anElem->nodesIteratorToUNV();
116           for ( aRec.node_labels.clear(); aNodesIter->more(); )
117           {
118             const SMDS_MeshNode* aNode = aNodesIter->next();
119             if ( nodeLabelByID.empty() )
120               aRec.node_labels.push_back( FromSmIdType<int>(aNode->GetID()) );
121             else
122               aRec.node_labels.push_back( nodeLabelByID[ aNode->GetID() ]);
123           }
124
125           aDataSet2412.push_back(aRec);
126         }
127       }
128
129       // -------------------
130       // Storing SMDS Faces
131       // -------------------
132       if ( myMesh->NbFaces() )
133       {
134         SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
135         while ( anIter->more() )
136         {
137           const SMDS_MeshFace* anElem = anIter->next();
138           if ( anElem->IsPoly() ) continue;
139
140           SMDS_NodeIteratorPtr aNodesIter = anElem->nodesIteratorToUNV();
141           for ( aRec.node_labels.clear(); aNodesIter->more();  ) {
142             const SMDS_MeshNode* aNode = aNodesIter->next();
143             if ( nodeLabelByID.empty() )
144               aRec.node_labels.push_back( FromSmIdType<int>(aNode->GetID()) );
145             else
146               aRec.node_labels.push_back( nodeLabelByID[ aNode->GetID() ]);
147           }
148           switch ( anElem->NbNodes() ) {
149           case 3: aRec.fe_descriptor_id = 41; break;
150           case 4: aRec.fe_descriptor_id = 44; break;
151           case 6: aRec.fe_descriptor_id = 42; break;
152           case 7: aRec.fe_descriptor_id = 42; break;
153           case 8: aRec.fe_descriptor_id = 45; break;
154           case 9: aRec.fe_descriptor_id = 45; aRec.node_labels.resize( 8 ); break;
155           default:
156             continue;
157           }
158           // aRec.label = anElem->GetID(); -- IPAL54452
159           ++aRec.label;
160           if ( !elemLabelByID.empty() )
161             elemLabelByID[ anElem->GetID() ] = aRec.label;
162
163           aDataSet2412.push_back(aRec);
164         }
165       }
166
167       // ---------------------
168       // Storing SMDS Volumes
169       // ---------------------
170       if ( myMesh->NbVolumes() )
171       {
172         SMDS_VolumeIteratorPtr anIter = myMesh->volumesIterator();
173         while ( anIter->more() )
174         {
175           const SMDS_MeshVolume* anElem = anIter->next();
176           if ( anElem->IsPoly() )
177             continue;
178           size_t aNbNodes = anElem->NbNodes();
179           switch( aNbNodes ) {
180           case 4:  aRec.fe_descriptor_id = 111; break;
181           case 6:  aRec.fe_descriptor_id = 112; break;
182           case 8:  aRec.fe_descriptor_id = 115; break;
183           case 10: aRec.fe_descriptor_id = 118; break;
184           case 13: aRec.fe_descriptor_id = 114; break;
185           case 15: aRec.fe_descriptor_id = 113; break;
186           case 20:
187           case 27: aRec.fe_descriptor_id = 116; aNbNodes = 20; break;
188           default:
189             continue;
190           }
191           // aRec.label = anElem->GetID(); -- IPAL54452
192           ++aRec.label;
193           if ( !elemLabelByID.empty() )
194             elemLabelByID[  anElem->GetID() ] = aRec.label;
195
196           aRec.node_labels.clear();
197           SMDS_NodeIteratorPtr aNodesIter = anElem->nodesIteratorToUNV();
198           while ( aNodesIter->more() && aRec.node_labels.size() < aNbNodes )
199           {
200             const SMDS_MeshElement* aNode = aNodesIter->next();
201             if ( nodeLabelByID.empty() )
202               aRec.node_labels.push_back( FromSmIdType<int>(aNode->GetID()) );
203             else
204               aRec.node_labels.push_back( nodeLabelByID[ aNode->GetID() ]);
205           }
206           aDataSet2412.push_back(aRec);
207         }
208       }
209       UNV2412::Write(out_stream,aDataSet2412);
210     }
211
212     // --------------------
213     // Storing SMDS Groups
214     // --------------------
215     {
216       using namespace UNV2417;
217       if ( myGroups.size() > 0 ) {
218         TRecord aRec;
219         TDataSet aDataSet2417;
220         TGroupList::const_iterator aIter = myGroups.begin();
221         for ( ; aIter != myGroups.end(); aIter++ )
222         {
223           SMESHDS_GroupBase* aGroupDS = *aIter;
224           aRec.GroupName = aGroupDS->GetStoreName();
225           aRec.NodeList.clear();
226           aRec.ElementList.clear();
227
228           SMDS_ElemIteratorPtr aIter = aGroupDS->GetElements();
229           if ( aGroupDS->GetType() == SMDSAbs_Node ) {
230             while ( aIter->more() ) {
231               const SMDS_MeshElement* aNode = aIter->next();
232               if ( nodeLabelByID.empty() )
233                 aRec.NodeList.push_back( FromSmIdType<int>(aNode->GetID()) );
234               else
235                 aRec.NodeList.push_back( nodeLabelByID[ aNode->GetID() ]);
236             }
237           }
238           else
239           {
240             while ( aIter->more() ) {
241               const SMDS_MeshElement* aElem = aIter->next();
242               if ( elemLabelByID.empty() )
243                 aRec.ElementList.push_back( FromSmIdType<int>(aElem->GetID()) );
244               else
245                 aRec.ElementList.push_back( elemLabelByID[ aElem->GetID() ]);
246             }
247           }
248           // 0019936: EDF 794 SMESH : Export UNV : Node color and group id
249           //aDataSet2417.insert(TDataSet::value_type(aGroupDS->GetID(), aRec));
250           aDataSet2417.insert(TDataSet::value_type(aGroupDS->GetID()+1, aRec));
251         }
252         UNV2417::Write(out_stream,aDataSet2417);
253         myGroups.clear();
254       }
255     }
256
257     out_stream.flush();
258     out_stream.close();
259     if (!check_file(myFile))
260       EXCEPTION(runtime_error,"ERROR: Output file not good.");
261   }
262   catch(const std::exception& exc){
263     INFOS("Follow exception was caught:\n\t"<<exc.what());
264     throw;
265   }
266   catch(...){
267     INFOS("Unknown exception was caught !!!");
268     throw;
269   }
270   return aResult;
271 }