Salome HOME
Merge branch 'V9_9_BR'
[modules/smesh.git] / src / DriverUNV / DriverUNV_W_SMDS_Mesh.cxx
1 // Copyright (C) 2007-2022  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() && myRenumber )
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         if ( !myRenumber )
79           aRec.label  = aNode->GetID(); //-- IPAL54452
80         if ( !nodeLabelByID.empty() )
81           nodeLabelByID[ aNode->GetID() ] = aRec.label;
82         aRec.coord[0] = aNode->X();
83         aRec.coord[1] = aNode->Y();
84         aRec.coord[2] = aNode->Z();
85         aDataSet2411.push_back( aRec );
86       }
87       UNV2411::Write(out_stream,aDataSet2411);
88     }
89
90     std::vector< size_t > elemLabelByID;
91     if ( !myGroups.empty() )
92       elemLabelByID.resize( myMesh->MaxElementID() + 1 );
93
94     {
95       using namespace UNV2412;
96       TDataSet aDataSet2412;
97       TRecord aRec;
98       aRec.label = 0;
99
100       // -------------------
101       // Storing SMDS Edges
102       // -------------------
103       if ( myMesh->NbEdges() )
104       {
105         SMDS_EdgeIteratorPtr anIter = myMesh->edgesIterator();
106         while ( anIter->more() )
107         {
108           const SMDS_MeshEdge* anElem = anIter->next();
109           if ( myRenumber ) // -- IPAL54452
110             ++aRec.label;
111           else
112             aRec.label = anElem->GetID();
113           if ( !elemLabelByID.empty() )
114             elemLabelByID[ anElem->GetID() ] = aRec.label;
115
116           aRec.fe_descriptor_id = anElem->IsQuadratic() ? 22 : 11;
117
118           SMDS_NodeIteratorPtr aNodesIter = anElem->nodesIteratorToUNV();
119           for ( aRec.node_labels.clear(); aNodesIter->more(); )
120           {
121             const SMDS_MeshNode* aNode = aNodesIter->next();
122             if ( nodeLabelByID.empty() )
123               aRec.node_labels.push_back( FromSmIdType<int>(aNode->GetID()) );
124             else
125               aRec.node_labels.push_back( nodeLabelByID[ aNode->GetID() ]);
126           }
127
128           aDataSet2412.push_back(aRec);
129         }
130       }
131
132       // -------------------
133       // Storing SMDS Faces
134       // -------------------
135       if ( myMesh->NbFaces() )
136       {
137         SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
138         while ( anIter->more() )
139         {
140           const SMDS_MeshFace* anElem = anIter->next();
141           if ( anElem->IsPoly() ) continue;
142
143           SMDS_NodeIteratorPtr aNodesIter = anElem->nodesIteratorToUNV();
144           for ( aRec.node_labels.clear(); aNodesIter->more();  )
145           {
146             const SMDS_MeshNode* aNode = aNodesIter->next();
147             if ( nodeLabelByID.empty() )
148               aRec.node_labels.push_back( FromSmIdType<int>(aNode->GetID()) );
149             else
150               aRec.node_labels.push_back( nodeLabelByID[ aNode->GetID() ]);
151           }
152           switch ( anElem->NbNodes() ) {
153           case 3: aRec.fe_descriptor_id = 41; break;
154           case 4: aRec.fe_descriptor_id = 44; break;
155           case 6: aRec.fe_descriptor_id = 42; break;
156           case 7: aRec.fe_descriptor_id = 42; break;
157           case 8: aRec.fe_descriptor_id = 45; break;
158           case 9: aRec.fe_descriptor_id = 45; aRec.node_labels.resize( 8 ); break;
159           default:
160             continue;
161           }
162           if ( myRenumber )
163             ++aRec.label;
164           else
165             aRec.label = anElem->GetID(); // -- IPAL54452
166           if ( !elemLabelByID.empty() )
167             elemLabelByID[ anElem->GetID() ] = aRec.label;
168
169           aDataSet2412.push_back(aRec);
170         }
171       }
172
173       // ---------------------
174       // Storing SMDS Volumes
175       // ---------------------
176       if ( myMesh->NbVolumes() )
177       {
178         SMDS_VolumeIteratorPtr anIter = myMesh->volumesIterator();
179         while ( anIter->more() )
180         {
181           const SMDS_MeshVolume* anElem = anIter->next();
182           if ( anElem->IsPoly() )
183             continue;
184           size_t aNbNodes = anElem->NbNodes();
185           switch( aNbNodes ) {
186           case 4:  aRec.fe_descriptor_id = 111; break;
187           case 6:  aRec.fe_descriptor_id = 112; break;
188           case 8:  aRec.fe_descriptor_id = 115; break;
189           case 10: aRec.fe_descriptor_id = 118; break;
190           case 13: aRec.fe_descriptor_id = 114; break;
191           case 15: aRec.fe_descriptor_id = 113; break;
192           case 20:
193           case 27: aRec.fe_descriptor_id = 116; aNbNodes = 20; break;
194           default:
195             continue;
196           }
197           if ( myRenumber )
198             ++aRec.label; // -- IPAL54452
199           else
200             aRec.label = anElem->GetID();
201           if ( !elemLabelByID.empty() )
202             elemLabelByID[  anElem->GetID() ] = aRec.label;
203
204           aRec.node_labels.clear();
205           SMDS_NodeIteratorPtr aNodesIter = anElem->nodesIteratorToUNV();
206           while ( aNodesIter->more() && aRec.node_labels.size() < aNbNodes )
207           {
208             const SMDS_MeshElement* aNode = aNodesIter->next();
209             if ( nodeLabelByID.empty() )
210               aRec.node_labels.push_back( FromSmIdType<int>(aNode->GetID()) );
211             else
212               aRec.node_labels.push_back( nodeLabelByID[ aNode->GetID() ]);
213           }
214           aDataSet2412.push_back(aRec);
215         }
216       }
217       UNV2412::Write(out_stream,aDataSet2412);
218     }
219
220     // --------------------
221     // Storing SMDS Groups
222     // --------------------
223     {
224       using namespace UNV2417;
225       if ( myGroups.size() > 0 )
226       {
227         TRecord aRec;
228         TDataSet aDataSet2417;
229         TGroupList::const_iterator aIter = myGroups.begin();
230         for ( ; aIter != myGroups.end(); aIter++ )
231         {
232           SMESHDS_GroupBase* aGroupDS = *aIter;
233           aRec.GroupName = aGroupDS->GetStoreName();
234           aRec.NodeList.clear();
235           aRec.ElementList.clear();
236
237           SMDS_ElemIteratorPtr aIter = aGroupDS->GetElements();
238           if ( aGroupDS->GetType() == SMDSAbs_Node ) {
239             while ( aIter->more() ) {
240               const SMDS_MeshElement* aNode = aIter->next();
241               if ( nodeLabelByID.empty() )
242                 aRec.NodeList.push_back( FromSmIdType<int>( aNode->GetID()) );
243               else
244                 aRec.NodeList.push_back( nodeLabelByID[ aNode->GetID() ]);
245             }
246           }
247           else
248           {
249             while ( aIter->more() ) {
250               const SMDS_MeshElement* aElem = aIter->next();
251               if ( elemLabelByID.empty() )
252                 aRec.ElementList.push_back( FromSmIdType<int>(aElem->GetID()) );
253               else
254                 aRec.ElementList.push_back( elemLabelByID[ aElem->GetID() ]);
255             }
256           }
257           // 0019936: EDF 794 SMESH : Export UNV : Node color and group id
258           //aDataSet2417.insert(TDataSet::value_type(aGroupDS->GetID(), aRec));
259           aDataSet2417.insert(TDataSet::value_type(aGroupDS->GetID()+1, aRec));
260         }
261         UNV2417::Write(out_stream,aDataSet2417);
262         myGroups.clear();
263       }
264     }
265
266     out_stream.flush();
267     out_stream.close();
268     if (!check_file(myFile))
269       EXCEPTION(runtime_error,"ERROR: Output file not good.");
270   }
271   catch(const std::exception& exc){
272     INFOS("Follow exception was caught:\n\t"<<exc.what());
273     throw;
274   }
275   catch(...){
276     INFOS("Unknown exception was caught !!!");
277     throw;
278   }
279   return aResult;
280 }