+ InsertAssociation( theShape1, theShape2, theMap, bidirect );
+ return true;
+ }
+ // ----------------------------------------------------------------------
+ case TopAbs_COMPOUND: { // GROUP
+ // ----------------------------------------------------------------------
+ // Maybe groups contain only one member
+ TopoDS_Iterator it1( theShape1 ), it2( theShape2 );
+ TopAbs_ShapeEnum memberType = it1.Value().ShapeType();
+ int nbMembers = Count( theShape1, memberType, true );
+ if ( nbMembers == 0 ) return true;
+ if ( nbMembers == 1 ) {
+ return FindSubShapeAssociation( it1.Value(), theMesh1, it2.Value(), theMesh2, theMap );
+ }
+ // Try to make shells of faces
+ //
+ BRep_Builder builder;
+ TopoDS_Shell shell1, shell2;
+ builder.MakeShell(shell1); builder.MakeShell(shell2);
+ if ( memberType == TopAbs_FACE ) {
+ // just add faces of groups to shells
+ for (; it1.More(); it1.Next(), it2.Next() )
+ builder.Add( shell1, it1.Value() ), builder.Add( shell2, it2.Value() );
+ }
+ else if ( memberType == TopAbs_EDGE ) {
+ // Try to add faces sharing more than one edge of a group or
+ // sharing all its vertices with the group
+ TopTools_IndexedMapOfShape groupVertices[2];
+ TopExp::MapShapes( theShape1, TopAbs_VERTEX, groupVertices[0]);
+ TopExp::MapShapes( theShape2, TopAbs_VERTEX, groupVertices[1]);
+ //
+ TopTools_MapOfShape groupEdges[2], addedFaces[2];
+ bool hasInitAssoc = (!theMap.IsEmpty()), initAssocOK = !hasInitAssoc;
+ for (; it1.More(); it1.Next(), it2.Next() ) {
+ groupEdges[0].Add( it1.Value() );
+ groupEdges[1].Add( it2.Value() );
+ if ( !initAssocOK ) {
+ // for shell association there must be an edge with both vertices bound
+ TopoDS_Vertex v1, v2;
+ TopExp::Vertices( TopoDS::Edge( it1.Value()), v1, v2 );
+ initAssocOK = ( theMap.IsBound( v1 ) && theMap.IsBound( v2 ));
+ }
+ }
+ for (int is2ndGroup = 0; initAssocOK && is2ndGroup < 2; ++is2ndGroup) {
+ const TopoDS_Shape& group = is2ndGroup ? theShape2: theShape1;
+ SMESH_Mesh* mesh = is2ndGroup ? theMesh2 : theMesh1;
+ TopoDS_Shell& shell = is2ndGroup ? shell2 : shell1;
+ for ( TopoDS_Iterator it( group ); it.More(); it.Next() ) {
+ const TopoDS_Edge& edge = TopoDS::Edge( it.Value() );
+ TopoDS_Face face;
+ for ( int iF = 0; iF < 2; ++iF ) { // loop on 2 faces sharing edge
+ face = GetNextFace(mesh->GetAncestorMap(), edge, face);
+ if ( !face.IsNull() ) {
+ int nbGroupEdges = 0;
+ for ( TopExp_Explorer f( face, TopAbs_EDGE ); f.More(); f.Next())
+ if ( groupEdges[ is2ndGroup ].Contains( f.Current() ))
+ if ( ++nbGroupEdges > 1 )
+ break;
+ bool add = (nbGroupEdges > 1 || Count( face, TopAbs_EDGE, true ) == 1 );
+ if ( !add ) {
+ add = true;
+ for ( TopExp_Explorer v( face, TopAbs_VERTEX ); add && v.More(); v.Next())
+ add = groupVertices[ is2ndGroup ].Contains( v.Current() );
+ }
+ if ( add && addedFaces[ is2ndGroup ].Add( face ))
+ builder.Add( shell, face );
+ }
+ }
+ }
+ }
+ } else {
+ RETURN_BAD_RESULT("Unexpected group type");
+ }
+ // Associate shells
+ //
+ int nbFaces1 = Count( shell1, TopAbs_FACE, 0 );
+ int nbFaces2 = Count( shell2, TopAbs_FACE, 0 );
+ if ( nbFaces1 != nbFaces2 )
+ RETURN_BAD_RESULT("Different nb of faces found for shells");
+ if ( nbFaces1 > 0 ) {
+ bool ok = false;
+ if ( nbFaces1 == 1 ) {
+ TopoDS_Shape F1 = TopoDS_Iterator( shell1 ).Value();
+ TopoDS_Shape F2 = TopoDS_Iterator( shell2 ).Value();
+ ok = FindSubShapeAssociation( F1, theMesh1, F2, theMesh2, theMap );
+ }
+ else {
+ ok = FindSubShapeAssociation(shell1, theMesh1, shell2, theMesh2, theMap );
+ }
+ // Check if all members are mapped
+ if ( ok ) {
+ TopTools_MapOfShape boundMembers[2];
+ TopoDS_Iterator mIt;
+ for ( mIt.Initialize( theShape1 ); mIt.More(); mIt.Next())
+ if ( theMap.IsBound( mIt.Value() )) {
+ boundMembers[0].Add( mIt.Value() );
+ boundMembers[1].Add( theMap( mIt.Value() ));
+ }
+ if ( boundMembers[0].Extent() != nbMembers ) {
+ // make compounds of not bound members
+ TopoDS_Compound comp[2];
+ for ( int is2ndGroup = 0; is2ndGroup < 2; ++is2ndGroup ) {
+ builder.MakeCompound( comp[is2ndGroup] );
+ for ( mIt.Initialize( is2ndGroup ? theShape2:theShape1 ); mIt.More(); mIt.Next())
+ if ( ! boundMembers[ is2ndGroup ].Contains( mIt.Value() ))
+ builder.Add( comp[ is2ndGroup ], mIt.Value() );
+ }
+ // check if theMap contains initial association for the comp's
+ bool hasInitialAssoc = false;
+ if ( memberType == TopAbs_EDGE ) {
+ for ( TopExp_Explorer v( comp[0], TopAbs_VERTEX ); v.More(); v.Next())
+ if ( theMap.IsBound( v.Current() )) {
+ hasInitialAssoc = true;
+ break;
+ }
+ }
+ if ( hasInitialAssoc == bool( !theMap.IsEmpty() ))
+ ok = FindSubShapeAssociation( comp[0], theMesh1, comp[1], theMesh2, theMap );
+ else {
+ TShapeShapeMap tmpMap;
+ ok = FindSubShapeAssociation( comp[0], theMesh1, comp[1], theMesh2, tmpMap );
+ if ( ok ) {
+ TopTools_DataMapIteratorOfDataMapOfShapeShape mapIt( tmpMap );
+ for ( ; mapIt.More(); mapIt.Next() )
+ theMap.Bind( mapIt.Key(), mapIt.Value());
+ }
+ }
+ }
+ }
+ return ok;
+ }
+ // Each edge of an edge group is shared by own faces
+ // ------------------------------------------------------------------
+ //
+ // map vertices to edges sharing them, avoid doubling edges in lists
+ TopTools_DataMapOfShapeListOfShape v2e[2];
+ for (int isFirst = 0; isFirst < 2; ++isFirst ) {
+ const TopoDS_Shape& group = isFirst ? theShape1 : theShape2;
+ TopTools_DataMapOfShapeListOfShape& veMap = v2e[ isFirst ? 0 : 1 ];
+ TopTools_MapOfShape addedEdges;
+ for ( TopExp_Explorer e( group, TopAbs_EDGE ); e.More(); e.Next() ) {
+ const TopoDS_Shape& edge = e.Current();
+ if ( addedEdges.Add( edge )) {
+ for ( TopExp_Explorer v( edge, TopAbs_VERTEX ); v.More(); v.Next()) {
+ const TopoDS_Shape& vertex = v.Current();
+ if ( !veMap.IsBound( vertex )) {
+ TopTools_ListOfShape l;
+ veMap.Bind( vertex, l );
+ }
+ veMap( vertex ).Append( edge );
+ }
+ }
+ }
+ }
+ while ( !v2e[0].IsEmpty() )
+ {
+ // find a bound vertex
+ TopoDS_Vertex V[2];
+ TopTools_DataMapIteratorOfDataMapOfShapeListOfShape v2eIt( v2e[0] );
+ for ( ; v2eIt.More(); v2eIt.Next())
+ if ( theMap.IsBound( v2eIt.Key() )) {
+ V[0] = TopoDS::Vertex( v2eIt.Key() );
+ V[1] = TopoDS::Vertex( theMap( V[0] ));
+ break;
+ }
+ if ( V[0].IsNull() )
+ RETURN_BAD_RESULT("No more bound vertices");
+
+ while ( !V[0].IsNull() && v2e[0].IsBound( V[0] )) {
+ TopTools_ListOfShape& edges0 = v2e[0]( V[0] );
+ TopTools_ListOfShape& edges1 = v2e[1]( V[1] );
+ int nbE0 = edges0.Extent(), nbE1 = edges1.Extent();
+ if ( nbE0 != nbE1 )
+ RETURN_BAD_RESULT("Different nb of edges: "<< nbE0 << " != " << nbE1);
+
+ if ( nbE0 == 1 )
+ {
+ TopoDS_Edge e0 = TopoDS::Edge( edges0.First() );
+ TopoDS_Edge e1 = TopoDS::Edge( edges1.First() );
+ v2e[0].UnBind( V[0] );
+ v2e[1].UnBind( V[1] );
+ InsertAssociation( e0, e1, theMap, bidirect );
+ MESSAGE("Assoc edge " << theMesh1->GetMeshDS()->ShapeToIndex( e0 )<<
+ " to " << theMesh2->GetMeshDS()->ShapeToIndex( e1 ));
+ V[0] = GetNextVertex( e0, V[0] );
+ V[1] = GetNextVertex( e1, V[1] );
+ if ( !V[0].IsNull() ) {
+ InsertAssociation( V[0], V[1], theMap, bidirect );
+ MESSAGE("Assoc vertex " << theMesh1->GetMeshDS()->ShapeToIndex( V[0] )<<
+ " to " << theMesh2->GetMeshDS()->ShapeToIndex( V[1] ));
+ }
+ }
+ else if ( nbE0 == 2 )
+ {
+ // one of edges must have both ends bound
+ TopoDS_Vertex v0e0 = GetNextVertex( TopoDS::Edge( edges0.First() ), V[0] );
+ TopoDS_Vertex v1e0 = GetNextVertex( TopoDS::Edge( edges0.Last() ), V[0] );
+ TopoDS_Vertex v0e1 = GetNextVertex( TopoDS::Edge( edges1.First() ), V[1] );
+ TopoDS_Vertex v1e1 = GetNextVertex( TopoDS::Edge( edges1.Last() ), V[1] );
+ TopoDS_Shape e0b, e1b, e0n, e1n, v1b; // bound and not-bound
+ TopoDS_Vertex v0n, v1n;
+ if ( theMap.IsBound( v0e0 )) {
+ v0n = v1e0; e0b = edges0.First(); e0n = edges0.Last(); v1b = theMap( v0e0 );
+ } else if ( theMap.IsBound( v1e0 )) {
+ v0n = v0e0; e0n = edges0.First(); e0b = edges0.Last(); v1b = theMap( v1e0 );
+ } else {
+ RETURN_BAD_RESULT("None of vertices bound");
+ }
+ if ( v1b.IsSame( v1e1 )) {
+ v1n = v0e1; e1n = edges1.First(); e1b = edges1.Last();
+ } else {
+ v1n = v1e1; e1b = edges1.First(); e1n = edges1.Last();
+ }
+ InsertAssociation( e0b, e1b, theMap, bidirect );
+ InsertAssociation( e0n, e1n, theMap, bidirect );
+ InsertAssociation( v0n, v1n, theMap, bidirect );
+ MESSAGE("Assoc edge " << theMesh1->GetMeshDS()->ShapeToIndex( e0b )<<
+ " to " << theMesh2->GetMeshDS()->ShapeToIndex( e1b ));
+ MESSAGE("Assoc edge " << theMesh1->GetMeshDS()->ShapeToIndex( e0n )<<
+ " to " << theMesh2->GetMeshDS()->ShapeToIndex( e1n ));
+ MESSAGE("Assoc vertex " << theMesh1->GetMeshDS()->ShapeToIndex( v0n )<<
+ " to " << theMesh2->GetMeshDS()->ShapeToIndex( v1n ));
+ v2e[0].UnBind( V[0] );
+ v2e[1].UnBind( V[1] );
+ V[0] = v0n;
+ V[1] = v1n;
+ }
+ else {
+ RETURN_BAD_RESULT("Not implemented");
+ }
+ }
+ } //while ( !v2e[0].IsEmpty() )