]> SALOME platform Git repositories - modules/smesh.git/blob - src/DriverUNV/DriverUNV_W_SMDS_Mesh.cxx
Salome HOME
Bug IPAL21435 - TC5.1.3: incorrect mapping of the object from imported UNV file in...
[modules/smesh.git] / src / DriverUNV / DriverUNV_W_SMDS_Mesh.cxx
1 //  Copyright (C) 2007-2008  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 #include <algorithm>
23
24 #include "DriverUNV_W_SMDS_Mesh.h"
25
26 #include "SMDS_Mesh.hxx"
27 #include "SMDS_QuadraticEdge.hxx"
28 #include "SMDS_QuadraticFaceOfNodes.hxx"
29 #include "SMDS_PolyhedralVolumeOfNodes.hxx"
30 #include "SMESHDS_GroupBase.hxx"
31
32 #include "utilities.h"
33
34 #include "UNV2411_Structure.hxx"
35 #include "UNV2412_Structure.hxx"
36 #include "UNV2417_Structure.hxx"
37 #include "UNV_Utilities.hxx"
38
39 using namespace std;
40 using namespace UNV;
41
42 namespace{
43   typedef std::vector<size_t> TConnect;
44
45   int GetConnect(const SMDS_ElemIteratorPtr& theNodesIter, 
46                  TConnect& theConnect)
47   {
48     theConnect.clear();
49     for(; theNodesIter->more();){
50       const SMDS_MeshElement* anElem = theNodesIter->next();
51       theConnect.push_back(anElem->GetID());
52     }
53     return theConnect.size();
54   }
55   
56 }
57
58 Driver_Mesh::Status DriverUNV_W_SMDS_Mesh::Perform()
59 {
60   UNV::Localizer loc;
61   Status aResult = DRS_OK;
62   std::ofstream out_stream(myFile.c_str());
63   try{
64     {
65       using namespace UNV2411;
66       TDataSet aDataSet2411;
67       // Storing SMDS nodes to the UNV file
68       //-----------------------------------
69       MESSAGE("Perform - myMesh->NbNodes() = "<<myMesh->NbNodes());
70       SMDS_NodeIteratorPtr aNodesIter = myMesh->nodesIterator();
71       for(; aNodesIter->more();){
72         const SMDS_MeshNode* aNode = aNodesIter->next();
73         TRecord aRec;
74         aRec.coord[0] = aNode->X();
75         aRec.coord[1] = aNode->Y();
76         aRec.coord[2] = aNode->Z();
77         const TNodeLab& aLabel = aNode->GetID();
78         aDataSet2411.insert(TDataSet::value_type(aLabel,aRec));
79       }
80       MESSAGE("Perform - aDataSet2411.size() = "<<aDataSet2411.size());
81       UNV2411::Write(out_stream,aDataSet2411);
82     }
83     {
84       using namespace UNV2412;
85       TDataSet aDataSet2412;
86       TConnect aConnect;
87
88       // Storing SMDS Edges
89       MESSAGE("Perform - myMesh->NbEdges() = "<<myMesh->NbEdges());
90       if(myMesh->NbEdges()){
91         SMDS_EdgeIteratorPtr anIter = myMesh->edgesIterator();
92         for(; anIter->more();){
93           const SMDS_MeshEdge* anElem = anIter->next();
94           TElementLab aLabel = anElem->GetID();
95           int aNbNodes = anElem->NbNodes();
96           TRecord aRec;
97           aRec.node_labels.reserve(aNbNodes);
98           SMDS_ElemIteratorPtr aNodesIter;
99           if( anElem->IsQuadratic() ) {
100             aNodesIter = static_cast<const SMDS_QuadraticEdge* >
101               ( anElem )->interlacedNodesElemIterator();
102             aRec.fe_descriptor_id = 22;
103           } else {
104             aNodesIter = anElem->nodesIterator();
105             aRec.fe_descriptor_id = 11;
106           }
107           for(; aNodesIter->more();){
108             const SMDS_MeshElement* aNode = aNodesIter->next();
109             aRec.node_labels.push_back(aNode->GetID());
110           }
111           aDataSet2412.insert(TDataSet::value_type(aLabel,aRec));
112         }
113         MESSAGE("Perform - aDataSet2412.size() = "<<aDataSet2412.size());
114       }
115
116       MESSAGE("Perform - myMesh->NbFaces() = "<<myMesh->NbFaces());
117       if(myMesh->NbFaces()){
118         SMDS_FaceIteratorPtr anIter = myMesh->facesIterator();
119         for(; anIter->more();){
120           const SMDS_MeshFace* anElem = anIter->next();
121           TElementLab aLabel = anElem->GetID();
122           int aNbNodes = anElem->NbNodes();
123           TRecord aRec;
124           aRec.node_labels.reserve(aNbNodes);
125           SMDS_ElemIteratorPtr aNodesIter;
126           if( anElem->IsQuadratic() )
127             aNodesIter = static_cast<const SMDS_QuadraticFaceOfNodes* >
128               ( anElem )->interlacedNodesElemIterator();
129           else
130             aNodesIter = anElem->nodesIterator();
131           for(; aNodesIter->more();){
132             const SMDS_MeshElement* aNode = aNodesIter->next();
133             aRec.node_labels.push_back(aNode->GetID());
134           }
135           switch(aNbNodes){
136           case 3:
137             aRec.fe_descriptor_id = 41;
138             break;
139           case 4:
140             aRec.fe_descriptor_id = 44;
141             break;
142           case 6:
143             aRec.fe_descriptor_id = 42;
144             break;
145           case 8:
146             aRec.fe_descriptor_id = 45;
147             break;
148           default:
149             continue;
150           }
151           aDataSet2412.insert(TDataSet::value_type(aLabel,aRec));
152         }
153         MESSAGE("Perform - aDataSet2412.size() = "<<aDataSet2412.size());
154       }
155
156       MESSAGE("Perform - myMesh->NbVolumes() = "<<myMesh->NbVolumes());
157       if(myMesh->NbVolumes()){
158         SMDS_VolumeIteratorPtr anIter = myMesh->volumesIterator();
159         for(; anIter->more();){
160           const SMDS_MeshVolume* anElem = anIter->next();
161           TElementLab aLabel = anElem->GetID();
162
163           int aNbNodes = anElem->NbNodes();
164           SMDS_ElemIteratorPtr aNodesIter = anElem->nodesIterator();
165           if ( anElem->IsPoly() ) {
166             if ( const SMDS_PolyhedralVolumeOfNodes* ph =
167                  dynamic_cast<const SMDS_PolyhedralVolumeOfNodes*> (anElem))
168             {
169               aNbNodes = ph->NbUniqueNodes();
170               aNodesIter = ph->uniqueNodesIterator();
171             }
172           }
173           aConnect.resize(aNbNodes);
174           GetConnect(aNodesIter,aConnect);
175
176           int anId = -1;
177           int* aConn = NULL;
178           switch(aNbNodes){
179           case 4: {
180             static int anIds[] = {0,2,1,3};
181             aConn = anIds;
182             anId = 111;
183             break;
184           }
185           case 6: {
186             static int anIds[] = {0,2,1,3,5,4};
187             aConn = anIds;
188             anId = 112;
189             break;
190           }
191           case 8: {
192             static int anIds[] = {0,3,2,1,4,7,6,5};
193             aConn = anIds;
194             anId = 115;
195             break;
196           }
197           case 10: {
198             static int anIds[] = {0,4,2,9,5,3, 1,6,8, 7};
199             aConn = anIds;
200             anId = 118;
201             break;
202           }
203           case 13: {
204             static int anIds[] = {0,6,4,2,7,5,3,1,8,11,10,9,12};
205             aConn = anIds;
206             anId = 114;
207             break;
208           }
209           case 15: {
210             static int anIds[] = {0,4,2,9,13,11,5,3,1,14,12,10,6,8,7};
211             aConn = anIds;
212             anId = 113;
213             break;
214           }
215           case 20: {
216             static int anIds[] = {0,6, 4,2, 12,18,16,14,7, 5, 3, 1, 19,17,15,13,8, 11,10,9};
217             aConn = anIds;
218             anId = 116;
219             break;
220           }
221           default:
222             continue;
223           }
224           if(aConn){
225             TRecord aRec;
226             aRec.fe_descriptor_id = anId;
227             aRec.node_labels.resize(aNbNodes);
228             for(int aNodeId = 0; aNodeId < aNbNodes; aNodeId++){
229               aRec.node_labels[aConn[aNodeId]] = aConnect[aNodeId];
230             }
231             aDataSet2412.insert(TDataSet::value_type(aLabel,aRec));
232           }
233         }
234         MESSAGE("Perform - aDataSet2412.size() = "<<aDataSet2412.size());
235       }
236       UNV2412::Write(out_stream,aDataSet2412);
237     }
238     {
239       using namespace UNV2417;
240       if (myGroups.size() > 0) {
241         TDataSet aDataSet2417;
242         TGroupList::const_iterator aIter = myGroups.begin();
243         for (; aIter != myGroups.end(); aIter++) {
244           SMESHDS_GroupBase* aGroupDS = *aIter;
245           TRecord aRec;
246           aRec.GroupName = aGroupDS->GetStoreName();
247
248           int i;
249           SMDS_ElemIteratorPtr aIter = aGroupDS->GetElements();
250           if (aGroupDS->GetType() == SMDSAbs_Node) {
251             aRec.NodeList.resize(aGroupDS->Extent());
252             i = 0;
253             while (aIter->more()) {
254               const SMDS_MeshElement* aElem = aIter->next();
255               aRec.NodeList[i] = aElem->GetID(); 
256               i++;
257             }
258           } else {
259             aRec.ElementList.resize(aGroupDS->Extent());
260             i = 0;
261             while (aIter->more()) {
262               const SMDS_MeshElement* aElem = aIter->next();
263               aRec.ElementList[i] = aElem->GetID(); 
264               i++;
265             }
266           }
267           // 0019936: EDF 794 SMESH : Export UNV : Node color and group id
268           //aDataSet2417.insert(TDataSet::value_type(aGroupDS->GetID(), aRec));
269           aDataSet2417.insert(TDataSet::value_type(aGroupDS->GetID()+1, aRec));
270         }
271         UNV2417::Write(out_stream,aDataSet2417);
272         myGroups.clear();
273       }
274     }
275     /*    {
276       using namespace UNV2417;
277       TDataSet aDataSet2417;
278       for ( TGroupsMap::iterator it = myGroupsMap.begin(); it != myGroupsMap.end(); it++ ) {
279         SMESH_Group*       aGroup   = it->second;
280         SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
281         if ( aGroupDS ) {
282           TRecord aRec;
283           aRec.GroupName = aGroup->GetName();
284           int i;
285           SMDS_ElemIteratorPtr aIter = aGroupDS->GetElements();
286           if (aGroupDS->GetType() == SMDSAbs_Node) {
287             aRec.NodeList.resize(aGroupDS->Extent());
288             i = 0;
289             while (aIter->more()) {
290               const SMDS_MeshElement* aElem = aIter->next();
291               aRec.NodeList[i] = aElem->GetID(); 
292               i++;
293             }
294           } else {
295             aRec.ElementList.resize(aGroupDS->Extent());
296             i = 0;
297             while (aIter->more()) {
298               const SMDS_MeshElement* aElem = aIter->next();
299               aRec.ElementList[i] = aElem->GetID(); 
300               i++;
301             }
302           }
303           aDataSet2417.insert(TDataSet::value_type(aGroupDS->GetID(), aRec));
304         }
305       }
306       UNV2417::Write(out_stream,aDataSet2417);
307       }*/
308
309     out_stream.flush();
310     out_stream.close();
311     if (!check_file(myFile))
312       EXCEPTION(runtime_error,"ERROR: Output file not good.");
313   }
314   catch(const std::exception& exc){
315     INFOS("Follow exception was cought:\n\t"<<exc.what());
316     throw;
317   }
318   catch(...){
319     INFOS("Unknown exception was cought !!!");
320     throw;
321   }
322   return aResult;
323 }