Salome HOME
MEDMEM suppression
[modules/med.git] / src / MedClient / src / GMESHClient.cxx
1 // Copyright (C) 2007-2013  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 "GMESHClient.hxx"
24 #include "MEDMEM_convert.hxx"
25 #include "UtilClient.hxx"
26 #include "COORDINATEClient.hxx"
27 #include "CONNECTIVITYClient.hxx"
28 #include "FAMILYClient.hxx"
29 #include "GROUPClient.hxx"
30 #include "MESHClient.hxx"
31 #include "Utils_CorbaException.hxx"
32
33 using namespace MEDMEM;
34 using namespace MED_EN;
35
36 //=============================================================================
37 /*!
38  * Constructeur
39  */
40 //=============================================================================
41
42 GMESHClient::GMESHClient(const SALOME_MED::GMESH_ptr m) : 
43   GMESH(), //_refCounter(1),
44   IOR_Mesh(SALOME_MED::GMESH::_duplicate(m)),
45   _complete(false), _uMesh(0)
46
47 {
48   ASSERT(m);
49
50   blankCopy();
51 }
52 //=============================================================================
53 /*!
54  * Transforme un IOR Famille en Famille Client
55  */
56 //=============================================================================
57
58 FAMILY * convertFamily(const SALOME_MED::FAMILY_ptr &F, GMESH *M) 
59 {
60   return new FAMILYClient(F, M);
61 }
62 //=============================================================================
63 /*!
64  * Transforme un IOR groupe en groupe Client
65  */
66 //=============================================================================
67 GROUP * convertGroup(const SALOME_MED::GROUP_ptr &F, GMESH *M) 
68 {
69   return new GROUPClient(F, M);
70 }
71
72 //=============================================================================
73 /*!
74  * Remplit les informations générales 
75  */
76 //=============================================================================
77 void GMESHClient::blankCopy()
78 {
79   SALOME_MED::GMESH::meshInfos_var all = IOR_Mesh->getMeshGlobal();
80
81   _name           = all->name;
82   _spaceDimension = all->spaceDimension;
83
84   convertCorbaArray<SALOME_MED::FAMILY_ptr>
85     (_familyNode, 
86      //IOR_Mesh->getFamilies(MED_NODE),
87      &all->famNode,
88      (void *) (convertFamily), this);
89  
90   convertCorbaArray<SALOME_MED::FAMILY_ptr>
91     (_familyEdge, 
92      //IOR_Mesh->getFamilies(MED_EDGE),
93      &all->famEdge,
94      (void *) (convertFamily), this);
95  
96   convertCorbaArray<SALOME_MED::FAMILY_ptr>
97     (_familyFace, 
98      //IOR_Mesh->getFamilies(MED_FACE),
99      &all->famFace,
100      (void *) (convertFamily), this);
101  
102   convertCorbaArray<SALOME_MED::FAMILY_ptr>
103     (_familyCell, 
104      //IOR_Mesh->getFamilies(MED_CELL),
105      &all->famCell,
106      (void *) (convertFamily), this);
107  
108   convertCorbaArray<SALOME_MED::GROUP_ptr>
109     (_groupNode, 
110      //IOR_Mesh->getGroups(MED_NODE),
111      &all->groupNode,
112      (void *) (convertGroup), this);
113  
114   convertCorbaArray<SALOME_MED::GROUP_ptr>
115     (_groupEdge, 
116      //IOR_Mesh->getGroups(MED_EDGE),
117      &all->groupEdge,
118      (void *) (convertGroup), this);
119  
120   convertCorbaArray<SALOME_MED::GROUP_ptr>
121     (_groupFace, 
122      //IOR_Mesh->getGroups(MED_FACE),
123      &all->groupFace,
124      (void *) (convertGroup), this);
125  
126   convertCorbaArray<SALOME_MED::GROUP_ptr>
127     (_groupCell, 
128      //IOR_Mesh->getGroups(MED_CELL),
129      &all->groupCell,
130      (void *) (convertGroup), this);
131
132   if ( !IOR_Mesh->getIsAGrid() && !dynamic_cast<MESHClient*>(this))
133   {
134     SALOME_MED::MESH_var umeshIOR = IOR_Mesh->convertInMESH();
135     _uMesh = new MESHClient( umeshIOR );
136   }
137
138   _complete = false;
139 }
140 //=============================================================================
141 /*!
142  * Remplit les informations sur les coordonnees et la connectivite 
143  */
144 //=============================================================================
145
146 void GMESHClient::fillCopy()
147 {
148   int size = _familyNode.size();
149
150   for (int i = 0; i < size; i++)
151     {
152       FAMILYClient * _fam = dynamic_cast<FAMILYClient *> (_familyNode[i]);
153       ASSERT(_fam);
154
155       _fam->fillCopy();
156     }
157
158   size = _familyCell.size();
159
160   for (int i = 0; i < size; i++)
161     {
162       FAMILYClient * _fam = dynamic_cast<FAMILYClient *> (_familyCell[i]);
163       ASSERT(_fam);
164
165       _fam->fillCopy();
166     }
167
168   size = _familyFace.size();
169
170   for (int i = 0; i < size; i++)
171     {
172       FAMILYClient * _fam = dynamic_cast<FAMILYClient *> (_familyFace[i]);
173       ASSERT(_fam);
174
175       _fam->fillCopy();
176     }
177
178   size = _familyEdge.size();
179
180   for (int i = 0; i < size; i++)
181     {
182       FAMILYClient * _fam = dynamic_cast<FAMILYClient *> (_familyEdge[i]);
183       ASSERT(_fam);
184
185       _fam->fillCopy();
186     }
187
188   if ( _uMesh )
189     _uMesh->fillCopy();
190   else
191     THROW_SALOME_CORBA_EXCEPTION("GRID client does not exists",SALOME::INTERNAL_ERROR);
192
193   _complete = true;
194 }
195
196 //=============================================================================
197 /*!
198  *  Test equality between 2 GMESHClients.
199  */
200 //=============================================================================
201 bool GMESHClient::operator==(const GMESH& other) const
202 {
203   const GMESHClient* otherClt=dynamic_cast<const GMESHClient *>(&other);
204   if(otherClt)
205     {
206       if(this==otherClt)
207         {
208           return true;
209         }
210       return IOR_Mesh->areEquals(otherClt->IOR_Mesh);
211     }
212   return false;
213 }
214
215 //=============================================================================
216 /*!
217  * Destructeur
218  */
219 //=============================================================================
220
221 GMESHClient::~GMESHClient()
222 {
223   if ( _uMesh )
224     _uMesh->removeReference();
225   IOR_Mesh->UnRegister();
226 }
227
228 //=============================================================================
229 /*!
230  * Write all the content of the GMESH using driver referenced by the integer handler index
231  */
232 //=============================================================================
233
234 void GMESHClient::write(int index/*=0*/) const
235 {
236   if ( index < 0 || index >= (int)_drivers.size() || !_drivers[index] )
237     throw MED_EXCEPTION ( LOCALIZED( STRING("GMESHClient::write(int index): ")
238                                      << "The index given is invalid, index must be between  0 and |"
239                                      << _drivers.size() ));
240
241   const_cast<GMESHClient*>(this)->fillCopy();
242   _uMesh->write( *_drivers[index], _drivers[index]->getAccessMode() );
243 }
244
245 //=============================================================================
246 /*!
247  * Write all the content of the GMESH using genDriver
248  */
249 //=============================================================================
250
251 void GMESHClient::write(const GENDRIVER &      genDriver,
252                         MED_EN::med_mode_acces medMode) const
253 {
254   const_cast<GMESHClient*>(this)->fillCopy();
255   _uMesh->write(genDriver,medMode);
256 }
257
258 //=============================================================================
259 /*!
260  * Write all the content of the GMESH
261  */
262 //=============================================================================
263
264 void GMESHClient::write(driverTypes        driverType,
265                         const std::string& filename,
266                         const std::string& meshname,
267                         MED_EN::med_mode_acces medMode) const
268 {
269   const_cast<GMESHClient*>(this)->fillCopy();
270   _uMesh->write(driverType, filename,meshname,medMode);
271 }
272
273 //================================================================================
274 /*!
275  * \brief fillCopy() and then print myself to a stream
276  */
277 //================================================================================
278
279 void GMESHClient::printMySelf(ostream &os) const
280 {
281   GMESHClient* that = (GMESHClient*)this;
282   that->fillCopy();
283   if ( _uMesh )
284     _uMesh->printMySelf( os );
285   else
286     os << "GMESHClient " << (void*) this;
287 }
288
289 bool GMESHClient::isEmpty() const
290 {
291   return _uMesh ? _uMesh->isEmpty() : (IOR_Mesh->getNumberOfNodes() < 1);
292 }
293
294 bool GMESHClient::deepCompare(const GMESH& other) const
295 {
296   if ( getIsAGrid() != other.getIsAGrid() ||
297        getIsAGrid()) // no client nor server for GRID -> can't compare
298     return false;
299
300   return _uMesh ? _uMesh->deepCompare( other ) : false;
301 }
302
303 int GMESHClient::getMeshDimension() const
304 {
305   return IOR_Mesh->getMeshDimension();
306 }
307
308 bool GMESHClient::getIsAGrid() const
309 {
310   return IOR_Mesh->getIsAGrid();
311 }
312
313 std::string GMESHClient::getCoordinatesSystem() const
314 {
315   CORBA::String_var s = IOR_Mesh->getCoordinatesSystem();
316   return s._retn();
317 }
318
319 const std::string* GMESHClient::getCoordinatesNames() const
320 {
321   return _uMesh ? _uMesh->getCoordinatesNames() : (std::string*) 0;
322 }
323
324 const std::string* GMESHClient::getCoordinatesUnits() const
325 {
326   return _uMesh ? _uMesh->getCoordinatesUnits() : (std::string*) 0;
327 }
328
329 int GMESHClient::getNumberOfNodes() const
330 {
331   return IOR_Mesh->getNumberOfNodes();
332 }
333
334 int GMESHClient::getNumberOfTypes(MED_EN::medEntityMesh entity) const
335 {
336   return IOR_Mesh->getNumberOfTypes(entity);
337 }
338
339 int GMESHClient::getNumberOfElements(MED_EN::medEntityMesh entity,
340                                      MED_EN::medGeometryElement type) const
341 {
342   return IOR_Mesh->getNumberOfElements(entity,type);
343 }
344
345 const MED_EN::medGeometryElement* GMESHClient::getTypes(MED_EN::medEntityMesh entity) const
346 {
347   return _uMesh ? _uMesh->getTypes(entity) : (MED_EN::medGeometryElement*) 0;
348 }
349
350 MED_EN::medGeometryElement GMESHClient::getElementType(MED_EN::medEntityMesh entity,
351                                                        int i) const
352 {
353   return IOR_Mesh->getElementType(entity,i);
354 }
355
356 const MESH* GMESHClient::convertInMESH() const
357 {
358   if ( !_uMesh )
359   {
360     SALOME_MED::MESH_var umeshIOR = IOR_Mesh->convertInMESH();
361     const_cast<GMESHClient*>(this)->_uMesh = new MESHClient( umeshIOR );
362   }
363   _uMesh->addReference();
364   return _uMesh;
365 }
366
367 SUPPORT* GMESHClient::getBoundaryElements(MED_EN::medEntityMesh entity) const throw (MEDEXCEPTION)
368 {
369   SALOME_MED::SUPPORT_var s = IOR_Mesh->getBoundaryElements(entity);
370   return new SUPPORTClient( s );
371 }
372
373 SUPPORT* GMESHClient::getSkin(const SUPPORT* sup) throw (MEDEXCEPTION)
374 {
375   return _uMesh ? _uMesh->getSkin( sup ) : (SUPPORT*)0;
376 }
377
378 SUPPORT* GMESHClient::buildSupportOnNodeFromElementList(const std::list<int>& elems,
379                                                         MED_EN::medEntityMesh entity) const
380    throw (MEDEXCEPTION)
381 {
382   return _uMesh ? _uMesh->buildSupportOnNodeFromElementList(elems,entity ) : (SUPPORT*)0;
383 }
384
385 void GMESHClient::fillSupportOnNodeFromElementList(const std::list<int>& elems,
386                                                    SUPPORT* sup) const
387    throw (MEDEXCEPTION)
388 {
389   if ( _uMesh ) _uMesh->fillSupportOnNodeFromElementList(elems,sup);
390 }
391
392 FIELD<double>* GMESHClient::getVolume(const SUPPORT* sup , bool isAbs) const
393   throw (MEDEXCEPTION)
394 {
395   FIELD<double>* f = (FIELD<double>*) 0;
396   if ( _uMesh && sup )
397   {
398     sup->setMesh( _uMesh );
399     f = _uMesh->getVolume(sup,isAbs);
400     sup->setMesh( this );
401   }
402   return f;
403 }
404
405 FIELD<double>* GMESHClient::getArea(const SUPPORT* sup) const throw (MEDEXCEPTION)
406 {
407   FIELD<double>* f = (FIELD<double>*) 0;
408   if ( _uMesh && sup )
409   {
410     sup->setMesh( _uMesh );
411     f = _uMesh->getArea(sup);
412     sup->setMesh( this );
413   }
414   return f;
415 }
416
417 FIELD<double>* GMESHClient::getLength(const SUPPORT* sup) const throw (MEDEXCEPTION)
418 {
419   FIELD<double>* f = (FIELD<double>*) 0;
420   if ( _uMesh && sup )
421   {
422     sup->setMesh( _uMesh );
423     f = _uMesh->getLength(sup);
424     sup->setMesh( this );
425   }
426   return f;
427 }
428
429 FIELD<double>* GMESHClient::getNormal(const SUPPORT* sup) const throw (MEDEXCEPTION)
430 {
431   FIELD<double>* f = (FIELD<double>*) 0;
432   if ( _uMesh && sup )
433   {
434     sup->setMesh( _uMesh );
435     f = _uMesh->getNormal(sup);
436     sup->setMesh( this );
437   }
438   return f;
439 }
440
441 FIELD<double>* GMESHClient::getBarycenter(const SUPPORT* sup) const throw (MEDEXCEPTION)
442 {
443   FIELD<double>* f = (FIELD<double>*) 0;
444   if ( _uMesh && sup )
445   {
446     sup->setMesh( _uMesh );
447     f = _uMesh->getBarycenter(sup);
448     sup->setMesh( this );
449   }
450   return f;
451 }
452
453 std::vector<std::vector<double> > GMESHClient::getBoundingBox() const
454 {
455   std::vector<std::vector<double> > bb;
456   if ( _uMesh )
457     bb = _uMesh->getBoundingBox();
458   return bb;
459 }
460