Salome HOME
PAL13639 (EDF PAL 317 : SMESH : Create "0D Hypothesis")
[modules/smesh.git] / src / SMESH / SMESH_Mesh.cxx
index a2cbb8e3caac3daa266e538a05a8debacfbd0fe7..3f3177694d1282c433e05067afed2f10105d1283 100644 (file)
@@ -49,6 +49,7 @@
 #include "DriverSTL_R_SMDS_Mesh.h"
 
 #include <BRepTools_WireExplorer.hxx>
+#include <BRepPrimAPI_MakeBox.hxx>
 #include <BRep_Builder.hxx>
 #include <gp_Pnt.hxx>
 
@@ -72,6 +73,7 @@ static int MYDEBUG = 0;
 static int MYDEBUG = 0;
 #endif
 
+#define cSMESH_Hyp(h) static_cast<const SMESH_Hypothesis*>(h)
 
 //=============================================================================
 /*!
@@ -86,13 +88,14 @@ SMESH_Mesh::SMESH_Mesh(int theLocalId,
                       SMESHDS_Document* theDocument):
   _groupId( 0 )
 {
-  INFOS("SMESH_Mesh::SMESH_Mesh(int localId)");
+  MESSAGE("SMESH_Mesh::SMESH_Mesh(int localId)");
   _id = theLocalId;
   _studyId = theStudyId;
   _gen = theGen;
   _myDocument = theDocument;
   _idDoc = theDocument->NewMesh(theIsEmbeddedMode);
   _myMeshDS = theDocument->GetMesh(_idDoc);
+  _myMeshDS->ShapeToMesh( PseudoShape() );
   _isShapeToMesh = false;
 }
 
@@ -116,7 +119,7 @@ SMESH_Mesh::~SMESH_Mesh()
 
 //=============================================================================
 /*!
- * 
+ * \brief Set geometry to be meshed
  */
 //=============================================================================
 
@@ -124,7 +127,11 @@ void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
 {
   if(MYDEBUG) MESSAGE("SMESH_Mesh::ShapeToMesh");
 
-  if ( !_myMeshDS->ShapeToMesh().IsNull() && aShape.IsNull() )
+  if ( !aShape.IsNull() && _isShapeToMesh )
+    throw SALOME_Exception(LOCALIZED ("a shape to mesh has already been defined"));
+
+  // clear current data
+  if ( !_myMeshDS->ShapeToMesh().IsNull() )
   {
     // removal of a shape to mesh, delete objects referring to sub-shapes:
     // - sub-meshes
@@ -143,28 +150,57 @@ void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
       else
         i_gr++;
     }
+    _mapAncestors.Clear();
     _mapPropagationChains.Clear();
+
+    // clear SMESHDS
+    TopoDS_Shape aNullShape;
+    _myMeshDS->ShapeToMesh( aNullShape );
   }
-  else
+
+  // set a new geometry
+  if ( !aShape.IsNull() )
   {
-    if (_isShapeToMesh)
-      throw SALOME_Exception(LOCALIZED ("a shape to mesh has already been defined"));
+    _myMeshDS->ShapeToMesh(aShape);
+    _isShapeToMesh = true;
+
+    // fill _mapAncestors
+    int desType, ancType;
+    for ( desType = TopAbs_VERTEX; desType > TopAbs_COMPOUND; desType-- )
+      for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
+        TopExp::MapShapesAndAncestors ( aShape,
+                                        (TopAbs_ShapeEnum) desType,
+                                        (TopAbs_ShapeEnum) ancType,
+                                        _mapAncestors );
   }
-  _isShapeToMesh = true;
-  _myMeshDS->ShapeToMesh(aShape);
-
-  // fill _mapAncestors
-  _mapAncestors.Clear();
-  int desType, ancType;
-  for ( desType = TopAbs_VERTEX; desType > TopAbs_COMPOUND; desType-- )
-    for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
-      TopExp::MapShapesAndAncestors ( aShape,
-                                     (TopAbs_ShapeEnum) desType,
-                                     (TopAbs_ShapeEnum) ancType,
-                                     _mapAncestors );
-
-  // NRI : 24/02/03
-  //EAP: 1/9/04 TopExp::MapShapes(aShape, _subShapes); USE the same map of _myMeshDS
+}
+
+//=======================================================================
+/*!
+ * \brief Return geometry to be meshed. (It may be a PseudoShape()!)
+ */
+//=======================================================================
+
+TopoDS_Shape SMESH_Mesh::GetShapeToMesh() const
+{
+  return _myMeshDS->ShapeToMesh();
+}
+
+//=======================================================================
+/*!
+ * \brief Return a solid which is returned by GetShapeToMesh() if
+ *        a real geometry to be meshed was not set
+ */
+//=======================================================================
+
+const TopoDS_Solid& SMESH_Mesh::PseudoShape()
+{
+  static TopoDS_Solid aSolid;
+  if ( aSolid.IsNull() )
+  {
+    aSolid = BRepPrimAPI_MakeBox(1,1,1);
+  }
+  return aSolid;
 }
 
 //=======================================================================
@@ -493,17 +529,6 @@ SMESH_Hypothesis::Hypothesis_Status
  */
 //=============================================================================
 
-SMESHDS_Mesh * SMESH_Mesh::GetMeshDS()
-{
-  return _myMeshDS;
-}
-
-//=============================================================================
-/*!
- * 
- */
-//=============================================================================
-
 const list<const SMESHDS_Hypothesis*>&
 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape) const
   throw(SALOME_Exception)
@@ -513,8 +538,13 @@ SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape) const
 }
 
 //=======================================================================
