Salome HOME
Merge from V6_5_BR 05/06/2012
[modules/smesh.git] / src / DriverUNV / DriverUNV_W_SMDS_Mesh.cxx
1 // Copyright (C) 2007-2012  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.
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           SMDS_ElemIteratorPtr aNodesIter;
110           aNodesIter = anElem->nodesIteratorToUNV();
111           if( anElem->IsQuadratic() ) {
112             aRec.fe_descriptor_id = 22;
113           } else {
114             aRec.fe_descriptor_id = 11;
115           }
116           while( aNodesIter->more())
117           {
118             const SMDS_MeshElement* aNode = aNodesIter->next();
119             aRec.node_labels.push_back(aNode->GetID());
120           }
121           aDataSet2412.push_back(aRec);
122         }
123         MESSAGE("Perform - aDataSet2412.size() = "<<aDataSet2412.size());
124       }
125
126       MESSAGE("Perform - myMesh->NbFaces() = "<<myMesh->NbFaces());
127       if ( myMesh->NbFaces() )
128       {
129         SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
130         while ( anIter->more())
131         {
132           const SMDS_MeshFace* anElem = anIter->next();
133           if ( anElem->IsPoly() ) continue;
134           int aNbNodes = anElem->NbNodes();
135           TRecord aRec;
136           aRec.label = anElem->GetID();
137           aRec.node_labels.reserve(aNbNodes);
138           SMDS_ElemIteratorPtr aNodesIter;
139           aNodesIter = anElem->nodesIteratorToUNV();
140           for(; aNodesIter->more();){
141             const SMDS_MeshElement* aNode = aNodesIter->next();
142             aRec.node_labels.push_back(aNode->GetID());
143           }
144           switch(aNbNodes){
145           case 3:
146             aRec.fe_descriptor_id = 41;
147             break;
148           case 4:
149             aRec.fe_descriptor_id = 44;
150             break;
151           case 6:
152             aRec.fe_descriptor_id = 42;
153             break;
154           case 8:
155             aRec.fe_descriptor_id = 45;
156             break;
157           case 9:
158             aRec.fe_descriptor_id = 45;
159             aRec.node_labels.resize( 8 );
160             break;
161           default:
162             continue;
163           }
164           aDataSet2412.push_back(aRec);
165         }
166         MESSAGE("Perform - aDataSet2412.size() = "<<aDataSet2412.size());
167       }
168
169       MESSAGE("Perform - myMesh->NbVolumes() = "<<myMesh->NbVolumes());
170       if ( myMesh->NbVolumes() )
171       {
172         SMDS_VolumeIteratorPtr anIter = myMesh->volumesIterator();
173         while ( anIter->more())
174         {
175           const SMDS_MeshVolume* anElem = anIter->next();
176           int aNbNodes = anElem->NbNodes();
177           //MESSAGE("aNbNodes="<<aNbNodes);
178           SMDS_ElemIteratorPtr aNodesIter;
179           aNodesIter = anElem->nodesIteratorToUNV();
180           if ( anElem->IsPoly() ) {
181             continue;
182             // MESSAGE("anElem->IsPoly");
183             // if ( const SMDS_VtkVolume* ph =
184             //      dynamic_cast<const SMDS_VtkVolume*> (anElem))
185             // {
186             //   aNbNodes = ph->NbUniqueNodes();
187             //   aNodesIter = ph->uniqueNodesIterator();
188             // }
189           }
190
191           int anId = -1;
192           switch(aNbNodes) {
193           case 4: {
194             anId = 111;
195             break;
196           }
197           case 6: {
198             anId = 112;
199             break;
200           }
201           case 8: {
202             anId = 115;
203             break;
204           }
205           case 10: {
206             anId = 118;
207             break;
208           }
209           case 13: {
210             anId = 114;
211             break;
212           }
213           case 15: {
214             anId = 113;
215             break;
216           }
217           case 20:
218           case 27: {
219             anId = 116;
220             aNbNodes = 20;
221             break;
222           }
223           default:
224             continue;
225           }
226           if(anId>0){
227             TRecord aRec;
228             aRec.label = anElem->GetID();
229             aRec.fe_descriptor_id = anId;
230             aRec.node_labels.reserve(aNbNodes);
231             while ( aNodesIter->more() && aRec.node_labels.size() < aNbNodes )
232             {
233               const SMDS_MeshElement* aNode = aNodesIter->next();
234               aRec.node_labels.push_back(aNode->GetID());
235             }
236             aDataSet2412.push_back(aRec);
237           }
238         }
239         MESSAGE("Perform - aDataSet2412.size() = "<<aDataSet2412.size());
240       }
241       UNV2412::Write(out_stream,aDataSet2412);
242     }
243     {
244       using namespace UNV2417;
245       if (myGroups.size() > 0) {
246         TDataSet aDataSet2417;
247         TGroupList::const_iterator aIter = myGroups.begin();
248         for (; aIter != myGroups.end(); aIter++) {
249           SMESHDS_GroupBase* aGroupDS = *aIter;
250           TRecord aRec;
251           aRec.GroupName = aGroupDS->GetStoreName();
252
253           int i;
254           SMDS_ElemIteratorPtr aIter = aGroupDS->GetElements();
255           if (aGroupDS->GetType() == SMDSAbs_Node) {
256             aRec.NodeList.resize(aGroupDS->Extent());
257             i = 0;
258             while (aIter->more()) {
259               const SMDS_MeshElement* aElem = aIter->next();
260               aRec.NodeList[i] = aElem->GetID(); 
261               i++;
262             }
263           } else {
264             aRec.ElementList.resize(aGroupDS->Extent());
265             i = 0;
266             while (aIter->more()) {
267               const SMDS_MeshElement* aElem = aIter->next();
268               aRec.ElementList[i] = aElem->GetID(); 
269               i++;
270             }
271           }
272           // 0019936: EDF 794 SMESH : Export UNV : Node color and group id
273           //aDataSet2417.insert(TDataSet::value_type(aGroupDS->GetID(), aRec));
274           aDataSet2417.insert(TDataSet::value_type(aGroupDS->GetID()+1, aRec));
275         }
276         UNV2417::Write(out_stream,aDataSet2417);
277         myGroups.clear();
278       }
279     }
280     /*    {
281       using namespace UNV2417;
282       TDataSet aDataSet2417;
283       for ( TGroupsMap::iterator it = myGroupsMap.begin(); it != myGroupsMap.end(); it++ ) {
284         SMESH_Group*       aGroup   = it->second;
285         SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
286         if ( aGroupDS ) {
287           TRecord aRec;
288           aRec.GroupName = aGroup->GetName();
289           int i;
290           SMDS_ElemIteratorPtr aIter = aGroupDS->GetElements();
291           if (aGroupDS->GetType() == SMDSAbs_Node) {
292             aRec.NodeList.resize(aGroupDS->Extent());
293             i = 0;
294             while (aIter->more()) {
295               const SMDS_MeshElement* aElem = aIter->next();
296               aRec.NodeList[i] = aElem->GetID(); 
297               i++;
298             }
299           } else {
300             aRec.ElementList.resize(aGroupDS->Extent());
301             i = 0;
302             while (aIter->more()) {
303               const SMDS_MeshElement* aElem = aIter->next();
304               aRec.ElementList[i] = aElem->GetID(); 
305               i++;
306             }
307           }
308           aDataSet2417.insert(TDataSet::value_type(aGroupDS->GetID(), aRec));
309         }
310       }
311       UNV2417::Write(out_stream,aDataSet2417);
312       }*/
313
314     out_stream.flush();
315     out_stream.close();
316     if (!check_file(myFile))
317       EXCEPTION(runtime_error,"ERROR: Output file not good.");
318   }
319   catch(const std::exception& exc){
320     INFOS("Follow exception was cought:\n\t"<<exc.what());
321     throw;
322   }
323   catch(...){
324     INFOS("Unknown exception was cought !!!");
325     throw;
326   }
327   return aResult;
328 }