Salome HOME
0020340: EDF 1022 SMESH : Crash with FindNodeClosestTo in a second new study
[modules/smesh.git] / src / SMESH / SMESH_Mesh.cxx
index 02383cb286a4b622da7e9b9a6ee7d1f727496fd3..00defb418e0b1ee50b988d70763105285cb8de6e 100644 (file)
@@ -1,31 +1,29 @@
-//  SMESH SMESH : implementaion of SMESH idl descriptions
+//  Copyright (C) 2007-2008  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
+//
+//  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.
 //
-//  Copyright (C) 2003  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 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 
-// 
-// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//  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
 //
+//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+//  SMESH SMESH : implementaion of SMESH idl descriptions
 //  File   : SMESH_Mesh.cxx
 //  Author : Paul RASCLE, EDF
 //  Module : SMESH
-//  $Header$
-
+//
 #include "SMESH_Mesh.hxx"
 #include "SMESH_subMesh.hxx"
 #include "SMESH_Gen.hxx"
@@ -50,6 +48,7 @@
 #include "DriverUNV_R_SMDS_Mesh.h"
 #include "DriverSTL_R_SMDS_Mesh.h"
 
+#undef _Precision_HeaderFile
 #include <BRepPrimAPI_MakeBox.hxx>
 #include <TopExp.hxx>
 #include <TopExp_Explorer.hxx>
@@ -69,6 +68,8 @@ static int MYDEBUG = 0;
 static int MYDEBUG = 0;
 #endif
 
+using namespace std;
+
 #define cSMESH_Hyp(h) static_cast<const SMESH_Hypothesis*>(h)
 
 typedef SMESH_HypoFilter THypType;
@@ -84,7 +85,7 @@ SMESH_Mesh::SMESH_Mesh(int               theLocalId,
                       SMESH_Gen*        theGen,
                       bool              theIsEmbeddedMode,
                       SMESHDS_Document* theDocument):
-  _groupId( 0 )
+  _groupId( 0 ), _nbSubShapes( 0 )
 {
   MESSAGE("SMESH_Mesh::SMESH_Mesh(int localId)");
   _id            = theLocalId;
@@ -108,6 +109,11 @@ SMESH_Mesh::~SMESH_Mesh()
 {
   INFOS("SMESH_Mesh::~SMESH_Mesh");
 
+  // issue 0020340: EDF 1022 SMESH : Crash with FindNodeClosestTo in a second new study
+  //   Notify event listeners at least that something happens
+  if ( SMESH_subMesh * sm = GetSubMeshContaining(1))
+    sm->ComputeStateEngine( SMESH_subMesh::MESH_ENTITY_REMOVED );
+
   // delete groups
   std::map < int, SMESH_Group * >::iterator itg;
   for (itg = _mapGroup.begin(); itg != _mapGroup.end(); itg++) {
@@ -162,6 +168,7 @@ void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
   {
     _myMeshDS->ShapeToMesh(aShape);
     _isShapeToMesh = true;
+    _nbSubShapes = _myMeshDS->MaxShapeIndex();
 
     // fill _mapAncestors
     int desType, ancType;
@@ -172,6 +179,11 @@ void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
                                         (TopAbs_ShapeEnum) ancType,
                                         _mapAncestors );
   }
+  else
+  {
+    _isShapeToMesh = false;
+    _myMeshDS->ShapeToMesh( PseudoShape() );
+  }
 }
 
 //=======================================================================
@@ -210,46 +222,59 @@ const TopoDS_Solid& SMESH_Mesh::PseudoShape()
 
 void SMESH_Mesh::Clear()
 {
-  // clear sub-meshes; get ready to re-compute as a side-effect 
+  // clear mesh data
+  _myMeshDS->ClearMesh();
 
-  if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
-  {
+  // update compute state of submeshes
+  if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) ) {
     SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
                                                              /*complexShapeFirst=*/false);
-    while ( smIt->more() )
-    {
+    while ( smIt->more() ) {
       sm = smIt->next();
-      TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();      
-      if ( shapeType == TopAbs_VERTEX || shapeType < TopAbs_SOLID )
-        // all other shapes depends on vertices so they are already cleaned
-        sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
-      // to recompute even if failed
       sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
     }
   }
 
-  // clear entities not on sub-meshes
-
-  SMDS_VolumeIteratorPtr vIt = _myMeshDS->volumesIterator();
-  while ( vIt->more() )
-    _myMeshDS->RemoveFreeElement( vIt->next(), 0 );
-
-  SMDS_FaceIteratorPtr fIt = _myMeshDS->facesIterator();
-  while ( fIt->more() )
-    _myMeshDS->RemoveFreeElement( fIt->next(), 0 );
-
-  SMDS_EdgeIteratorPtr eIt = _myMeshDS->edgesIterator();
-  while ( eIt->more() )
-    _myMeshDS->RemoveFreeElement( eIt->next(), 0 );
-
-  SMDS_NodeIteratorPtr nIt = _myMeshDS->nodesIterator();
-  while ( nIt->more() ) {
-    const SMDS_MeshNode * node = nIt->next();
-    if ( node->NbInverseElements() == 0 )
-      _myMeshDS->RemoveFreeNode( node, 0 );
-    else
-      _myMeshDS->RemoveNode(node);
-  }
+//   // clear sub-meshes; get ready to re-compute as a side-effect 
+
+//   if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
+//   {
+//     SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
+//                                                              /*complexShapeFirst=*/false);
+//     while ( smIt->more() )
+//     {
+//       sm = smIt->next();
+//       TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();      
+//       if ( shapeType == TopAbs_VERTEX || shapeType < TopAbs_SOLID )
+//         // all other shapes depends on vertices so they are already cleaned
+//         sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
+//       // to recompute even if failed
+//       sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
+//     }
+//   }
+
+//   // clear entities not on sub-meshes
+
+//   SMDS_VolumeIteratorPtr vIt = _myMeshDS->volumesIterator();
+//   while ( vIt->more() )
+//     _myMeshDS->RemoveFreeElement( vIt->next(), 0 );
+
+//   SMDS_FaceIteratorPtr fIt = _myMeshDS->facesIterator();
+//   while ( fIt->more() )
+//     _myMeshDS->RemoveFreeElement( fIt->next(), 0 );
+
+//   SMDS_EdgeIteratorPtr eIt = _myMeshDS->edgesIterator();
+//   while ( eIt->more() )
+//     _myMeshDS->RemoveFreeElement( eIt->next(), 0 );
+
+//   SMDS_NodeIteratorPtr nIt = _myMeshDS->nodesIterator();
+//   while ( nIt->more() ) {
+//     const SMDS_MeshNode * node = nIt->next();
+//     if ( node->NbInverseElements() == 0 )
+//       _myMeshDS->RemoveFreeNode( node, 0 );
+//     else
+//       _myMeshDS->RemoveNode(node);
+//   }
 }
 
 //=======================================================================
@@ -749,7 +774,7 @@ SMESH_subMesh *SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
   int index = _myMeshDS->ShapeToIndex(aSubShape);
 
   // for submeshes on GEOM Group
-  if ( !index && aSubShape.ShapeType() == TopAbs_COMPOUND ) {
+  if (( !index || index > _nbSubShapes ) && aSubShape.ShapeType() == TopAbs_COMPOUND ) {
     TopoDS_Iterator it( aSubShape );
     if ( it.More() )
       index = _myMeshDS->AddCompoundSubmesh( aSubShape, it.Value().ShapeType() );