Salome HOME
Merge branch 'V9_9_BR'
[modules/smesh.git] / src / DriverDAT / DriverDAT_W_SMDS_Mesh.cxx
1 // Copyright (C) 2007-2022  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 <stdio.h>
24
25 #include "DriverDAT_W_SMDS_Mesh.h"
26
27 #include "SMDS_Mesh.hxx"
28
29 #include "utilities.h"
30
31 #include <Basics_Utils.hxx>
32
33 using namespace std;
34
35 //================================================================================
36 /*!
37  * \brief Write mesh data to a file
38  *  \param [in] renumber - if true, renumber nodes and cell starting from 1
39  *  \return Driver_Mesh::Status - Ok or not
40  */
41 //================================================================================
42
43 Driver_Mesh::Status DriverDAT_W_SMDS_Mesh::Perform()
44 {
45   Kernel_Utils::Localizer loc;
46   Status aResult = DRS_OK;
47
48   smIdType nbNodes, nbCells;
49 #if defined(WIN32) && defined(UNICODE)
50   std::wstring file2Read = Kernel_Utils::utf8_decode_s(myFile);
51   FILE* aFileId = _wfopen(file2Read.c_str(), L"w+");
52
53 #else
54   char *file2Read = (char *)myFile.c_str();
55   FILE* aFileId = fopen(file2Read, "w+");
56 #endif
57   if ( !aFileId )
58   {
59     fprintf(stderr, ">> ERREUR : ouverture du fichier %s \n", file2Read);
60     return DRS_FAIL;
61   }
62   SCRUTE(myMesh);
63   /****************************************************************************
64    *                       NOMBRES D'OBJETS                                    *
65    ****************************************************************************/
66
67   /* Combien de noeuds ? */
68   nbNodes = myMesh->NbNodes();
69
70   /* Combien de mailles, faces ou aretes ? */
71   smIdType nb_of_edges, nb_of_faces, nb_of_volumes;
72   nb_of_edges = myMesh->NbEdges();
73   nb_of_faces = myMesh->NbFaces();
74   nb_of_volumes = myMesh->NbVolumes();
75   nbCells = nb_of_edges + nb_of_faces + nb_of_volumes;
76   SCRUTE(nb_of_edges);
77   SCRUTE(nb_of_faces);
78   SCRUTE(nb_of_volumes);
79
80   //fprintf(stdout, "%d %d\n", nbNodes, nbCells);
81   fprintf(aFileId, "%ld %ld\n", static_cast< long >( nbNodes ), static_cast< long >( nbCells ));
82
83   /****************************************************************************
84    *                       ECRITURE DES NOEUDS                                 *
85    ****************************************************************************/
86
87   std::vector< size_t > nodeNumByID;
88   if ( myRenumber && myMesh->HasNumerationHoles() )
89     nodeNumByID.resize( myMesh->MaxNodeID() + 1 );
90
91   smIdType num;
92   SMDS_NodeIteratorPtr itNodes=myMesh->nodesIterator();
93   for ( num = 1; itNodes->more(); ++num )
94   {
95     const SMDS_MeshNode * node = itNodes->next();
96     if ( !myRenumber )
97       num = node->GetID();
98
99     fprintf(aFileId, "%d %.14e %.14e %.14e\n", num, node->X(), node->Y(), node->Z());
100
101     if ( !nodeNumByID.empty() )
102       nodeNumByID[ node->GetID() ] = num;
103   }
104
105   /****************************************************************************
106    *                       ECRITURE DES ELEMENTS                                *
107    ****************************************************************************/
108   /* Ecriture des connectivites, noms, numeros des mailles */
109
110   num = 1;
111   for ( SMDS_EdgeIteratorPtr itEdges = myMesh->edgesIterator(); itEdges->more(); ++num )
112   {
113     const SMDS_MeshElement * elem = itEdges->next();
114     if ( !myRenumber )
115       num = elem->GetID();
116
117     fprintf(aFileId, "%d %d ", num, 100 + elem->NbNodes());
118
119     for ( SMDS_ElemIteratorPtr it = elem->nodesIterator(); it->more(); )
120     {
121       smIdType nodeID = it->next()->GetID();
122       if ( !nodeNumByID.empty() )
123         nodeID = nodeNumByID[ nodeID ];
124       fprintf(aFileId, "%ld ", static_cast< long >( nodeID ));
125     }
126     fprintf(aFileId, "\n");
127   }
128
129   for ( SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator(); itFaces->more(); ++num )
130   {
131     const SMDS_MeshElement * elem = itFaces->next();
132     if ( !myRenumber )
133       num = elem->GetID();
134
135     fprintf(aFileId, "%d %d ", num, (elem->IsPoly() ? 400 : 200 ) + elem->NbNodes() );
136
137     for( SMDS_ElemIteratorPtr it = elem->nodesIterator(); it->more(); )
138     {
139       smIdType nodeID = it->next()->GetID();
140       if ( !nodeNumByID.empty() )
141         nodeID = nodeNumByID[ nodeID ];
142       fprintf(aFileId, "%ld ", static_cast< long >( nodeID ));
143     }
144     fprintf(aFileId, "\n");
145   }
146
147
148   const SMDS_MeshVolume* v;
149   for ( SMDS_VolumeIteratorPtr itVolumes=myMesh->volumesIterator(); itVolumes->more(); ++num )
150   {
151     const SMDS_MeshElement * elem = itVolumes->next();
152     if ( !myRenumber )
153       num = elem->GetID();
154
155     if ( elem->IsPoly() )
156     {
157       fprintf(aFileId, "%d %d ", num, 500 + elem->NbNodes());
158
159       if (( v = myMesh->DownCast< SMDS_MeshVolume >( elem )))
160       {
161         std::vector<int> quant = v->GetQuantities();
162         if ( !quant.empty() )
163         {
164           fprintf(aFileId, "%d %d ", (int)quant.size(), quant[0]);
165           for ( size_t i = 1; i < quant.size(); ++i )
166             fprintf(aFileId, "%d ", quant[i]);
167         }
168       }
169     }
170     else
171     {
172       fprintf(aFileId, "%d %d ", num, 300 + elem->NbNodes());
173     }
174
175     for( SMDS_ElemIteratorPtr it = elem->nodesIterator(); it->more(); )
176     {
177       smIdType nodeID = it->next()->GetID();
178       if ( !nodeNumByID.empty() )
179         nodeID = nodeNumByID[ nodeID ];
180       fprintf(aFileId, "%ld ", static_cast< long >( nodeID ));
181     }
182
183     fprintf(aFileId, "\n");
184   }
185
186   fclose(aFileId);
187
188   return aResult;
189 }