Salome HOME
PAL8021. Correct work with submeshes on shape more complex than SHELL.
[modules/smesh.git] / src / SMESH_I / SMESH_subMesh_i.cxx
index 3ed4d296571979c468fd50a90f71ca30a99b0ad5..50d689eb3e131c42f4d20d83d1d59d96ed2bd65e 100644 (file)
@@ -26,7 +26,6 @@
 //  Module : SMESH
 //  $Header$
 
-using namespace std;
 using namespace std;
 #include "SMESH_subMesh_i.hxx"
 #include "SMESH_Gen_i.hxx"
@@ -35,6 +34,7 @@ using namespace std;
 #include "Utils_CorbaException.hxx"
 #include "utilities.h"
 #include "OpUtil.hxx"
+#include "Utils_ExceptHandlers.hxx"
 
 //=============================================================================
 /*!
@@ -43,6 +43,7 @@ using namespace std;
 //=============================================================================
 
 SMESH_subMesh_i::SMESH_subMesh_i()
+     : SALOME::GenericObj_i( PortableServer::POA::_nil() )
 {
   MESSAGE("SMESH_subMesh_i::SMESH_subMesh_i default, not for use");
     ASSERT(0);
@@ -54,14 +55,17 @@ SMESH_subMesh_i::SMESH_subMesh_i()
  */
 //=============================================================================
 
-SMESH_subMesh_i::SMESH_subMesh_i(SMESH_Gen_i* gen_i,
-                                SMESH_Mesh_i* mesh_i,
-                                int localId)
+SMESH_subMesh_i::SMESH_subMesh_i( PortableServer::POA_ptr thePOA,
+                                 SMESH_Gen_i*            gen_i,
+                                 SMESH_Mesh_i*           mesh_i,
+                                 int                     localId )
+     : SALOME::GenericObj_i( thePOA )
 {
   MESSAGE("SMESH_subMesh_i::SMESH_subMesh_i");
   _gen_i = gen_i;
   _mesh_i = mesh_i;
   _localId = localId;
+  thePOA->activate_object( this );
   // ****
 }
 //=============================================================================
@@ -85,8 +89,29 @@ SMESH_subMesh_i::~SMESH_subMesh_i()
 CORBA::Long SMESH_subMesh_i::GetNumberOfElements()
   throw (SALOME::SALOME_Exception)
 {
+  Unexpect aCatch(SALOME_SalomeException);
   MESSAGE("SMESH_subMesh_i::GetNumberOfElements");
-  // ****
+  if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
+    return 0;
+
+  ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
+  SMESHDS_SubMesh* aSubMeshDS = aSubMesh->GetSubMeshDS();
+
+  int nbElems = aSubMeshDS ? aSubMeshDS->NbElements() : 0;
+
+  // volumes are bound to shell
+  if ( nbElems == 0 && aSubMesh->GetSubShape().ShapeType() <= TopAbs_SOLID )
+  {
+    SMESHDS_Mesh* aMeshDS = aSubMesh->GetFather()->GetMeshDS();
+    TopExp_Explorer exp( aSubMesh->GetSubShape(), TopAbs_SHELL );
+    for ( ; exp.More(); exp.Next() )
+    {
+      aSubMeshDS = aMeshDS->MeshElements( exp.Current() );
+      if ( aSubMeshDS )
+        nbElems += aSubMeshDS->NbElements();
+    }
+  }
+  return nbElems;
 }
 
 //=============================================================================
@@ -95,11 +120,55 @@ CORBA::Long SMESH_subMesh_i::GetNumberOfElements()
  */
 //=============================================================================
 
-CORBA::Long SMESH_subMesh_i::GetNumberOfNodes()
+CORBA::Long SMESH_subMesh_i::GetNumberOfNodes(CORBA::Boolean all)
   throw (SALOME::SALOME_Exception)
 {
+  Unexpect aCatch(SALOME_SalomeException);
   MESSAGE("SMESH_subMesh_i::GetNumberOfNodes");
-  // ****
+  if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
+    return 0;
+
+  ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
+  SMESHDS_SubMesh* aSubMeshDS = aSubMesh->GetSubMeshDS();
+
+  set<int> nodeIds;
+
+  // node are bound to shell instead of solid
+  if ( all && aSubMesh->GetSubShape().ShapeType() <= TopAbs_SOLID )
+  {
+    SMESHDS_Mesh* aMeshDS = aSubMesh->GetFather()->GetMeshDS();
+    TopExp_Explorer exp( aSubMesh->GetSubShape(), TopAbs_SHELL );
+    for ( ; exp.More(); exp.Next() )
+    {
+      aSubMeshDS = aMeshDS->MeshElements( exp.Current() );
+      if ( aSubMeshDS ) {
+        SMDS_ElemIteratorPtr eIt = aSubMeshDS->GetElements();
+        while ( eIt->more() ) {
+          const SMDS_MeshElement* anElem = eIt->next();
+          SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
+          while ( nIt->more() )
+            nodeIds.insert( nIt->next()->GetID() );
+        }
+      }
+    }
+    return nodeIds.size();
+  }
+
+  if ( aSubMeshDS == NULL )
+    return 0;
+
+  if ( all ) { // all nodes of submesh elements
+    SMDS_ElemIteratorPtr eIt = aSubMeshDS->GetElements();
+    while ( eIt->more() ) {
+      const SMDS_MeshElement* anElem = eIt->next();
+      SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
+      while ( nIt->more() )
+        nodeIds.insert( nIt->next()->GetID() );
+    }
+    return nodeIds.size();
+  }
+    
+  return aSubMeshDS->NbNodes();
 }
 
 //=============================================================================
