Salome HOME
Nerge with PAL/SALOME 2.1.0d
[modules/smesh.git] / src / SMESH_I / SMESH_subMesh_i.cxx
index 3ed4d296571979c468fd50a90f71ca30a99b0ad5..3ea9c4c0d629ff433d39b666a680d4f25cada6c5 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,16 @@ 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;
+
+  SMESHDS_SubMesh* aSubMeshDS = _mesh_i->_mapSubMesh[_localId]->GetSubMeshDS();
+  if ( aSubMeshDS == NULL )
+    return 0;
+
+  return aSubMeshDS->NbElements();
 }
 
 //=============================================================================
@@ -95,11 +107,31 @@ 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;
+
+  SMESHDS_SubMesh* aSubMeshDS = _mesh_i->_mapSubMesh[_localId]->GetSubMeshDS();
+  if ( aSubMeshDS == NULL )
+    return 0;
+
+  if ( all ) { // all nodes of submesh elements
+    set<int> nodeIds;
+    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 +143,91 @@ 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();
+
+  SMESHDS_SubMesh* aSubMeshDS = _mesh_i->_mapSubMesh[_localId]->GetSubMeshDS();
+  if ( aSubMeshDS == NULL )
+    return aResult._retn();
+
+  aResult->length( aSubMeshDS->NbElements() );
+  SMDS_ElemIteratorPtr anIt = aSubMeshDS->GetElements();
+  for ( int i = 0, 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();
+
+  SMESHDS_SubMesh* aSubMeshDS = _mesh_i->_mapSubMesh[_localId]->GetSubMeshDS();
+  if ( aSubMeshDS == NULL )
+    return aResult._retn();
+
+  // No sense in returning ids of elements along with ids of nodes:
+  // when theElemType == SMESH::ALL, return node ids only if
+  // there are no elements
+  bool retNodes = (theElemType == SMESH::NODE ||
+                   (theElemType == SMESH::ALL && aSubMeshDS->NbElements() == 0));
+
+  // PAL5440, return all nodes belonging to elements of submesh
+  set<int> nodeIds;
+  if ( retNodes ) {
+    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::ALL )
+    aResult->length( aSubMeshDS->NbElements() + nodeIds.size());
+  else if ( theElemType == SMESH::NODE )
+    aResult->length( nodeIds.size() );
+  else
+    aResult->length( aSubMeshDS->NbElements() );
+
+  int i = 0, n = aResult->length();
+
+  if ( retNodes && !nodeIds.empty() ) {
+    set<int>::iterator idIt = nodeIds.begin();
+    for ( ; i < n && idIt != nodeIds.end() ; i++, idIt++ )
+      aResult[i] = *idIt;
+  }
+
+  if ( theElemType == SMESH::ALL || theElemType != SMESH::NODE ) {
+    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 +239,23 @@ 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 = new SMESH::long_array();
+
+  if ( _mesh_i->_mapSubMesh.find( _localId ) == _mesh_i->_mapSubMesh.end() )
+    return aResult._retn();
+
+  SMESHDS_SubMesh* aSubMeshDS = _mesh_i->_mapSubMesh[_localId]->GetSubMeshDS();
+  if ( aSubMeshDS == NULL )
+    return aResult._retn();
+
+  aResult->length( aSubMeshDS->NbNodes() );
+  SMDS_NodeIteratorPtr anIt = aSubMeshDS->GetNodes();
+  for ( int i = 0, n = aSubMeshDS->NbNodes(); i < n && anIt->more(); i++ )
+    aResult[i] = anIt->next()->GetID();
+
+  return aResult._retn();
 }
 
 //=============================================================================
@@ -137,9 +267,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 +284,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 +315,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 +325,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();
 }