+
+//================================================================================
+/*!
+ * \brief Constructor of OwnListenerData
+ */
+//================================================================================
+
+SMESH_subMesh::OwnListenerData::OwnListenerData( SMESH_subMesh* sm, EventListener* el):
+ mySubMesh( sm ),
+ myMeshID( sm ? sm->GetFather()->GetId() : -1 ),
+ mySubMeshID( sm ? sm->GetId() : -1 ),
+ myListener( el )
+{
+}
+
+//================================================================================
+/*!
+ * \brief Sets an event listener and its data to a submesh
+ * \param listener - the listener to store
+ * \param data - the listener data to store
+ * \param where - the submesh to store the listener and it's data
+ *
+ * It remembers the submesh where it puts the listener in order to delete
+ * them when HYP_OK algo_state is lost
+ * After being set, event listener is notified on each event of where submesh.
+ */
+//================================================================================
+
+void SMESH_subMesh::SetEventListener(EventListener* listener,
+ EventListenerData* data,
+ SMESH_subMesh* where)
+{
+ if ( listener && where ) {
+ where->setEventListener( listener, data );
+ _ownListeners.push_back( OwnListenerData( where, listener ));
+ }
+}
+
+//================================================================================
+/*!
+ * \brief Sets an event listener and its data to a submesh
+ * \param listener - the listener to store
+ * \param data - the listener data to store
+ *
+ * After being set, event listener is notified on each event of a submesh.
+ */
+//================================================================================
+
+void SMESH_subMesh::setEventListener(EventListener* listener,
+ EventListenerData* data)
+{
+ map< EventListener*, EventListenerData* >::iterator l_d =
+ _eventListeners.find( listener );
+ if ( l_d != _eventListeners.end() ) {
+ EventListenerData* curData = l_d->second;
+ if ( curData && curData != data && curData->IsDeletable() )
+ delete curData;
+ l_d->second = data;
+ }
+ else
+ {
+ for ( l_d = _eventListeners.begin(); l_d != _eventListeners.end(); ++l_d )
+ if ( listener->GetName() == l_d->first->GetName() )
+ {
+ EventListenerData* curData = l_d->second;
+ if ( curData && curData != data && curData->IsDeletable() )
+ delete curData;
+ if ( l_d->first != listener && l_d->first->IsDeletable() )
+ delete l_d->first;
+ _eventListeners.erase( l_d );
+ break;
+ }
+ _eventListeners.insert( make_pair( listener, data ));
+ }
+}
+
+//================================================================================
+/*!
+ * \brief Return an event listener data
+ * \param listener - the listener whose data is
+ * \param myOwn - if \c true, returns a listener set by this sub-mesh,
+ * else returns a listener listening to events of this sub-mesh
+ * \retval EventListenerData* - found data, maybe NULL
+ */
+//================================================================================
+
+EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener,
+ const bool myOwn) const
+{
+ if ( myOwn )
+ {
+ list< OwnListenerData >::const_iterator d;
+ for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
+ {
+ if ( d->myListener == listener && _father->MeshExists( d->myMeshID ))
+ return d->mySubMesh->GetEventListenerData( listener, !myOwn );
+ }
+ }
+ else
+ {
+ map< EventListener*, EventListenerData* >::const_iterator l_d =
+ _eventListeners.find( listener );
+ if ( l_d != _eventListeners.end() )
+ return l_d->second;
+ }
+ return 0;
+}
+
+//================================================================================
+/*!
+ * \brief Return an event listener data
+ * \param listenerName - the listener name
+ * \param myOwn - if \c true, returns a listener set by this sub-mesh,
+ * else returns a listener listening to events of this sub-mesh
+ * \retval EventListenerData* - found data, maybe NULL
+ */
+//================================================================================
+
+EventListenerData* SMESH_subMesh::GetEventListenerData(const string& listenerName,
+ const bool myOwn) const
+{
+ if ( myOwn )
+ {
+ list< OwnListenerData >::const_iterator d;
+ for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
+ {
+ if ( _father->MeshExists( d->myMeshID ) && listenerName == d->myListener->GetName())
+ return d->mySubMesh->GetEventListenerData( listenerName, !myOwn );
+ }
+ }
+ else
+ {
+ map< EventListener*, EventListenerData* >::const_iterator l_d = _eventListeners.begin();
+ for ( ; l_d != _eventListeners.end(); ++l_d )
+ if ( listenerName == l_d->first->GetName() )
+ return l_d->second;
+ }
+ return 0;
+}
+
+//================================================================================
+/*!
+ * \brief Notify stored event listeners on the occured event
+ * \param event - algo_event or compute_event itself
+ * \param eventType - algo_event or compute_event
+ * \param hyp - hypothesis, if eventType is algo_event
+ */
+//================================================================================
+
+void SMESH_subMesh::notifyListenersOnEvent( const int event,
+ const event_type eventType,
+ SMESH_Hypothesis* hyp)
+{
+ list< pair< EventListener*, EventListenerData* > > eventListeners( _eventListeners.begin(),
+ _eventListeners.end());
+ list< pair< EventListener*, EventListenerData* > >::iterator l_d = eventListeners.begin();
+ for ( ; l_d != eventListeners.end(); ++l_d )
+ {
+ std::pair< EventListener*, EventListenerData* > li_da = *l_d;
+ if ( !_eventListeners.count( li_da.first )) continue;
+
+ if ( li_da.first->myBusySM.insert( this ).second )
+ {
+ const bool isDeletable = li_da.first->IsDeletable();
+
+ li_da.first->ProcessEvent( event, eventType, this, li_da.second, hyp );
+
+ if ( !isDeletable || _eventListeners.count( li_da.first ))
+ li_da.first->myBusySM.erase( this ); // a listener is hopefully not dead
+ }
+ }
+}
+
+//================================================================================
+/*!
+ * \brief Unregister the listener and delete listener's data
+ * \param listener - the event listener
+ */
+//================================================================================
+
+void SMESH_subMesh::DeleteEventListener(EventListener* listener)
+{
+ map< EventListener*, EventListenerData* >::iterator l_d =
+ _eventListeners.find( listener );
+ if ( l_d != _eventListeners.end() && l_d->first )
+ {
+ if ( l_d->second && l_d->second->IsDeletable() )
+ {
+ delete l_d->second;
+ }
+ l_d->first->myBusySM.erase( this );
+ if ( l_d->first->IsDeletable() )
+ {
+ l_d->first->BeforeDelete( this, l_d->second );
+ delete l_d->first;
+ }
+ _eventListeners.erase( l_d );
+ }
+}
+
+//================================================================================
+/*!
+ * \brief Delete event listeners depending on algo of this submesh
+ */
+//================================================================================
+
+void SMESH_subMesh::deleteOwnListeners()
+{
+ list< OwnListenerData >::iterator d;
+ for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
+ {
+ SMESH_Mesh* mesh = _father->FindMesh( d->myMeshID );
+ if ( !mesh || !mesh->GetSubMeshContaining( d->mySubMeshID ))
+ continue;
+ d->mySubMesh->DeleteEventListener( d->myListener );
+ }
+ _ownListeners.clear();
+}
+
+//=======================================================================
+//function : loadDependentMeshes
+//purpose : loads dependent meshes on SUBMESH_LOADED event
+//=======================================================================
+
+void SMESH_subMesh::loadDependentMeshes()
+{
+ list< OwnListenerData >::iterator d;
+ for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
+ if ( _father != d->mySubMesh->_father )
+ d->mySubMesh->_father->Load();
+
+ // map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
+ // for ( ; l_d != _eventListeners.end(); ++l_d )
+ // if ( l_d->second )
+ // {
+ // const list<SMESH_subMesh*>& smList = l_d->second->mySubMeshes;
+ // list<SMESH_subMesh*>::const_iterator sm = smList.begin();
+ // for ( ; sm != smList.end(); ++sm )
+ // if ( _father != (*sm)->_father )
+ // (*sm)->_father->Load();
+ // }
+}
+
+//================================================================================
+/*!
+ * \brief Do something on a certain event
+ * \param event - algo_event or compute_event itself
+ * \param eventType - algo_event or compute_event
+ * \param subMesh - the submesh where the event occures
+ * \param data - listener data stored in the subMesh
+ * \param hyp - hypothesis, if eventType is algo_event
+ *
+ * The base implementation translates CLEAN event to the subMesh
+ * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
+ * successful COMPUTE event.
+ */
+//================================================================================
+
+void SMESH_subMeshEventListener::ProcessEvent(const int event,
+ const int eventType,
+ SMESH_subMesh* subMesh,
+ EventListenerData* data,
+ const SMESH_Hypothesis* /*hyp*/)
+{
+ if ( data && !data->mySubMeshes.empty() &&
+ eventType == SMESH_subMesh::COMPUTE_EVENT)
+ {
+ ASSERT( data->mySubMeshes.front() != subMesh );
+ list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
+ list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
+ switch ( event ) {
+ case SMESH_subMesh::CLEAN:
+ for ( ; smIt != smEnd; ++ smIt)
+ (*smIt)->ComputeStateEngine( event );
+ break;
+ case SMESH_subMesh::COMPUTE:
+ case SMESH_subMesh::COMPUTE_SUBMESH:
+ if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
+ for ( ; smIt != smEnd; ++ smIt)
+ (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
+ break;
+ default:;
+ }
+ }
+}
+
+namespace {
+
+ //================================================================================
+ /*!
+ * \brief Iterator over submeshes and optionally prepended or appended one
+ */
+ //================================================================================
+
+ struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
+ {
+ _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
+ SMESH_subMesh* prepend,
+ SMESH_subMesh* append): myIt(subIt),myAppend(append)
+ {
+ myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
+ if ( myCur == append ) append = 0;
+ }
+ /// Return true if and only if there are other object in this iterator
+ virtual bool more()
+ {
+ return myCur;
+ }
+ /// Return the current object and step to the next one
+ virtual SMESH_subMesh* next()
+ {
+ SMESH_subMesh* res = myCur;
+ if ( myIt->more() ) { myCur = myIt->next(); }
+ else { myCur = myAppend; myAppend = 0; }
+ return res;
+ }
+ /// ~
+ ~_Iterator()
+ { delete myIt; }
+ ///
+ SMESH_subMesh *myAppend, *myCur;
+ SMDS_Iterator<SMESH_subMesh*> *myIt;
+ };
+}
+
+//================================================================================
+/*!
+ * \brief Return iterator on the submeshes this one depends on
+ * \param includeSelf - this submesh to be returned also
+ * \param reverse - if true, complex shape submeshes go first
+ */
+//================================================================================
+
+SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
+ const bool reverse) const
+{
+ SMESH_subMesh *me = (SMESH_subMesh*) this;
+ SMESH_subMesh *prepend=0, *append=0;
+ if ( includeSelf ) {
+ if ( reverse ) prepend = me;
+ else append = me;
+ }
+ typedef map < int, SMESH_subMesh * > TMap;
+ if ( reverse )
+ {
+ return SMESH_subMeshIteratorPtr
+ ( new _Iterator( new SMDS_mapReverseIterator<TMap>( me->DependsOn() ), prepend, append ));
+ }
+ {
+ return SMESH_subMeshIteratorPtr
+ ( new _Iterator( new SMDS_mapIterator<TMap>( me->DependsOn() ), prepend, append ));
+ }
+}
+
+//================================================================================
+/*!
+ * \brief Returns ancestor sub-meshes. Finds them if not yet found.
+ */
+//================================================================================
+
+const std::vector< SMESH_subMesh * > & SMESH_subMesh::GetAncestors() const
+{
+ if ( _ancestors.empty() &&
+ !_subShape.IsSame( _father->GetShapeToMesh() ))
+ {
+ const TopTools_ListOfShape& ancShapes = _father->GetAncestors( _subShape );
+
+ SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
+ me->_ancestors.reserve( ancShapes.Extent() );
+
+ TopTools_MapOfShape map;
+
+ for ( TopTools_ListIteratorOfListOfShape it( ancShapes ); it.More(); it.Next() )
+ if ( SMESH_subMesh* sm = _father->GetSubMeshContaining( it.Value() ))
+ if ( map.Add( it.Value() ))
+ me->_ancestors.push_back( sm );
+ }
+
+ return _ancestors;
+}
+
+//================================================================================
+/*!
+ * \brief Clears the vector of ancestor sub-meshes
+ */
+//================================================================================
+
+void SMESH_subMesh::ClearAncestors()
+{
+ _ancestors.clear();
+}
+
+//================================================================================
+/*!
+ * \brief Find common submeshes (based on shared sub-shapes with other
+ * \param theOther submesh to check
+ * \param theSetOfCommon set of common submesh
+ */
+//================================================================================
+
+bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
+ std::set<const SMESH_subMesh*>& theSetOfCommon ) const
+{
+ int oldNb = theSetOfCommon.size();
+
+ // check main submeshes
+ const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
+ if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
+ theSetOfCommon.insert( this );
+ if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
+ theSetOfCommon.insert( theOther );
+
+ // check common submeshes
+ map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
+ for( ; mapIt != _mapDepend.end(); mapIt++ )
+ if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
+ theSetOfCommon.insert( (*mapIt).second );
+ return oldNb < theSetOfCommon.size();
+}