+ if ( CORBA::is_nil( theSubObject ) && theMesh->HasShapeToMesh())
+ THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
+
+ if ( CORBA::is_nil( theMesh ) )
+ THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",SALOME::BAD_PARAM );
+
+ SMESH::algo_error_array_var error_array = new SMESH::algo_error_array;
+ try {
+ SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
+ ASSERT( meshServant );
+ if ( meshServant ) {
+ TopoDS_Shape myLocShape;
+ if(theMesh->HasShapeToMesh())
+ myLocShape = GeomObjectToShape( theSubObject );
+ else
+ myLocShape = SMESH_Mesh::PseudoShape();
+
+ ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
+ list< ::SMESH_Gen::TAlgoStateError > error_list;
+ list< ::SMESH_Gen::TAlgoStateError >::iterator error;
+ // call ::SMESH_Gen::GetAlgoState()
+ myGen.GetAlgoState( myLocMesh, myLocShape, error_list );
+ error_array->length( error_list.size() );
+ int i = 0;
+ for ( error = error_list.begin(); error != error_list.end(); ++error )
+ {
+ // fill AlgoStateError structure
+ SMESH::AlgoStateError & errStruct = error_array[ i++ ];
+ errStruct.state = SMESH_Mesh_i::ConvertHypothesisStatus( error->_name );
+ errStruct.algoDim = error->_algoDim;
+ errStruct.isGlobalAlgo = error->_isGlobalAlgo;
+ errStruct.algoName = "";
+ SALOMEDS::SObject_wrap algoSO = GetAlgoSO( error->_algo );
+ if ( !algoSO->_is_nil() ) {
+ CORBA::String_var algoName = algoSO->GetName();
+ errStruct.algoName = algoName.in();
+ }
+ }
+ }
+ }
+ catch ( SALOME_Exception& S_ex ) {
+ INFOS( "catch exception "<< S_ex.what() );
+ }
+ return error_array._retn();
+}
+
+//=============================================================================
+/*!
+ * SMESH_Gen_i::GetSubShapesId
+ *
+ * Get sub-shapes unique ID's list
+ */
+//=============================================================================
+
+SMESH::long_array* SMESH_Gen_i::GetSubShapesId( GEOM::GEOM_Object_ptr theMainShapeObject,
+ const SMESH::object_array& theListOfSubShapeObject )
+ throw ( SALOME::SALOME_Exception )
+{
+ Unexpect aCatch(SALOME_SalomeException);
+ if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetSubShapesId" );
+
+ SMESH::long_array_var shapesId = new SMESH::long_array;
+ set<int> setId;
+
+ if ( CORBA::is_nil( theMainShapeObject ) )
+ THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
+ SALOME::BAD_PARAM );
+
+ try
+ {
+ TopoDS_Shape myMainShape = GeomObjectToShape(theMainShapeObject);
+ TopTools_IndexedMapOfShape myIndexToShape;
+ TopExp::MapShapes(myMainShape,myIndexToShape);
+
+ for ( int i = 0; i < theListOfSubShapeObject.length(); i++ )
+ {
+ GEOM::GEOM_Object_var aShapeObject
+ = GEOM::GEOM_Object::_narrow(theListOfSubShapeObject[i]);
+ if ( CORBA::is_nil( aShapeObject ) )
+ THROW_SALOME_CORBA_EXCEPTION ("bad shape object reference", \
+ SALOME::BAD_PARAM );
+
+ TopoDS_Shape locShape = GeomObjectToShape(aShapeObject);
+ for (TopExp_Explorer exp(locShape,TopAbs_FACE); exp.More(); exp.Next())
+ {
+ const TopoDS_Face& F = TopoDS::Face(exp.Current());
+ setId.insert(myIndexToShape.FindIndex(F));
+ if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(F));
+ }
+ for (TopExp_Explorer exp(locShape,TopAbs_EDGE); exp.More(); exp.Next())
+ {
+ const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
+ setId.insert(myIndexToShape.FindIndex(E));
+ if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(E));
+ }
+ for (TopExp_Explorer exp(locShape,TopAbs_VERTEX); exp.More(); exp.Next())
+ {
+ const TopoDS_Vertex& V = TopoDS::Vertex(exp.Current());
+ setId.insert(myIndexToShape.FindIndex(V));
+ if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(V));
+ }
+ }
+ shapesId->length(setId.size());
+ set<int>::iterator iind;
+ int i=0;
+ for (iind = setId.begin(); iind != setId.end(); iind++)
+ {
+ if(MYDEBUG) SCRUTE((*iind));
+ shapesId[i] = (*iind);
+ if(MYDEBUG) SCRUTE(shapesId[i]);
+ i++;
+ }
+ }
+ catch (SALOME_Exception& S_ex)
+ {
+ THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
+ }
+
+ return shapesId._retn();
+}
+
+//=============================================================================
+/*!
+ * SMESH_Gen_i::Compute
+ *
+ * Compute mesh on a shape
+ */
+//=============================================================================
+
+CORBA::Boolean SMESH_Gen_i::Compute( SMESH::SMESH_Mesh_ptr theMesh,
+ GEOM::GEOM_Object_ptr theShapeObject )
+ throw ( SALOME::SALOME_Exception )
+{
+ MEMOSTAT;
+ Unexpect aCatch(SALOME_SalomeException);
+ if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Compute" );
+
+ if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
+ THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
+ SALOME::BAD_PARAM );
+
+ if ( CORBA::is_nil( theMesh ) )
+ THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
+ SALOME::BAD_PARAM );
+
+ // Update Python script
+ TPythonDump() << "isDone = " << this << ".Compute( "
+ << theMesh << ", " << theShapeObject << ")";
+
+ try {
+ // get mesh servant
+ SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
+ meshServant->Load();
+ ASSERT( meshServant );
+ if ( meshServant ) {
+ // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
+ meshServant->CheckGeomGroupModif();
+ // get local TopoDS_Shape
+ TopoDS_Shape myLocShape;
+ if(theMesh->HasShapeToMesh())
+ myLocShape = GeomObjectToShape( theShapeObject );
+ else
+ myLocShape = SMESH_Mesh::PseudoShape();
+ // call implementation compute
+ ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
+#ifdef WITH_SMESH_CANCEL_COMPUTE
+ myGen.PrepareCompute( myLocMesh, myLocShape);
+#endif
+ bool ok = myGen.Compute( myLocMesh, myLocShape);
+ meshServant->CreateGroupServants(); // algos can create groups (issue 0020918)
+ myLocMesh.GetMeshDS()->Modified();
+ return ok;
+ }
+ }
+ catch ( std::bad_alloc ) {
+ INFOS( "Compute(): lack of memory" );
+ }
+ catch ( SALOME_Exception& S_ex ) {
+ INFOS( "Compute(): catch exception "<< S_ex.what() );
+ }
+ catch ( ... ) {
+ INFOS( "Compute(): unknown exception " );
+ }
+ return false;
+}
+
+//=============================================================================
+/*!
+ * SMESH_Gen_i::CancelCompute
+ *
+ * Cancel Compute mesh on a shape
+ */
+//=============================================================================
+
+void SMESH_Gen_i::CancelCompute( SMESH::SMESH_Mesh_ptr theMesh,
+ GEOM::GEOM_Object_ptr theShapeObject )
+{
+#ifdef WITH_SMESH_CANCEL_COMPUTE
+ SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
+ ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
+ TopoDS_Shape myLocShape;
+ if(theMesh->HasShapeToMesh())
+ myLocShape = GeomObjectToShape( theShapeObject );
+ else
+ myLocShape = SMESH_Mesh::PseudoShape();
+ myGen.CancelCompute( myLocMesh, myLocShape);
+#endif
+}
+
+//=============================================================================
+/*!
+ * SMESH_Gen_i::Precompute
+ *
+ * Compute mesh as preview till indicated dimension on shape
+ */
+//=============================================================================
+
+SMESH::MeshPreviewStruct* SMESH_Gen_i::Precompute( SMESH::SMESH_Mesh_ptr theMesh,
+ GEOM::GEOM_Object_ptr theShapeObject,
+ SMESH::Dimension theDimension,
+ SMESH::long_array& theShapesId)
+ throw ( SALOME::SALOME_Exception )
+{
+ Unexpect aCatch(SALOME_SalomeException);
+ if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Precompute" );
+
+ if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
+ THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
+ SALOME::BAD_PARAM );
+
+ if ( CORBA::is_nil( theMesh ) )
+ THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
+ SALOME::BAD_PARAM );
+
+ SMESH::MeshPreviewStruct_var result = new SMESH::MeshPreviewStruct;
+ try {
+ // get mesh servant
+ SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
+ meshServant->Load();
+ ASSERT( meshServant );
+ if ( meshServant ) {
+ // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
+ meshServant->CheckGeomGroupModif();
+ // get local TopoDS_Shape
+ TopoDS_Shape myLocShape;
+ if(theMesh->HasShapeToMesh())
+ myLocShape = GeomObjectToShape( theShapeObject );
+ else
+ return result._retn();;
+
+ // call implementation compute
+ ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
+ TSetOfInt shapeIds;
+ ::MeshDimension aDim = (MeshDimension)theDimension;
+ if ( myGen.Compute( myLocMesh, myLocShape, false, false, aDim, &shapeIds ) )
+ {
+ int nbShapeId = shapeIds.size();
+ theShapesId.length( nbShapeId );
+ // iterates on shapes and collect mesh entities into mesh preview
+ TSetOfInt::const_iterator idIt = shapeIds.begin();
+ TSetOfInt::const_iterator idEnd = shapeIds.end();
+ std::map< int, int > mapOfShIdNb;
+ std::set< SMESH_TLink > setOfEdge;
+ std::list< SMDSAbs_ElementType > listOfElemType;
+ typedef map<const SMDS_MeshElement*, int > TNode2LocalIDMap;
+ typedef TNode2LocalIDMap::iterator TNodeLocalID;
+ TNode2LocalIDMap mapNode2LocalID;
+ list< TNodeLocalID > connectivity;
+ int i, nbConnNodes = 0;
+ std::set< const SMESH_subMesh* > setOfVSubMesh;
+ // iterates on shapes
+ for ( ; idIt != idEnd; idIt++ )
+ {
+ if ( mapOfShIdNb.find( *idIt ) != mapOfShIdNb.end() )
+ continue;
+ SMESH_subMesh* sm = myLocMesh.GetSubMeshContaining(*idIt);
+ if ( !sm || !sm->IsMeshComputed() )
+ continue;
+
+ const TopoDS_Shape& aSh = sm->GetSubShape();
+ const int shDim = myGen.GetShapeDim( aSh );
+ if ( shDim < 1 || shDim > theDimension )
+ continue;
+
+ mapOfShIdNb[ *idIt ] = 0;
+ theShapesId[ mapOfShIdNb.size() - 1 ] = *idIt;
+
+ SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
+ if ( !smDS ) continue;
+
+ if ( theDimension == SMESH::DIM_2D )
+ {
+ SMDS_ElemIteratorPtr faceIt = smDS->GetElements();
+ while ( faceIt->more() )
+ {
+ const SMDS_MeshElement* face = faceIt->next();
+ int aNbNode = face->NbNodes();
+ if ( aNbNode > 4 )
+ aNbNode /= 2; // do not take into account additional middle nodes
+
+ SMDS_MeshNode* node1 = (SMDS_MeshNode*)face->GetNode( 0 );
+ for ( int nIndx = 0; nIndx < aNbNode; nIndx++ )
+ {
+ SMDS_MeshNode* node2 = (SMDS_MeshNode*)face->GetNode( nIndx+1 < aNbNode ? nIndx+1 : 0 );
+ if ( setOfEdge.insert( SMESH_TLink ( node1, node2 ) ).second )
+ {
+ listOfElemType.push_back( SMDSAbs_Edge );
+ connectivity.push_back
+ ( mapNode2LocalID.insert( make_pair( node1, ++nbConnNodes)).first );
+ connectivity.push_back
+ ( mapNode2LocalID.insert( make_pair( node2, ++nbConnNodes)).first );
+ }
+ node1 = node2;
+ }
+ }
+ }
+ else if ( theDimension == SMESH::DIM_1D )
+ {
+ SMDS_NodeIteratorPtr nodeIt = smDS->GetNodes();
+ while ( nodeIt->more() )
+ {
+ listOfElemType.push_back( SMDSAbs_Node );
+ connectivity.push_back
+ ( mapNode2LocalID.insert( make_pair( nodeIt->next(), ++nbConnNodes)).first );
+ }
+ // add corner nodes by first vertex from edge
+ SMESH_subMeshIteratorPtr edgeSmIt =
+ sm->getDependsOnIterator(/*includeSelf*/false,
+ /*complexShapeFirst*/false);
+ while ( edgeSmIt->more() )
+ {
+ SMESH_subMesh* vertexSM = edgeSmIt->next();
+ // check that vertex is not already treated
+ if ( !setOfVSubMesh.insert( vertexSM ).second )
+ continue;
+ if ( vertexSM->GetSubShape().ShapeType() != TopAbs_VERTEX )
+ continue;
+
+ const SMESHDS_SubMesh* vertexSmDS = vertexSM->GetSubMeshDS();
+ SMDS_NodeIteratorPtr nodeIt = vertexSmDS->GetNodes();
+ while ( nodeIt->more() )
+ {
+ listOfElemType.push_back( SMDSAbs_Node );
+ connectivity.push_back
+ ( mapNode2LocalID.insert( make_pair( nodeIt->next(), ++nbConnNodes)).first );
+ }
+ }
+ }
+ }
+
+ // fill node coords and assign local ids to the nodes
+ int nbNodes = mapNode2LocalID.size();
+ result->nodesXYZ.length( nbNodes );
+ TNodeLocalID node2ID = mapNode2LocalID.begin();
+ for ( i = 0; i < nbNodes; ++i, ++node2ID ) {
+ node2ID->second = i;
+ const SMDS_MeshNode* node = (const SMDS_MeshNode*) node2ID->first;
+ result->nodesXYZ[i].x = node->X();
+ result->nodesXYZ[i].y = node->Y();
+ result->nodesXYZ[i].z = node->Z();
+ }
+ // fill connectivity
+ result->elementConnectivities.length( nbConnNodes );
+ list< TNodeLocalID >::iterator connIt = connectivity.begin();
+ for ( i = 0; i < nbConnNodes; ++i, ++connIt ) {
+ result->elementConnectivities[i] = (*connIt)->second;
+ }
+
+ // fill element types
+ result->elementTypes.length( listOfElemType.size() );
+ std::list< SMDSAbs_ElementType >::const_iterator typeIt = listOfElemType.begin();
+ std::list< SMDSAbs_ElementType >::const_iterator typeEnd = listOfElemType.end();
+ for ( i = 0; typeIt != typeEnd; ++i, ++typeIt )
+ {
+ SMDSAbs_ElementType elemType = *typeIt;
+ result->elementTypes[i].SMDS_ElementType = (SMESH::ElementType)elemType;
+ result->elementTypes[i].isPoly = false;
+ result->elementTypes[i].nbNodesInElement = elemType == SMDSAbs_Edge ? 2 : 1;
+ }
+
+ // correct number of shapes
+ theShapesId.length( mapOfShIdNb.size() );
+ }
+ }
+ }
+ catch ( std::bad_alloc ) {
+ INFOS( "Precompute(): lack of memory" );
+ }
+ catch ( SALOME_Exception& S_ex ) {
+ INFOS( "Precompute(): catch exception "<< S_ex.what() );
+ }
+ catch ( ... ) {
+ INFOS( "Precompute(): unknown exception " );
+ }
+ return result._retn();
+}
+
+
+//=============================================================================
+/*!
+ * SMESH_Gen_i::Evaluate
+ *
+ * Evaluate mesh on a shape
+ */
+//=============================================================================
+
+SMESH::long_array* SMESH_Gen_i::Evaluate(SMESH::SMESH_Mesh_ptr theMesh,
+ GEOM::GEOM_Object_ptr theShapeObject)
+// SMESH::long_array& theNbElems)
+ throw ( SALOME::SALOME_Exception )
+{
+ Unexpect aCatch(SALOME_SalomeException);
+ if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Evaluate" );
+
+ if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
+ THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
+ SALOME::BAD_PARAM );
+
+ if ( CORBA::is_nil( theMesh ) )
+ THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
+ SALOME::BAD_PARAM );
+
+ SMESH::long_array_var nbels = new SMESH::long_array;
+ nbels->length(SMESH::Entity_Last);
+ int i = SMESH::Entity_Node;
+ for (; i < SMESH::Entity_Last; i++)
+ nbels[i] = 0;
+
+ // Update Python script
+ TPythonDump() << "theNbElems = " << this << ".Evaluate( "
+ << theMesh << ", " << theShapeObject << ")";
+
+ try {
+ // get mesh servant
+ SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
+ ASSERT( meshServant );
+ if ( meshServant ) {
+ // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
+ meshServant->CheckGeomGroupModif();
+ // get local TopoDS_Shape
+ TopoDS_Shape myLocShape;
+ if(theMesh->HasShapeToMesh())
+ myLocShape = GeomObjectToShape( theShapeObject );
+ else
+ myLocShape = SMESH_Mesh::PseudoShape();
+ // call implementation compute
+ ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
+ MapShapeNbElems aResMap;
+ /*CORBA::Boolean ret =*/ myGen.Evaluate( myLocMesh, myLocShape, aResMap);
+ MapShapeNbElemsItr anIt = aResMap.begin();
+ for(; anIt!=aResMap.end(); anIt++) {
+ const vector<int>& aVec = (*anIt).second;
+ for(i = SMESH::Entity_Node; i < aVec.size(); i++) {
+ int nbElem = aVec[i];
+ if ( nbElem < 0 ) // algo failed, check that it has reported a message
+ {
+ SMESH_subMesh* sm = anIt->first;
+ SMESH_ComputeErrorPtr& error = sm->GetComputeError();
+ const SMESH_Algo* algo = myGen.GetAlgo( myLocMesh, sm->GetSubShape());
+ if ( (algo && !error.get()) || error->IsOK() )
+ error.reset( new SMESH_ComputeError( COMPERR_ALGO_FAILED,"Failed to evaluate",algo));
+ }
+ else
+ {
+ nbels[i] += aVec[i];
+ }
+ }
+ }
+ return nbels._retn();
+ }
+ }
+ catch ( std::bad_alloc ) {
+ INFOS( "Evaluate(): lack of memory" );
+ }
+ catch ( SALOME_Exception& S_ex ) {
+ INFOS( "Evaluate(): catch exception "<< S_ex.what() );
+ }
+ catch ( ... ) {
+ INFOS( "Evaluate(): unknown exception " );
+ }
+
+ return nbels._retn();
+}
+
+//================================================================================
+/*!
+ * \brief Return geometrical object the given element is built on
+ * \param theMesh - the mesh the element is in
+ * \param theElementID - the element ID
+ * \param theGeomName - the name of the result geom object if it is not yet published
+ * \retval GEOM::GEOM_Object_ptr - the found or just published geom object (no need to UnRegister())
+ */
+//================================================================================
+
+GEOM::GEOM_Object_ptr
+SMESH_Gen_i::GetGeometryByMeshElement( SMESH::SMESH_Mesh_ptr theMesh,
+ CORBA::Long theElementID,
+ const char* theGeomName)
+ throw ( SALOME::SALOME_Exception )
+{
+ Unexpect aCatch(SALOME_SalomeException);
+
+ GEOM::GEOM_Object_wrap geom = FindGeometryByMeshElement(theMesh, theElementID);
+ if ( !geom->_is_nil() ) {
+ GEOM::GEOM_Object_var mainShape = theMesh->GetShapeToMesh();
+ GEOM::GEOM_Gen_ptr geomGen = GetGeomEngine();
+
+ // try to find the corresponding SObject
+ SALOMEDS::SObject_wrap SObj = ObjectToSObject( myCurrentStudy, geom.in() );
+ if ( SObj->_is_nil() ) // submesh can be not found even if published
+ {
+ // try to find published submesh
+ GEOM::ListOfLong_var list = geom->GetSubShapeIndices();
+ if ( !geom->IsMainShape() && list->length() == 1 ) {
+ SALOMEDS::SObject_wrap mainSO = ObjectToSObject( myCurrentStudy, mainShape );
+ SALOMEDS::ChildIterator_wrap it;
+ if ( !mainSO->_is_nil() ) {
+ it = myCurrentStudy->NewChildIterator( mainSO );
+ }
+ if ( !it->_is_nil() ) {
+ for ( it->InitEx(true); it->More(); it->Next() ) {
+ SALOMEDS::SObject_wrap so = it->Value();
+ CORBA::Object_var obj = SObjectToObject( so );
+ GEOM::GEOM_Object_var subGeom = GEOM::GEOM_Object::_narrow( obj );
+ if ( !subGeom->_is_nil() ) {
+ GEOM::ListOfLong_var subList = subGeom->GetSubShapeIndices();
+ if ( subList->length() == 1 && list[0] == subList[0] ) {
+ SObj = so;
+ geom = subGeom;
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+ if ( SObj->_is_nil() ) // publish a new subshape
+ SObj = geomGen->AddInStudy( myCurrentStudy, geom, theGeomName, mainShape );
+
+ // return only published geometry
+ if ( !SObj->_is_nil() ) {
+ //return geom._retn(); -- servant of geom must be UnRegister()ed;
+ CORBA::Object_var obj = SObjectToObject( SObj );
+ GEOM::GEOM_Object_var go = GEOM::GEOM_Object::_narrow( obj );
+ return go._retn();
+ }
+ }
+ return GEOM::GEOM_Object::_nil();
+}
+
+//================================================================================
+/*!
+ * \brief Return geometrical object the given element is built on.
+ * \param theMesh - the mesh the element is in
+ * \param theElementID - the element ID
+ * \retval GEOM::GEOM_Object_ptr - the found or created (UnRegister()!) geom object
+ */
+//================================================================================
+
+GEOM::GEOM_Object_ptr
+SMESH_Gen_i::FindGeometryByMeshElement( SMESH::SMESH_Mesh_ptr theMesh,
+ CORBA::Long theElementID)
+ throw ( SALOME::SALOME_Exception )
+{
+ Unexpect aCatch(SALOME_SalomeException);
+ if ( CORBA::is_nil( theMesh ) )
+ THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference", SALOME::BAD_PARAM );
+
+ GEOM::GEOM_Object_var mainShape = theMesh->GetShapeToMesh();
+ GEOM::GEOM_Gen_ptr geomGen = GetGeomEngine();
+
+ // get a core mesh DS
+ SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
+ if ( meshServant && !geomGen->_is_nil() && !mainShape->_is_nil() )
+ {
+ ::SMESH_Mesh & mesh = meshServant->GetImpl();
+ SMESHDS_Mesh* meshDS = mesh.GetMeshDS();
+ // find the element in mesh
+ if ( const SMDS_MeshElement * elem = meshDS->FindElement( theElementID ) ) {
+ // find a shape id by the element
+ if ( int shapeID = ::SMESH_MeshEditor( &mesh ).FindShape( elem )) {
+ // get a geom object by the shape id
+ GEOM::GEOM_Object_var geom = ShapeToGeomObject( meshDS->IndexToShape( shapeID ));
+ if ( geom->_is_nil() ) {
+ // try to find a published sub-shape
+ SALOMEDS::SObject_wrap mainSO = ObjectToSObject( myCurrentStudy, mainShape );
+ SALOMEDS::ChildIterator_wrap it;
+ if ( !mainSO->_is_nil() ) {
+ it = myCurrentStudy->NewChildIterator( mainSO );
+ }
+ if ( !it->_is_nil() ) {
+ for ( it->InitEx(true); it->More(); it->Next() ) {
+ SALOMEDS::SObject_wrap so = it->Value();
+ CORBA::Object_var obj = SObjectToObject( so );
+ GEOM::GEOM_Object_var subGeom = GEOM::GEOM_Object::_narrow( obj );
+ if ( !subGeom->_is_nil() ) {
+ GEOM::ListOfLong_var subList = subGeom->GetSubShapeIndices();
+ if ( subList->length() == 1 && shapeID == subList[0] ) {
+ geom = subGeom;
+ break;
+ }
+ }
+ }
+ }
+ }
+ if ( geom->_is_nil() ) {
+ // explode
+ GEOM::GEOM_IShapesOperations_wrap op =
+ geomGen->GetIShapesOperations( GetCurrentStudyID() );
+ if ( !op->_is_nil() )
+ geom = op->GetSubShape( mainShape, shapeID );
+ }
+ else {
+ geom->Register();
+ }
+ if ( !geom->_is_nil() ) {
+ GeomObjectToShape( geom ); // let geom client remember the found shape
+ return geom._retn();
+ }
+ }
+ }
+ }
+ return GEOM::GEOM_Object::_nil();
+}
+
+//================================================================================
+/*!
+ * SMESH_Gen_i::Concatenate
+ *
+ * Concatenate the given meshes into one mesh
+ */
+//================================================================================
+
+SMESH::SMESH_Mesh_ptr SMESH_Gen_i::Concatenate(const SMESH::mesh_array& theMeshesArray,
+ CORBA::Boolean theUniteIdenticalGroups,
+ CORBA::Boolean theMergeNodesAndElements,
+ CORBA::Double theMergeTolerance)
+ throw ( SALOME::SALOME_Exception )
+{
+ return ConcatenateCommon(theMeshesArray,
+ theUniteIdenticalGroups,
+ theMergeNodesAndElements,
+ theMergeTolerance,
+ false);
+}
+
+//================================================================================
+/*!
+ * SMESH_Gen_i::ConcatenateWithGroups
+ *
+ * Concatenate the given meshes into one mesh
+ * Create the groups of all elements from initial meshes
+ */
+//================================================================================
+
+SMESH::SMESH_Mesh_ptr
+SMESH_Gen_i::ConcatenateWithGroups(const SMESH::mesh_array& theMeshesArray,
+ CORBA::Boolean theUniteIdenticalGroups,
+ CORBA::Boolean theMergeNodesAndElements,
+ CORBA::Double theMergeTolerance)
+ throw ( SALOME::SALOME_Exception )
+{
+ return ConcatenateCommon(theMeshesArray,
+ theUniteIdenticalGroups,
+ theMergeNodesAndElements,
+ theMergeTolerance,
+ true);
+}
+
+//================================================================================
+/*!
+ * SMESH_Gen_i::ConcatenateCommon
+ *
+ * Concatenate the given meshes into one mesh
+ */
+//================================================================================
+
+SMESH::SMESH_Mesh_ptr
+SMESH_Gen_i::ConcatenateCommon(const SMESH::mesh_array& theMeshesArray,
+ CORBA::Boolean theUniteIdenticalGroups,
+ CORBA::Boolean theMergeNodesAndElements,
+ CORBA::Double theMergeTolerance,
+ CORBA::Boolean theCommonGroups)
+ throw ( SALOME::SALOME_Exception )
+{
+ typedef map<int, int> TIDsMap;
+ typedef list<SMESH::SMESH_Group_var> TListOfNewGroups;
+ typedef map< pair<string, SMESH::ElementType>, TListOfNewGroups > TGroupsMap;
+ typedef std::set<SMESHDS_GroupBase*> TGroups;
+
+ TPythonDump* pPythonDump = new TPythonDump;
+ TPythonDump& aPythonDump = *pPythonDump; // prevent dump of called methods
+
+ // create mesh
+ SMESH::SMESH_Mesh_var aNewMesh = CreateEmptyMesh();
+
+ SMESHDS_Mesh* aNewMeshDS = 0;
+ if ( !aNewMesh->_is_nil() ) {
+ SMESH_Mesh_i* aNewImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( aNewMesh ).in() );
+ if ( aNewImpl ) {
+ ::SMESH_Mesh& aLocMesh = aNewImpl->GetImpl();
+ aNewMeshDS = aLocMesh.GetMeshDS();
+
+ TGroupsMap aGroupsMap;
+ TListOfNewGroups aListOfNewGroups;
+ SMESH_MeshEditor aNewEditor = ::SMESH_MeshEditor(&aLocMesh);
+ SMESH::ListOfGroups_var aListOfGroups = new SMESH::ListOfGroups();
+
+ // loop on meshes
+ for ( int i = 0; i < theMeshesArray.length(); i++) {
+ SMESH::SMESH_Mesh_var anInitMesh = theMeshesArray[i];
+ if ( !anInitMesh->_is_nil() ) {
+ SMESH_Mesh_i* anInitImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( anInitMesh ).in() );
+ if ( anInitImpl ) {
+ ::SMESH_Mesh& aInitLocMesh = anInitImpl->GetImpl();
+ aInitLocMesh.Load();
+ SMESHDS_Mesh* anInitMeshDS = aInitLocMesh.GetMeshDS();
+
+ TIDsMap nodesMap;
+ TIDsMap elemsMap;
+
+ // loop on elements of mesh
+ SMDS_ElemIteratorPtr itElems = anInitMeshDS->elementsIterator();
+ const SMDS_MeshElement* anElem = 0;
+ const SMDS_MeshElement* aNewElem = 0;
+ int anElemNbNodes = 0;
+
+ int anNbNodes = 0;
+ int anNbEdges = 0;
+ int anNbFaces = 0;
+ int anNbVolumes = 0;
+ int aNbBalls = 0;
+
+ SMESH::long_array_var anIDsNodes = new SMESH::long_array();
+ SMESH::long_array_var anIDsEdges = new SMESH::long_array();
+ SMESH::long_array_var anIDsFaces = new SMESH::long_array();
+ SMESH::long_array_var anIDsVolumes = new SMESH::long_array();
+ SMESH::long_array_var anIDsBalls = new SMESH::long_array();
+
+ if( theCommonGroups ) {
+ anIDsNodes->length( anInitMeshDS->NbNodes() );
+ anIDsEdges->length( anInitMeshDS->NbEdges() );
+ anIDsFaces->length( anInitMeshDS->NbFaces() );
+ anIDsVolumes->length( anInitMeshDS->NbVolumes() );
+ anIDsBalls->length( anInitMeshDS->NbBalls() );
+ }
+
+ for ( int j = 0; itElems->more(); j++) {
+ anElem = itElems->next();
+ SMDSAbs_ElementType anElemType = anElem->GetType();
+ anElemNbNodes = anElem->NbNodes();
+ std::vector<const SMDS_MeshNode*> aNodesArray (anElemNbNodes);
+
+ // loop on nodes of element
+ const SMDS_MeshNode* aNode = 0;
+ const SMDS_MeshNode* aNewNode = 0;
+ SMDS_ElemIteratorPtr itNodes = anElem->nodesIterator();
+
+ for ( int k = 0; itNodes->more(); k++) {
+ aNode = static_cast<const SMDS_MeshNode*>(itNodes->next());
+ if ( nodesMap.find(aNode->GetID()) == nodesMap.end() ) {
+ aNewNode = aNewMeshDS->AddNode(aNode->X(), aNode->Y(), aNode->Z());
+ nodesMap.insert( make_pair(aNode->GetID(), aNewNode->GetID()) );
+ if( theCommonGroups )
+ anIDsNodes[anNbNodes++] = aNewNode->GetID();
+ }
+ else
+ aNewNode = aNewMeshDS->FindNode( nodesMap.find(aNode->GetID())->second );
+ aNodesArray[k] = aNewNode;
+ }//nodes loop
+
+ // creates a corresponding element on existent nodes in new mesh
+ switch ( anElem->GetEntityType() ) {
+ case SMDSEntity_Polyhedra:
+ if ( const SMDS_VtkVolume* aVolume =
+ dynamic_cast<const SMDS_VtkVolume*> (anElem))
+ {
+ aNewElem = aNewMeshDS->AddPolyhedralVolume(aNodesArray,
+ aVolume->GetQuantities());
+ elemsMap.insert(make_pair(anElem->GetID(), aNewElem->GetID()));
+ if( theCommonGroups )
+ anIDsVolumes[anNbVolumes++] = aNewElem->GetID();
+ }
+ break;
+ case SMDSEntity_Ball:
+ if ( const SMDS_BallElement* aBall =
+ dynamic_cast<const SMDS_BallElement*> (anElem))
+ {
+ aNewElem = aNewEditor.AddElement(aNodesArray, SMDSAbs_Ball,
+ /*isPoly=*/false, /*id=*/0,
+ aBall->GetDiameter() );
+ elemsMap.insert(make_pair(anElem->GetID(), aNewElem->GetID()));
+ if( theCommonGroups )
+ anIDsBalls[aNbBalls++] = aNewElem->GetID();
+ }
+ break;
+ default:
+ {
+ aNewElem = aNewEditor.AddElement(aNodesArray,
+ anElemType,
+ anElem->IsPoly());
+ elemsMap.insert(make_pair(anElem->GetID(), aNewElem->GetID()));
+ if( theCommonGroups ) {
+ if( anElemType == SMDSAbs_Edge )
+ anIDsEdges[anNbEdges++] = aNewElem->GetID();
+ else if( anElemType == SMDSAbs_Face )
+ anIDsFaces[anNbFaces++] = aNewElem->GetID();
+ else if( anElemType == SMDSAbs_Volume )
+ anIDsVolumes[anNbVolumes++] = aNewElem->GetID();
+ }
+ }
+ }
+ } //elems loop
+
+ // copy orphan nodes
+ SMDS_NodeIteratorPtr itNodes = anInitMeshDS->nodesIterator();
+ while ( itNodes->more() )
+ {
+ const SMDS_MeshNode* aNode = itNodes->next();
+ if ( aNode->NbInverseElements() == 0 )
+ {
+ const SMDS_MeshNode* aNewNode =
+ aNewMeshDS->AddNode(aNode->X(), aNode->Y(), aNode->Z());
+ nodesMap.insert( make_pair(aNode->GetID(), aNewNode->GetID()) );
+ if( theCommonGroups )
+ anIDsNodes[anNbNodes++] = aNewNode->GetID();
+ }
+ }
+
+
+ aListOfGroups = anInitImpl->GetGroups();
+ SMESH::SMESH_GroupBase_ptr aGroup;
+
+ // loop on groups of mesh
+ SMESH::long_array_var anInitIDs = new SMESH::long_array();
+ SMESH::long_array_var anNewIDs = new SMESH::long_array();
+ SMESH::SMESH_Group_var aNewGroup;
+
+ SMESH::ElementType aGroupType;
+ CORBA::String_var aGroupName;
+ if ( theCommonGroups ) {
+ for(aGroupType=SMESH::NODE;aGroupType<=SMESH::BALL;aGroupType=(SMESH::ElementType)(aGroupType+1)) {
+ string str = "Gr";
+ SALOMEDS::SObject_wrap aMeshSObj = ObjectToSObject( myCurrentStudy, anInitMesh );
+ if(aMeshSObj)
+ str += aMeshSObj->GetName();
+ str += "_";
+
+ int anLen = 0;
+
+ switch(aGroupType) {
+ case SMESH::NODE:
+ str += "Nodes";
+ anIDsNodes->length(anNbNodes);
+ anLen = anNbNodes;
+ break;
+ case SMESH::EDGE:
+ str += "Edges";
+ anIDsEdges->length(anNbEdges);
+ anLen = anNbEdges;
+ break;
+ case SMESH::FACE:
+ str += "Faces";
+ anIDsFaces->length(anNbFaces);
+ anLen = anNbFaces;
+ break;
+ case SMESH::VOLUME:
+ str += "Volumes";
+ anIDsVolumes->length(anNbVolumes);
+ anLen = anNbVolumes;
+ break;
+ case SMESH::BALL:
+ str += "Balls";
+ anIDsBalls->length(aNbBalls);
+ anLen = aNbBalls;
+ break;
+ default:
+ break;
+ }
+
+ if(anLen) {
+ aGroupName = str.c_str();
+
+ // add a new group in the mesh
+ aNewGroup = aNewImpl->CreateGroup(aGroupType, aGroupName);
+
+ switch(aGroupType) {
+ case SMESH::NODE:
+ aNewGroup->Add( anIDsNodes );
+ break;
+ case SMESH::EDGE:
+ aNewGroup->Add( anIDsEdges );
+ break;
+ case SMESH::FACE:
+ aNewGroup->Add( anIDsFaces );
+ break;
+ case SMESH::VOLUME:
+ aNewGroup->Add( anIDsVolumes );
+ break;
+ case SMESH::BALL:
+ aNewGroup->Add( anIDsBalls );
+ break;
+ default:
+ break;
+ }
+
+ aListOfNewGroups.clear();
+ aListOfNewGroups.push_back(aNewGroup);
+ aGroupsMap.insert(make_pair( make_pair(aGroupName, aGroupType), aListOfNewGroups ));
+ }
+ }
+ }