Salome HOME
Avoid too often Standard_OutOfRange
[modules/smesh.git] / src / SMESHDS / SMESHDS_Mesh.cxx
index a42b9acc0f07774b9dacb630d577ffd27d18ad30..6766fbda76a822021b0426e748ef5dd874b84b67 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2014  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
@@ -6,7 +6,7 @@
 // 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.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -202,10 +202,10 @@ SMDS_MeshNode* SMESHDS_Mesh::AddNodeWithID(double x, double y, double z, int ID)
 //function : MoveNode
 //purpose  : 
 //=======================================================================
+
 void SMESHDS_Mesh::MoveNode(const SMDS_MeshNode *n, double x, double y, double z)
 {
-  SMDS_MeshNode * node=const_cast<SMDS_MeshNode*>(n);
-  node->setXYZ(x,y,z);
+  SMDS_Mesh::MoveNode( n, x, y, z );
   myScript->MoveNode(n->GetID(), x, y, z);
 }
 
@@ -823,7 +823,7 @@ static void removeFromContainers (map<int,SMESHDS_SubMesh*>&     theSubMeshes,
   // Element should belong to only one sub-mesh
   if ( !theSubMeshes.empty() )
   {
-    SMESHDS_Mesh* mesh = theSubMeshes.begin()->second->getParent();
+    SMESHDS_Mesh* mesh = theSubMeshes.begin()->second->GetParent();
     list<const SMDS_MeshElement *>::iterator elIt = theElems.begin();
     if ( isNode ) {
       for ( ; elIt != theElems.end(); ++elIt )
@@ -888,15 +888,16 @@ void SMESHDS_Mesh::RemoveFreeNode(const SMDS_MeshNode * n,
     set<SMESHDS_GroupBase*>::iterator GrIt = myGroups.begin();
     for (; GrIt != myGroups.end(); GrIt++) {
       SMESHDS_Group* group = dynamic_cast<SMESHDS_Group*>(*GrIt);
-      if (!group || group->IsEmpty()) continue;
-      group->SMDSGroup().Remove(n);
+      if (group && !group->IsEmpty())
+        group->SMDSGroup().Remove(n);
     }
   }
 
   // Rm from sub-mesh
   // Node should belong to only one sub-mesh
-  if( subMesh )
-    subMesh->RemoveNode(n,/*deleted=*/false);
+  if ( !subMesh || !subMesh->RemoveNode(n,/*deleted=*/false))
+    if (( subMesh = MeshElements( n->getshapeId() )))
+      subMesh->RemoveNode(n,/*deleted=*/false );
 
   SMDS_Mesh::RemoveFreeElement(n);
 }
@@ -915,11 +916,12 @@ void SMESHDS_Mesh::RemoveElement(const SMDS_MeshElement * elt)
   if (!hasConstructionEdges() && !hasConstructionFaces())
   {
     SMESHDS_SubMesh* subMesh=0;
-    map<int,SMESHDS_SubMesh*>::iterator SubIt = myShapeIndexToSubMesh.begin();
-    for ( ; !subMesh && SubIt != myShapeIndexToSubMesh.end(); SubIt++ )
-      if (!SubIt->second->IsComplexSubmesh() && SubIt->second->Contains( elt ))
+    if ( elt->getshapeId() > 0 )
+    {
+      map<int,SMESHDS_SubMesh*>::iterator SubIt = myShapeIndexToSubMesh.find( elt->getshapeId() );
+      if ( SubIt != myShapeIndexToSubMesh.end() )
         subMesh = SubIt->second;
-    //MESSAGE("subMesh " << elt->getshapeId());
+    }
     RemoveFreeElement( elt, subMesh, true);
     return;
   }
@@ -997,6 +999,10 @@ void SMESHDS_Mesh::ClearMesh()
       g->Clear();
       g->SetType( groupType );
     }
+    else
+    {
+      (*group)->Extent(); // to free cashed elements in GroupOnFilter's
+    }
   }
 }
 
@@ -1070,58 +1076,63 @@ bool SMESHDS_Mesh::add(const SMDS_MeshElement* elem, SMESHDS_SubMesh* subMesh )
 //function : SetNodeOnVolume
 //purpose  : 
 //=======================================================================
