Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[modules/smesh.git] / src / DriverUNV / DriverUNV_W_SMDS_Mesh.cxx
1 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 // 
4 //  This library is free software; you can redistribute it and/or 
5 //  modify it under the terms of the GNU Lesser General Public 
6 //  License as published by the Free Software Foundation; either 
7 //  version 2.1 of the License. 
8 // 
9 //  This library is distributed in the hope that it will be useful, 
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 //  Lesser General Public License for more details. 
13 // 
14 //  You should have received a copy of the GNU Lesser General Public 
15 //  License along with this library; if not, write to the Free Software 
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17 // 
18 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
19
20 #include <algorithm>
21
22 #include "DriverUNV_W_SMDS_Mesh.h"
23
24 #include "SMDS_Mesh.hxx"
25
26 #include "utilities.h"
27
28 #include "UNV2411_Structure.hxx"
29 #include "UNV2412_Structure.hxx"
30 #include "UNV_Utilities.hxx"
31
32 using namespace std;
33
34 namespace{
35   typedef std::vector<size_t> TConnect;
36
37   int GetConnect(const SMDS_ElemIteratorPtr& theNodesIter, 
38                  TConnect& theConnect)
39   {
40     theConnect.clear();
41     for(; theNodesIter->more();){
42       const SMDS_MeshElement* anElem = theNodesIter->next();
43       theConnect.push_back(anElem->GetID());
44     }
45     return theConnect.size();
46   }
47   
48 }
49
50 Driver_Mesh::Status DriverUNV_W_SMDS_Mesh::Perform()
51 {
52   Status aResult = DRS_OK;
53   std::ofstream out_stream(myFile.c_str());
54   try{
55     {
56       using namespace UNV2411;
57       TDataSet aDataSet2411;
58       // Storing SMDS nodes to the UNV file
59       //-----------------------------------
60       MESSAGE("Perform - myMesh->NbNodes() = "<<myMesh->NbNodes());
61       SMDS_NodeIteratorPtr aNodesIter = myMesh->nodesIterator();
62       for(; aNodesIter->more();){
63         const SMDS_MeshNode* aNode = aNodesIter->next();
64         TRecord aRec;
65         aRec.coord[0] = aNode->X();
66         aRec.coord[1] = aNode->Y();
67         aRec.coord[2] = aNode->Z();
68         const TNodeLab& aLabel = aNode->GetID();
69         aDataSet2411.insert(TDataSet::value_type(aLabel,aRec));
70       }
71       MESSAGE("Perform - aDataSet2411.size() = "<<aDataSet2411.size());
72       UNV2411::Write(out_stream,aDataSet2411);
73     }
74     {
75       using namespace UNV2412;
76       TDataSet aDataSet2412;
77       TConnect aConnect;
78
79       // Storing SMDS Edges
80       MESSAGE("Perform - myMesh->NbEdges() = "<<myMesh->NbEdges());
81       if(myMesh->NbEdges()){
82         SMDS_EdgeIteratorPtr anIter = myMesh->edgesIterator();
83         for(; anIter->more();){
84           const SMDS_MeshEdge* anElem = anIter->next();
85           TElementLab aLabel = anElem->GetID();
86           int aNbNodes = anElem->NbNodes();
87           TRecord aRec;
88           aRec.node_labels.reserve(aNbNodes);
89           SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
90           for(; aNodesIter->more();){
91             const SMDS_MeshElement* aNode = aNodesIter->next();
92             aRec.node_labels.push_back(aNode->GetID());
93           }
94           if(aNbNodes==2)
95             aRec.fe_descriptor_id = 11;
96           else
97             aRec.fe_descriptor_id = 21;
98           aDataSet2412.insert(TDataSet::value_type(aLabel,aRec));
99         }
100         MESSAGE("Perform - aDataSet2412.size() = "<<aDataSet2412.size());
101       }
102
103       MESSAGE("Perform - myMesh->NbFaces() = "<<myMesh->NbFaces());
104       if(myMesh->NbFaces()){
105         SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
106         for(; anIter->more();){
107           const SMDS_MeshFace* anElem = anIter->next();
108           TElementLab aLabel = anElem->GetID();
109           int aNbNodes = anElem->NbNodes();
110           TRecord aRec;
111           aRec.node_labels.reserve(aNbNodes);
112           SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
113           for(; aNodesIter->more();){
114             const SMDS_MeshElement* aNode = aNodesIter->next();
115             aRec.node_labels.push_back(aNode->GetID());
116           }
117           switch(aNbNodes){
118           case 3:
119             aRec.fe_descriptor_id = 41;
120             break;
121           case 4:
122             aRec.fe_descriptor_id = 44;
123             break;
124           case 6:
125             aRec.fe_descriptor_id = 42;
126             break;
127           case 8:
128             aRec.fe_descriptor_id = 45;
129             break;
130           default:
131             continue;
132           }
133           aDataSet2412.insert(TDataSet::value_type(aLabel,aRec));
134         }
135         MESSAGE("Perform - aDataSet2412.size() = "<<aDataSet2412.size());
136       }
137
138       MESSAGE("Perform - myMesh->NbVolumes() = "<<myMesh->NbVolumes());
139       if(myMesh->NbVolumes()){
140         SMDS_VolumeIteratorPtr anIter = myMesh->volumesIterator();
141         for(; anIter->more();){
142           const SMDS_MeshVolume* anElem = anIter->next();
143           TElementLab aLabel = anElem->GetID();
144
145           int aNbNodes = anElem->NbNodes();
146           aConnect.resize(aNbNodes);
147
148           SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
149           GetConnect(aNodesIter,aConnect);
150
151           int anId = -1;
152           int* aConn = NULL;
153           switch(aNbNodes){
154           case 4: {
155             static int anIds[] = {0,2,1,3};
156             aConn = anIds;
157             anId = 111;
158             break;
159           }
160           case 6: {
161             static int anIds[] = {0,2,1,3,5,4};
162             aConn = anIds;
163             anId = 112;
164             break;
165           }
166           case 8: {
167             static int anIds[] = {0,3,2,1,4,7,6,5};
168             aConn = anIds;
169             anId = 115;
170             break;
171           }
172           case 10: {
173             static int anIds[] = {0,2,1,3,6,5,4,7,9,8};
174             aConn = anIds;
175             anId = 118;
176             break;
177           }
178           case 13: {
179             static int anIds[] = {0,3,2,1,4,8,7,6,5,9,12,11,10};
180             aConn = anIds;
181             anId = 114;
182             break;
183           }
184           case 15: {
185             static int anIds[] = {0,2,1,3,5,4,8,7,6,11,10,9,12,14,13};
186             aConn = anIds;
187             anId = 113;
188             break;
189           }
190           case 20: {
191             static int anIds[] = {0,3,2,1,4,7,6,5,11,10,9,8,15,14,13,12,16,19,18,17};
192             aConn = anIds;
193             anId = 116;
194             break;
195           }
196           default:
197             continue;
198           }
199           if(aConn){
200             TRecord aRec;
201             aRec.fe_descriptor_id = anId;
202             aRec.node_labels.resize(aNbNodes);
203             for(int aNodeId = 0; aNodeId < aNbNodes; aNodeId++){
204               aRec.node_labels[aConn[aNodeId]] = aConnect[aNodeId];
205             }
206             aDataSet2412.insert(TDataSet::value_type(aLabel,aRec));
207           }
208         }
209         MESSAGE("Perform - aDataSet2412.size() = "<<aDataSet2412.size());
210       }
211       UNV2412::Write(out_stream,aDataSet2412);
212     }
213   }
214   catch(const std::exception& exc){
215     INFOS("Follow exception was cought:\n\t"<<exc.what());
216   }
217   catch(...){
218     INFOS("Unknown exception was cought !!!");
219   }
220   return aResult;
221 }