Salome HOME
SALOME Forum bug: sub-mesh removal leads to an Exception in a re-opened study.
[modules/smesh.git] / src / SMESH_I / SMESH_subMesh_i.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 //  SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
24 //  File   : SMESH_subMesh_i.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //
28 #include "SMESH_subMesh_i.hxx"
29 #include "SMESH_Gen_i.hxx"
30 #include "SMESH_Mesh_i.hxx"
31 #include "SMESH_PreMeshInfo.hxx"
32
33 #include "Utils_CorbaException.hxx"
34 #include "utilities.h"
35 #include "OpUtil.hxx"
36 #include "Utils_ExceptHandlers.hxx"
37
38 #include <TopoDS_Iterator.hxx>
39 #include <TopExp_Explorer.hxx>
40
41 using namespace std;
42
43 //=============================================================================
44 /*!
45  *  
46  */
47 //=============================================================================
48
49 SMESH_subMesh_i::SMESH_subMesh_i()
50      : SALOME::GenericObj_i( PortableServer::POA::_nil() )
51 {
52   MESSAGE("SMESH_subMesh_i::SMESH_subMesh_i default, not for use");
53   ASSERT(0);
54 }
55
56 //=============================================================================
57 /*!
58  *  
59  */
60 //=============================================================================
61
62 SMESH_subMesh_i::SMESH_subMesh_i( PortableServer::POA_ptr thePOA,
63                                   SMESH_Gen_i*            gen_i,
64                                   SMESH_Mesh_i*           mesh_i,
65                                   int                     localId )
66      : SALOME::GenericObj_i( thePOA )
67 {
68   _gen_i = gen_i;
69   _mesh_i = mesh_i;
70   _localId = localId;
71   _preMeshInfo = NULL;
72 }
73 //=============================================================================
74 /*!
75  *  
76  */
77 //=============================================================================
78
79 SMESH_subMesh_i::~SMESH_subMesh_i()
80 {
81   MESSAGE("SMESH_subMesh_i::~SMESH_subMesh_i");
82   if ( _preMeshInfo ) delete _preMeshInfo;
83   _preMeshInfo = NULL;
84 }
85
86 //=======================================================================
87 //function : getSubMeshes
88 //purpose  : for a submesh on shape to which elements are not bound directly,
89 //           return submeshes containing elements
90 //=======================================================================
91
92 typedef list<SMESHDS_SubMesh*> TListOfSubMeshes;
93
94 bool getSubMeshes(::SMESH_subMesh*  theSubMesh,
95                   TListOfSubMeshes& theSubMeshList)
96 {
97   int size = theSubMeshList.size();
98
99   SMESH_Mesh*      aMesh      = theSubMesh->GetFather();
100   SMESHDS_Mesh*    aMeshDS    = aMesh->GetMeshDS();
101   SMESHDS_SubMesh* aSubMeshDS = theSubMesh->GetSubMeshDS();
102
103   // nodes can be bound to either vertex, edge, face or solid_or_shell
104   TopoDS_Shape aShape = theSubMesh->GetSubShape();
105   switch ( aShape.ShapeType() )
106   {
107   case TopAbs_SOLID: {
108     // add submesh of solid itself
109     aSubMeshDS = aMeshDS->MeshElements( aShape );
110     if ( aSubMeshDS )
111       theSubMeshList.push_back( aSubMeshDS );
112     // and of the first shell
113     TopExp_Explorer exp( aShape, TopAbs_SHELL );
114     if ( exp.More() ) {
115       aSubMeshDS = aMeshDS->MeshElements( exp.Current() );
116       if ( aSubMeshDS )
117         theSubMeshList.push_back( aSubMeshDS );
118     }
119     break;
120   }
121   case TopAbs_WIRE:
122   case TopAbs_COMPOUND:
123   case TopAbs_COMPSOLID: {
124     // call getSubMeshes() for sub-shapes
125     list<TopoDS_Shape> shapeList;
126     shapeList.push_back( aShape );
127     list<TopoDS_Shape>::iterator sh = shapeList.begin();
128     for ( ; sh != shapeList.end(); ++sh ) {
129       for ( TopoDS_Iterator it( *sh ); it.More(); it.Next() ) {
130         if ( ::SMESH_subMesh* aSubMesh = aMesh->GetSubMeshContaining( it.Value() ))
131           getSubMeshes( aSubMesh, theSubMeshList ); // add found submesh or explore deeper
132         else
133           // no submesh for a compound inside compound
134           shapeList.push_back( it.Value() );
135       }
136     }
137     // return only unique submeshes
138     set<SMESHDS_SubMesh*> smSet;
139     TListOfSubMeshes::iterator sm = theSubMeshList.begin();
140     while ( sm != theSubMeshList.end() ) {
141       if ( !smSet.insert( *sm ).second )
142         sm = theSubMeshList.erase( sm );
143       else
144         ++sm;
145     }
146     break;
147   }
148   default:
149     if ( aSubMeshDS )
150       theSubMeshList.push_back( aSubMeshDS );
151   }
152   return size < theSubMeshList.size();
153 }
154
155 //=============================================================================
156 /*!
157  *  
158  */
159 //=============================================================================
160
161 CORBA::Long SMESH_subMesh_i::GetNumberOfElements()
162   throw (SALOME::SALOME_Exception)
163 {
164   Unexpect aCatch(SALOME_SalomeException);
165
166   if ( _preMeshInfo )
167     return _preMeshInfo->NbElements();
168
169   if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
170     return 0;
171
172   ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
173   SMESHDS_SubMesh* aSubMeshDS = aSubMesh->GetSubMeshDS();
174
175   int nbElems = aSubMeshDS ? aSubMeshDS->NbElements() : 0;
176
177   // volumes are bound to shell
178   TListOfSubMeshes smList;
179   if ( nbElems == 0 && getSubMeshes( aSubMesh, smList ))
180   {
181     TListOfSubMeshes::iterator sm = smList.begin();
182     for ( ; sm != smList.end(); ++sm )
183       nbElems += (*sm)->NbElements();
184   }
185   return nbElems;
186 }
187
188 //=============================================================================
189 /*!
190  *  
191  */
192 //=============================================================================
193
194 CORBA::Long SMESH_subMesh_i::GetNumberOfNodes(CORBA::Boolean all)
195   throw (SALOME::SALOME_Exception)
196 {
197   Unexpect aCatch(SALOME_SalomeException);
198
199   if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
200     return 0;
201
202   if ( _preMeshInfo )
203   {
204     if ( all ) return _preMeshInfo->NbNodes();
205     else _preMeshInfo->FullLoadFromFile();
206   }
207   ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
208   SMESHDS_SubMesh* aSubMeshDS = aSubMesh->GetSubMeshDS();
209
210   if ( aSubMeshDS && aSubMeshDS->IsComplexSubmesh() )
211   {
212     // sub-mesh on a geom group, always return all nodes
213     return aSubMeshDS->NbNodes();
214   }
215   if ( aSubMeshDS && !all )
216   {
217     // return anything we have
218     return aSubMeshDS->NbNodes();
219   }
220   if ( all ) // get nodes from aSubMesh and all child sub-meshes
221   {
222     int nbNodes = 0;
223     SMESH_subMeshIteratorPtr smIt = aSubMesh->getDependsOnIterator( /*includeSelf=*/true );
224     while ( smIt->more() )
225     {
226       aSubMesh = smIt->next();
227       if (( aSubMeshDS = aSubMesh->GetSubMeshDS() ))
228         nbNodes += aSubMeshDS->NbNodes();
229     }
230   }
231
232   return aSubMeshDS ? aSubMeshDS->NbNodes() : 0;
233 }
234
235 //=============================================================================
236 /*!
237  *  
238  */
239 //=============================================================================
240
241 SMESH::long_array* SMESH_subMesh_i::GetElementsId()
242   throw (SALOME::SALOME_Exception)
243 {
244   Unexpect aCatch(SALOME_SalomeException);
245
246   SMESH::long_array_var aResult = new SMESH::long_array();
247
248   if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
249     return aResult._retn();
250
251   if ( _preMeshInfo )
252     _preMeshInfo->FullLoadFromFile();
253
254   ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
255   SMESHDS_SubMesh* aSubMeshDS = aSubMesh->GetSubMeshDS();
256
257   int nbElems = aSubMeshDS ? aSubMeshDS->NbElements() : 0;
258   TListOfSubMeshes smList;
259   if ( nbElems )
260     smList.push_back( aSubMeshDS );
261
262   // volumes are bound to shell
263   if ( nbElems == 0 && getSubMeshes( aSubMesh, smList ))
264   {
265     TListOfSubMeshes::iterator sm = smList.begin();
266     for ( ; sm != smList.end(); ++sm )
267       nbElems += (*sm)->NbElements();
268   }
269
270   aResult->length( nbElems );
271   if ( nbElems )
272   {
273     TListOfSubMeshes::iterator sm = smList.begin();
274     for ( int i = 0; sm != smList.end(); sm++ )
275     {
276       SMDS_ElemIteratorPtr anIt = (*sm)->GetElements();
277       for ( ; i < nbElems && anIt->more(); i++ )
278         aResult[i] = anIt->next()->GetID();
279     }
280   }
281   return aResult._retn();
282 }
283
284
285 //=============================================================================
286 /*!
287  *  
288  */
289 //=============================================================================
290
291 SMESH::long_array* SMESH_subMesh_i::GetElementsByType( SMESH::ElementType theElemType )
292     throw (SALOME::SALOME_Exception)
293 {
294   Unexpect aCatch(SALOME_SalomeException);
295
296   SMESH::long_array_var aResult = new SMESH::long_array();
297
298   if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
299     return aResult._retn();
300
301   if ( _preMeshInfo )
302     _preMeshInfo->FullLoadFromFile();
303
304   ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
305   SMESHDS_SubMesh* aSubMeshDS = aSubMesh->GetSubMeshDS();
306
307   // PAL5440, return all nodes belonging to elements of submesh
308   set<int> nodeIds;
309   int nbElems = aSubMeshDS ? aSubMeshDS->NbElements() : 0;
310
311   // volumes may be bound to shell instead of solid
312   TListOfSubMeshes smList;
313   if ( nbElems == 0 && getSubMeshes( aSubMesh, smList ))
314   {
315     TListOfSubMeshes::iterator sm = smList.begin();
316     for ( ; sm != smList.end(); ++sm )
317     {
318       if ( theElemType == SMESH::NODE )
319       {
320         SMDS_ElemIteratorPtr eIt = (*sm)->GetElements();
321         if ( eIt->more() ) {
322           while ( eIt->more() ) {
323             const SMDS_MeshElement* anElem = eIt->next();
324             SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
325             while ( nIt->more() )
326               nodeIds.insert( nIt->next()->GetID() );
327           }
328         } else {
329           SMDS_NodeIteratorPtr nIt = (*sm)->GetNodes();
330           while ( nIt->more() )
331             nodeIds.insert( nIt->next()->GetID() );
332         }
333       }
334       else
335       {
336         nbElems += (*sm)->NbElements();
337       }
338     }
339     aSubMeshDS = 0;
340   }
341   else
342   {
343     if ( nbElems )
344       smList.push_back( aSubMeshDS );
345   }
346
347   if ( theElemType == SMESH::NODE && aSubMeshDS )
348   {
349     SMDS_ElemIteratorPtr eIt = aSubMeshDS->GetElements();
350     if ( eIt->more() ) {
351       while ( eIt->more() ) {
352         const SMDS_MeshElement* anElem = eIt->next();
353         SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
354         while ( nIt->more() )
355           nodeIds.insert( nIt->next()->GetID() );
356       }
357     } else {
358       SMDS_NodeIteratorPtr nIt = aSubMeshDS->GetNodes();
359       while ( nIt->more() )
360         nodeIds.insert( nIt->next()->GetID() );
361     }
362   }
363
364   if ( theElemType == SMESH::NODE )
365     aResult->length( nodeIds.size() );
366   else
367     aResult->length( nbElems );
368
369   int i = 0, n = aResult->length();
370
371   if ( theElemType == SMESH::NODE && !nodeIds.empty() ) {
372     set<int>::iterator idIt = nodeIds.begin();
373     for ( ; i < n && idIt != nodeIds.end() ; i++, idIt++ )
374       aResult[i] = *idIt;
375   }
376
377   if ( theElemType != SMESH::NODE ) {
378     TListOfSubMeshes::iterator sm = smList.begin();
379     for ( i = 0; sm != smList.end(); sm++ )
380     {
381       aSubMeshDS = *sm;
382       SMDS_ElemIteratorPtr anIt = aSubMeshDS->GetElements();
383       while ( i < n && anIt->more() ) {
384         const SMDS_MeshElement* anElem = anIt->next();
385         if ( theElemType == SMESH::ALL || anElem->GetType() == (SMDSAbs_ElementType)theElemType )
386           aResult[i++] = anElem->GetID();
387       }
388     }
389   }
390
391   aResult->length( i );
392
393   return aResult._retn();
394 }
395
396 //=============================================================================
397 /*!
398  *  
399  */
400 //=============================================================================
401   
402 SMESH::long_array* SMESH_subMesh_i::GetNodesId()
403   throw (SALOME::SALOME_Exception)
404 {
405   Unexpect aCatch(SALOME_SalomeException);
406
407   SMESH::long_array_var aResult = GetElementsByType( SMESH::NODE );
408   return aResult._retn();
409 }
410
411 //=============================================================================
412 /*!
413  *  
414  */
415 //=============================================================================
416   
417 SMESH::SMESH_Mesh_ptr SMESH_subMesh_i::GetFather()
418   throw (SALOME::SALOME_Exception)
419 {
420   Unexpect aCatch(SALOME_SalomeException);
421   return _mesh_i->_this();
422 }
423
424 //=============================================================================
425 /*!
426  *  
427  */
428 //=============================================================================
429   
430 CORBA::Long SMESH_subMesh_i::GetId()
431 {
432   return _localId;
433 }
434
435 //=======================================================================
436 //function : GetSubShape
437 //purpose  : 
438 //=======================================================================
439
440 GEOM::GEOM_Object_ptr SMESH_subMesh_i::GetSubShape()
441      throw (SALOME::SALOME_Exception)
442 {
443   Unexpect aCatch(SALOME_SalomeException);
444   GEOM::GEOM_Object_var aShapeObj;
445   try {
446     if ( _mesh_i->_mapSubMesh.find( _localId ) != _mesh_i->_mapSubMesh.end()) {
447       TopoDS_Shape S = _mesh_i->_mapSubMesh[ _localId ]->GetSubShape();
448       if ( !S.IsNull() ) {
449         aShapeObj = _gen_i->ShapeToGeomObject( S );
450         //mzn: N7PAL16232, N7PAL16233
451         //In some cases it's possible that GEOM_Client contains the shape same to S, but
452         //with another orientation.
453         if (aShapeObj->_is_nil())
454           aShapeObj = _gen_i->ShapeToGeomObject( S.Reversed() );
455       }
456     }
457   }
458   catch(SALOME_Exception & S_ex) {
459     THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
460   }
461   return aShapeObj._retn();
462 }
463
464 //=============================================================================
465 /*!
466  *  
467  */
468 //=============================================================================
469 SMESH::long_array* SMESH_subMesh_i::GetIDs()
470 {
471   return GetElementsId();
472 }
473
474 //=============================================================================
475 /*!
476  *
477  */
478 //=============================================================================
479 SMESH::ElementType SMESH_subMesh_i::GetElementType( const CORBA::Long id, const bool iselem )
480   throw (SALOME::SALOME_Exception)
481 {
482   if ( _preMeshInfo )
483     _preMeshInfo->FullLoadFromFile();
484   return GetFather()->GetElementType( id, iselem );
485 }
486
487 //=============================================================================
488 /*
489  * Returns number of mesh elements of each \a EntityType
490  * @return array of number of elements per \a EntityType
491  */
492 //=============================================================================
493
494 SMESH::long_array* SMESH_subMesh_i::GetMeshInfo()
495 {
496   if ( _preMeshInfo )
497     return _preMeshInfo->GetMeshInfo();
498
499   SMESH::long_array_var aRes = new SMESH::long_array();
500   aRes->length(SMESH::Entity_Last);
501   for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
502     aRes[i] = 0;
503   
504   // get number of nodes
505   aRes[ SMESH::Entity_Node ] = GetNumberOfNodes(true);
506  
507   ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
508
509   // get statistic from child sub-meshes
510   TListOfSubMeshes smList;
511   if ( getSubMeshes( aSubMesh, smList ) )
512     for ( TListOfSubMeshes::iterator sm = smList.begin(); sm != smList.end(); ++sm )
513       SMESH_Mesh_i::CollectMeshInfo( (*sm)->GetElements(), aRes );
514
515   return aRes._retn();
516 }
517
518 //=======================================================================
519 /*
520  * Returns number of mesh elements of each \a ElementType
521  */
522 //=======================================================================
523
524 SMESH::long_array* SMESH_subMesh_i::GetNbElementsByType()
525 {
526   SMESH::long_array_var aRes = new SMESH::long_array();
527   aRes->length(SMESH::NB_ELEMENT_TYPES);
528   for (int i = 0; i < SMESH::NB_ELEMENT_TYPES; i++)
529     if ( _preMeshInfo )
530       aRes[ i ] = _preMeshInfo->NbElements( SMDSAbs_ElementType( i ));
531     else
532       aRes[ i ] = 0;
533
534   if ( !_preMeshInfo )
535   {
536     aRes[ SMESH::NODE ] = GetNumberOfNodes(true);
537
538     ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
539     if ( SMESHDS_SubMesh* smDS = aSubMesh->GetSubMeshDS() )
540     {
541       SMDS_ElemIteratorPtr eIt = smDS->GetElements();
542       if ( eIt->more() )
543         aRes[ eIt->next()->GetType() ] = smDS->NbElements();
544     }
545   }
546   return aRes._retn();  
547 }
548
549
550 //=======================================================================
551 //function : GetTypes
552 //purpose  : Returns types of elements it contains
553 //=======================================================================
554
555 SMESH::array_of_ElementType* SMESH_subMesh_i::GetTypes()
556 {
557   if ( _preMeshInfo )
558     return _preMeshInfo->GetTypes();
559
560   SMESH::array_of_ElementType_var types = new SMESH::array_of_ElementType;
561
562   ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
563   if ( SMESHDS_SubMesh* smDS = aSubMesh->GetSubMeshDS() )
564   {
565     SMDS_ElemIteratorPtr eIt = smDS->GetElements();
566     if ( eIt->more() )
567     {
568       types->length( 1 );
569       types[0] = SMESH::ElementType( eIt->next()->GetType());
570     }
571     else if ( smDS->GetNodes()->more() )
572     {
573       TopoDS_Shape shape = aSubMesh->GetSubShape();
574       while ( !shape.IsNull() && shape.ShapeType() == TopAbs_COMPOUND )
575       {
576         TopoDS_Iterator it( shape );
577         shape = it.More() ? it.Value() : TopoDS_Shape();
578       }
579       if ( !shape.IsNull() && shape.ShapeType() == TopAbs_VERTEX )
580       {
581         types->length( 1 );
582         types[0] = SMESH::NODE;
583       }
584     }
585   }
586   return types._retn();
587 }
588
589 //=======================================================================
590 //function : GetMesh
591 //purpose  : interface SMESH_IDSource
592 //=======================================================================
593
594 SMESH::SMESH_Mesh_ptr SMESH_subMesh_i::GetMesh()
595 {
596   return GetFather();
597 }
598
599 //=======================================================================
600 //function : IsMeshInfoCorrect
601 //purpose  : * Returns false if GetMeshInfo() returns incorrect information that may
602 //           * happen if mesh data is not yet fully loaded from the file of study.
603 //=======================================================================
604
605 bool SMESH_subMesh_i::IsMeshInfoCorrect()
606 {
607   return _preMeshInfo ? _preMeshInfo->IsMeshInfoCorrect() : true;
608 }