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