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