Salome HOME
Correct some memory leaks
[modules/smesh.git] / src / SMESH / SMESH_Gen.cxx
index e6ed980b45c1b1c87e58d94fbaa45172b5fe860a..ec40c71ff9a7d3335fdee3cb5a09e6687dd55897 100644 (file)
@@ -1,20 +1,33 @@
-using namespace std;
-//=============================================================================
-// File      : SMESH_Gen.cxx
-// Created   : sam mai 18 09:34:35 CEST 2002
-// Author    : Paul RASCLE, EDF
-// Project   : SALOME
-// Copyright : EDF 2002
-// $Header$
-//=============================================================================
-using namespace std;
+//  SMESH SMESH : implementaion of SMESH idl descriptions
+//
+//  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
+//
+//
+//
+//  File   : SMESH_Gen.cxx
+//  Author : Paul RASCLE, EDF
+//  Module : SMESH
+//  $Header$
 
 #include "SMESH_Gen.hxx"
-
 #include "SMESH_subMesh.hxx"
-
-#include "SMESHDS_ListOfPtrHypothesis.hxx"
-#include "SMESHDS_ListIteratorOfListOfPtrHypothesis.hxx"
 #include "SMDS_MeshElement.hxx"
 #include "SMDS_MeshNode.hxx"
 
@@ -32,9 +45,9 @@ using namespace std;
 
 SMESH_Gen::SMESH_Gen()
 {
-  MESSAGE("SMESH_Gen::SMESH_Gen");
-  _localId = 0;
-  _hypothesisFactory.SetGen(this);
+       MESSAGE("SMESH_Gen::SMESH_Gen");
+       _localId = 0;
+       _hypothesisFactory.SetGen(this);
 }
 
 //=============================================================================
@@ -45,7 +58,7 @@ SMESH_Gen::SMESH_Gen()
 
 SMESH_Gen::~SMESH_Gen()
 {
-  MESSAGE("SMESH_Gen::~SMESH_Gen");
+       MESSAGE("SMESH_Gen::~SMESH_Gen");
 }
 
 //=============================================================================
@@ -54,27 +67,27 @@ SMESH_Gen::~SMESH_Gen()
  */
 //=============================================================================
 
-SMESH_Hypothesis* SMESH_Gen::CreateHypothesis(const char* anHyp,
-                                             int studyId)
-  throw (SALOME_Exception)
+SMESH_Hypothesis *SMESH_Gen::CreateHypothesis(const char *anHyp, int studyId)
+       throw(SALOME_Exception)
 {
-  MESSAGE("SMESH_Gen::CreateHypothesis");
 
-  // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
+       MESSAGE("CreateHypothesis("<<anHyp<<","<<studyId<<")");
+       // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
 
-  StudyContextStruct* myStudyContext = GetStudyContext(studyId);
+       StudyContextStruct *myStudyContext = GetStudyContext(studyId);
 
-  // create a new hypothesis object, store its ref. in studyContext
+       // create a new hypothesis object, store its ref. in studyContext
 
-  SMESH_Hypothesis* myHypothesis = _hypothesisFactory.Create(anHyp, studyId);
-  int hypId = myHypothesis->GetID();
-  myStudyContext->mapHypothesis[hypId] = myHypothesis;
-  SCRUTE(studyId);
-  SCRUTE(hypId);
+       SMESH_Hypothesis *myHypothesis = _hypothesisFactory.Create(anHyp, studyId);
+       int hypId = myHypothesis->GetID();
+       myStudyContext->mapHypothesis[hypId] = myHypothesis;
+       SCRUTE(studyId);
+       SCRUTE(hypId);
 
-  // store hypothesis in SMESHDS document
+       // store hypothesis in SMESHDS document
 
-  myStudyContext->myDocument->AddHypothesis(myHypothesis);
+       myStudyContext->myDocument->AddHypothesis(myHypothesis);
+       return myHypothesis;
 }
 
 //=============================================================================
