+ //-----------------------------------------------------------------------------
+ /*!
+ * \brief Data to re-create a group on geometry
+ */
+ struct TGroupOnGeomData
+ {
+ int _oldID;
+ TopoDS_Shape _shape;
+ SMDSAbs_ElementType _type;
+ std::string _name;
+ Quantity_Color _color;
+
+ TGroupOnGeomData( const SMESHDS_GroupOnGeom* group )
+ {
+ _oldID = group->GetID();
+ _type = group->GetType();
+ _name = group->GetStoreName();
+ _color = group->GetColor();
+ }
+ };
+
+ //-----------------------------------------------------------------------------
+ /*!
+ * \brief Check if a filter is still valid after geometry removal
+ */
+ bool isValidGeomFilter( SMESH::Filter_var theFilter )
+ {
+ if ( theFilter->_is_nil() )
+ return false;
+ SMESH::Filter::Criteria_var criteria;
+ theFilter->GetCriteria( criteria.out() );
+
+ for ( CORBA::ULong iCr = 0; iCr < criteria->length(); ++iCr )
+ {
+ const char* thresholdID = criteria[ iCr ].ThresholdID.in();
+ std::string entry;
+ switch ( criteria[ iCr ].Type )
+ {
+ case SMESH::FT_BelongToGeom:
+ case SMESH::FT_BelongToPlane:
+ case SMESH::FT_BelongToCylinder:
+ case SMESH::FT_BelongToGenSurface:
+ case SMESH::FT_LyingOnGeom:
+ entry = thresholdID;
+ break;
+ case SMESH::FT_ConnectedElements:
+ if ( thresholdID )
+ {
+ entry = thresholdID;
+ break;
+ }
+ default:
+ continue;
+ }
+ SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen();
+ SALOMEDS::SObject_wrap so = gen->getStudyServant()->FindObjectID( entry.c_str() );
+ if ( so->_is_nil() )
+ return false;
+ CORBA::Object_var obj = so->GetObject();
+ GEOM::GEOM_Object_var geom = GEOM::GEOM_Object::_narrow( obj );
+ if ( gen->GeomObjectToShape( geom ).IsNull() )
+ return false;
+
+ } // loop on criteria
+
+ return true;
+ }
+}
+
+//=============================================================================
+/*!
+ * \brief Update data if geometry changes
+ *
+ * Issue 0022501
+ */
+//=============================================================================
+
+void SMESH_Mesh_i::CheckGeomModif( bool theIsBreakLink )
+{
+ SMESH::SMESH_Mesh_var me = _this();
+ GEOM::GEOM_Object_var mainGO = GetShapeToMesh();
+
+ TPythonDump dumpNothing; // prevent any dump
+
+ //bool removedFromClient = false;
+
+ if ( mainGO->_is_nil() ) // GEOM_Client cleared or geometry removed? (IPAL52735, PAL23636)
+ {
+ //removedFromClient = _impl->HasShapeToMesh();
+
+ // try to find geometry by study reference
+ SALOMEDS::SObject_wrap meshSO = _gen_i->ObjectToSObject( me );
+ SALOMEDS::SObject_wrap geomRefSO, geomSO;
+ if ( !meshSO->_is_nil() &&
+ meshSO->FindSubObject( SMESH::Tag_RefOnShape, geomRefSO.inout() ) &&
+ geomRefSO->ReferencedObject( geomSO.inout() ))
+ {
+ CORBA::Object_var geomObj = _gen_i->SObjectToObject( geomSO );
+ mainGO = GEOM::GEOM_Object::_narrow( geomObj );
+ }
+
+ if ( mainGO->_is_nil() && // geometry removed ==>
+ !geomRefSO->_is_nil() ) // remove geom dependent data: sub-meshes etc.
+ {
+ // convert geom dependent groups into standalone ones
+ CheckGeomGroupModif();
+
+ _impl->ShapeToMesh( TopoDS_Shape() );
+
+ // remove sub-meshes
+ std::map<int, SMESH::SMESH_subMesh_ptr>::iterator i_sm = _mapSubMeshIor.begin();
+ while ( i_sm != _mapSubMeshIor.end() )
+ {
+ SMESH::SMESH_subMesh_ptr sm = i_sm->second;
+ ++i_sm;
+ RemoveSubMesh( sm );
+ }
+ // remove all children except groups in the study
+ SALOMEDS::StudyBuilder_var builder = _gen_i->getStudyServant()->NewBuilder();
+ SALOMEDS::SObject_wrap so;
+ for ( CORBA::Long tag = SMESH::Tag_RefOnShape; tag <= SMESH::Tag_LastSubMesh; ++tag )
+ if ( meshSO->FindSubObject( tag, so.inout() ))
+ builder->RemoveObjectWithChildren( so );
+
+ _gen_i->SetPixMap( meshSO, "ICON_SMESH_TREE_MESH_IMPORTED" );
+
+ return;
+ }
+ }
+
+ if ( !_impl->HasShapeToMesh() ) return;
+
+
+ // Update after group modification
+
+ const bool geomChanged = ( mainGO->GetTick() != _mainShapeTick );
+ if ( !theIsBreakLink )
+ if ( mainGO->GetType() == GEOM_GROUP || !geomChanged ) // is group or not modified
+ {
+ int nb = NbNodes() + NbElements();
+ CheckGeomGroupModif();
+ if ( nb != NbNodes() + NbElements() ) // something removed due to hypotheses change
+ _gen_i->UpdateIcons( me );
+ return;
+ }
+
+ // Update after shape modification or breakLink w/o geometry change
+
+ GEOM_Client* geomClient = _gen_i->GetShapeReader();
+ if ( !geomClient ) return;
+ GEOM::GEOM_Gen_var geomGen = _gen_i->GetGeomEngine( mainGO );
+ if ( geomGen->_is_nil() ) return;
+ CORBA::String_var geomComponentType = geomGen->ComponentDataType();
+ bool isShaper = ( strcmp( geomComponentType.in(), "SHAPERSTUDY" ) == 0 );
+
+ SMESHDS_Mesh * meshDS = _impl->GetMeshDS();
+
+ TopoDS_Shape newShape = _gen_i->GeomObjectToShape( mainGO );
+ if ( meshDS->ShapeToIndex( newShape ) == 1 ) // not yet updated
+ {
+ CORBA::String_var ior = geomGen->GetStringFromIOR( mainGO );
+ geomClient->RemoveShapeFromBuffer( ior.in() );
+ newShape = _gen_i->GeomObjectToShape( mainGO );
+ }
+
+ // Update data taking into account that if topology doesn't change
+ // all sub-shapes change but IDs of sub-shapes remain (except for geom groups)
+
+ if ( _preMeshInfo )
+ _preMeshInfo->ForgetAllData();
+
+ if ( geomChanged || !isShaper )
+ _impl->Clear();
+ if ( newShape.IsNull() )
+ return;
+
+ _mainShapeTick = mainGO->GetTick();
+
+ // store data of groups on geometry including new TopoDS_Shape's
+ std::vector< TGroupOnGeomData > groupsData;
+ const std::set<SMESHDS_GroupBase*>& groups = meshDS->GetGroups();
+ groupsData.reserve( groups.size() );
+ TopTools_DataMapOfShapeShape old2newShapeMap;
+ std::set<SMESHDS_GroupBase*>::const_iterator g = groups.begin();
+ for ( ; g != groups.end(); ++g )
+ {
+ if ( const SMESHDS_GroupOnGeom* group = dynamic_cast< SMESHDS_GroupOnGeom* >( *g ))
+ {
+ groupsData.push_back( TGroupOnGeomData( group ));
+
+ // get a new shape
+ SMESH::SMESH_GroupOnGeom_var gog;
+ std::map<int, SMESH::SMESH_GroupBase_ptr>::iterator i_grp = _mapGroups.find( group->GetID() );
+ if ( i_grp != _mapGroups.end() )
+ gog = SMESH::SMESH_GroupOnGeom::_narrow( i_grp->second );
+
+ GEOM::GEOM_Object_var geom;
+ if ( !gog->_is_nil() )
+ {
+ if ( !theIsBreakLink )
+ geom = gog->GetShape();
+
+ if ( theIsBreakLink || geom->_is_nil() )
+ {
+ SALOMEDS::SObject_wrap grpSO = _gen_i->ObjectToSObject( gog );
+ SALOMEDS::SObject_wrap geomRefSO, geomSO;
+ if ( !grpSO->_is_nil() &&
+ grpSO->FindSubObject( SMESH::Tag_RefOnShape, geomRefSO.inout() ) &&
+ geomRefSO->ReferencedObject( geomSO.inout() ))
+ {
+ CORBA::Object_var geomObj = _gen_i->SObjectToObject( geomSO );
+ geom = GEOM::GEOM_Object::_narrow( geomObj );
+ }
+ }
+ }
+ if ( old2newShapeMap.IsBound( group->GetShape() ))
+ {
+ groupsData.back()._shape = old2newShapeMap( group->GetShape() );
+ }
+ else if ( !geom->_is_nil() )
+ {
+ groupsData.back()._shape = _gen_i->GeomObjectToShape( geom );
+ if ( meshDS->IsGroupOfSubShapes( groupsData.back()._shape ))
+ {
+ CORBA::String_var ior = geomGen->GetStringFromIOR( geom );
+ geomClient->RemoveShapeFromBuffer( ior.in() );
+ groupsData.back()._shape = _gen_i->GeomObjectToShape( geom );
+ }
+ old2newShapeMap.Bind( group->GetShape(), groupsData.back()._shape );
+ }
+
+ }
+ }
+ // store assigned hypotheses
+ std::vector< pair< int, THypList > > ids2Hyps;
+ const ShapeToHypothesis & hyps = meshDS->GetHypotheses();
+ for ( ShapeToHypothesis::Iterator s2hyps( hyps ); s2hyps.More(); s2hyps.Next() )
+ {
+ const TopoDS_Shape& s = s2hyps.Key();
+ const THypList& hyps = s2hyps.ChangeValue();
+ ids2Hyps.push_back( make_pair( meshDS->ShapeToIndex( s ), hyps ));
+ }
+
+ std::multimap< std::set<int>, int > ii2iMap; // group sub-ids to group id in SMESHDS
+
+ // count shapes excluding compounds corresponding to geom groups
+ int oldNbSubShapes = meshDS->MaxShapeIndex();
+ for ( ; oldNbSubShapes > 0; --oldNbSubShapes )
+ {
+ const TopoDS_Shape& s = meshDS->IndexToShape( oldNbSubShapes );
+ if ( s.IsNull() || s.ShapeType() != TopAbs_COMPOUND )
+ break;
+ // fill ii2iMap
+ std::set<int> subIds;
+ for ( TopoDS_Iterator it( s ); it.More(); it.Next() )
+ subIds.insert( meshDS->ShapeToIndex( it.Value() ));
+ ii2iMap.insert( std::make_pair( subIds, oldNbSubShapes ));
+ }
+
+ // check if shape topology changes - save shape type per shape ID
+ std::vector< TopAbs_ShapeEnum > shapeTypes( Max( oldNbSubShapes + 1, 1 ));
+ for ( int shapeID = oldNbSubShapes; shapeID > 0; --shapeID )
+ shapeTypes[ shapeID ] = meshDS->IndexToShape( shapeID ).ShapeType();
+
+ // change shape to mesh
+ _impl->ShapeToMesh( TopoDS_Shape() );
+ _impl->ShapeToMesh( newShape );
+
+ // check if shape topology changes - check new shape types
+ bool sameTopology = ( oldNbSubShapes == meshDS->MaxShapeIndex() );
+ for ( int shapeID = oldNbSubShapes; shapeID > 0 && sameTopology; --shapeID )
+ {
+ const TopoDS_Shape& s = meshDS->IndexToShape( shapeID );
+ sameTopology = ( !s.IsNull() && s.ShapeType() == shapeTypes[ shapeID ]);
+ }
+
+ // re-add shapes (compounds) of geom groups
+ typedef std::map< std::vector< int >, TGeomGroupData* > TIndices2GroupData;
+ TIndices2GroupData ii2grData;
+ std::vector< int > ii;
+ std::map< int, int > old2newIDs; // group IDs
+ std::list<TGeomGroupData>::iterator dataIt = _geomGroupData.begin();
+ for ( ; dataIt != _geomGroupData.end(); ++dataIt )
+ {
+ TGeomGroupData* data = &(*dataIt);
+ ii.reserve( data->_indices.size() );
+ ii.assign( data->_indices.begin(), data->_indices.end() );
+ TIndices2GroupData::iterator ii2gd = ii2grData.insert( std::make_pair( ii, data )).first;
+ if ( ii2gd->second != data )
+ {
+ data->_groupEntry = ii2gd->second->_groupEntry;
+ data->_indices = ii2gd->second->_indices;
+ continue;
+ }
+ const int oldNbSub = data->_indices.size();
+ const int soleOldID = oldNbSub == 1 ? *data->_indices.begin() : 0;
+ int oldID = 0;
+ std::multimap< std::set<int>, int >::iterator ii2i = ii2iMap.find( data->_indices );
+ if ( ii2i != ii2iMap.end() )
+ {
+ oldID = ii2i->second;
+ ii2iMap.erase( ii2i );
+ }
+ if ( !oldID && oldNbSub == 1 )
+ oldID = soleOldID;
+ if ( old2newIDs.count( oldID ))
+ continue;
+
+ int how = ( theIsBreakLink || !sameTopology ) ? IS_BREAK_LINK : MAIN_TRANSFORMED;
+ newShape = newGroupShape( *data, how );
+
+ if ( !newShape.IsNull() )
+ {
+ if ( oldNbSub > 1 && meshDS->ShapeToIndex( newShape ) > 0 ) // group reduced to one sub-shape
+ {
+ TopoDS_Compound compound;
+ BRep_Builder().MakeCompound( compound );
+ BRep_Builder().Add( compound, newShape );
+ newShape = compound;
+ }
+ int newID = _impl->GetSubMesh( newShape )->GetId();
+ if ( oldID /*&& oldID != newID*/ )
+ old2newIDs.insert( std::make_pair( oldID, newID ));
+ if ( oldNbSub == 1 )
+ old2newIDs.insert( std::make_pair( soleOldID, newID ));
+ }
+ }
+
+ // re-assign hypotheses
+ for ( size_t i = 0; i < ids2Hyps.size(); ++i )
+ {
+ int sID = ids2Hyps[i].first;
+ if ( sID != 1 )
+ {
+ std::map< int, int >::iterator o2n = old2newIDs.find( sID );
+ if ( o2n != old2newIDs.end() )
+ sID = o2n->second;
+ else if ( !sameTopology )
+ continue;
+ }
+ const TopoDS_Shape& s = meshDS->IndexToShape( sID );
+ if ( s.IsNull() )
+ continue;
+ const THypList& hyps = ids2Hyps[i].second;
+ THypList::const_iterator h = hyps.begin();
+ for ( ; h != hyps.end(); ++h )
+ _impl->AddHypothesis( s, (*h)->GetID() );
+ }
+
+ {
+ // restore groups on geometry
+ for ( size_t i = 0; i < groupsData.size(); ++i )
+ {
+ const TGroupOnGeomData& data = groupsData[i];
+ if ( data._shape.IsNull() )
+ continue;
+
+ std::map<int, SMESH::SMESH_GroupBase_ptr>::iterator i2g = _mapGroups.find( data._oldID );
+ if ( i2g == _mapGroups.end() ) continue;
+
+ SMESH_GroupBase_i* gr_i = SMESH::DownCast<SMESH_GroupBase_i*>( i2g->second );
+ if ( !gr_i ) continue;
+
+ SMESH_Group* g = _impl->AddGroup( data._type, data._name.c_str(), data._oldID, data._shape );
+ if ( !g )
+ _mapGroups.erase( i2g );
+ else
+ g->GetGroupDS()->SetColor( data._color );
+ }
+
+ if ( !sameTopology )
+ {
+ std::map< int, int >::iterator o2n = old2newIDs.begin();
+ for ( ; o2n != old2newIDs.end(); ++o2n )
+ {
+ int newID = o2n->second, oldID = o2n->first;
+ if ( newID == oldID || !_mapSubMesh.count( oldID ))
+ continue;
+ if ( newID > 0 )
+ {
+ _mapSubMesh [ newID ] = _impl->GetSubMeshContaining( newID );
+ _mapSubMesh_i [ newID ] = _mapSubMesh_i [ oldID ];
+ _mapSubMeshIor[ newID ] = _mapSubMeshIor[ oldID ];
+ }
+ _mapSubMesh. erase(oldID);
+ _mapSubMesh_i. erase(oldID);
+ _mapSubMeshIor.erase(oldID);
+ if ( newID > 0 )
+ _mapSubMesh_i [ newID ]->changeLocalId( newID );
+ }
+ }
+
+ // update _mapSubMesh
+ std::map<int, ::SMESH_subMesh*>::iterator i_sm = _mapSubMesh.begin();
+ for ( ; i_sm != _mapSubMesh.end(); ++i_sm )
+ i_sm->second = _impl->GetSubMesh( meshDS->IndexToShape( i_sm->first ));
+ }
+
+ if ( !sameTopology )
+ {
+ // remove invalid study sub-objects
+ CheckGeomGroupModif();
+ }
+
+ _gen_i->UpdateIcons( me );
+
+ if ( !theIsBreakLink && isShaper )
+ {
+ SALOMEDS::SObject_wrap meshSO = _gen_i->ObjectToSObject( me );
+ if ( !meshSO->_is_nil() )
+ _gen_i->SetPixMap(meshSO, "ICON_SMESH_TREE_GEOM_MODIF");
+ }