@@ -111,8 +180,150 @@ CORBA::Long SMESH_subMesh_i::GetNumberOfNodes()
 SMESH::long_array* SMESH_subMesh_i::GetElementsId()
   throw (SALOME::SALOME_Exception)
 {
+  Unexpect aCatch(SALOME_SalomeException);
   MESSAGE("SMESH_subMesh_i::GetElementsId");
-  // ****
+  SMESH::long_array_var aResult = new SMESH::long_array();
+
+  if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
+    return aResult._retn();
+
+  ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
+  SMESHDS_SubMesh* aSubMeshDS = aSubMesh->GetSubMeshDS();
+
+  int nbElems = aSubMeshDS ? aSubMeshDS->NbElements() : 0;
+  list<SMESHDS_SubMesh*> smList;
+  if ( nbElems )
+    smList.push_back( aSubMeshDS );
+
+  // volumes are bound to shell
+  if ( nbElems == 0 && aSubMesh->GetSubShape().ShapeType() <= TopAbs_SOLID )
+  {
+    SMESHDS_Mesh* aMeshDS = aSubMesh->GetFather()->GetMeshDS();
+    TopExp_Explorer exp( aSubMesh->GetSubShape(), TopAbs_SHELL );
+    for ( ; exp.More(); exp.Next() )
+    {
+      aSubMeshDS = aMeshDS->MeshElements( exp.Current() );
+      if ( aSubMeshDS ) {
+        smList.push_back( aSubMeshDS );
+        nbElems += aSubMeshDS->NbElements();
+      }
+    }
+  }
+
+  if ( nbElems )
+  {
+    aResult->length( nbElems );
+    list<SMESHDS_SubMesh*>::iterator sm = smList.begin();
+    for ( int i = 0; sm != smList.end(); sm++ )
+    {
+      aSubMeshDS = *sm;
+      SMDS_ElemIteratorPtr anIt = aSubMeshDS->GetElements();
+      for ( int n = aSubMeshDS->NbElements(); i < n && anIt->more(); i++ )
+        aResult[i] = anIt->next()->GetID();
+    }
+  }
+  return aResult._retn();
+}
+
+
+//=============================================================================
+/*!
+ *  
+ */
+//=============================================================================
+
+SMESH::long_array* SMESH_subMesh_i::GetElementsByType( SMESH::ElementType theElemType )
+    throw (SALOME::SALOME_Exception)
+{
+  Unexpect aCatch(SALOME_SalomeException);
+  MESSAGE("SMESH_subMesh_i::GetElementsByType");
+  SMESH::long_array_var aResult = new SMESH::long_array();
+
+  if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
+    return aResult._retn();
+
+  ::SMESH_subMesh* aSubMesh = _mesh_i->_mapSubMesh[_localId];
+  SMESHDS_SubMesh* aSubMeshDS = aSubMesh->GetSubMeshDS();
+
+  // PAL5440, return all nodes belonging to elements of submesh
+  set<int> nodeIds;
+  int nbElems = aSubMeshDS ? aSubMeshDS->NbElements() : 0;
+
+  // volumes may be bound to shell instead of solid
+  list< SMESHDS_SubMesh* > smList;
+  if ( nbElems == 0 && aSubMesh->GetSubShape().ShapeType() <= TopAbs_SOLID )
+  {
+    SMESHDS_Mesh* aMeshDS = aSubMesh->GetFather()->GetMeshDS();
+    TopExp_Explorer exp( aSubMesh->GetSubShape(), TopAbs_SHELL );
+    for ( ; exp.More(); exp.Next() )
+    {
+      aSubMeshDS = aMeshDS->MeshElements( exp.Current() );
+      if ( !aSubMeshDS ) continue;
+      if ( theElemType == SMESH::NODE )
+      {
+        SMDS_ElemIteratorPtr eIt = aSubMeshDS->GetElements();
+        while ( eIt->more() ) {
+          const SMDS_MeshElement* anElem = eIt->next();
+          SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
+          while ( nIt->more() )
+            nodeIds.insert( nIt->next()->GetID() );
+        }
+      }
+      else
+      {
+        smList.push_back( aSubMeshDS );
+        nbElems += aSubMeshDS->NbElements();
+      }
+    }
+    aSubMeshDS = 0;
+  }
+  else
+  {
+    if ( nbElems )
+      smList.push_back( aSubMeshDS );
+  }
+
+  if ( theElemType == SMESH::NODE && aSubMeshDS )
+  {
+    SMDS_ElemIteratorPtr eIt = aSubMeshDS->GetElements();
+    while ( eIt->more() ) {
+      const SMDS_MeshElement* anElem = eIt->next();
+      SMDS_ElemIteratorPtr nIt = anElem->nodesIterator();
+      while ( nIt->more() )
+        nodeIds.insert( nIt->next()->GetID() );
+    }
+  }
+
+  if ( theElemType == SMESH::NODE )
+    aResult->length( nodeIds.size() );
+  else
+    aResult->length( nbElems );
+
+  int i = 0, n = aResult->length();
+
+  if ( theElemType == SMESH::NODE && !nodeIds.empty() ) {
+    set<int>::iterator idIt = nodeIds.begin();
+    for ( ; i < n && idIt != nodeIds.end() ; i++, idIt++ )
+      aResult[i] = *idIt;
+  }
+
+  if ( theElemType != SMESH::NODE ) {
+    list<SMESHDS_SubMesh*>::iterator sm = smList.begin();
+    for ( i = 0; sm != smList.end(); sm++ )
+    {
+      aSubMeshDS = *sm;
+      SMDS_ElemIteratorPtr anIt = aSubMeshDS->GetElements();
+      while ( i < n && anIt->more() ) {
+        const SMDS_MeshElement* anElem = anIt->next();
+        if ( theElemType == SMESH::ALL || anElem->GetType() == (SMDSAbs_ElementType)theElemType )
+          aResult[i++] = anElem->GetID();
+      }
+    }
+  }
+
+  aResult->length( i );
+
+  return aResult._retn();
 }
 
 //=============================================================================