-void SMESHDS_Mesh::SetNodeInVolume(SMDS_MeshNode *      aNode,
+void SMESHDS_Mesh::SetNodeInVolume(const SMDS_MeshNode* aNode,
                                    const TopoDS_Shell & S)
 {
   if ( add( aNode, getSubmesh(S) ))
-    aNode->SetPosition ( SMDS_SpacePosition::originSpacePosition() );
+    const_cast< SMDS_MeshNode* >
+      ( aNode )->SetPosition( SMDS_SpacePosition::originSpacePosition() );
 }
 
 //=======================================================================
 //function : SetNodeOnVolume
 //purpose  : 
 //=======================================================================
-void SMESHDS_Mesh::SetNodeInVolume(SMDS_MeshNode *      aNode,
+void SMESHDS_Mesh::SetNodeInVolume(const SMDS_MeshNode *      aNode,
                                    const TopoDS_Solid & S)
 {
   if ( add( aNode, getSubmesh(S) ))
-    aNode->SetPosition ( SMDS_SpacePosition::originSpacePosition() );
+    const_cast< SMDS_MeshNode* >
+      ( aNode )->SetPosition( SMDS_SpacePosition::originSpacePosition() );
 }
 
 //=======================================================================
 //function : SetNodeOnFace
 //purpose  : 
 //=======================================================================
-void SMESHDS_Mesh::SetNodeOnFace(SMDS_MeshNode *     aNode,
+void SMESHDS_Mesh::SetNodeOnFace(const SMDS_MeshNode *     aNode,
                                  const TopoDS_Face & S,
                                  double              u,
                                  double              v)
 {
   if ( add( aNode, getSubmesh(S) ))
-    aNode->SetPosition(SMDS_PositionPtr(new SMDS_FacePosition( u, v)));
+    const_cast< SMDS_MeshNode* >
+      ( aNode )->SetPosition(SMDS_PositionPtr(new SMDS_FacePosition( u, v)));
 }
 
 //=======================================================================
 //function : SetNodeOnEdge
 //purpose  : 
 //=======================================================================
-void SMESHDS_Mesh::SetNodeOnEdge(SMDS_MeshNode *     aNode,
+void SMESHDS_Mesh::SetNodeOnEdge(const SMDS_MeshNode *     aNode,
                                  const TopoDS_Edge & S,
                                  double              u)
 {
   if ( add( aNode, getSubmesh(S) ))
-    aNode->SetPosition(SMDS_PositionPtr(new SMDS_EdgePosition(u)));
+    const_cast< SMDS_MeshNode* >
+      ( aNode )->SetPosition(SMDS_PositionPtr(new SMDS_EdgePosition(u)));
 }
 
 //=======================================================================
 //function : SetNodeOnVertex
 //purpose  : 
 //=======================================================================
-void SMESHDS_Mesh::SetNodeOnVertex(SMDS_MeshNode *       aNode,
+void SMESHDS_Mesh::SetNodeOnVertex(const SMDS_MeshNode *       aNode,
                                    const TopoDS_Vertex & S)
 {
   if ( add( aNode, getSubmesh(S) ))
-    aNode->SetPosition(SMDS_PositionPtr(new SMDS_VertexPosition()));
+    const_cast< SMDS_MeshNode* >
+      ( aNode )->SetPosition(SMDS_PositionPtr(new SMDS_VertexPosition()));
 }
 
 //=======================================================================
@@ -1365,7 +1376,8 @@ const TopoDS_Shape& SMESHDS_Mesh::IndexToShape(int ShapeIndex) const
 {
   try
   {
-    return myIndexToShape.FindKey(ShapeIndex);
+    if ( ShapeIndex > 0 )
+      return myIndexToShape.FindKey(ShapeIndex);
   }
   catch ( Standard_OutOfRange )
   {
@@ -1414,35 +1426,38 @@ void SMESHDS_Mesh::SetNodeInVolume(const SMDS_MeshNode* aNode, int Index)
 //function : SetNodeOnFace
 //purpose  : 
 //=======================================================================
-void SMESHDS_Mesh::SetNodeOnFace(SMDS_MeshNode* aNode, int Index, double u, double v)
+void SMESHDS_Mesh::SetNodeOnFace(const SMDS_MeshNode* aNode, int Index, double u, double v)
 {
   //Set Position on Node
   if ( add( aNode, getSubmesh( Index )))
-    aNode->SetPosition(SMDS_PositionPtr(new SMDS_FacePosition( u, v)));
+    const_cast< SMDS_MeshNode* >
+      ( aNode )->SetPosition(SMDS_PositionPtr(new SMDS_FacePosition( u, v)));
 }
 
 //=======================================================================
 //function : SetNodeOnEdge
 //purpose  : 
 //=======================================================================
-void SMESHDS_Mesh::SetNodeOnEdge(SMDS_MeshNode* aNode,
-                                 int            Index,
-                                 double         u)
+void SMESHDS_Mesh::SetNodeOnEdge(const SMDS_MeshNode* aNode,
+                                 int                  Index,
+                                 double               u)
 {
   //Set Position on Node
   if ( add( aNode, getSubmesh( Index )))
-    aNode->SetPosition(SMDS_PositionPtr(new SMDS_EdgePosition(u)));
+    const_cast< SMDS_MeshNode* >
+      ( aNode )->SetPosition(SMDS_PositionPtr(new SMDS_EdgePosition(u)));
 }
 
 //=======================================================================
 //function : SetNodeOnVertex
 //purpose  : 
 //=======================================================================
-void SMESHDS_Mesh::SetNodeOnVertex(SMDS_MeshNode* aNode, int Index)
+void SMESHDS_Mesh::SetNodeOnVertex(const SMDS_MeshNode* aNode, int Index)
 {
   //Set Position on Node
   if ( add( aNode, getSubmesh( Index )))
-    aNode->SetPosition(SMDS_PositionPtr(new SMDS_VertexPosition()));
+    const_cast< SMDS_MeshNode* >
+      ( aNode )->SetPosition(SMDS_PositionPtr(new SMDS_VertexPosition()));
 }
 
 //=======================================================================
@@ -1568,6 +1583,56 @@ SMDS_MeshFace* SMESHDS_Mesh::AddFaceWithID(const SMDS_MeshNode * n1,
                        ID);
 }
 
+//=======================================================================
+//function : AddFace
+//purpose  : 
+//=======================================================================
+SMDS_MeshFace* SMESHDS_Mesh::AddFace(const SMDS_MeshNode * n1,
+                                     const SMDS_MeshNode * n2,
+                                     const SMDS_MeshNode * n3,
+                                     const SMDS_MeshNode * n12,
+                                     const SMDS_MeshNode * n23,
+                                     const SMDS_MeshNode * n31,
+                                     const SMDS_MeshNode * nCenter)
+{
+  SMDS_MeshFace *anElem = SMDS_Mesh::AddFace(n1,n2,n3,n12,n23,n31,nCenter);
+  if(anElem) myScript->AddFace(anElem->GetID(), 
+                               n1->GetID(), n2->GetID(), n3->GetID(),
+                               n12->GetID(), n23->GetID(), n31->GetID(),
+                               nCenter->GetID());
+  return anElem;
+}
+
+//=======================================================================
+//function : AddFaceWithID
+//purpose  : 
+//=======================================================================
+SMDS_MeshFace* SMESHDS_Mesh::AddFaceWithID(int n1, int n2, int n3,
+                                           int n12,int n23,int n31, int nCenter, int ID)
+{
+  SMDS_MeshFace *anElem = SMDS_Mesh::AddFaceWithID(n1,n2,n3,n12,n23,n31,nCenter,ID);
+  if(anElem) myScript->AddFace(ID,n1,n2,n3,n12,n23,n31,nCenter);
+  return anElem;
+}
+
+//=======================================================================
+//function : AddFaceWithID
+//purpose  : 
+//=======================================================================
+SMDS_MeshFace* SMESHDS_Mesh::AddFaceWithID(const SMDS_MeshNode * n1,
+                                           const SMDS_MeshNode * n2,
+                                           const SMDS_MeshNode * n3,
+                                           const SMDS_MeshNode * n12,
+                                           const SMDS_MeshNode * n23,
+                                           const SMDS_MeshNode * n31, 
+                                           const SMDS_MeshNode * nCenter, 
+                                           int ID)
+{
+  return AddFaceWithID(n1->GetID(), n2->GetID(), n3->GetID(),
+                       n12->GetID(), n23->GetID(), n31->GetID(),
+                       nCenter->GetID(), ID);
+}
+
 
 //=======================================================================
 //function : AddFace