Salome HOME
bos #24257 [CEA] Fatal error when creating a submesh if the mesh is on a sub-shape
[modules/smesh.git] / src / SMESHGUI / SMESHGUI_MeshOp.cxx
index c33a4fe927cf45fac1d706b6b8439eef6b475445..68791630a070bacdbb64d2f1fe3a7a8ea4f50350 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2019  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2021  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
@@ -43,6 +43,7 @@
 // SALOME GEOM includes
 #include <GEOMBase.h>
 #include <GEOMImpl_Types.hxx>
+#include <GEOM_Client.hxx>
 #include <GEOM_SelectionFilter.h>
 #include <GEOM_wrap.hxx>
 #include <GeometryGUI.h>
@@ -318,10 +319,29 @@ SUIT_SelectionFilter* SMESHGUI_MeshOp::createFilter( const int theId ) const
     return 0;
 }
 
+//================================================================================
+/*!
+ * \brief Return type of shape contained in a group
+ */
+//================================================================================
+
+TopAbs_ShapeEnum getGroupType(const TopoDS_Shape& group)
+{
+  if ( group.ShapeType() != TopAbs_COMPOUND )
+    return group.ShapeType();
+
+  // iterate on a compound
+  TopoDS_Iterator it( group );
+  if ( it.More() )
+    return getGroupType( it.Value() );
+
+  return TopAbs_SHAPE;
+}
+
 //================================================================================
 /*!
  * \brief check if selected shape is a sub-shape of the shape to mesh
 * \retval bool - check result
 \retval bool - check result
  */
 //================================================================================
 bool SMESHGUI_MeshOp::isSubshapeOk() const