@@ -124,8 +335,10 @@ SMESH::long_array* SMESH_subMesh_i::GetElementsId()
 SMESH::long_array* SMESH_subMesh_i::GetNodesId()
   throw (SALOME::SALOME_Exception)
 {
+  Unexpect aCatch(SALOME_SalomeException);
   MESSAGE("SMESH_subMesh_i::GetNodesId");
-  // ****
+  SMESH::long_array_var aResult = GetElementsByType( SMESH::NODE );
+  return aResult._retn();
 }
 
 //=============================================================================
@@ -137,9 +350,9 @@ SMESH::long_array* SMESH_subMesh_i::GetNodesId()
 SMESH::SMESH_Mesh_ptr SMESH_subMesh_i::GetFather()
   throw (SALOME::SALOME_Exception)
 {
+  Unexpect aCatch(SALOME_SalomeException);
   MESSAGE("SMESH_subMesh_i::GetFather");
-  SMESH::SMESH_Mesh_var meshIor = _mesh_i->GetIor();
-  return SMESH::SMESH_Mesh::_duplicate(meshIor);
+  return _mesh_i->_this();
 }
 
 //=============================================================================
@@ -154,6 +367,29 @@ CORBA::Long SMESH_subMesh_i::GetId()
   return _localId;
 }
 
+//=======================================================================
+//function : GetSubShape
+//purpose  : 
+//=======================================================================
+
+GEOM::GEOM_Object_ptr SMESH_subMesh_i::GetSubShape()
+     throw (SALOME::SALOME_Exception)
+{
+  Unexpect aCatch(SALOME_SalomeException);
+  GEOM::GEOM_Object_var aShapeObj;
+  try {
+    if ( _mesh_i->_mapSubMesh.find( _localId ) != _mesh_i->_mapSubMesh.end()) {
+      TopoDS_Shape S = _mesh_i->_mapSubMesh[ _localId ]->GetSubShape();
+      if ( !S.IsNull() )
+        aShapeObj = _gen_i->ShapeToGeomObject( S );
+    }
+  }
+  catch(SALOME_Exception & S_ex) {
+    THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
+  }
+  return aShapeObj._retn();
+}
+
 //=============================================================================
 /*!
  *  
@@ -162,6 +398,7 @@ CORBA::Long SMESH_subMesh_i::GetId()
 SALOME_MED::FAMILY_ptr SMESH_subMesh_i::GetFamily()
   throw (SALOME::SALOME_Exception)
 {
+  Unexpect aCatch(SALOME_SalomeException);
   SALOME_MED::MESH_var MEDMesh = GetFather()->GetMEDMesh();
 
   SALOME_MED::Family_array_var families = 
@@ -171,4 +408,17 @@ SALOME_MED::FAMILY_ptr SMESH_subMesh_i::GetFamily()
     if ( families[i]->getIdentifier() == ( _localId ) )
       return families[i];
   }
+  
+  return SALOME_MED::FAMILY::_nil();
+}
+
+//=============================================================================
+/*!
+ *  
+ */
+//=============================================================================
+SMESH::long_array* SMESH_subMesh_i::GetIDs()
+{
+  SMESH::long_array_var aResult = GetElementsId();
+  return aResult._retn();
 }