Salome HOME
PR: synchro V6_main tag mergeto_V7_main_11Feb13
[modules/smesh.git] / src / SMESHDS / SMESHDS_SubMesh.cxx
index 58f29ea4e422efd0cbe95ee64ed346b90aaa4c54..9eb49ff077cf7ca1fd3212a1839497bee27b73d8 100644 (file)
@@ -1,23 +1,23 @@
-//  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
 //
-//  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 //
-//  This library is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU Lesser General Public
-//  License as published by the Free Software Foundation; either
-//  version 2.1 of the License.
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License.
 //
-//  This library is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-//  Lesser General Public License for more details.
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
-//  You should have received a copy of the GNU Lesser General Public
-//  License along with this library; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 //  SMESH SMESHDS : management of mesh data and SMESH document
 
 using namespace std;
 
+
+//================================================================================
+/*!
+ * \brief Constructor
+ */
+//================================================================================
+
 SMESHDS_SubMesh::SMESHDS_SubMesh(SMESHDS_Mesh *parent, int index)
 {
   myParent = parent;
@@ -46,6 +53,16 @@ SMESHDS_SubMesh::SMESHDS_SubMesh(SMESHDS_Mesh *parent, int index)
   myUnusedIdElements = 0;
 }
 
+//================================================================================
+/*!
+ * \brief Destructor
+ */
+//================================================================================
+
+SMESHDS_SubMesh::~SMESHDS_SubMesh()
+{
+}
+
 //=======================================================================
 //function : AddElement
 //purpose  : 
@@ -54,14 +71,45 @@ void SMESHDS_SubMesh::AddElement(const SMDS_MeshElement * ME)
 {
   if (!IsComplexSubmesh())
     {
+      if ( ME->GetType() == SMDSAbs_Node )
+      {
+        AddNode( static_cast< const SMDS_MeshNode* >( ME ));
+        return;
+      }
       //MESSAGE("in " << myIndex << " AddElement "<< ME->GetID());
-      int idInSubShape = ME->getIdInShape();
-      if (idInSubShape != -1)
+      int oldShapeId = ME->getshapeId();
+      if ( oldShapeId > 0 )
         {
-          MESSAGE("add element in subshape already belonging to a subshape "
-              << ME->GetID() << "  " << ME->getIdInShape() << " " << ME->getshapeId());
-          throw SALOME_Exception(LOCALIZED("add element in subshape already belonging to a subshape"));
+          if (oldShapeId != myIndex)
+            {
+              MESSAGE("add element in subshape already belonging to another subshape "
+                << ME->GetID() << " " << oldShapeId << " " << myIndex);
+              throw SALOME_Exception(LOCALIZED("add element in subshape already belonging to a subshape"));
+            }
+          else
+            {
+              int idInSubShape = ME->getIdInShape();
+              if (idInSubShape >= 0)
+                {
+                  MESSAGE("add element in subshape already belonging to that subshape "
+                      << ME->GetID() << " " << oldShapeId << " " << idInSubShape);
+                  // check if ok: do nothing if ok
+                  if (idInSubShape >= myElements.size())
+                    {
+                      MESSAGE("out of bounds " << idInSubShape << " " << myElements.size());
+                      throw SALOME_Exception(LOCALIZED("out of bounds"));
+                    }
+                  if (ME != myElements[idInSubShape])
+                    {
+                      MESSAGE("not the same element");
+                      throw SALOME_Exception(LOCALIZED("not the same element"));
+                    }
+                  MESSAGE("already done, OK, nothing to do");
+                  return;
+                }
+            }
         }
+
       SMDS_MeshElement* elem = (SMDS_MeshElement*) (ME);
       elem->setShapeId(myIndex);
       elem->setIdInShape(myElements.size());
@@ -83,8 +131,8 @@ bool SMESHDS_SubMesh::RemoveElement(const SMDS_MeshElement * ME, bool isElemDele
   //MESSAGE("-----------------> RemoveElement "<< ME->GetID() << " " << isElemDeleted);
   if (!IsComplexSubmesh())
     {
-      //      if (!isElemDeleted) // alive element has valid ID and can be found
-      //  {
+      if ( ME->getshapeId() != myIndex )
+        return false;
       int idInSubShape = ME->getIdInShape();
       //MESSAGE("in "<< myIndex << " RemoveElement " << ME->GetID() << " " << idInSubShape << " " << myUnusedIdElements);
       SMDS_MeshElement* elem = (SMDS_MeshElement*) (ME);
@@ -115,9 +163,9 @@ void SMESHDS_SubMesh::AddNode(const SMDS_MeshNode * N)
       int shapeId = N->getshapeId();
       if ((shapeId > 0) && (idInSubShape >= 0))
         {
-          MESSAGE("========== AddNode already belonging to other subShape " << N->GetID());
+//           MESSAGE("========== AddNode already belonging to other subShape " << N->GetID());
           // OK for vertex nodes
-          //this->getParent()->UnSetNodeOnShape(N);
+          throw SALOME_Exception(LOCALIZED("add node in subshape already belonging to a subshape"));
         }
       SMDS_MeshNode* node = (SMDS_MeshNode*)(N);
       node->setShapeId(myIndex);
@@ -139,6 +187,8 @@ bool SMESHDS_SubMesh::RemoveNode(const SMDS_MeshNode * N, bool isNodeDeleted)
     {
       // if (!isNodeDeleted) // alive node has valid ID and can be found
       // {
+      if ( N->getshapeId() != myIndex )
+        return false;
       int idInSubShape = N->getIdInShape();
       //int shapeId = N->getshapeId();
       //MESSAGE("in "<< myIndex << " RemoveNode " << shapeId << " " << idInSubShape << " " << N->GetID());
@@ -379,6 +429,16 @@ bool SMESHDS_SubMesh::RemoveSubMesh( const SMESHDS_SubMesh* theSubMesh )
   return mySubMeshes.erase( theSubMesh );
 }
 
+//=======================================================================
+//function : RemoveAllSubmeshes
+//purpose  : 
+//=======================================================================
+
+void SMESHDS_SubMesh::RemoveAllSubmeshes()
+{
+  mySubMeshes.clear();
+}
+
 //=======================================================================
 //function : ContainsSubMesh
 //purpose  : 
@@ -409,8 +469,8 @@ SMESHDS_SubMeshIteratorPtr SMESHDS_SubMesh::GetSubMeshIterator() const
 
 void SMESHDS_SubMesh::Clear()
 {
-  myElements.clear();
-  myNodes.clear();
+  clearVector( myElements );
+  clearVector( myNodes );
   myUnusedIdNodes = 0;
   myUnusedIdElements = 0;
   SMESHDS_SubMeshIteratorPtr sub = GetSubMeshIterator();