@@ -346,7 +366,7 @@ bool SMESHGUI_MeshOp::isSubshapeOk() const
   myDlg->selectedObject(SMESHGUI_MeshDlg::Geom, aGEOMs);
 
   if (aGEOMs.count() > 0) {
-    GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
+    GEOM::GEOM_Gen_var geomGen = mainGeom->GetGen();
     if (geomGen->_is_nil()) return false;
 
     GEOM::GEOM_IGroupOperations_wrap op = geomGen->GetIGroupOperations();
@@ -354,7 +374,8 @@ bool SMESHGUI_MeshOp::isSubshapeOk() const
 
     // check all selected shapes
     QStringList::const_iterator aSubShapesIter = aGEOMs.begin();
-    for ( ; aSubShapesIter != aGEOMs.end(); aSubShapesIter++) {
+    for ( ; aSubShapesIter != aGEOMs.end(); aSubShapesIter++)
+    {
       QString aSubGeomEntry = (*aSubShapesIter);
       _PTR(SObject) pSubGeom = SMESH::getStudy()->FindObjectID(aSubGeomEntry.toUtf8().data());
       if (!pSubGeom) return false;
@@ -366,7 +387,8 @@ bool SMESHGUI_MeshOp::isSubshapeOk() const
       // skl for NPAL14695 - implementation of searching of mainObj
       GEOM::GEOM_Object_var mainObj = op->GetMainShape(aSubGeomVar); /* _var not _wrap as
                                                                         mainObj already exists! */
-      while( !mainObj->_is_nil()) {
+      while( !mainObj->_is_nil())
+      {
         CORBA::String_var entry1 = mainObj->GetEntry();
         CORBA::String_var entry2 = mainGeom->GetEntry();
         if (std::string( entry1.in() ) == entry2.in() )
@@ -382,21 +404,18 @@ bool SMESHGUI_MeshOp::isSubshapeOk() const
                                                             GEOM::SHAPE,/*sorted=*/false);
         if ( ids->length() > 0 )
         {
-          ids->length( 1 );
-          GEOM::GEOM_Object_var compSub = geomGen->AddSubShape( aSubGeomVar, ids );
-          if ( !compSub->_is_nil() )
-          {
-            GEOM::ListOfGO_var shared = sop->GetSharedShapes( mainGeom,
-                                                              compSub,
-                                                              compSub->GetShapeType() );
-            geomGen->RemoveObject( compSub );
-            compSub->UnRegister();
-            if ( shared->length() > 0 ) {
-              geomGen->RemoveObject( shared[0] );
-              shared[0]->UnRegister();
-            }
-            return ( shared->length() > 0 );
-          }
+          GEOM_Client geomClient;
+          TopoDS_Shape  subShape = geomClient.GetShape( geomGen, aSubGeomVar );
+          TopoDS_Shape mainShape = geomClient.GetShape( geomGen, mainGeom );
+          if ( subShape.IsNull() || mainShape.IsNull() )
+            return false;
+
+          TopAbs_ShapeEnum subType = getGroupType( subShape );
+          TopTools_IndexedMapOfShape subMap;
+          TopExp::MapShapes( subShape, subType, subMap );
+          for ( TopExp_Explorer exp( mainShape, subType ); exp.More(); exp.Next() )
+            if ( subMap.Contains( exp.Current() ))
+              return true;
         }
       }
     }
@@ -780,7 +799,7 @@ bool SMESHGUI_MeshOp::isCompatibleToGeometry(HypothesisData* theAlgoData,
 
   bool isApplicable = false;
   if ( myGeomEntry == myLastGeomEntry && !myGeomEntry.isEmpty() ) {
-    THypLabelIsAppMap::const_iterator lab2isApp = myHypMapIsApplicable.find( theAlgoData->Label );
+    THypLabelIsAppMap::const_iterator lab2isApp = myHypMapIsApplicable.find( theAlgoData->TypeName );
     if ( lab2isApp != myHypMapIsApplicable.end() ) {
       isApplicable = lab2isApp.value();
       return isApplicable;
@@ -791,7 +810,7 @@ bool SMESHGUI_MeshOp::isCompatibleToGeometry(HypothesisData* theAlgoData,
     toCheckIsApplicableToAll = ( myGeom->GetType() == GEOM_GROUP );
 
   isApplicable = SMESH::IsApplicable( theAlgoData->TypeName, myGeom, toCheckIsApplicableToAll );
-  myHypMapIsApplicable.insert( theAlgoData->Label, isApplicable );
+  myHypMapIsApplicable.insert( theAlgoData->TypeName, isApplicable );
   return isApplicable;
 }
 
@@ -882,7 +901,7 @@ void SMESHGUI_MeshOp::availableHyps( const int       theDim,
                                      THypDataList&   theDataList,
                                      HypothesisData* thePrevAlgoData,
                                      HypothesisData* theNextAlgoData,
-                                     const QString&  theMeshType)
+                                     const QString&  /*theMeshType*/)
 {
   theDataList.clear();
   theHyps.clear();
@@ -1434,7 +1453,7 @@ void SMESHGUI_MeshOp::onEditHyp( const int theHypType, const int theIndex )
  *   1 = accepted
  */
 //================================================================================
-void SMESHGUI_MeshOp::onHypoEdited( int result )
+void SMESHGUI_MeshOp::onHypoEdited( int /*result*/ )
 {
   int obj = myDlg->getActiveObject();
   onActivateObject( obj ); // Issue 0020170. Restore filters
@@ -1756,7 +1775,7 @@ void SMESHGUI_MeshOp::createSubMeshOnInternalEdges( SMESH::SMESH_Mesh_ptr theMes
   for ( size_t i = 0; i < internalEdges.size(); ++i )
     intIDSet.insert( shapeIDs.FindIndex( internalEdges[ i ]));
 
-  GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
+  GEOM::GEOM_Gen_var geomGen = theMainShape->GetGen();
   if (geomGen->_is_nil()) return;
 
   GEOM::GEOM_Object_var edgeGroup;
@@ -1913,6 +1932,7 @@ void SMESHGUI_MeshOp::onHypoSet( const QString& theSetName )
   if ( aHypoSet->toUseCommonSize() && !getAverageSize( myAverageSize ))
     return;
 
+  int maxDim = -1;
   for ( int isAlgo = 1; isAlgo >= 0; --isAlgo )
     for ( aHypoSet->init( isAlgo, setType ); aHypoSet->more(); aHypoSet->next() )
     {
@@ -1930,6 +1950,7 @@ void SMESHGUI_MeshOp::onHypoSet( const QString& theSetName )
         {
           setCurrentHyp( myDim, Algo, index );
           onAlgoSelected( index, myDim );
+          maxDim = Max( maxDim, myDim );
         }
       }
       else
@@ -1963,6 +1984,9 @@ void SMESHGUI_MeshOp::onHypoSet( const QString& theSetName )
       }
     }
 
+  if ( maxDim > 0 )
+    myDlg->setCurrentTab( maxDim );
+
   return;
 }
 
@@ -2062,7 +2086,7 @@ bool SMESHGUI_MeshOp::createMesh( QString& theMess, QStringList& theEntryList )
     {
       // Create groups on all geom groups
 
-      GEOM::GEOM_Gen_var           geomGen = SMESH::GetGEOMGen();
+      GEOM::GEOM_Gen_var           geomGen = aGeomVar->GetGen();
       GEOM::GEOM_IShapesOperations_wrap op = geomGen->GetIShapesOperations();
       GEOM::ListOfGO_var        geomGroups = op->GetExistingSubObjects( aGeomVar,
                                                                         /*groupsOnly=*/false );
@@ -2138,11 +2162,11 @@ bool SMESHGUI_MeshOp::createSubMesh( QString& theMess, QStringList& theEntryList
   else if (aGEOMs.count() > 1)
   {
     // create a GEOM group
-    GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
-    if (!geomGen->_is_nil()) {
-      GEOM::GEOM_IGroupOperations_wrap op =
-        geomGen->GetIGroupOperations();
-      if (!op->_is_nil()) {
+    GEOM::GEOM_Gen_var geomGen = mainGeom->GetGen();
+    if ( !geomGen->_is_nil() ) {
+      GEOM::GEOM_IGroupOperations_wrap op = geomGen->GetIGroupOperations();
+      if ( !op->_is_nil() )
+      {
         // check and add all selected GEOM objects: they must be
         // a sub-shapes of the main GEOM and must be of one type
         int iSubSh = 0;
@@ -2946,9 +2970,19 @@ void SMESHGUI_MeshOp::updateHypoSets()
       }
       currentHypoSet->setAlgoAvailable( sType, isAvailable );
     }
+    if ( currentHypoSet->hasAlgo( HypothesesSet::MAIN ) &&
+         currentHypoSet->hasAlgo( HypothesesSet::ALT ))
+    {
+      HypothesesSet::SetType setType = HypothesesSet::getPreferredHypType();
+      if ( !currentHypoSet->getAlgoAvailable( setType ))
+        continue; // not add if a preferred type not available currently
+    }
+
     if ( currentHypoSet->getAlgoAvailable( HypothesesSet::MAIN ) ||
          currentHypoSet->getAlgoAvailable( HypothesesSet::ALT ))
+    {
       aFilteredHypothesesSetsList.append( *inHypoSetName );
+    }
   }
   myDlg->setHypoSets( aFilteredHypothesesSetsList );
 }
@@ -2960,7 +2994,7 @@ void SMESHGUI_MeshOp::updateHypoSets()
  * \param theIndex - Index of current type of mesh
  */
 //================================================================================
-void SMESHGUI_MeshOp::onAlgoSetByMeshType( const int theTabIndex, const int theIndex )
+void SMESHGUI_MeshOp::onAlgoSetByMeshType( const int /*theTabIndex*/, const int /*theIndex*/ )
 {
   setFilteredAlgoData();
 }