Salome HOME
d83601308b18a7a7f1122bdff22caf1a9c4f27e6
[modules/smesh.git] / src / DriverUNV / DriverUNV_W_SMDS_Mesh.cxx
1 // Copyright (C) 2007-2013  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           SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIteratorToUNV();
178           if ( anElem->IsPoly() ) {
179             continue;
180           }
181           int anId = -1;
182           switch(aNbNodes) {
183           case 4:  anId = 111; break;
184           case 6:  anId = 112; break;
185           case 8:  anId = 115; break;
186           case 10: anId = 118; break;
187           case 13: anId = 114; break;
188           case 15: anId = 113; break;
189           case 20:
190           case 27: anId = 116; aNbNodes = 20; break;
191           default:
192             continue;
193           }
194           if(anId>0){
195             TRecord aRec;
196             aRec.label = anElem->GetID();
197             aRec.fe_descriptor_id = anId;
198             aRec.node_labels.reserve(aNbNodes);
199             while ( aNodesIter->more() && aRec.node_labels.size() < aNbNodes )
200             {
201               const SMDS_MeshElement* aNode = aNodesIter->next();
202               aRec.node_labels.push_back(aNode->GetID());
203             }
204             aDataSet2412.push_back(aRec);
205           }
206         }
207         MESSAGE("Perform - aDataSet2412.size() = "<<aDataSet2412.size());
208       }
209       UNV2412::Write(out_stream,aDataSet2412);
210     }
211     {
212       using namespace UNV2417;
213       if (myGroups.size() > 0) {
214         TDataSet aDataSet2417;
215         TGroupList::const_iterator aIter = myGroups.begin();
216         for (; aIter != myGroups.end(); aIter++) {
217           SMESHDS_GroupBase* aGroupDS = *aIter;
218           TRecord aRec;
219           aRec.GroupName = aGroupDS->GetStoreName();
220
221           int i;
222           SMDS_ElemIteratorPtr aIter = aGroupDS->GetElements();
223           if (aGroupDS->GetType() == SMDSAbs_Node) {
224             aRec.NodeList.resize(aGroupDS->Extent());
225             i = 0;
226             while (aIter->more()) {
227               const SMDS_MeshElement* aElem = aIter->next();
228               aRec.NodeList[i] = aElem->GetID(); 
229               i++;
230             }
231           } else {
232             aRec.ElementList.resize(aGroupDS->Extent());
233             i = 0;
234             while (aIter->more()) {
235               const SMDS_MeshElement* aElem = aIter->next();
236               aRec.ElementList[i] = aElem->GetID(); 
237               i++;
238             }
239           }
240           // 0019936: EDF 794 SMESH : Export UNV : Node color and group id
241           //aDataSet2417.insert(TDataSet::value_type(aGroupDS->GetID(), aRec));
242           aDataSet2417.insert(TDataSet::value_type(aGroupDS->GetID()+1, aRec));
243         }
244         UNV2417::Write(out_stream,aDataSet2417);
245         myGroups.clear();
246       }
247     }
248     /*    {
249       using namespace UNV2417;
250       TDataSet aDataSet2417;
251       for ( TGroupsMap::iterator it = myGroupsMap.begin(); it != myGroupsMap.end(); it++ ) {
252         SMESH_Group*       aGroup   = it->second;
253         SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
254         if ( aGroupDS ) {
255           TRecord aRec;
256           aRec.GroupName = aGroup->GetName();
257           int i;
258           SMDS_ElemIteratorPtr aIter = aGroupDS->GetElements();
259           if (aGroupDS->GetType() == SMDSAbs_Node) {
260             aRec.NodeList.resize(aGroupDS->Extent());
261             i = 0;
262             while (aIter->more()) {
263               const SMDS_MeshElement* aElem = aIter->next();
264               aRec.NodeList[i] = aElem->GetID(); 
265               i++;
266             }
267           } else {
268             aRec.ElementList.resize(aGroupDS->Extent());
269             i = 0;
270             while (aIter->more()) {
271               const SMDS_MeshElement* aElem = aIter->next();
272               aRec.ElementList[i] = aElem->GetID(); 
273               i++;
274             }
275           }
276           aDataSet2417.insert(TDataSet::value_type(aGroupDS->GetID(), aRec));
277         }
278       }
279       UNV2417::Write(out_stream,aDataSet2417);
280       }*/
281
282     out_stream.flush();
283     out_stream.close();
284     if (!check_file(myFile))
285       EXCEPTION(runtime_error,"ERROR: Output file not good.");
286   }
287   catch(const std::exception& exc){
288     INFOS("Follow exception was cought:\n\t"<<exc.what());
289     throw;
290   }
291   catch(...){
292     INFOS("Unknown exception was cought !!!");
293     throw;
294   }
295   return aResult;
296 }