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