+//=======================================================================
+//function : IsManifold
+//purpose : Check if a 2D mesh is manifold
+//=======================================================================
+
+CORBA::Boolean SMESH_MeshEditor_i::IsManifold()
+{
+ bool isManifold = true;
+
+ SMESH_TRY;
+ SMESH_MeshAlgos::TFreeBorderVec foundFreeBordes;
+ SMESH_MeshAlgos::FindFreeBorders( *getMeshDS(),
+ foundFreeBordes,
+ /*closedOnly=*/true,
+ &isManifold );
+ SMESH_CATCH( SMESH::throwCorbaException );
+
+ return isManifold;
+}
+
+//=======================================================================
+//function : IsCoherentOrientation2D
+//purpose : Check if orientation of 2D elements is coherent
+//=======================================================================
+
+CORBA::Boolean SMESH_MeshEditor_i::IsCoherentOrientation2D()
+{
+ bool isGoodOri = true;
+
+ SMESH_TRY;
+ SMESH_MeshAlgos::TFreeBorderVec foundFreeBordes;
+ SMESH_MeshAlgos::FindFreeBorders( *getMeshDS(),
+ foundFreeBordes,
+ /*closedOnly=*/true,
+ /*isManifold=*/0,
+ &isGoodOri);
+ SMESH_CATCH( SMESH::throwCorbaException );
+
+ return isGoodOri;
+}
+
+//=======================================================================
+//function : Get1DBranches
+//purpose : Partition given 1D elements into groups of contiguous edges.
+// A node where number of meeting edges != 2 is a group end.
+// An optional startNode is used to orient groups it belongs to.
+//return : a list of edge groups and a list of corresponding node groups.
+// If a group is closed, the first and last nodes of the group are same.
+//=======================================================================
+
+SMESH::array_of_long_array*
+SMESH_MeshEditor_i::Get1DBranches( SMESH::SMESH_IDSource_ptr theEdges,
+ SMESH::smIdType theStartNode,
+ SMESH::array_of_long_array_out theNodeGroups )
+{
+ if ( CORBA::is_nil( theEdges ))
+ THROW_SALOME_CORBA_EXCEPTION("Get1DBranches(): NULL group given", SALOME::BAD_PARAM);
+
+ SMESH::array_of_long_array_var edgeGroupArray = new SMESH::array_of_long_array;
+ theNodeGroups = new SMESH::array_of_long_array;
+
+ SMESH_TRY;
+
+ prepareIdSource( theEdges );
+
+ SMESH_MeshAlgos::TElemGroupVector edgeBranches;
+ SMESH_MeshAlgos::TNodeGroupVector nodeBranches;
+ SMESH_MeshAlgos::Get1DBranches( SMESH_Mesh_i::GetElements( theEdges, SMESH::EDGE ),
+ edgeBranches,
+ nodeBranches,
+ getMeshDS()->FindNode( theStartNode ));
+
+ edgeGroupArray->length( edgeBranches.size() );
+ for ( size_t iG = 0; iG < edgeBranches.size(); ++iG )
+ {
+ edgeGroupArray[ iG ].length( edgeBranches[ iG ].size() );
+ for ( size_t i = 0; i < edgeBranches[ iG ].size(); ++i )
+ edgeGroupArray[ iG ][ i ] = edgeBranches[ iG ][ i ]->GetID();
+ }
+
+ theNodeGroups->length( nodeBranches.size() );
+ for ( size_t iG = 0; iG < nodeBranches.size(); ++iG )
+ {
+ theNodeGroups[ iG ].length( nodeBranches[ iG ].size() );
+ for ( size_t i = 0; i < nodeBranches[ iG ].size(); ++i )
+ theNodeGroups[ iG ][ i ] = nodeBranches[ iG ][ i ]->GetID();
+ }
+
+ SMESH_CATCH( SMESH::throwCorbaException );
+
+ return edgeGroupArray._retn();
+}
+
+//=======================================================================
+//function : FindSharpEdges
+//purpose : Return sharp edges of faces and non-manifold ones. Optionally add existing edges.
+//=======================================================================
+
+SMESH::ListOfEdges* SMESH_MeshEditor_i::FindSharpEdges(CORBA::Double theAngle,
+ CORBA::Boolean theAddExisting)
+{
+ SMESH::ListOfEdges_var resultEdges = new SMESH::ListOfEdges;
+ SMESH_TRY;
+
+ initData();
+
+ std::vector< SMESH_MeshAlgos::Edge > edges =
+ SMESH_MeshAlgos::FindSharpEdges( getMeshDS(), theAngle, theAddExisting );
+
+ if ( myIsPreviewMode ) // fill a preview mesh with edges
+ {
+ TPreviewMesh* mesh = getPreviewMesh( SMDSAbs_Edge );
+ SMDS_Mesh* meshDS = mesh->GetMeshDS();
+ for ( size_t i = 0; i < edges.size(); ++i )
+ {
+ SMESH_NodeXYZ xyz1( edges[i]._node1), xyz2( edges[i]._node2);
+ SMDS_MeshNode* n1 = meshDS->AddNode( xyz1.X(), xyz1.Y(), xyz1.Z() );
+ SMDS_MeshNode* n2 = meshDS->AddNode( xyz2.X(), xyz2.Y(), xyz2.Z() );
+ if ( edges[i]._medium )
+ {
+ xyz1.Set( edges[i]._medium );
+ SMDS_MeshNode* nm = meshDS->AddNode( xyz1.X(), xyz1.Y(), xyz1.Z() );
+ mesh->GetMeshDS()->AddEdge( n1, n2, nm );
+ }
+ else
+ {
+ mesh->GetMeshDS()->AddEdge( n1, n2 );
+ }
+ }
+ }
+ else
+ {
+ resultEdges->length( edges.size() );
+ for ( size_t i = 0; i < edges.size(); ++i )
+ {
+ resultEdges[ i ].node1 = edges[i]._node1->GetID();
+ resultEdges[ i ].node2 = edges[i]._node2->GetID();
+ resultEdges[ i ].medium = edges[i]._medium ? edges[i]._medium->GetID() : 0;
+ }
+ }
+ SMESH_CATCH( SMESH::throwCorbaException );
+ return resultEdges._retn();
+}
+
+//=======================================================================
+//function : FindFreeBorders
+//purpose : Returns all or only closed FreeBorder's.
+//=======================================================================
+
+SMESH::ListOfFreeBorders* SMESH_MeshEditor_i::FindFreeBorders(CORBA::Boolean closedOnly)
+{
+ SMESH::ListOfFreeBorders_var resBorders = new SMESH::ListOfFreeBorders;
+ SMESH_TRY;
+
+ SMESH_MeshAlgos::TFreeBorderVec foundFreeBordes;
+ SMESH_MeshAlgos::FindFreeBorders( *getMeshDS(), foundFreeBordes, closedOnly );
+
+ resBorders->length( foundFreeBordes.size() );
+ for ( size_t i = 0; i < foundFreeBordes.size(); ++i )
+ {
+ const SMESH_MeshAlgos::TFreeBorder& bordNodes = foundFreeBordes[i];
+ SMESH::FreeBorder& bordOut = resBorders[i];
+ bordOut.nodeIDs.length( bordNodes.size() );
+ for ( size_t iN = 0; iN < bordNodes.size(); ++iN )
+ bordOut.nodeIDs[ iN ] = bordNodes[ iN ]->GetID();
+ }
+
+ SMESH_CATCH( SMESH::throwCorbaException );
+
+ return resBorders._retn();
+}
+
+//=======================================================================
+//function : FillHole
+//purpose : Fill with 2D elements a hole defined by a FreeBorder.
+//=======================================================================
+
+SMESH::SMESH_Group_ptr
+SMESH_MeshEditor_i::FillHole(const SMESH::FreeBorder& theHole,
+ const char* theGroupName)
+{
+ initData();
+
+ if ( theHole.nodeIDs.length() < 4 )
+ THROW_SALOME_CORBA_EXCEPTION("A hole should be bound by at least 3 nodes", SALOME::BAD_PARAM);
+ if ( theHole.nodeIDs[0] != theHole.nodeIDs[ theHole.nodeIDs.length()-1 ] )
+ THROW_SALOME_CORBA_EXCEPTION("Not closed hole boundary. "
+ "First and last nodes must be same", SALOME::BAD_PARAM);
+
+ SMESH_MeshAlgos::TFreeBorder bordNodes;
+ bordNodes.resize( theHole.nodeIDs.length() );
+ for ( size_t iN = 0; iN < theHole.nodeIDs.length(); ++iN )
+ {
+ bordNodes[ iN ] = getMeshDS()->FindNode( theHole.nodeIDs[ iN ]);
+ if ( !bordNodes[ iN ] )
+ THROW_SALOME_CORBA_EXCEPTION(SMESH_Comment("Node #") << theHole.nodeIDs[ iN ]
+ << " does not exist", SALOME::BAD_PARAM);
+ }
+
+ SMESH_TRY;
+
+ // prepare a preview mesh
+ MeshEditor_I::TPreviewMesh* previewMesh = 0;
+ SMDS_Mesh* meshDS = getMeshDS();
+ if ( myIsPreviewMode )
+ {
+ // copy faces sharing nodes of theHole
+ TIDSortedElemSet holeFaces;
+ previewMesh = getPreviewMesh( SMDSAbs_Face );
+ for ( size_t i = 0; i < bordNodes.size(); ++i )
+ {
+ SMDS_ElemIteratorPtr fIt = bordNodes[i]->GetInverseElementIterator( SMDSAbs_Face );
+ while ( fIt->more() )
+ {
+ const SMDS_MeshElement* face = fIt->next();
+ if ( holeFaces.insert( face ).second )
+ previewMesh->Copy( face );
+ }
+ bordNodes[i] = previewMesh->GetMeshDS()->FindNode( bordNodes[i]->GetID() );
+ ASSERT( bordNodes[i] );
+ }
+ meshDS = previewMesh->GetMeshDS();
+ }
+
+ // fill the hole
+ std::vector<const SMDS_MeshElement*> newFaces;
+ SMESH_MeshAlgos::FillHole( bordNodes, *meshDS, newFaces );
+
+ if ( myIsPreviewMode )
+ {
+ // show new faces
+ previewMesh->Clear();
+ for ( size_t i = 0; i < newFaces.size(); ++i )
+ previewMesh->Copy( newFaces[i] );
+ }
+ else
+ {
+ // return new faces via a group
+ SMESH::SMESH_Group_var group;
+ if ( theGroupName && theGroupName[0] && !newFaces.empty() )
+ {
+ SMESH::ListOfGroups_var groups = myMesh_i->GetGroups();
+ for ( CORBA::ULong i = 0; i < groups->length(); ++i )
+ {
+ SMESH::SMESH_GroupBase_var g = groups[ i ];
+ if ( g->GetType() != SMESH::FACE ) continue;
+ SMESH::SMESH_Group_var standalone = SMESH::SMESH_Group::_narrow( g );
+ if ( standalone->_is_nil() ) continue;
+ CORBA::String_var name = g->GetName();
+ if ( strcmp( theGroupName, name.in() ) == 0 )
+ {
+ group = standalone;
+ break;
+ }
+ }
+ if ( group->_is_nil() )
+ group = myMesh_i->CreateGroup( SMESH::FACE, theGroupName );
+
+ if ( !group->_is_nil() )
+ {
+ SMESH_GroupBase_i * grpI = SMESH::DownCast< SMESH_GroupBase_i* >( group );
+ SMESHDS_Group* grpDS = static_cast< SMESHDS_Group* >( grpI->GetGroupDS() );
+ for ( size_t i = 0; i < newFaces.size(); ++i )
+ grpDS->Add( newFaces[ i ]);
+ }
+ }
+
+ // fill LastCreated
+ getEditor().ClearLastCreated();
+ SMESH_SequenceOfElemPtr& aSeq =
+ const_cast<SMESH_SequenceOfElemPtr&>( getEditor().GetLastCreatedElems() );
+ aSeq.swap( newFaces );
+
+ TPythonDump pyDump;
+ if ( group->_is_nil() ) pyDump << "_group = ";
+ else pyDump << group << " = ";
+ pyDump << this << ".FillHole( SMESH.FreeBorder(" << theHole.nodeIDs << " ))";
+
+ return group._retn();
+ }
+
+ SMESH_CATCH( SMESH::throwCorbaException );
+
+ return SMESH::SMESH_Group::_nil();
+}
+