1 // Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH_I : idl implementation based on 'SMESH' unit's calsses
24 // File : SMESH_subMesh_i.cxx
25 // Author : Paul RASCLE, EDF
28 #include "SMESH_subMesh_i.hxx"
29 #include "SMESH_Gen_i.hxx"
30 #include "SMESH_Mesh_i.hxx"
31 #include "SMESH_PreMeshInfo.hxx"
33 #include "Utils_CorbaException.hxx"
34 #include "utilities.h"
36 #include "Utils_ExceptHandlers.hxx"
38 #include <TopoDS_Iterator.hxx>
39 #include <TopExp_Explorer.hxx>
43 //=============================================================================
47 //=============================================================================
49 SMESH_subMesh_i::SMESH_subMesh_i()
50 : SALOME::GenericObj_i( PortableServer::POA::_nil() )
52 MESSAGE("SMESH_subMesh_i::SMESH_subMesh_i default, not for use");
56 //=============================================================================
60 //=============================================================================
62 SMESH_subMesh_i::SMESH_subMesh_i( PortableServer::POA_ptr thePOA,
66 : SALOME::GenericObj_i( thePOA )
73 //=============================================================================
77 //=============================================================================
79 SMESH_subMesh_i::~SMESH_subMesh_i()
81 MESSAGE("SMESH_subMesh_i::~SMESH_subMesh_i");
82 if ( _preMeshInfo ) delete _preMeshInfo;
86 //=======================================================================
87 //function : getSubMeshes
88 //purpose : for a submesh on shape to which elements are not bound directly,
89 // return submeshes containing elements
90 //=======================================================================
92 typedef list<SMESHDS_SubMesh*> TListOfSubMeshes;
94 bool getSubMeshes(::SMESH_subMesh* theSubMesh,
95 TListOfSubMeshes& theSubMeshList)
97 int size = theSubMeshList.size();
99 SMESH_Mesh* aMesh = theSubMesh->GetFather();
100 SMESHDS_Mesh* aMeshDS = aMesh->GetMeshDS();
101 SMESHDS_SubMesh* aSubMeshDS = theSubMesh->GetSubMeshDS();
103 // nodes can be bound to either vertex, edge, face or solid_or_shell
104 TopoDS_Shape aShape = theSubMesh->GetSubShape();
105 switch ( aShape.ShapeType() )
108 // add submesh of solid itself
109 aSubMeshDS = aMeshDS->MeshElements( aShape );
111 theSubMeshList.push_back( aSubMeshDS );
112 // and of the first shell
113 TopExp_Explorer exp( aShape, TopAbs_SHELL );
115 aSubMeshDS = aMeshDS->MeshElements( exp.Current() );
117 theSubMeshList.push_back( aSubMeshDS );
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
133 // no submesh for a compound inside compound
134 shapeList.push_back( it.Value() );
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 );
150 theSubMeshList.push_back( aSubMeshDS );
152 return size < theSubMeshList.size();
155 //=============================================================================
159 //=============================================================================
161 CORBA::Long SMESH_subMesh_i::GetNumberOfElements()
162 throw (SALOME::SALOME_Exception)
164 Unexpect aCatch(SALOME_SalomeException);
167 return _preMeshInfo->NbElements();
169 if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
172 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
173 SMESHDS_SubMesh* aSubMeshDS = aSubMesh->GetSubMeshDS();
175 int nbElems = aSubMeshDS ? aSubMeshDS->NbElements() : 0;
177 // volumes are bound to shell
178 TListOfSubMeshes smList;
179 if ( nbElems == 0 && getSubMeshes( aSubMesh, smList ))
181 TListOfSubMeshes::iterator sm = smList.begin();
182 for ( ; sm != smList.end(); ++sm )
183 nbElems += (*sm)->NbElements();
188 //=============================================================================
192 //=============================================================================
194 CORBA::Long SMESH_subMesh_i::GetNumberOfNodes(CORBA::Boolean all)
195 throw (SALOME::SALOME_Exception)
197 Unexpect aCatch(SALOME_SalomeException);
199 if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
204 if ( all ) return _preMeshInfo->NbNodes();
205 else _preMeshInfo->FullLoadFromFile();
207 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
208 SMESHDS_SubMesh* aSubMeshDS = aSubMesh->GetSubMeshDS();
212 // nodes are bound to shell instead of solid
213 TListOfSubMeshes smList;
214 if ( all && getSubMeshes( aSubMesh, smList ))
216 TListOfSubMeshes::iterator sm = smList.begin();
217 for ( ; sm != smList.end(); ++sm )
219 SMDS_ElemIteratorPtr eIt = (*sm)->GetElements();
221 while ( eIt->more() ) {
222 const SMDS_MeshElement* anElem = eIt->next();
223 SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
224 while ( nIt->more() )
225 nodeIds.insert( nIt->next()->GetID() );
228 SMDS_NodeIteratorPtr nIt = (*sm)->GetNodes();
229 while ( nIt->more() )
230 nodeIds.insert( nIt->next()->GetID() );
233 return nodeIds.size();
236 if ( aSubMeshDS == NULL )
239 if ( all ) { // all nodes of submesh elements
240 SMDS_ElemIteratorPtr eIt = aSubMeshDS->GetElements();
242 while ( eIt->more() ) {
243 const SMDS_MeshElement* anElem = eIt->next();
244 SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
245 while ( nIt->more() )
246 nodeIds.insert( nIt->next()->GetID() );
249 SMDS_NodeIteratorPtr nIt = aSubMeshDS->GetNodes();
250 while ( nIt->more() )
251 nodeIds.insert( nIt->next()->GetID() );
253 return nodeIds.size();
256 return aSubMeshDS->NbNodes();
259 //=============================================================================
263 //=============================================================================
265 SMESH::long_array* SMESH_subMesh_i::GetElementsId()
266 throw (SALOME::SALOME_Exception)
268 Unexpect aCatch(SALOME_SalomeException);
270 SMESH::long_array_var aResult = new SMESH::long_array();
272 if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
273 return aResult._retn();
276 _preMeshInfo->FullLoadFromFile();
278 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
279 SMESHDS_SubMesh* aSubMeshDS = aSubMesh->GetSubMeshDS();
281 int nbElems = aSubMeshDS ? aSubMeshDS->NbElements() : 0;
282 TListOfSubMeshes smList;
284 smList.push_back( aSubMeshDS );
286 // volumes are bound to shell
287 if ( nbElems == 0 && getSubMeshes( aSubMesh, smList ))
289 TListOfSubMeshes::iterator sm = smList.begin();
290 for ( ; sm != smList.end(); ++sm )
291 nbElems += (*sm)->NbElements();
294 aResult->length( nbElems );
297 TListOfSubMeshes::iterator sm = smList.begin();
298 for ( int i = 0; sm != smList.end(); sm++ )
300 SMDS_ElemIteratorPtr anIt = (*sm)->GetElements();
301 for ( ; i < nbElems && anIt->more(); i++ )
302 aResult[i] = anIt->next()->GetID();
305 return aResult._retn();
309 //=============================================================================
313 //=============================================================================
315 SMESH::long_array* SMESH_subMesh_i::GetElementsByType( SMESH::ElementType theElemType )
316 throw (SALOME::SALOME_Exception)
318 Unexpect aCatch(SALOME_SalomeException);
320 SMESH::long_array_var aResult = new SMESH::long_array();
322 if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
323 return aResult._retn();
326 _preMeshInfo->FullLoadFromFile();
328 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
329 SMESHDS_SubMesh* aSubMeshDS = aSubMesh->GetSubMeshDS();
331 // PAL5440, return all nodes belonging to elements of submesh
333 int nbElems = aSubMeshDS ? aSubMeshDS->NbElements() : 0;
335 // volumes may be bound to shell instead of solid
336 TListOfSubMeshes smList;
337 if ( nbElems == 0 && getSubMeshes( aSubMesh, smList ))
339 TListOfSubMeshes::iterator sm = smList.begin();
340 for ( ; sm != smList.end(); ++sm )
342 if ( theElemType == SMESH::NODE )
344 SMDS_ElemIteratorPtr eIt = (*sm)->GetElements();
346 while ( eIt->more() ) {
347 const SMDS_MeshElement* anElem = eIt->next();
348 SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
349 while ( nIt->more() )
350 nodeIds.insert( nIt->next()->GetID() );
353 SMDS_NodeIteratorPtr nIt = (*sm)->GetNodes();
354 while ( nIt->more() )
355 nodeIds.insert( nIt->next()->GetID() );
360 nbElems += (*sm)->NbElements();
368 smList.push_back( aSubMeshDS );
371 if ( theElemType == SMESH::NODE && aSubMeshDS )
373 SMDS_ElemIteratorPtr eIt = aSubMeshDS->GetElements();
375 while ( eIt->more() ) {
376 const SMDS_MeshElement* anElem = eIt->next();
377 SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
378 while ( nIt->more() )
379 nodeIds.insert( nIt->next()->GetID() );
382 SMDS_NodeIteratorPtr nIt = aSubMeshDS->GetNodes();
383 while ( nIt->more() )
384 nodeIds.insert( nIt->next()->GetID() );
388 if ( theElemType == SMESH::NODE )
389 aResult->length( nodeIds.size() );
391 aResult->length( nbElems );
393 int i = 0, n = aResult->length();
395 if ( theElemType == SMESH::NODE && !nodeIds.empty() ) {
396 set<int>::iterator idIt = nodeIds.begin();
397 for ( ; i < n && idIt != nodeIds.end() ; i++, idIt++ )
401 if ( theElemType != SMESH::NODE ) {
402 TListOfSubMeshes::iterator sm = smList.begin();
403 for ( i = 0; sm != smList.end(); sm++ )
406 SMDS_ElemIteratorPtr anIt = aSubMeshDS->GetElements();
407 while ( i < n && anIt->more() ) {
408 const SMDS_MeshElement* anElem = anIt->next();
409 if ( theElemType == SMESH::ALL || anElem->GetType() == (SMDSAbs_ElementType)theElemType )
410 aResult[i++] = anElem->GetID();
415 aResult->length( i );
417 return aResult._retn();
420 //=============================================================================
424 //=============================================================================
426 SMESH::long_array* SMESH_subMesh_i::GetNodesId()
427 throw (SALOME::SALOME_Exception)
429 Unexpect aCatch(SALOME_SalomeException);
431 SMESH::long_array_var aResult = GetElementsByType( SMESH::NODE );
432 return aResult._retn();
435 //=============================================================================
439 //=============================================================================
441 SMESH::SMESH_Mesh_ptr SMESH_subMesh_i::GetFather()
442 throw (SALOME::SALOME_Exception)
444 Unexpect aCatch(SALOME_SalomeException);
445 return _mesh_i->_this();
448 //=============================================================================
452 //=============================================================================
454 CORBA::Long SMESH_subMesh_i::GetId()
459 //=======================================================================
460 //function : GetSubShape
462 //=======================================================================
464 GEOM::GEOM_Object_ptr SMESH_subMesh_i::GetSubShape()
465 throw (SALOME::SALOME_Exception)
467 Unexpect aCatch(SALOME_SalomeException);
468 GEOM::GEOM_Object_var aShapeObj;
470 if ( _mesh_i->_mapSubMesh.find( _localId ) != _mesh_i->_mapSubMesh.end()) {
471 TopoDS_Shape S = _mesh_i->_mapSubMesh[ _localId ]->GetSubShape();
473 aShapeObj = _gen_i->ShapeToGeomObject( S );
474 //mzn: N7PAL16232, N7PAL16233
475 //In some cases it's possible that GEOM_Client contains the shape same to S, but
476 //with another orientation.
477 if (aShapeObj->_is_nil())
478 aShapeObj = _gen_i->ShapeToGeomObject( S.Reversed() );
482 catch(SALOME_Exception & S_ex) {
483 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
485 return aShapeObj._retn();
488 //=============================================================================
492 //=============================================================================
493 SALOME_MED::FAMILY_ptr SMESH_subMesh_i::GetFamily()
494 throw (SALOME::SALOME_Exception)
496 Unexpect aCatch(SALOME_SalomeException);
498 _preMeshInfo->FullLoadFromFile();
499 SALOME_MED::MESH_var MEDMesh = GetFather()->GetMEDMesh();
501 SALOME_MED::Family_array_var families =
502 MEDMesh->getFamilies(SALOME_MED::MED_NODE);
504 for ( int i = 0; i < families->length(); i++ ) {
505 if ( families[i]->getIdentifier() == ( _localId ) )
509 return SALOME_MED::FAMILY::_nil();
512 //=============================================================================
516 //=============================================================================
517 SMESH::long_array* SMESH_subMesh_i::GetIDs()
519 return GetElementsId();
522 //=============================================================================
526 //=============================================================================
527 SMESH::ElementType SMESH_subMesh_i::GetElementType( const CORBA::Long id, const bool iselem )
528 throw (SALOME::SALOME_Exception)
531 _preMeshInfo->FullLoadFromFile();
532 return GetFather()->GetElementType( id, iselem );
536 //=============================================================================
538 * Returns statistic of mesh elements
539 * Result array of number enityties
540 * Inherited from SMESH_IDSource
542 //=============================================================================
543 SMESH::long_array* SMESH_subMesh_i::GetMeshInfo()
546 return _preMeshInfo->GetMeshInfo();
548 SMESH::long_array_var aRes = new SMESH::long_array();
549 aRes->length(SMESH::Entity_Last);
550 for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
553 // get number of nodes
554 aRes[ SMESH::Entity_Node ] = GetNumberOfNodes(true);
556 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
558 // get statistic from child sub-meshes
559 TListOfSubMeshes smList;
560 if ( getSubMeshes( aSubMesh, smList ) )
561 for ( TListOfSubMeshes::iterator sm = smList.begin(); sm != smList.end(); ++sm )
562 SMESH_Mesh_i::CollectMeshInfo( (*sm)->GetElements(), aRes );
568 //=======================================================================
569 //function : GetTypes
570 //purpose : Returns types of elements it contains
571 //=======================================================================
573 SMESH::array_of_ElementType* SMESH_subMesh_i::GetTypes()
576 return _preMeshInfo->GetTypes();
578 SMESH::array_of_ElementType_var types = new SMESH::array_of_ElementType;
580 ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
581 if ( SMESHDS_SubMesh* smDS = aSubMesh->GetSubMeshDS() )
583 SMDS_ElemIteratorPtr eIt = smDS->GetElements();
587 types[0] = SMESH::ElementType( eIt->next()->GetType());
589 else if ( smDS->GetNodes()->more() )
591 TopoDS_Shape shape = aSubMesh->GetSubShape();
592 while ( !shape.IsNull() && shape.ShapeType() == TopAbs_COMPOUND )
594 TopoDS_Iterator it( shape );
595 shape = it.More() ? it.Value() : TopoDS_Shape();
597 if ( !shape.IsNull() && shape.ShapeType() == TopAbs_VERTEX )
600 types[0] = SMESH::NODE;
604 return types._retn();
607 //=======================================================================
609 //purpose : interface SMESH_IDSource
610 //=======================================================================
612 SMESH::SMESH_Mesh_ptr SMESH_subMesh_i::GetMesh()
617 //=======================================================================
618 //function : IsMeshInfoCorrect
619 //purpose : * Returns false if GetMeshInfo() returns incorrect information that may
620 // * happen if mesh data is not yet fully loaded from the file of study.
621 //=======================================================================
623 bool SMESH_subMesh_i::IsMeshInfoCorrect()
625 return _preMeshInfo ? _preMeshInfo->IsMeshInfoCorrect() : true;