@@ -83,32 +96,32 @@ SMESH_Hypothesis* SMESH_Gen::CreateHypothesis(const char* anHyp,
  */
 //=============================================================================
 
-SMESH_Mesh* SMESH_Gen::Init(int studyId, const TopoDS_Shape& aShape)
-  throw (SALOME_Exception)
+SMESH_Mesh *SMESH_Gen::Init(int studyId, const TopoDS_Shape & aShape)
+throw(SALOME_Exception)
 {
-  MESSAGE("SMESH_Gen::Init");
+       MESSAGE("SMESH_Gen::Init");
 //   if (aShape.ShapeType() == TopAbs_COMPOUND)
 //     {
 //       INFOS("Mesh Compound not yet implemented!");
 //       throw(SALOME_Exception(LOCALIZED("Mesh Compound not yet implemented!")));
 //     }
 
-  // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
+       // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
 
-  StudyContextStruct* myStudyContext = GetStudyContext(studyId);
+       StudyContextStruct *myStudyContext = GetStudyContext(studyId);
 
-  // create a new SMESH_mesh object 
+       // create a new SMESH_mesh object 
 
-  SMESH_Mesh* mesh = new SMESH_Mesh(_localId++,
-                                   studyId,
-                                   this,
-                                   myStudyContext->myDocument);
-  myStudyContext->mapMesh[_localId] = mesh;
+       SMESH_Mesh *mesh = new SMESH_Mesh(_localId++,
+               studyId,
+               this,
+               myStudyContext->myDocument);
+       myStudyContext->mapMesh[_localId] = mesh;
 
-  // associate a TopoDS_Shape to the mesh
+       // associate a TopoDS_Shape to the mesh
 
-  mesh->ShapeToMesh(aShape);
-  return mesh;
+       mesh->ShapeToMesh(aShape);
+       return mesh;
 }
 
 //=============================================================================
@@ -117,10 +130,10 @@ SMESH_Mesh* SMESH_Gen::Init(int studyId, const TopoDS_Shape& aShape)
  */
 //=============================================================================
 
-bool SMESH_Gen::Compute(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
-  throw (SALOME_Exception)
+bool SMESH_Gen::Compute(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
+throw(SALOME_Exception)
 {
-  MESSAGE("SMESH_Gen::Compute");
+       MESSAGE("SMESH_Gen::Compute");
 //   bool isDone = false;
 /* 
 Algo : s'appuie ou non sur une geometrie
@@ -131,49 +144,46 @@ Face, Shell, collection de Face et Shells : 2D
 Solid, Collection de Solid : 3D
 */
 // *** corriger commentaires
-  // check hypothesis associated to the mesh :
-  // - only one algo : type compatible with the type of the shape
-  // - hypothesis = compatible with algo
-  //    - check if hypothesis are applicable to this algo
-  //    - check contradictions within hypothesis
-  //    (test if enough hypothesis is done further)
+       // check hypothesis associated to the mesh :
+       // - only one algo : type compatible with the type of the shape
+       // - hypothesis = compatible with algo
+       //    - check if hypothesis are applicable to this algo
+       //    - check contradictions within hypothesis
+       //    (test if enough hypothesis is done further)
 
-  bool ret = true;
+       bool ret = true;
 
-  SMESH_subMesh* sm = aMesh.GetSubMesh(aShape);
+       SMESH_subMesh *sm = aMesh.GetSubMesh(aShape);
 //   SCRUTE(sm);
-  SMESH_subMesh* smToCompute = sm->GetFirstToCompute();
-  while (smToCompute)
-    {
-      TopoDS_Shape subShape = smToCompute->GetSubShape();
-      int dim = GetShapeDim(subShape);
-      //SCRUTE(dim);
-      if (dim > 0)
+       SMESH_subMesh *smToCompute = sm->GetFirstToCompute();
+       while (smToCompute)
        {
-         bool ret1 = smToCompute->ComputeStateEngine(SMESH_subMesh::COMPUTE);
-         ret = ret && ret1;
-       }
-      else
-       {
-         ASSERT(dim == 0);
-         ASSERT(smToCompute->_vertexSet == false);
-         TopoDS_Vertex V1 = TopoDS::Vertex(subShape);
-         gp_Pnt P1 = BRep_Tool::Pnt(V1);
-         const Handle(SMESHDS_Mesh)& meshDS = aMesh.GetMeshDS();
-         int nodeId = meshDS->AddNode(P1.X(), P1.Y(), P1.Z());
-         //MESSAGE("point "<<nodeId<<" "<<P1.X()<<" "<<P1.Y()<<" "<<P1.Z());
-         Handle (SMDS_MeshElement) elt = meshDS->FindNode(nodeId);
-         Handle (SMDS_MeshNode) node = meshDS->GetNode(1, elt);
-         meshDS->SetNodeOnVertex(node, V1);
-         const Handle(SMESHDS_SubMesh)& subMeshDS
-           = smToCompute->GetSubMeshDS();
-         smToCompute->_vertexSet = true;
-         bool ret1 = smToCompute->ComputeStateEngine(SMESH_subMesh::COMPUTE);
+               TopoDS_Shape subShape = smToCompute->GetSubShape();
+               int dim = GetShapeDim(subShape);
+               //SCRUTE(dim);
+               if (dim > 0)
+               {
+                       bool ret1 = smToCompute->ComputeStateEngine(SMESH_subMesh::COMPUTE);
+                       ret = ret && ret1;
+               }
+               else
+               {
+                       ASSERT(dim == 0);
+                       ASSERT(smToCompute->_vertexSet == false);
+                       TopoDS_Vertex V1 = TopoDS::Vertex(subShape);
+                       gp_Pnt P1 = BRep_Tool::Pnt(V1);
+                       SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
+                       //MESSAGE("point "<<nodeId<<" "<<P1.X()<<" "<<P1.Y()<<" "<<P1.Z());
+                       SMDS_MeshNode * node = meshDS->AddNode(P1.X(), P1.Y(), P1.Z());
+                       meshDS->SetNodeOnVertex(node, V1);
+                       smToCompute->GetSubMeshDS();
+                       smToCompute->_vertexSet = true;
+                       smToCompute->ComputeStateEngine(SMESH_subMesh::COMPUTE);
+               }
+               smToCompute = sm->GetFirstToCompute();
        }
-      smToCompute = sm->GetFirstToCompute();
-    }
 
-  return ret;
+       return ret;
 }
 
 //=============================================================================
@@ -182,83 +192,90 @@ Solid, Collection de Solid : 3D
  */
 //=============================================================================
 
-SMESH_Algo* SMESH_Gen::GetAlgo(SMESH_Mesh& aMesh,
-                              const TopoDS_Shape& aShape)
+SMESH_Algo *SMESH_Gen::GetAlgo(SMESH_Mesh & aMesh, const TopoDS_Shape & aShape)
 {
-  //MESSAGE("SMESH_Gen::GetAlgo");
+       //MESSAGE("SMESH_Gen::GetAlgo");
 
-  SMESHDS_Hypothesis* theHyp = NULL;
-  SMESH_Algo* algo = NULL;
-  const Handle(SMESHDS_Mesh)& meshDS = aMesh.GetMeshDS();
-  int hypType;
-  int hypId;
-  int algoDim;
+       const SMESHDS_Hypothesis *theHyp = NULL;
+       SMESH_Algo *algo = NULL;
+       const SMESHDS_Mesh * meshDS = aMesh.GetMeshDS();
+       int hypType;
+       int hypId;
+       int algoDim;
 
-  // try shape first, then main shape
+       // try shape first, then main shape
 
-  TopoDS_Shape mainShape = meshDS->ShapeToMesh();
-  const TopoDS_Shape* shapeToTry[2] = {&aShape, &mainShape};
+       TopoDS_Shape mainShape = meshDS->ShapeToMesh();
+       const TopoDS_Shape *shapeToTry[2] = { &aShape, &mainShape };
 
-  for (int iShape=0; iShape<2; iShape++)
-    {
-      TopoDS_Shape tryShape = (*shapeToTry[iShape]);
-
-      const SMESHDS_ListOfPtrHypothesis& listHyp
-       = meshDS->GetHypothesis(tryShape);
-      SMESHDS_ListIteratorOfListOfPtrHypothesis it(listHyp);
+       for (int iShape = 0; iShape < 2; iShape++)
+       {
+               TopoDS_Shape tryShape = (*shapeToTry[iShape]);
 
-      int nb_algo = 0;
-      int shapeDim = GetShapeDim(aShape);
-      int typeOfShape = aShape.ShapeType();
+               const list<const SMESHDS_Hypothesis*>& listHyp =
+                       meshDS->GetHypothesis(tryShape);
+               list<const SMESHDS_Hypothesis*>::const_iterator it=listHyp.begin();
+               
+               int nb_algo = 0;
+               int shapeDim = GetShapeDim(aShape);
+               int typeOfShape = aShape.ShapeType();
 
-      while (it.More())
-       {
-         SMESHDS_Hypothesis* anHyp = it.Value();
-         hypType = anHyp->GetType();
-//       SCRUTE(hypType);
-         if (hypType > SMESHDS_Hypothesis::PARAM_ALGO)
-           {
-             switch (hypType)
+               while (it!=listHyp.end())
                {
-               case SMESHDS_Hypothesis::ALGO_1D: algoDim=1; break;
-               case SMESHDS_Hypothesis::ALGO_2D: algoDim=2; break;
-               case SMESHDS_Hypothesis::ALGO_3D: algoDim=3; break;
-               default: algoDim=0; break;
-               }
-//           SCRUTE(algoDim);
-//           SCRUTE(shapeDim);
-//           SCRUTE(typeOfShape);
-             if (shapeDim == algoDim)        // count only algos of shape dim.
-               {                             // discard algos for subshapes
-                 hypId = anHyp->GetID();     // (of lower dim.)
-                 ASSERT(_mapAlgo.find(hypId) != _mapAlgo.end());
-                 SMESH_Algo* anAlgo = _mapAlgo[hypId];
-                 //SCRUTE(anAlgo->GetShapeType());
-//               if (anAlgo->GetShapeType() == typeOfShape)
-                 if ((anAlgo->GetShapeType()) & (1 << typeOfShape))
-                   {                         // only specific TopoDS_Shape
-                     nb_algo++;                  
-                     theHyp = anHyp;
-                   }
+                       const SMESHDS_Hypothesis *anHyp = *it;
+                       hypType = anHyp->GetType();
+                       //SCRUTE(hypType);
+                       if (hypType > SMESHDS_Hypothesis::PARAM_ALGO)
+                       {
+                               switch (hypType)
+                               {
+                               case SMESHDS_Hypothesis::ALGO_1D:
+                                       algoDim = 1;
+                                       break;
+                               case SMESHDS_Hypothesis::ALGO_2D:
+                                       algoDim = 2;
+                                       break;
+                               case SMESHDS_Hypothesis::ALGO_3D:
+                                       algoDim = 3;
+                                       break;
+                               default:
+                                       algoDim = 0;
+                                       break;
+                               }
+                               //SCRUTE(algoDim);
+                               //SCRUTE(shapeDim);
+                               //SCRUTE(typeOfShape);
+                               if (shapeDim == algoDim)        // count only algos of shape dim.
+                               {                               // discard algos for subshapes
+                                       hypId = anHyp->GetID(); // (of lower dim.)
+                                       ASSERT(_mapAlgo.find(hypId) != _mapAlgo.end());
+                                       SMESH_Algo *anAlgo = _mapAlgo[hypId];
+                                       //SCRUTE(anAlgo->GetShapeType());
+                                       //if (anAlgo->GetShapeType() == typeOfShape)
+                                       if ((anAlgo->GetShapeType()) & (1 << typeOfShape))
+                                       {                       // only specific TopoDS_Shape
+                                               nb_algo++;
+                                               theHyp = anHyp;
+                                       }
+                               }
+                       }
+                       if (nb_algo > 1) return NULL;   // more than one algo
+                       it++;
                }
-           }
-         if (nb_algo > 1) return NULL;  // more than one algo
-         it.Next();
+               if (nb_algo == 1)               // one algo found : OK
+                       break;                          // do not try a parent shape
        }
-      if (nb_algo == 1)                  // one algo found : OK
-       break;                           // do not try a parent shape
-    }
 
-  if (!theHyp) return NULL;              // no algo found
+       if (!theHyp)
+               return NULL;                    // no algo found
 
-  hypType = theHyp->GetType();
-  hypId = theHyp->GetID();
+       hypType = theHyp->GetType();
+       hypId = theHyp->GetID();
 
-  ASSERT(_mapAlgo.find(hypId) != _mapAlgo.end());
-  algo = _mapAlgo[hypId];
-  const char* algoName = algo->GetName();
-  //MESSAGE("Algo found " << algoName << " Id " << hypId);
-  return algo;
+       ASSERT(_mapAlgo.find(hypId) != _mapAlgo.end());
+       algo = _mapAlgo[hypId];
+       //MESSAGE("Algo found " << algo->GetName() << " Id " << hypId);
+       return algo;
 }
 
 //=============================================================================
@@ -267,18 +284,18 @@ SMESH_Algo* SMESH_Gen::GetAlgo(SMESH_Mesh& aMesh,
  */
 //=============================================================================
 
-StudyContextStructSMESH_Gen::GetStudyContext(int studyId)
+StudyContextStruct *SMESH_Gen::GetStudyContext(int studyId)
 {
-  // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
-
-  if (_mapStudyContext.find(studyId) == _mapStudyContext.end())
-    {
-      _mapStudyContext[studyId] = new StudyContextStruct;
-      _mapStudyContext[studyId]->myDocument = new SMESHDS_Document(studyId);
-    }
-  StudyContextStruct* myStudyContext = _mapStudyContext[studyId];
+       // Get studyContext, create it if it does'nt exist, with a SMESHDS_Document
+
+       if (_mapStudyContext.find(studyId) == _mapStudyContext.end())
+       {
+               _mapStudyContext[studyId] = new StudyContextStruct;
+               _mapStudyContext[studyId]->myDocument = new SMESHDS_Document(studyId);
+       }
+       StudyContextStruct *myStudyContext = _mapStudyContext[studyId];
 //   ASSERT(_mapStudyContext.find(studyId) != _mapStudyContext.end());
-  return myStudyContext;
+       return myStudyContext;
 }
 
 //=============================================================================
@@ -299,7 +316,7 @@ void SMESH_Gen::Save(int studyId, const char *aUrlOfFile)
 
 void SMESH_Gen::Load(int studyId, const char *aUrlOfFile)
 {
-} 
+}
 
 //=============================================================================
 /*!
@@ -310,26 +327,25 @@ void SMESH_Gen::Load(int studyId, const char *aUrlOfFile)
 void SMESH_Gen::Close(int studyId)
 {
 }
+
 //=============================================================================
 /*!
  * 
  */
 //=============================================================================
 
-const charSMESH_Gen::ComponentDataType()
+const char *SMESH_Gen::ComponentDataType()
 {
 }
 
-
 //=============================================================================
 /*!
  * 
  */
 //=============================================================================
 
-const char* SMESH_Gen::IORToLocalPersistentID(const char* IORString,
-                                             bool& IsAFile)
+const char *SMESH_Gen::IORToLocalPersistentID(const char *IORString,
+       bool & IsAFile)
 {
 }
 
@@ -339,7 +355,7 @@ const char* SMESH_Gen::IORToLocalPersistentID(const char* IORString,
  */
 //=============================================================================
 
-const char* SMESH_Gen::LocalPersistentIDToIOR(const char* aLocalPersistentID)
+const char *SMESH_Gen::LocalPersistentIDToIOR(const char *aLocalPersistentID)
 {
 }
 
@@ -349,42 +365,42 @@ const char* SMESH_Gen::LocalPersistentIDToIOR(const char* aLocalPersistentID)
  */
 //=============================================================================
 
-int SMESH_Gen::GetShapeDim(const TopoDS_Shape& aShape)
+int SMESH_Gen::GetShapeDim(const TopoDS_Shape & aShape)
 {
-  int shapeDim = -1;              // Shape dimension: 0D, 1D, 2D, 3D
-  int type = aShape.ShapeType();
-  switch (type)
-    {
+       int shapeDim = -1;                      // Shape dimension: 0D, 1D, 2D, 3D
+       int type = aShape.ShapeType();
+       switch (type)
+       {
 //     case TopAbs_COMPOUND:
 //       {
-//     break;
+//  break;
 //       }
-    case TopAbs_COMPOUND:
-    case TopAbs_COMPSOLID:
-    case TopAbs_SOLID:
-    case TopAbs_SHELL:
-      {
-       shapeDim = 3;
-       break;
-      }
-      //    case TopAbs_SHELL:
-    case TopAbs_FACE:
-      {
-       shapeDim = 2;
-       break;
-      }
-    case TopAbs_WIRE:
-    case TopAbs_EDGE:
-      {
-       shapeDim = 1;
-       break;
-      }
-    case TopAbs_VERTEX:
-      {
-       shapeDim = 0;
-       break;
-      }
-    }
+       case TopAbs_COMPOUND:
+       case TopAbs_COMPSOLID:
+       case TopAbs_SOLID:
+       case TopAbs_SHELL:
+       {
+               shapeDim = 3;
+               break;
+       }
+               //    case TopAbs_SHELL:
+       case TopAbs_FACE:
+       {
+               shapeDim = 2;
+               break;
+       }
+       case TopAbs_WIRE:
+       case TopAbs_EDGE:
+       {
+               shapeDim = 1;
+               break;
+       }
+       case TopAbs_VERTEX:
+       {
+               shapeDim = 0;
+               break;
+       }
+       }
 //   SCRUTE(shapeDim);
-  return shapeDim;
+       return shapeDim;
 }