+//=======================================================================
+/*!
+ * Returns groups of FreeBorder's coincident within the given tolerance.
+ * If the tolerance <= 0.0 then one tenth of an average size of elements adjacent
+ * to free borders being compared is used.
+ */
+//=======================================================================
+
+SMESH::CoincidentFreeBorders*
+SMESH_MeshEditor_i::FindCoincidentFreeBorders(CORBA::Double tolerance)
+{
+ SMESH::CoincidentFreeBorders_var aCFB = new SMESH::CoincidentFreeBorders;
+
+ SMESH_TRY;
+
+ SMESH_MeshAlgos::CoincidentFreeBorders cfb;
+ SMESH_MeshAlgos::FindCoincidentFreeBorders( *getMeshDS(), tolerance, cfb );
+
+ // copy free borders
+ aCFB->borders.length( cfb._borders.size() );
+ for ( size_t i = 0; i < cfb._borders.size(); ++i )
+ {
+ SMESH_MeshAlgos::TFreeBorder& nodes = cfb._borders[i];
+ SMESH::FreeBorder& aBRD = aCFB->borders[i];
+ aBRD.nodeIDs.length( nodes.size() );
+ for ( size_t iN = 0; iN < nodes.size(); ++iN )
+ aBRD.nodeIDs[ iN ] = nodes[ iN ]->GetID();
+ }
+
+ // copy coincident parts
+ aCFB->coincidentGroups.length( cfb._coincidentGroups.size() );
+ for ( size_t i = 0; i < cfb._coincidentGroups.size(); ++i )
+ {
+ SMESH_MeshAlgos::TCoincidentGroup& grp = cfb._coincidentGroups[i];
+ SMESH::FreeBordersGroup& aGRP = aCFB->coincidentGroups[i];
+ aGRP.length( grp.size() );
+ for ( size_t iP = 0; iP < grp.size(); ++iP )
+ {
+ SMESH_MeshAlgos::TFreeBorderPart& part = grp[ iP ];
+ SMESH::FreeBorderPart& aPART = aGRP[ iP ];
+ aPART.border = part._border;
+ aPART.node1 = part._node1;
+ aPART.node2 = part._node2;
+ aPART.nodeLast = part._nodeLast;
+ }
+ }
+ SMESH_CATCH( SMESH::doNothing );
+
+ TPythonDump() << "CoincidentFreeBorders = "
+ << this << ".FindCoincidentFreeBorders( " << tolerance << " )";
+
+ return aCFB._retn();
+}
+
+//=======================================================================
+/*!
+ * Sew FreeBorder's of each group
+ */
+//=======================================================================
+
+CORBA::Short SMESH_MeshEditor_i::
+SewCoincidentFreeBorders(const SMESH::CoincidentFreeBorders& freeBorders,
+ CORBA::Boolean createPolygons,
+ CORBA::Boolean createPolyhedra)
+ throw (SALOME::SALOME_Exception)
+{
+ CORBA::Short nbSewed = 0;
+
+ SMESH_MeshAlgos::TFreeBorderVec groups;
+ SMESH_MeshAlgos::TFreeBorder borderNodes; // triples of nodes for every FreeBorderPart
+
+ // check the input and collect nodes
+ for ( CORBA::ULong i = 0; i < freeBorders.coincidentGroups.length(); ++i )
+ {
+ borderNodes.clear();
+ const SMESH::FreeBordersGroup& aGRP = freeBorders.coincidentGroups[ i ];
+ for ( CORBA::ULong iP = 0; iP < aGRP.length(); ++iP )
+ {
+ const SMESH::FreeBorderPart& aPART = aGRP[ iP ];
+ if ( aPART.border < 0 || aPART.border >= freeBorders.borders.length() )
+ THROW_SALOME_CORBA_EXCEPTION("Invalid FreeBorderPart::border index", SALOME::BAD_PARAM);
+
+ const SMESH::FreeBorder& aBRD = freeBorders.borders[ aPART.border ];
+
+ if ( aPART.node1 < 0 || aPART.node1 > aBRD.nodeIDs.length() )
+ THROW_SALOME_CORBA_EXCEPTION("Invalid FreeBorderPart::node1", SALOME::BAD_PARAM);
+ if ( aPART.node2 < 0 || aPART.node2 > aBRD.nodeIDs.length() )
+ THROW_SALOME_CORBA_EXCEPTION("Invalid FreeBorderPart::node2", SALOME::BAD_PARAM);
+ if ( aPART.nodeLast < 0 || aPART.nodeLast > aBRD.nodeIDs.length() )
+ THROW_SALOME_CORBA_EXCEPTION("Invalid FreeBorderPart::nodeLast", SALOME::BAD_PARAM);
+
+ // do not keep these nodes for further sewing as nodes can be removed by the sewing
+ const SMDS_MeshNode* n1 = getMeshDS()->FindNode( aBRD.nodeIDs[ aPART.node1 ]);
+ const SMDS_MeshNode* n2 = getMeshDS()->FindNode( aBRD.nodeIDs[ aPART.node2 ]);
+ const SMDS_MeshNode* n3 = getMeshDS()->FindNode( aBRD.nodeIDs[ aPART.nodeLast ]);
+ if ( !n1)
+ THROW_SALOME_CORBA_EXCEPTION("Nonexistent FreeBorderPart::node1", SALOME::BAD_PARAM);
+ if ( !n2 )
+ THROW_SALOME_CORBA_EXCEPTION("Nonexistent FreeBorderPart::node2", SALOME::BAD_PARAM);
+ if ( !n3 )
+ THROW_SALOME_CORBA_EXCEPTION("Nonexistent FreeBorderPart::nodeLast", SALOME::BAD_PARAM);
+
+ borderNodes.push_back( n1 );
+ borderNodes.push_back( n2 );
+ borderNodes.push_back( n3 );
+ }
+ groups.push_back( borderNodes );
+ }
+
+ // SewFreeBorder() can merge nodes, thus nodes stored in 'groups' can become dead;
+ // to get nodes that replace other nodes during merge we create 0D elements
+ // on each node and MergeNodes() will replace underlying nodes of 0D elements by
+ // new ones.
+
+ vector< const SMDS_MeshElement* > tmp0Delems;
+ for ( size_t i = 0; i < groups.size(); ++i )
+ {
+ SMESH_MeshAlgos::TFreeBorder& nodes = groups[i];
+ for ( size_t iN = 0; iN < nodes.size(); ++iN )
+ {
+ SMDS_ElemIteratorPtr it0D = nodes[iN]->GetInverseElementIterator(SMDSAbs_0DElement);
+ if ( it0D->more() )
+ tmp0Delems.push_back( it0D->next() );
+ else
+ tmp0Delems.push_back( getMeshDS()->Add0DElement( nodes[iN] ));
+ }
+ }
+
+ // cout << endl << "INIT" << endl;
+ // for ( size_t i = 0; i < tmp0Delems.size(); ++i )
+ // {
+ // cout << i << " ";
+ // if ( i % 3 == 0 ) cout << "^ ";
+ // tmp0Delems[i]->GetNode(0)->Print( cout );
+ // }
+
+ SMESH_TRY;
+
+ ::SMESH_MeshEditor::Sew_Error res, ok = ::SMESH_MeshEditor::SEW_OK;
+ int i0D = 0;
+ for ( size_t i = 0; i < groups.size(); ++i )
+ {
+ bool isBordToBord = true;
+ bool groupSewed = false;
+ SMESH_MeshAlgos::TFreeBorder& nodes = groups[i];
+ for ( size_t iN = 3; iN+2 < nodes.size(); iN += 3 )
+ {
+ const SMDS_MeshNode* n0 = tmp0Delems[ i0D + 0 ]->GetNode( 0 );
+ const SMDS_MeshNode* n1 = tmp0Delems[ i0D + 1 ]->GetNode( 0 );
+ const SMDS_MeshNode* n2 = tmp0Delems[ i0D + 2 ]->GetNode( 0 );
+
+ const SMDS_MeshNode* n3 = tmp0Delems[ i0D + 0 + iN ]->GetNode( 0 );
+ const SMDS_MeshNode* n4 = tmp0Delems[ i0D + 1 + iN ]->GetNode( 0 );
+ const SMDS_MeshNode* n5 = tmp0Delems[ i0D + 2 + iN ]->GetNode( 0 );
+
+ if ( !n0 || !n1 || !n2 || !n3 || !n4 || !n5 )
+ continue;
+
+ // TIDSortedElemSet emptySet, avoidSet;
+ // if ( !SMESH_MeshAlgos::FindFaceInSet( n0, n1, emptySet, avoidSet))
+ // {
+ // cout << "WRONG 2nd 1" << endl;
+ // n0->Print( cout );
+ // n1->Print( cout );
+ // }
+ // if ( !SMESH_MeshAlgos::FindFaceInSet( n3, n4, emptySet, avoidSet))
+ // {
+ // cout << "WRONG 2nd 2" << endl;
+ // n3->Print( cout );
+ // n4->Print( cout );
+ // }
+
+ if ( !isBordToBord )
+ {
+ n1 = n2; // at border-to-side sewing only last side node (n1) is needed
+ n2 = 0; // and n2 is not used
+ }
+ // 1st border moves to 2nd
+ res = getEditor().SewFreeBorder( n3, n4, n5 ,// 1st
+ n0 ,n1 ,n2 ,// 2nd
+ /*2ndIsFreeBorder=*/ isBordToBord,
+ createPolygons, createPolyhedra);
+ groupSewed = ( res == ok );
+
+ isBordToBord = false;
+ // cout << endl << "SEWED GROUP " << i << " PART " << iN / 3 << endl;
+ // for ( size_t t = 0; t < tmp0Delems.size(); ++t )
+ // {
+ // cout << t << " ";
+ // if ( t % 3 == 0 ) cout << "^ ";
+ // tmp0Delems[t]->GetNode(0)->Print( cout );
+ // }
+ }
+ i0D += nodes.size();
+ nbSewed += groupSewed;
+ }
+
+ TPythonDump() << "nbSewed = " << this << ".SewCoincidentFreeBorders( "
+ << freeBorders << ", "
+ << createPolygons << ", "
+ << createPolyhedra << " )";
+
+ SMESH_CATCH( SMESH::doNothing );
+
+ declareMeshModified( /*isReComputeSafe=*/false );
+
+ // remove tmp 0D elements
+ SMESH_TRY;
+ set< const SMDS_MeshElement* > removed0D;
+ for ( size_t i = 0; i < tmp0Delems.size(); ++i )
+ {
+ if ( removed0D.insert( tmp0Delems[i] ).second )
+ getMeshDS()->RemoveFreeElement( tmp0Delems[i], /*sm=*/0, /*fromGroups=*/false );
+ }
+ SMESH_CATCH( SMESH::throwCorbaException );
+
+ return nbSewed;
+}
+