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