Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[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 #include "SMESHDS_GroupBase.hxx"
26 //#include "SMESH_Group.hxx"
27 #include "SMDS_QuadraticEdge.hxx"
28 #include "SMDS_QuadraticFaceOfNodes.hxx"
29
30 #include "utilities.h"
31
32 #include "UNV2411_Structure.hxx"
33 #include "UNV2412_Structure.hxx"
34 #include "UNV2417_Structure.hxx"
35 #include "UNV_Utilities.hxx"
36
37 using namespace std;
38
39 namespace{
40   typedef std::vector<size_t> TConnect;
41
42   int GetConnect(const SMDS_ElemIteratorPtr& theNodesIter, 
43                  TConnect& theConnect)
44   {
45     theConnect.clear();
46     for(; theNodesIter->more();){
47       const SMDS_MeshElement* anElem = theNodesIter->next();
48       theConnect.push_back(anElem->GetID());
49     }
50     return theConnect.size();
51   }
52   
53 }
54
55 Driver_Mesh::Status DriverUNV_W_SMDS_Mesh::Perform()
56 {
57   Status aResult = DRS_OK;
58   std::ofstream out_stream(myFile.c_str());
59   try{
60     {
61       using namespace UNV2411;
62       TDataSet aDataSet2411;
63       // Storing SMDS nodes to the UNV file
64       //-----------------------------------
65       MESSAGE("Perform - myMesh->NbNodes() = "<<myMesh->NbNodes());
66       SMDS_NodeIteratorPtr aNodesIter = myMesh->nodesIterator();
67       for(; aNodesIter->more();){
68         const SMDS_MeshNode* aNode = aNodesIter->next();
69         TRecord aRec;
70         aRec.coord[0] = aNode->X();
71         aRec.coord[1] = aNode->Y();
72         aRec.coord[2] = aNode->Z();
73         const TNodeLab& aLabel = aNode->GetID();
74         aDataSet2411.insert(TDataSet::value_type(aLabel,aRec));
75       }
76       MESSAGE("Perform - aDataSet2411.size() = "<<aDataSet2411.size());
77       UNV2411::Write(out_stream,aDataSet2411);
78     }
79     {
80       using namespace UNV2412;
81       TDataSet aDataSet2412;
82       TConnect aConnect;
83
84       // Storing SMDS Edges
85       MESSAGE("Perform - myMesh->NbEdges() = "<<myMesh->NbEdges());
86       if(myMesh->NbEdges()){
87         SMDS_EdgeIteratorPtr anIter = myMesh->edgesIterator();
88         for(; anIter->more();){
89           const SMDS_MeshEdge* anElem = anIter->next();
90           TElementLab aLabel = anElem->GetID();
91           int aNbNodes = anElem->NbNodes();
92           TRecord aRec;
93           aRec.node_labels.reserve(aNbNodes);
94           SMDS_ElemIteratorPtr aNodesIter;
95           if( anElem->IsQuadratic() ) {
96             aNodesIter = static_cast<const SMDS_QuadraticEdge* >
97               ( anElem )->interlacedNodesElemIterator();
98             aRec.fe_descriptor_id = 22;
99           } else {
100             aNodesIter = anElem->nodesIterator();
101             aRec.fe_descriptor_id = 11;
102           }
103           for(; aNodesIter->more();){
104             const SMDS_MeshElement* aNode = aNodesIter->next();
105             aRec.node_labels.push_back(aNode->GetID());
106           }
107           aDataSet2412.insert(TDataSet::value_type(aLabel,aRec));
108         }
109         MESSAGE("Perform - aDataSet2412.size() = "<<aDataSet2412.size());
110       }
111
112       MESSAGE("Perform - myMesh->NbFaces() = "<<myMesh->NbFaces());
113       if(myMesh->NbFaces()){
114         SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
115         for(; anIter->more();){
116           const SMDS_MeshFace* anElem = anIter->next();
117           TElementLab aLabel = anElem->GetID();
118           int aNbNodes = anElem->NbNodes();
119           TRecord aRec;
120           aRec.node_labels.reserve(aNbNodes);
121           SMDS_ElemIteratorPtr aNodesIter;
122           if( anElem->IsQuadratic() )
123             aNodesIter = static_cast<const SMDS_QuadraticFaceOfNodes* >
124               ( anElem )->interlacedNodesElemIterator();
125           else
126             aNodesIter = anElem->nodesIterator();
127           for(; aNodesIter->more();){
128             const SMDS_MeshElement* aNode = aNodesIter->next();
129             aRec.node_labels.push_back(aNode->GetID());
130           }
131           switch(aNbNodes){
132           case 3:
133             aRec.fe_descriptor_id = 41;
134             break;
135           case 4:
136             aRec.fe_descriptor_id = 44;
137             break;
138           case 6:
139             aRec.fe_descriptor_id = 42;
140             break;
141           case 8:
142             aRec.fe_descriptor_id = 45;
143             break;
144           default:
145             continue;
146           }
147           aDataSet2412.insert(TDataSet::value_type(aLabel,aRec));
148         }
149         MESSAGE("Perform - aDataSet2412.size() = "<<aDataSet2412.size());
150       }
151
152       MESSAGE("Perform - myMesh->NbVolumes() = "<<myMesh->NbVolumes());
153       if(myMesh->NbVolumes()){
154         SMDS_VolumeIteratorPtr anIter = myMesh->volumesIterator();
155         for(; anIter->more();){
156           const SMDS_MeshVolume* anElem = anIter->next();
157           TElementLab aLabel = anElem->GetID();
158
159           int aNbNodes = anElem->NbNodes();
160           aConnect.resize(aNbNodes);
161
162           SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
163           GetConnect(aNodesIter,aConnect);
164
165           int anId = -1;
166           int* aConn = NULL;
167           switch(aNbNodes){
168           case 4: {
169             static int anIds[] = {0,2,1,3};
170             aConn = anIds;
171             anId = 111;
172             break;
173           }
174           case 6: {
175             static int anIds[] = {0,2,1,3,5,4};
176             aConn = anIds;
177             anId = 112;
178             break;
179           }
180           case 8: {
181             static int anIds[] = {0,3,2,1,4,7,6,5};
182             aConn = anIds;
183             anId = 115;
184             break;
185           }
186           case 10: {
187             static int anIds[] = {0,4,2,9,5,3, 1,6,8, 7};
188             aConn = anIds;
189             anId = 118;
190             break;
191           }
192           case 13: {
193             static int anIds[] = {0,6,4,2,7,5,3,1,8,11,10,9,12};
194             aConn = anIds;
195             anId = 114;
196             break;
197           }
198           case 15: {
199             static int anIds[] = {0,4,2,9,13,11,5,3,1,14,12,10,6,8,7};
200             aConn = anIds;
201             anId = 113;
202             break;
203           }
204           case 20: {
205             static int anIds[] = {0,6, 4,2, 12,18,16,14,7, 5, 3, 1, 19,17,15,13,8, 11,10,9};
206             aConn = anIds;
207             anId = 116;
208             break;
209           }
210           default:
211             continue;
212           }
213           if(aConn){
214             TRecord aRec;
215             aRec.fe_descriptor_id = anId;
216             aRec.node_labels.resize(aNbNodes);
217             for(int aNodeId = 0; aNodeId < aNbNodes; aNodeId++){
218               aRec.node_labels[aConn[aNodeId]] = aConnect[aNodeId];
219             }
220             aDataSet2412.insert(TDataSet::value_type(aLabel,aRec));
221           }
222         }
223         MESSAGE("Perform - aDataSet2412.size() = "<<aDataSet2412.size());
224       }
225       UNV2412::Write(out_stream,aDataSet2412);
226     }
227     {
228       using namespace UNV2417;
229       if (myGroups.size() > 0) {
230         TDataSet aDataSet2417;
231         TGroupList::const_iterator aIter = myGroups.begin();
232         for (; aIter != myGroups.end(); aIter++) {
233           SMESHDS_GroupBase* aGroupDS = *aIter;
234           TRecord aRec;
235           aRec.GroupName = aGroupDS->GetStoreName();
236
237           int i;
238           SMDS_ElemIteratorPtr aIter = aGroupDS->GetElements();
239           if (aGroupDS->GetType() == SMDSAbs_Node) {
240             aRec.NodeList.resize(aGroupDS->Extent());
241             i = 0;
242             while (aIter->more()) {
243               const SMDS_MeshElement* aElem = aIter->next();
244               aRec.NodeList[i] = aElem->GetID(); 
245               i++;
246             }
247           } else {
248             aRec.ElementList.resize(aGroupDS->Extent());
249             i = 0;
250             while (aIter->more()) {
251               const SMDS_MeshElement* aElem = aIter->next();
252               aRec.ElementList[i] = aElem->GetID(); 
253               i++;
254             }
255           }
256           aDataSet2417.insert(TDataSet::value_type(aGroupDS->GetID(), aRec));
257         }
258         UNV2417::Write(out_stream,aDataSet2417);
259         myGroups.clear();
260       }
261     }
262     /*    {
263       using namespace UNV2417;
264       TDataSet aDataSet2417;
265       for ( TGroupsMap::iterator it = myGroupsMap.begin(); it != myGroupsMap.end(); it++ ) {
266         SMESH_Group*       aGroup   = it->second;
267         SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
268         if ( aGroupDS ) {
269           TRecord aRec;
270           aRec.GroupName = aGroup->GetName();
271           int i;
272           SMDS_ElemIteratorPtr aIter = aGroupDS->GetElements();
273           if (aGroupDS->GetType() == SMDSAbs_Node) {
274             aRec.NodeList.resize(aGroupDS->Extent());
275             i = 0;
276             while (aIter->more()) {
277               const SMDS_MeshElement* aElem = aIter->next();
278               aRec.NodeList[i] = aElem->GetID(); 
279               i++;
280             }
281           } else {
282             aRec.ElementList.resize(aGroupDS->Extent());
283             i = 0;
284             while (aIter->more()) {
285               const SMDS_MeshElement* aElem = aIter->next();
286               aRec.ElementList[i] = aElem->GetID(); 
287               i++;
288             }
289           }
290           aDataSet2417.insert(TDataSet::value_type(aGroupDS->GetID(), aRec));
291         }
292       }
293       UNV2417::Write(out_stream,aDataSet2417);
294       }*/
295   }
296   catch(const std::exception& exc){
297     INFOS("Follow exception was cought:\n\t"<<exc.what());
298   }
299   catch(...){
300     INFOS("Unknown exception was cought !!!");
301   }
302   return aResult;
303 }