Salome HOME
Update copyright
[modules/smesh.git] / src / DriverUNV / DriverUNV_W_SMDS_Mesh.cxx
1 // Copyright (C) 2007-2011  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 "UNV2411_Structure.hxx"
36 #include "UNV2412_Structure.hxx"
37 #include "UNV2417_Structure.hxx"
38 #include "UNV_Utilities.hxx"
39
40 #include <Basics_Utils.hxx>
41
42 using namespace std;
43 using namespace UNV;
44
45 namespace{
46   typedef std::vector<size_t> TConnect;
47
48   int GetConnect(const SMDS_ElemIteratorPtr& theNodesIter, 
49                  TConnect& theConnect)
50   {
51     theConnect.clear();
52     for(; theNodesIter->more();){
53       const SMDS_MeshElement* anElem = theNodesIter->next();
54       theConnect.push_back(anElem->GetID());
55     }
56     return theConnect.size();
57   }
58   
59 }
60
61 Driver_Mesh::Status DriverUNV_W_SMDS_Mesh::Perform()
62 {
63   Kernel_Utils::Localizer loc;
64   Status aResult = DRS_OK;
65   std::ofstream out_stream(myFile.c_str());
66   try{
67     {
68       using namespace UNV2411;
69       TDataSet aDataSet2411;
70       // Storing SMDS nodes to the UNV file
71       //-----------------------------------
72       MESSAGE("Perform - myMesh->NbNodes() = "<<myMesh->NbNodes());
73       SMDS_NodeIteratorPtr aNodesIter = myMesh->nodesIterator();
74       for(; aNodesIter->more();){
75         const SMDS_MeshNode* aNode = aNodesIter->next();
76         TRecord aRec;
77         aRec.coord[0] = aNode->X();
78         aRec.coord[1] = aNode->Y();
79         aRec.coord[2] = aNode->Z();
80         const TNodeLab& aLabel = aNode->GetID();
81         aDataSet2411.insert(TDataSet::value_type(aLabel,aRec));
82       }
83       MESSAGE("Perform - aDataSet2411.size() = "<<aDataSet2411.size());
84       UNV2411::Write(out_stream,aDataSet2411);
85     }
86     {
87       using namespace UNV2412;
88       TDataSet aDataSet2412;
89       TConnect aConnect;
90
91       // Storing SMDS Edges
92       MESSAGE("Perform - myMesh->NbEdges() = "<<myMesh->NbEdges());
93       if(myMesh->NbEdges()){
94         SMDS_EdgeIteratorPtr anIter = myMesh->edgesIterator();
95         for(; anIter->more();){
96           const SMDS_MeshEdge* anElem = anIter->next();
97           TElementLab aLabel = anElem->GetID();
98           int aNbNodes = anElem->NbNodes();
99           TRecord aRec;
100           aRec.node_labels.reserve(aNbNodes);
101           SMDS_ElemIteratorPtr aNodesIter;
102           aNodesIter = anElem->nodesIteratorToUNV();
103           if( anElem->IsQuadratic() ) {
104             aRec.fe_descriptor_id = 22;
105           } else {
106             aRec.fe_descriptor_id = 11;
107           }
108           for(; aNodesIter->more();){
109             const SMDS_MeshElement* aNode = aNodesIter->next();
110             aRec.node_labels.push_back(aNode->GetID());
111           }
112           aDataSet2412.insert(TDataSet::value_type(aLabel,aRec));
113         }
114         MESSAGE("Perform - aDataSet2412.size() = "<<aDataSet2412.size());
115       }
116
117       MESSAGE("Perform - myMesh->NbFaces() = "<<myMesh->NbFaces());
118       if(myMesh->NbFaces()){
119         SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
120         for(; anIter->more();){
121           const SMDS_MeshFace* anElem = anIter->next();
122           TElementLab aLabel = anElem->GetID();
123           int aNbNodes = anElem->NbNodes();
124           TRecord aRec;
125           aRec.node_labels.reserve(aNbNodes);
126           SMDS_ElemIteratorPtr aNodesIter;
127           aNodesIter = anElem->nodesIteratorToUNV();
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           //MESSAGE("aNbNodes="<<aNbNodes);
162           SMDS_ElemIteratorPtr aNodesIter;
163           aNodesIter = anElem->nodesIteratorToUNV();
164           if ( anElem->IsPoly() ) {
165             MESSAGE("anElem->IsPoly");
166             if ( const SMDS_VtkVolume* ph =
167                  dynamic_cast<const SMDS_VtkVolume*> (anElem))
168             {
169               aNbNodes = ph->NbUniqueNodes();
170               aNodesIter = ph->uniqueNodesIterator();
171             }
172           }
173
174           int anId = -1;
175           switch(aNbNodes){
176           case 4: {
177             anId = 111;
178             break;
179           }
180           case 6: {
181             anId = 112;
182             break;
183           }
184           case 8: {
185             anId = 115;
186             break;
187           }
188           case 10: {
189             anId = 118;
190             break;
191           }
192           case 13: {
193             anId = 114;
194             break;
195           }
196           case 15: {
197             anId = 113;
198             break;
199           }
200           case 20: {
201             anId = 116;
202             break;
203           }
204           default:
205             continue;
206           }
207           if(anId>0){
208             TRecord aRec;
209             aRec.fe_descriptor_id = anId;
210             aRec.node_labels.reserve(aNbNodes);
211             for(; aNodesIter->more();){
212               const SMDS_MeshElement* aNode = aNodesIter->next();
213               aRec.node_labels.push_back(aNode->GetID());
214             }
215             aDataSet2412.insert(TDataSet::value_type(aLabel,aRec));
216           }
217         }
218         MESSAGE("Perform - aDataSet2412.size() = "<<aDataSet2412.size());
219       }
220       UNV2412::Write(out_stream,aDataSet2412);
221     }
222     {
223       using namespace UNV2417;
224       if (myGroups.size() > 0) {
225         TDataSet aDataSet2417;
226         TGroupList::const_iterator aIter = myGroups.begin();
227         for (; aIter != myGroups.end(); aIter++) {
228           SMESHDS_GroupBase* aGroupDS = *aIter;
229           TRecord aRec;
230           aRec.GroupName = aGroupDS->GetStoreName();
231
232           int i;
233           SMDS_ElemIteratorPtr aIter = aGroupDS->GetElements();
234           if (aGroupDS->GetType() == SMDSAbs_Node) {
235             aRec.NodeList.resize(aGroupDS->Extent());
236             i = 0;
237             while (aIter->more()) {
238               const SMDS_MeshElement* aElem = aIter->next();
239               aRec.NodeList[i] = aElem->GetID(); 
240               i++;
241             }
242           } else {
243             aRec.ElementList.resize(aGroupDS->Extent());
244             i = 0;
245             while (aIter->more()) {
246               const SMDS_MeshElement* aElem = aIter->next();
247               aRec.ElementList[i] = aElem->GetID(); 
248               i++;
249             }
250           }
251           // 0019936: EDF 794 SMESH : Export UNV : Node color and group id
252           //aDataSet2417.insert(TDataSet::value_type(aGroupDS->GetID(), aRec));
253           aDataSet2417.insert(TDataSet::value_type(aGroupDS->GetID()+1, aRec));
254         }
255         UNV2417::Write(out_stream,aDataSet2417);
256         myGroups.clear();
257       }
258     }
259     /*    {
260       using namespace UNV2417;
261       TDataSet aDataSet2417;
262       for ( TGroupsMap::iterator it = myGroupsMap.begin(); it != myGroupsMap.end(); it++ ) {
263         SMESH_Group*       aGroup   = it->second;
264         SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
265         if ( aGroupDS ) {
266           TRecord aRec;
267           aRec.GroupName = aGroup->GetName();
268           int i;
269           SMDS_ElemIteratorPtr aIter = aGroupDS->GetElements();
270           if (aGroupDS->GetType() == SMDSAbs_Node) {
271             aRec.NodeList.resize(aGroupDS->Extent());
272             i = 0;
273             while (aIter->more()) {
274               const SMDS_MeshElement* aElem = aIter->next();
275               aRec.NodeList[i] = aElem->GetID(); 
276               i++;
277             }
278           } else {
279             aRec.ElementList.resize(aGroupDS->Extent());
280             i = 0;
281             while (aIter->more()) {
282               const SMDS_MeshElement* aElem = aIter->next();
283               aRec.ElementList[i] = aElem->GetID(); 
284               i++;
285             }
286           }
287           aDataSet2417.insert(TDataSet::value_type(aGroupDS->GetID(), aRec));
288         }
289       }
290       UNV2417::Write(out_stream,aDataSet2417);
291       }*/
292
293     out_stream.flush();
294     out_stream.close();
295     if (!check_file(myFile))
296       EXCEPTION(runtime_error,"ERROR: Output file not good.");
297   }
298   catch(const std::exception& exc){
299     INFOS("Follow exception was cought:\n\t"<<exc.what());
300     throw;
301   }
302   catch(...){
303     INFOS("Unknown exception was cought !!!");
304     throw;
305   }
306   return aResult;
307 }