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