Salome HOME
d94571ab649d285dc0c46c822e6819198572efbb
[modules/smesh.git] / src / DriverUNV / DriverUNV_W_SMDS_Mesh.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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 Driver_Mesh::Status DriverUNV_W_SMDS_Mesh::Perform()
48 {
49   Kernel_Utils::Localizer loc;
50   Status aResult = DRS_OK;
51   std::ofstream out_stream(myFile.c_str());
52   try{
53
54     UNV164::Write( out_stream ); // unit system
55     UNV2420::Write( out_stream, myMeshName ); // Coordinate system
56
57     {
58       using namespace UNV2411;
59       TDataSet aDataSet2411;
60       // Storing SMDS nodes to the UNV file
61       //-----------------------------------
62       MESSAGE("Perform - myMesh->NbNodes() = "<<myMesh->NbNodes());
63       SMDS_NodeIteratorPtr aNodesIter = myMesh->nodesIterator();
64       TRecord aRec;
65       while ( aNodesIter->more() )
66       {
67         const SMDS_MeshNode* aNode = aNodesIter->next();
68         aRec.label    = aNode->GetID();
69         aRec.coord[0] = aNode->X();
70         aRec.coord[1] = aNode->Y();
71         aRec.coord[2] = aNode->Z();
72         aDataSet2411.push_back( aRec );
73       }
74       MESSAGE("Perform - aDataSet2411.size() = "<<aDataSet2411.size());
75       UNV2411::Write(out_stream,aDataSet2411);
76     }
77     {
78       using namespace UNV2412;
79       TDataSet aDataSet2412;
80
81       // Storing SMDS Edges
82       MESSAGE("Perform - myMesh->NbEdges() = "<<myMesh->NbEdges());
83       if(myMesh->NbEdges()){
84         SMDS_EdgeIteratorPtr anIter = myMesh->edgesIterator();
85         while( anIter->more() )
86         {
87           const SMDS_MeshEdge* anElem = anIter->next();
88           int aNbNodes = anElem->NbNodes();
89           TRecord aRec;
90           aRec.label = anElem->GetID();
91           aRec.node_labels.reserve(aNbNodes);
92           if( anElem->IsQuadratic() ) {
93             aRec.fe_descriptor_id = 22;
94           } else {
95             aRec.fe_descriptor_id = 11;
96           }
97           SMDS_NodeIteratorPtr aNodesIter = anElem->nodesIteratorToUNV();
98           while( aNodesIter->more())
99           {
100             const SMDS_MeshNode* aNode = aNodesIter->next();
101             aRec.node_labels.push_back( aNode->GetID() );
102           }
103           aDataSet2412.push_back(aRec);
104         }
105         MESSAGE("Perform - aDataSet2412.size() = "<<aDataSet2412.size());
106       }
107
108       MESSAGE("Perform - myMesh->NbFaces() = "<<myMesh->NbFaces());
109       if ( myMesh->NbFaces() )
110       {
111         SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
112         while ( anIter->more())
113         {
114           const SMDS_MeshFace* anElem = anIter->next();
115           if ( anElem->IsPoly() ) continue;
116           int aNbNodes = anElem->NbNodes();
117           TRecord aRec;
118           aRec.label = anElem->GetID();
119           aRec.node_labels.reserve(aNbNodes);
120           SMDS_NodeIteratorPtr aNodesIter = anElem->nodesIteratorToUNV();
121           while( aNodesIter->more() ) {
122             const SMDS_MeshNode* aNode = aNodesIter->next();
123             aRec.node_labels.push_back( aNode->GetID() );
124           }
125           switch ( aNbNodes ) {
126           case 3: aRec.fe_descriptor_id = 41; break;
127           case 4: aRec.fe_descriptor_id = 44; break;
128           case 6: aRec.fe_descriptor_id = 42; break;
129           case 7: aRec.fe_descriptor_id = 42; break;
130           case 8: aRec.fe_descriptor_id = 45; break;
131           case 9: aRec.fe_descriptor_id = 45; aRec.node_labels.resize( 8 ); break;
132           default:
133             continue;
134           }
135           aDataSet2412.push_back(aRec);
136         }
137         MESSAGE("Perform - aDataSet2412.size() = "<<aDataSet2412.size());
138       }
139
140       MESSAGE("Perform - myMesh->NbVolumes() = "<<myMesh->NbVolumes());
141       if ( myMesh->NbVolumes() )
142       {
143         SMDS_VolumeIteratorPtr anIter = myMesh->volumesIterator();
144         while ( anIter->more())
145         {
146           const SMDS_MeshVolume* anElem = anIter->next();
147           if ( anElem->IsPoly() )
148             continue;
149           int aNbNodes = anElem->NbNodes();
150           int anId = -1;
151           switch(aNbNodes) {
152           case 4:  anId = 111; break;
153           case 6:  anId = 112; break;
154           case 8:  anId = 115; break;
155           case 10: anId = 118; break;
156           case 13: anId = 114; break;
157           case 15: anId = 113; break;
158           case 20:
159           case 27: anId = 116; aNbNodes = 20; break;
160           default:
161             continue;
162           }
163           if(anId>0){
164             TRecord aRec;
165             aRec.label = anElem->GetID();
166             aRec.fe_descriptor_id = anId;
167             aRec.node_labels.reserve(aNbNodes);
168             SMDS_NodeIteratorPtr aNodesIter = anElem->nodesIteratorToUNV();
169             while ( aNodesIter->more() && (int)aRec.node_labels.size() < aNbNodes )
170             {
171               const SMDS_MeshElement* aNode = aNodesIter->next();
172               aRec.node_labels.push_back(aNode->GetID());
173             }
174             aDataSet2412.push_back(aRec);
175           }
176         }
177         MESSAGE("Perform - aDataSet2412.size() = "<<aDataSet2412.size());
178       }
179       UNV2412::Write(out_stream,aDataSet2412);
180     }
181     {
182       using namespace UNV2417;
183       if (myGroups.size() > 0) {
184         TDataSet aDataSet2417;
185         TGroupList::const_iterator aIter = myGroups.begin();
186         for (; aIter != myGroups.end(); aIter++) {
187           SMESHDS_GroupBase* aGroupDS = *aIter;
188           TRecord aRec;
189           aRec.GroupName = aGroupDS->GetStoreName();
190
191           int i;
192           SMDS_ElemIteratorPtr aIter = aGroupDS->GetElements();
193           if (aGroupDS->GetType() == SMDSAbs_Node) {
194             aRec.NodeList.resize(aGroupDS->Extent());
195             i = 0;
196             while (aIter->more()) {
197               const SMDS_MeshElement* aElem = aIter->next();
198               aRec.NodeList[i] = aElem->GetID(); 
199               i++;
200             }
201           } else {
202             aRec.ElementList.resize(aGroupDS->Extent());
203             i = 0;
204             while (aIter->more()) {
205               const SMDS_MeshElement* aElem = aIter->next();
206               aRec.ElementList[i] = aElem->GetID(); 
207               i++;
208             }
209           }
210           // 0019936: EDF 794 SMESH : Export UNV : Node color and group id
211           //aDataSet2417.insert(TDataSet::value_type(aGroupDS->GetID(), aRec));
212           aDataSet2417.insert(TDataSet::value_type(aGroupDS->GetID()+1, aRec));
213         }
214         UNV2417::Write(out_stream,aDataSet2417);
215         myGroups.clear();
216       }
217     }
218     /*    {
219       using namespace UNV2417;
220       TDataSet aDataSet2417;
221       for ( TGroupsMap::iterator it = myGroupsMap.begin(); it != myGroupsMap.end(); it++ ) {
222         SMESH_Group*       aGroup   = it->second;
223         SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
224         if ( aGroupDS ) {
225           TRecord aRec;
226           aRec.GroupName = aGroup->GetName();
227           int i;
228           SMDS_ElemIteratorPtr aIter = aGroupDS->GetElements();
229           if (aGroupDS->GetType() == SMDSAbs_Node) {
230             aRec.NodeList.resize(aGroupDS->Extent());
231             i = 0;
232             while (aIter->more()) {
233               const SMDS_MeshElement* aElem = aIter->next();
234               aRec.NodeList[i] = aElem->GetID(); 
235               i++;
236             }
237           } else {
238             aRec.ElementList.resize(aGroupDS->Extent());
239             i = 0;
240             while (aIter->more()) {
241               const SMDS_MeshElement* aElem = aIter->next();
242               aRec.ElementList[i] = aElem->GetID(); 
243               i++;
244             }
245           }
246           aDataSet2417.insert(TDataSet::value_type(aGroupDS->GetID(), aRec));
247         }
248       }
249       UNV2417::Write(out_stream,aDataSet2417);
250       }*/
251
252     out_stream.flush();
253     out_stream.close();
254     if (!check_file(myFile))
255       EXCEPTION(runtime_error,"ERROR: Output file not good.");
256   }
257   catch(const std::exception& exc){
258     INFOS("Follow exception was cought:\n\t"<<exc.what());
259     throw;
260   }
261   catch(...){
262     INFOS("Unknown exception was cought !!!");
263     throw;
264   }
265   return aResult;
266 }