-//function : GetHypothesis
-//purpose  : 
+/*!
+ * \brief Return the hypothesis assigned to the shape
+  * \param aSubShape - the shape to check
+  * \param aFilter - the hypothesis filter
+  * \param andAncestors - flag to check hypos assigned to ancestors of the shape
+  * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
+ */
 //=======================================================================
 
 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const TopoDS_Shape &    aSubShape,
@@ -525,7 +555,7 @@ const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const TopoDS_Shape &    aSubS
     const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
     list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
     for ( ; hyp != hypList.end(); hyp++ ) {
-      const SMESH_Hypothesis * h = static_cast<const SMESH_Hypothesis*>( *hyp );
+      const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
       if ( aFilter.IsOk( h, aSubShape))
         return h;
     }
@@ -538,7 +568,7 @@ const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const TopoDS_Shape &    aSubS
       const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(it.Value());
       list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
       for ( ; hyp != hypList.end(); hyp++ ) {
-        const SMESH_Hypothesis * h = static_cast<const SMESH_Hypothesis*>( *hyp );
+        const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
         if (aFilter.IsOk( h, it.Value() ))
           return h;
       }
@@ -547,11 +577,6 @@ const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const TopoDS_Shape &    aSubS
   return 0;
 }
 
-//=======================================================================
-//function : GetHypotheses
-//purpose  : 
-//=======================================================================
-
 //================================================================================
 /*!
  * \brief Return hypothesis assigned to the shape
@@ -571,21 +596,30 @@ int SMESH_Mesh::GetHypotheses(const TopoDS_Shape &                aSubShape,
   set<string> hypTypes; // to exclude same type hypos from the result list
   int nbHyps = 0;
 
+  // only one main hypothesis is allowed
+  bool mainHypFound = false;
+
   // fill in hypTypes
   list<const SMESHDS_Hypothesis*>::const_iterator hyp;
-  for ( hyp = aHypList.begin(); hyp != aHypList.end(); hyp++ )
+  for ( hyp = aHypList.begin(); hyp != aHypList.end(); hyp++ ) {
     if ( hypTypes.insert( (*hyp)->GetName() ).second )
       nbHyps++;
+    if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
+      mainHypFound = true;
+  }
 
   // get hypos from aSubShape
   {
     const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
     for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
-      if ( aFilter.IsOk (static_cast<const SMESH_Hypothesis*>( *hyp ), aSubShape) &&
+      if ( aFilter.IsOk (cSMESH_Hyp( *hyp ), aSubShape) &&
+           ( cSMESH_Hyp(*hyp)->IsAuxiliary() || !mainHypFound ) &&
            hypTypes.insert( (*hyp)->GetName() ).second )
       {
         aHypList.push_back( *hyp );
         nbHyps++;
+        if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
+          mainHypFound = true;
       }
   }
 
@@ -600,10 +634,14 @@ int SMESH_Mesh::GetHypotheses(const TopoDS_Shape &                aSubShape,
         continue;
       const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(it.Value());
       for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
-        if (aFilter.IsOk( static_cast<const SMESH_Hypothesis*>( *hyp ), it.Value() ) &&
-            hypTypes.insert( (*hyp)->GetName() ).second ) {
+        if (aFilter.IsOk( cSMESH_Hyp( *hyp ), it.Value() ) &&
+            ( cSMESH_Hyp(*hyp)->IsAuxiliary() || !mainHypFound ) &&
+            hypTypes.insert( (*hyp)->GetName() ).second )
+        {
           aHypList.push_back( *hyp );
           nbHyps++;
+          if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
+            mainHypFound = true;
         }
     }
   }
@@ -635,29 +673,6 @@ void SMESH_Mesh::ClearLog() throw(SALOME_Exception)
   _myMeshDS->GetScript()->Clear();
 }
 
-//=============================================================================
-/*!
- * 
- */
-//=============================================================================
-
-int SMESH_Mesh::GetId()
-{
-  if(MYDEBUG) MESSAGE("SMESH_Mesh::GetId");
-  return _id;
-}
-
-//=============================================================================
-/*!
- * 
- */
-//=============================================================================
-
-SMESH_Gen *SMESH_Mesh::GetGen()
-{
-  return _gen;
-}
-
 //=============================================================================
 /*!
  * Get or Create the SMESH_subMesh object implementation
@@ -804,7 +819,7 @@ void SMESH_Mesh::NotifySubMeshesHypothesisModification(const SMESH_Hypothesis* t
 {
   Unexpect aCatch(SalomeException);
 
-  const SMESH_Hypothesis* hyp = static_cast<const SMESH_Hypothesis*>(theChangedHyp);
+  const SMESH_Hypothesis* hyp = cSMESH_Hyp(theChangedHyp);
 
   const SMESH_Algo *foundAlgo = 0;
   SMESH_HypoFilter algoKind( SMESH_HypoFilter::IsAlgo() );
@@ -843,7 +858,8 @@ void SMESH_Mesh::NotifySubMeshesHypothesisModification(const SMESH_Hypothesis* t
         if ( GetHypotheses( aSubShape, compatibleHypoKind, usedHyps, true ) &&
              find( usedHyps.begin(), usedHyps.end(), hyp ) != usedHyps.end() )
         {
-          aSubMesh->ComputeStateEngine(SMESH_subMesh::MODIF_HYP);
+          aSubMesh->AlgoStateEngine(SMESH_subMesh::MODIF_HYP,
+                                    const_cast< SMESH_Hypothesis*>( hyp ));
 
           if ( algo->GetDim() == 1 && IsPropagationHypothesis( aSubShape ))
             CleanMeshOnPropagationChain( aSubShape );