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