+//=============================================================================
+/*!
+ * \brief remove submesh order from Mesh
+ */
+//=============================================================================
+
+void SMESH_Mesh::ClearMeshOrder()
+{
+ _mySubMeshOrder.clear();
+}
+
+//=============================================================================
+/*!
+ * \brief remove submesh order from Mesh
+ */
+//=============================================================================
+
+void SMESH_Mesh::SetMeshOrder(const TListOfListOfInt& theOrder )
+{
+ _mySubMeshOrder = theOrder;
+}
+
+//=============================================================================
+/*!
+ * \brief return submesh order if any
+ */
+//=============================================================================
+
+const TListOfListOfInt& SMESH_Mesh::GetMeshOrder() const
+{
+ return _mySubMeshOrder;
+}
+
+//=============================================================================
+/*!
+ * \brief fill _mapAncestors
+ */
+//=============================================================================
+
+void SMESH_Mesh::fillAncestorsMap(const TopoDS_Shape& theShape)
+{
+
+ int desType, ancType;
+ if ( !theShape.IsSame( GetShapeToMesh()) && theShape.ShapeType() == TopAbs_COMPOUND )
+ {
+ // a geom group is added. Insert it into lists of ancestors before
+ // the first ancestor more complex than group members
+ int memberType = TopoDS_Iterator( theShape ).Value().ShapeType();
+ for ( desType = TopAbs_VERTEX; desType >= memberType; desType-- )
+ for (TopExp_Explorer des( theShape, TopAbs_ShapeEnum( desType )); des.More(); des.Next())
+ {
+ TopTools_ListOfShape& ancList = _mapAncestors.ChangeFromKey( des.Current() );
+ TopTools_ListIteratorOfListOfShape ancIt (ancList);
+ while ( ancIt.More() && ancIt.Value().ShapeType() >= memberType )
+ ancIt.Next();
+ if ( ancIt.More() )
+ ancList.InsertBefore( theShape, ancIt );
+ }
+ }
+ {
+ for ( desType = TopAbs_VERTEX; desType > TopAbs_COMPOUND; desType-- )
+ for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
+ TopExp::MapShapesAndAncestors ( theShape,
+ (TopAbs_ShapeEnum) desType,
+ (TopAbs_ShapeEnum) ancType,
+ _mapAncestors );
+ }
+}
+
+//=============================================================================
+/*!
+ * \brief sort submeshes according to stored mesh order
+ * \param theListToSort in out list to be sorted
+ * \return FALSE if nothing sorted
+ */
+//=============================================================================
+
+bool SMESH_Mesh::SortByMeshOrder(list<SMESH_subMesh*>& theListToSort) const
+{
+ if ( !_mySubMeshOrder.size() || theListToSort.size() < 2)
+ return true;
+
+ bool res = false;
+ list<SMESH_subMesh*> onlyOrderedList;
+ // collect all ordered submeshes in one list as pointers
+ // and get their positions within theListToSort
+ typedef list<SMESH_subMesh*>::iterator TPosInList;
+ map< int, TPosInList > sortedPos;
+ TPosInList smBeg = theListToSort.begin(), smEnd = theListToSort.end();
+ TListOfListOfInt::const_iterator listIddIt = _mySubMeshOrder.begin();
+ for( ; listIddIt != _mySubMeshOrder.end(); listIddIt++) {
+ const TListOfInt& listOfId = *listIddIt;
+ TListOfInt::const_iterator idIt = listOfId.begin();
+ for ( ; idIt != listOfId.end(); idIt++ ) {
+ if ( SMESH_subMesh * sm = GetSubMeshContaining( *idIt )) {
+ TPosInList smPos = find( smBeg, smEnd, sm );
+ if ( smPos != smEnd ) {
+ onlyOrderedList.push_back( sm );
+ sortedPos[ distance( smBeg, smPos )] = smPos;
+ }
+ }
+ }
+ }
+ if (onlyOrderedList.size() < 2)
+ return res;
+ res = true;
+
+ list<SMESH_subMesh*>::iterator onlyBIt = onlyOrderedList.begin();
+ list<SMESH_subMesh*>::iterator onlyEIt = onlyOrderedList.end();
+
+ // iterates on ordered submeshes and insert them in detected positions
+ map< int, TPosInList >::iterator i_pos = sortedPos.begin();
+ for ( ; onlyBIt != onlyEIt; ++onlyBIt, ++i_pos )
+ *(i_pos->second) = *onlyBIt;
+
+ return res;
+}
+
+//=============================================================================
+/*!
+ * \brief sort submeshes according to stored mesh order
+ * \param theListToSort in out list to be sorted
+ * \return FALSE if nothing sorted
+ */
+//=============================================================================
+
+list<SMESH_subMesh*> SMESH_Mesh::getAncestorsSubMeshes
+ (const TopoDS_Shape& theSubShape) const
+{
+ list<SMESH_subMesh*> listOfSubMesh;
+ TopTools_ListIteratorOfListOfShape it( GetAncestors( theSubShape ));
+ for (; it.More(); it.Next() )
+ if ( SMESH_subMesh* sm = GetSubMeshContaining( it.Value() ))
+ listOfSubMesh.push_back(sm);
+
+ // sort submeshes according to stored mesh order
+ SortByMeshOrder( listOfSubMesh );
+
+ return listOfSubMesh;
+}