Back to index

salome-smesh  6.5.0
SMESHGUI_MeshOp.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 //  File   : SMESHGUI_MeshOp.cxx
00023 //  Author : Sergey LITONIN, Open CASCADE S.A.S.
00024 
00025 // SMESH includes
00026 #include "SMESHGUI_MeshOp.h"
00027 
00028 #include "SMESHGUI.h"
00029 #include "SMESHGUI_MeshDlg.h"
00030 #include "SMESHGUI_ShapeByMeshDlg.h"
00031 #include "SMESHGUI_HypothesesUtils.h"
00032 #include "SMESHGUI_Hypotheses.h"
00033 #include "SMESHGUI_Utils.h"
00034 #include "SMESHGUI_GEOMGenUtils.h"
00035 
00036 #include <SMESH_TypeFilter.hxx>
00037 #include <SMESH_NumberFilter.hxx>
00038 
00039 // SALOME GEOM includes
00040 #include <GEOM_SelectionFilter.h>
00041 #include <GEOMBase.h>
00042 #include <GeometryGUI.h>
00043 
00044 // SALOME GUI includes
00045 #include <SalomeApp_Tools.h>
00046 #include <SalomeApp_Application.h>
00047 #include <LightApp_Application.h>
00048 #include <LightApp_SelectionMgr.h>
00049 #include <LightApp_UpdateFlags.h>
00050 #include <SUIT_MessageBox.h>
00051 #include <SUIT_OverrideCursor.h>
00052 #include <SUIT_Session.h>
00053 #include <SALOME_InteractiveObject.hxx>
00054 #include <SALOME_ListIO.hxx>
00055 
00056 // SALOME KERNEL includes
00057 #include <SALOMEDS_SComponent.hxx>
00058 #include <SALOMEDS_SObject.hxx>
00059 
00060 // Qt includes
00061 #include <QStringList>
00062 #include <QLineEdit>
00063 
00064 // OCCT includes
00065 #include <TopoDS.hxx>
00066 #include <TopoDS_Shape.hxx>
00067 #include <TopoDS_Shell.hxx>
00068 #include <TopExp_Explorer.hxx>
00069 #include <BRep_Tool.hxx>
00070 
00071 // IDL includes
00072 #include <SALOMEconfig.h>
00073 #include CORBA_CLIENT_HEADER(SMESH_Gen)
00074 
00075 //To disable automatic genericobj management, the following line should be commented.
00076 //Otherwise, it should be uncommented. Refer to KERNEL_SRC/src/SALOMEDSImpl/SALOMEDSImpl_AttributeIOR.cxx
00077 #define WITHGENERICOBJ
00078 
00079 //================================================================================
00087 //================================================================================
00088 SMESHGUI_MeshOp::SMESHGUI_MeshOp( const bool theToCreate, const bool theIsMesh )
00089 : SMESHGUI_SelectionOp(),
00090   myToCreate( theToCreate ),
00091   myIsMesh( theIsMesh ),
00092   myDlg( 0 ),
00093   myShapeByMeshOp( 0 ),
00094   myHypoSet( 0 )
00095 {
00096   if ( GeometryGUI::GetGeomGen()->_is_nil() )// check that GEOM_Gen exists
00097     GeometryGUI::InitGeomGen();
00098   myIsOnGeometry = true;
00099 }
00100 
00101 //================================================================================
00105 //================================================================================
00106 SMESHGUI_MeshOp::~SMESHGUI_MeshOp()
00107 {
00108   if ( myDlg )
00109     delete myDlg;
00110 }
00111 
00112 //================================================================================
00117 //================================================================================
00118 LightApp_Dialog* SMESHGUI_MeshOp::dlg() const
00119 {
00120   return myDlg;
00121 }
00122 
00123 //================================================================================
00131 //================================================================================
00132 bool SMESHGUI_MeshOp::onApply()
00133 {
00134   if (isStudyLocked())
00135     return false;
00136 
00137   QString aMess;
00138   if ( !isValid( aMess ) )
00139   {
00140     dlg()->show();
00141     if ( aMess != "" )
00142       SUIT_MessageBox::warning( myDlg, tr( "SMESH_WRN_WARNING" ), aMess );
00143     return false;
00144   }
00145 
00146   bool aResult = false;
00147   aMess = "";
00148   try
00149   {
00150     QStringList anEntryList;
00151     if ( myToCreate && myIsMesh )
00152       aResult = createMesh( aMess, anEntryList );
00153     if ( myToCreate && !myIsMesh )
00154       aResult = createSubMesh( aMess, anEntryList );
00155     else if ( !myToCreate )
00156       aResult = editMeshOrSubMesh( aMess );
00157     if ( aResult )
00158     {
00159       SMESHGUI::Modified();
00160       update( UF_ObjBrowser | UF_Model );
00161       if( LightApp_Application* anApp =
00162           dynamic_cast<LightApp_Application*>( SUIT_Session::session()->activeApplication() ) )
00163         myObjectToSelect = anApp->browseObjects( anEntryList, isApplyAndClose() );
00164     }
00165   }
00166   catch ( const SALOME::SALOME_Exception& S_ex )
00167   {
00168     SalomeApp_Tools::QtCatchCorbaException( S_ex );
00169     aResult = false;
00170   }
00171   catch ( ... )
00172   {
00173     aResult = false;
00174   }
00175 
00176   if ( aResult )
00177   {
00178     if ( myToCreate )
00179       setDefaultName();
00180   }
00181   else
00182   {
00183     if ( aMess == "" )
00184       aMess = tr( "SMESH_OPERATION_FAILED" );
00185     SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ), aMess );
00186   }
00187 
00188   return aResult;
00189 }
00190 
00191 //================================================================================
00198 //================================================================================
00199 void SMESHGUI_MeshOp::startOperation()
00200 {
00201   if (!myDlg)
00202   {
00203     myDlg = new SMESHGUI_MeshDlg( myToCreate, myIsMesh );
00204     for ( int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++ )
00205     {
00206       connect( myDlg->tab( i ), SIGNAL( createHyp( const int, const int ) ),
00207               this, SLOT( onCreateHyp( const int, const int ) ) );
00208       connect( myDlg->tab( i ), SIGNAL( editHyp( const int, const int ) ),
00209               this, SLOT( onEditHyp( const int, const int ) ) );
00210       connect( myDlg->tab( i ), SIGNAL( selectAlgo( const int ) ),
00211               this, SLOT( onAlgoSelected( const int ) ) );
00212     }
00213     connect( myDlg, SIGNAL( hypoSet( const QString& )), SLOT( onHypoSet( const QString& )));
00214     connect( myDlg, SIGNAL( geomSelectionByMesh( bool )), SLOT( onGeomSelectionByMesh( bool )));
00215 
00216     if ( myToCreate )
00217       if ( myIsMesh ) myHelpFileName = "constructing_meshes_page.html";
00218       else myHelpFileName = "constructing_submeshes_page.html";
00219     else myHelpFileName = "editing_meshes_page.html";
00220   }
00221   SMESHGUI_SelectionOp::startOperation();
00222 
00223   // iterate through dimensions and get available algoritms, set them to the dialog
00224   _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
00225   for ( int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++ )
00226   {
00227     SMESHGUI_MeshTab* aTab = myDlg->tab( i );
00228     QStringList hypList;
00229     // clear available hypotheses
00230     aTab->setAvailableHyps( MainHyp, hypList );
00231     aTab->setAvailableHyps( AddHyp, hypList );
00232     aTab->setExistingHyps( MainHyp, hypList );
00233     aTab->setExistingHyps( AddHyp, hypList );
00234     myExistingHyps[ i ][ MainHyp ].clear();
00235     myExistingHyps[ i ][ AddHyp ].clear();
00236     // set algos
00237     availableHyps( i, Algo, hypList, myAvailableHypData[i][Algo] );
00238     aTab->setAvailableHyps( Algo, hypList );
00239   }
00240   if ( myToCreate )
00241   {
00242     setDefaultName();
00243     myDlg->activateObject( myIsMesh ? SMESHGUI_MeshDlg::Geom : SMESHGUI_MeshDlg::Mesh );
00244   }
00245   else
00246     myDlg->activateObject( SMESHGUI_MeshDlg::Obj );
00247 
00248   myDlg->setCurrentTab( SMESH::DIM_3D );
00249   myDlg->show();
00250   myDlg->setGeomPopupEnabled(false);
00251   selectionDone();
00252 
00253   myIgnoreAlgoSelection = false;
00254 
00255   myObjectToSelect.clear();
00256 }
00257 
00258 //=================================================================================
00266 //=================================================================================
00267 void SMESHGUI_MeshOp::commitOperation()
00268 {
00269   SMESHGUI_SelectionOp::commitOperation();
00270 
00271   if ( !myObjectToSelect.isEmpty() ) {
00272     if ( LightApp_SelectionMgr* aSelectionMgr = selectionMgr() ) {
00273       SUIT_DataOwnerPtrList aList;
00274       aList.append( new LightApp_DataOwner( myObjectToSelect ) );
00275       aSelectionMgr->setSelected( aList );
00276     }
00277   }
00278 }
00279 
00280 //================================================================================
00288 //================================================================================
00289 SUIT_SelectionFilter* SMESHGUI_MeshOp::createFilter( const int theId ) const
00290 {
00291   if ( theId == SMESHGUI_MeshDlg::Geom )
00292   {
00293 //     TColStd_MapOfInteger allTypesMap;
00294 //     for ( int i = 0; i < 10; i++ )
00295 //       allTypesMap.Add( i );
00296 //     return new SMESH_NumberFilter( "GEOM", TopAbs_SHAPE, 0, allTypesMap );
00297     return new GEOM_SelectionFilter( (SalomeApp_Study*)study(), true );
00298   }
00299   else if ( theId == SMESHGUI_MeshDlg::Obj && !myToCreate )
00300     return new SMESH_TypeFilter( MESHorSUBMESH );
00301   else if ( theId == SMESHGUI_MeshDlg::Mesh )
00302     return new SMESH_TypeFilter( MESH );
00303   else
00304     return 0;
00305 }
00306 
00307 //================================================================================
00312 //================================================================================
00313 bool SMESHGUI_MeshOp::isSubshapeOk() const
00314 {
00315   if ( !myToCreate || myIsMesh ) // not submesh creation
00316     return false;
00317 
00318   // mesh
00319   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
00320   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
00321   if (!pMesh) return false;
00322 
00323   SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
00324   if (mesh->_is_nil()) return false;
00325 
00326   // main shape of the mesh
00327   GEOM::GEOM_Object_var mainGeom = mesh->GetShapeToMesh();
00328   if (mainGeom->_is_nil()) return false;
00329 
00330   // geometry
00331   QStringList aGEOMs;
00332   myDlg->selectedObject(SMESHGUI_MeshDlg::Geom, aGEOMs);
00333 
00334   if (aGEOMs.count() > 0) {
00335     GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
00336     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
00337     if (geomGen->_is_nil() || !aStudy) return false;
00338 
00339     GEOM::GEOM_IGroupOperations_var op =
00340         geomGen->GetIGroupOperations(aStudy->StudyId());
00341     if (op->_is_nil()) return false;
00342 
00343     // check all selected shapes
00344     QStringList::const_iterator aSubShapesIter = aGEOMs.begin();
00345     for ( ; aSubShapesIter != aGEOMs.end(); aSubShapesIter++) {
00346       QString aSubGeomEntry = (*aSubShapesIter);
00347       _PTR(SObject) pSubGeom = studyDS()->FindObjectID(aSubGeomEntry.toLatin1().data());
00348       if (!pSubGeom) return false;
00349 
00350       GEOM::GEOM_Object_var aSubGeomVar =
00351         GEOM::GEOM_Object::_narrow(_CAST(SObject,pSubGeom)->GetObject());
00352       if (aSubGeomVar->_is_nil()) return false;
00353 
00354       // skl for NPAL14695 - implementation of searching of mainObj
00355       GEOM::GEOM_Object_var mainObj = op->GetMainShape(aSubGeomVar);
00356       //if (mainObj->_is_nil() ||
00357       //    string(mainObj->GetEntry()) != string(mainGeom->GetEntry())) return false;
00358       while(1) {
00359         if (mainObj->_is_nil())
00360           return false;
00361         if (std::string(mainObj->GetEntry()) == std::string(mainGeom->GetEntry()))
00362           return true;
00363         mainObj = op->GetMainShape(mainObj);
00364       }
00365     }
00366     //return true;
00367   }
00368 
00369   return false;
00370 }
00371 
00372 //================================================================================
00378 //================================================================================
00379 char* SMESHGUI_MeshOp::isSubmeshIgnored() const
00380 {
00381   if ( myToCreate && !myIsMesh ) {
00382 
00383     QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
00384     QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
00385     _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
00386     if ( pMesh ) {
00387 
00388       QStringList algoNames;
00389       THypList    algoList;
00390       existingHyps(3, Algo, pMesh, algoNames, algoList);
00391       if (!algoList.empty()) {
00392         HypothesisData* algo = SMESH::GetHypothesisData( algoList[0].first->GetName() );
00393         if ( algo &&
00394              algo->InputTypes.empty() && // builds all dimensions it-self
00395              !algo->IsSupportSubmeshes )
00396           return CORBA::string_dup( algoNames[0].toLatin1().data() );
00397       }
00398 
00399 //       GEOM::GEOM_Object_var geom;
00400 //       if (_PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() ))
00401 //         geom = SMESH::SObjectToInterface<GEOM::GEOM_Object>( pGeom );
00402 
00403 //       if ( !geom->_is_nil() && geom->GetShapeType() >= GEOM::FACE ) { // WIRE, EDGE as well
00404         existingHyps(2, Algo, pMesh, algoNames, algoList);
00405         if (!algoList.empty()) {
00406           HypothesisData* algo = SMESH::GetHypothesisData( algoList[0].first->GetName() );
00407           if ( algo &&
00408                algo->InputTypes.empty() && // builds all dimensions it-self
00409                !algo->IsSupportSubmeshes )
00410             return CORBA::string_dup( algoNames[0].toLatin1().data() );
00411         }
00412 //       }
00413     }
00414   }
00415   return 0;
00416 }
00417 
00418 //================================================================================
00423 //================================================================================
00424 _PTR(SObject) SMESHGUI_MeshOp::getSubmeshByGeom() const
00425 {
00426   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
00427   QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
00428   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
00429   _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() );
00430   if ( pMesh && pGeom ) {
00431     GEOM::GEOM_Object_var geom = SMESH::SObjectToInterface<GEOM::GEOM_Object>( pGeom );
00432     if ( !geom->_is_nil() ) {
00433       int tag = -1;
00434       switch ( geom->GetShapeType() ) {
00435       case GEOM::VERTEX:   tag = SMESH::Tag_SubMeshOnVertex;   break;
00436       case GEOM::EDGE:     tag = SMESH::Tag_SubMeshOnEdge;     break;
00437       case GEOM::WIRE:     tag = SMESH::Tag_SubMeshOnWire;     break;
00438       case GEOM::FACE:     tag = SMESH::Tag_SubMeshOnFace;     break;
00439       case GEOM::SHELL:    tag = SMESH::Tag_SubMeshOnShell;    break;
00440       case GEOM::SOLID:    tag = SMESH::Tag_SubMeshOnSolid;    break;
00441       case GEOM::COMPOUND: tag = SMESH::Tag_SubMeshOnCompound; break;
00442       default:;
00443       }
00444       _PTR(GenericAttribute) anAttr;
00445       _PTR(SObject) aSubmeshRoot;
00446       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
00447       if ( pMesh->FindSubObject( tag, aSubmeshRoot ) )
00448       {
00449         _PTR(ChildIterator) smIter = aStudy->NewChildIterator( aSubmeshRoot );
00450         for ( ; smIter->More(); smIter->Next() )
00451         {
00452           _PTR(SObject) aSmObj = smIter->Value();
00453           if ( ! aSmObj->FindAttribute( anAttr, "AttributeIOR" ))
00454             continue;
00455           _PTR(ChildIterator) anIter1 = aStudy->NewChildIterator(aSmObj);
00456           for ( ; anIter1->More(); anIter1->Next()) {
00457             _PTR(SObject) pGeom2 = anIter1->Value();
00458             if ( pGeom2->ReferencedObject( pGeom2 ) &&
00459                  pGeom2->GetID() == pGeom->GetID() )
00460               return aSmObj;
00461           }
00462         }
00463       }
00464     }
00465   }
00466   return _PTR(SObject)();
00467 }
00468 
00469 //================================================================================
00475 //================================================================================
00476 void SMESHGUI_MeshOp::selectionDone()
00477 {
00478   if (!dlg()->isVisible() || !myDlg->isEnabled())
00479     return;
00480 
00481   SMESHGUI_SelectionOp::selectionDone();
00482 
00483   try
00484   {
00485     myIsOnGeometry = true;
00486 
00487     //Check geometry for mesh
00488     QString anObjEntry = myDlg->selectedObject(SMESHGUI_MeshDlg::Obj);
00489     _PTR(SObject) pObj = studyDS()->FindObjectID(anObjEntry.toLatin1().data());
00490     if (pObj)
00491     {
00492       SMESH::SMESH_Mesh_var aMeshVar =
00493         SMESH::SMESH_Mesh::_narrow(_CAST(SObject,pObj)->GetObject());
00494       if (!aMeshVar->_is_nil()) {
00495         if (!myToCreate && !aMeshVar->HasShapeToMesh())
00496           myIsOnGeometry = false;
00497       }
00498     }
00499 
00500     if (myIsOnGeometry)
00501     {
00502       // Enable tabs according to shape dimension
00503 
00504       int shapeDim = 3;
00505 
00506       QStringList aGEOMs;
00507       myDlg->selectedObject(SMESHGUI_MeshDlg::Geom, aGEOMs);
00508       GEOM::ListOfGO_var aSeq = new GEOM::ListOfGO;
00509 
00510       if (aGEOMs.count() > 0) {
00511         // one or more GEOM shape selected
00512         aSeq->length(aGEOMs.count());
00513         QStringList::const_iterator aSubShapesIter = aGEOMs.begin();
00514         int iSubSh = 0;
00515         for ( ; aSubShapesIter != aGEOMs.end(); aSubShapesIter++, iSubSh++) {
00516           QString aSubGeomEntry = (*aSubShapesIter);
00517           _PTR(SObject) pSubGeom = studyDS()->FindObjectID(aSubGeomEntry.toLatin1().data());
00518           GEOM::GEOM_Object_var aSubGeomVar =
00519             GEOM::GEOM_Object::_narrow(_CAST(SObject,pSubGeom)->GetObject());
00520           aSeq[iSubSh] = aSubGeomVar;
00521         }
00522       } else {
00523         // get geometry by selected sub-mesh
00524         QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
00525         _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
00526         GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
00527         if (!aGeomVar->_is_nil()) {
00528           aSeq->length(1);
00529           aSeq[0] = aGeomVar;
00530         }
00531       }
00532 
00533       if (aSeq->length() > 0) {
00534         shapeDim = -1;
00535         for (int iss = 0; iss < aSeq->length() && shapeDim < 3; iss++) {
00536           GEOM::GEOM_Object_var aGeomVar = aSeq[iss];
00537           switch ( aGeomVar->GetShapeType() ) {
00538           case GEOM::SOLID:  shapeDim = 3; break;
00539           case GEOM::SHELL:
00540             // Bug 0016155: EDF PAL 447: If the shape is a Shell, disable 3D tab
00541             // {
00542             //   TopoDS_Shape aShape;
00543             //   bool isClosed = GEOMBase::GetShape(aGeomVar, aShape) && /*aShape.Closed()*/BRep_Tool::IsClosed(aShape);
00544             //   shapeDim = qMax(isClosed ? 3 : 2, shapeDim);
00545             // }
00546             // break;
00547           case GEOM::FACE:   shapeDim = qMax(2, shapeDim); break;
00548           case GEOM::WIRE:
00549           case GEOM::EDGE:   shapeDim = qMax(1, shapeDim); break;
00550           case GEOM::VERTEX: shapeDim = qMax(0, shapeDim); break;
00551           default:
00552             {
00553               TopoDS_Shape aShape;
00554               if (GEOMBase::GetShape(aGeomVar, aShape))
00555               {
00556                 TopExp_Explorer exp (aShape, TopAbs_SOLID);
00557                 if (exp.More()) {
00558                   shapeDim = 3;
00559                 }
00560                 // Bug 0016155: EDF PAL 447: If the shape is a Shell, disable 3D tab
00561                 // else if ( exp.Init( aShape, TopAbs_SHELL ), exp.More() )
00562                 // {
00563                 //   shapeDim = 2;
00564                 //   for (; exp.More() && shapeDim == 2; exp.Next()) {
00565                 //     if (/*exp.Current().Closed()*/BRep_Tool::IsClosed(exp.Current()))
00566                 //       shapeDim = 3;
00567                 //   }
00568                 // }
00569                 else if ( exp.Init( aShape, TopAbs_FACE ), exp.More() )
00570                   shapeDim = qMax(2, shapeDim);
00571                 else if ( exp.Init( aShape, TopAbs_EDGE ), exp.More() )
00572                   shapeDim = qMax(1, shapeDim);
00573                 else if ( exp.Init( aShape, TopAbs_VERTEX ), exp.More() )
00574                   shapeDim = qMax(0, shapeDim);
00575               }
00576             }
00577           }
00578           if ( shapeDim == 3 )
00579             break;
00580         }
00581       }
00582       for (int i = SMESH::DIM_3D; i > shapeDim; i--) {
00583         // reset algos before disabling tabs (0020138)
00584         onAlgoSelected(-1, i);
00585       }
00586       myDlg->setMaxHypoDim( shapeDim );
00587       myDlg->setHypoSets( SMESH::GetHypothesesSets( shapeDim ));
00588 
00589       if (!myToCreate) // edition: read hypotheses
00590       {
00591         if (pObj != 0)
00592         {
00593           SMESH::SMESH_subMesh_var aVar =
00594             SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
00595           myDlg->setObjectShown( SMESHGUI_MeshDlg::Mesh, !aVar->_is_nil() );
00596           myDlg->setObjectShown( SMESHGUI_MeshDlg::Geom, true );
00597           myDlg->objectWg( SMESHGUI_MeshDlg::Mesh, SMESHGUI_MeshDlg::Btn )->hide();
00598           myDlg->objectWg( SMESHGUI_MeshDlg::Geom, SMESHGUI_MeshDlg::Btn )->hide();
00599           myDlg->updateGeometry();
00600           myDlg->adjustSize();
00601           readMesh();
00602         }
00603         else
00604           myDlg->reset();
00605       }
00606       else if ( !myIsMesh ) // submesh creation
00607       {
00608         // if a submesh on the selected shape already exist, pass to submesh edition mode
00609         if ( _PTR(SObject) pSubmesh = getSubmeshByGeom() ) {
00610           SMESH::SMESH_subMesh_var sm =
00611             SMESH::SObjectToInterface<SMESH::SMESH_subMesh>( pSubmesh );
00612           bool editSubmesh = ( !sm->_is_nil() &&
00613                                SUIT_MessageBox::question( myDlg, tr( "SMESH_WARNING" ),
00614                                                           tr( "EDIT_SUBMESH_QUESTION"),
00615                                                           SUIT_MessageBox::Yes |
00616                                                           SUIT_MessageBox::No,
00617                                                           SUIT_MessageBox::No )
00618                                == SUIT_MessageBox::Yes );
00619           if ( editSubmesh )
00620           {
00621             selectionMgr()->clearFilters();
00622             selectObject( pSubmesh );
00623             SMESHGUI::GetSMESHGUI()->switchToOperation(704);
00624             return;
00625           }
00626           else
00627           {
00628             myDlg->selectObject( "", SMESHGUI_MeshDlg::Geom, "" );
00629             selectObject( _PTR(SObject)() );
00630             selectionDone();
00631             return;
00632           }
00633         }
00634         // discard selected mesh if submesh creation not allowed because of
00635         // a global algorithm that does not support submeshes
00636         if ( char* algoName = isSubmeshIgnored() ) {
00637           SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ),
00638                                     tr("SUBMESH_NOT_ALLOWED").arg(algoName));
00639           CORBA::string_free( algoName );
00640           myDlg->selectObject( "", SMESHGUI_MeshDlg::Mesh, "" );
00641           selectObject( _PTR(SObject)() );
00642           selectionDone();
00643           return;
00644         }
00645 
00646         // enable/disable popup for choice of geom selection way
00647         bool enable = false;
00648         QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
00649         if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() )) {
00650           SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
00651           if ( !mesh->_is_nil() ) {
00652             //rnv: issue 21056: EDF 1608 SMESH: Dialog Box "Create Sub Mesh": focus should automatically switch to geometry
00653             QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
00654             _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() );
00655             if ( !pGeom || GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() )->_is_nil() )
00656               myDlg->activateObject(SMESHGUI_MeshDlg::Geom);
00657             enable = ( shapeDim > 1 ) && ( mesh->NbEdges() > 0 );
00658           }
00659         }
00660         myDlg->setGeomPopupEnabled( enable );
00661       }
00662     }
00663     else { // no geometry defined
00664       myDlg->enableTab( SMESH::DIM_3D );
00665       QStringList hypList;
00666       availableHyps( SMESH::DIM_3D, Algo, hypList,
00667                      myAvailableHypData[SMESH::DIM_3D][Algo]);
00668 
00669       SMESHGUI_MeshTab* aTab = myDlg->tab( SMESH::DIM_3D );
00670       aTab->setAvailableHyps( Algo, hypList );
00671       for (int i = SMESH::DIM_0D;i < SMESH::DIM_3D; ++i) {
00672         myDlg->disableTab(i);
00673       }
00674       //Hide labels and fields (Mesh ang Geometry)
00675       myDlg->setObjectShown( SMESHGUI_MeshDlg::Mesh, false );
00676       myDlg->setObjectShown( SMESHGUI_MeshDlg::Geom, false );
00677       myDlg->adjustSize();
00678       readMesh();
00679     }
00680   }
00681   catch ( const SALOME::SALOME_Exception& S_ex )
00682   {
00683     SalomeApp_Tools::QtCatchCorbaException( S_ex );
00684   }
00685   catch ( ... )
00686   {
00687   }
00688 }
00689 
00690 //================================================================================
00699 //================================================================================
00700 bool SMESHGUI_MeshOp::isValid( QString& theMess ) const
00701 {
00702   // Selected object to be  edited
00703   if ( !myToCreate && myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ) == "" )
00704   {
00705     theMess = tr( "THERE_IS_NO_OBJECT_FOR_EDITING" );
00706     return false;
00707   }
00708 
00709   // Name
00710   QString aMeshName = myDlg->objectText( SMESHGUI_MeshDlg::Obj ).trimmed();
00711   if ( aMeshName.isEmpty() )
00712   {
00713     theMess = myIsMesh ? tr( "NAME_OF_MESH_IS_EMPTY" ) : tr( "NAME_OF_SUBMESH_IS_EMPTY" );
00714     return false;
00715   }
00716 
00717 /*  // Imported mesh, if create sub-mesh or edit mesh
00718   if ( !myToCreate || ( myToCreate && !myIsMesh ))
00719   {
00720     QString aMeshEntry = myDlg->selectedObject
00721       ( myToCreate ? SMESHGUI_MeshDlg::Mesh : SMESHGUI_MeshDlg::Obj );
00722     if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() )) {
00723       SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
00724       if ( !mesh->_is_nil() && CORBA::is_nil( mesh->GetShapeToMesh() )) {
00725         theMess = tr( "IMPORTED_MESH" );
00726         return false;
00727       }
00728     }
00729   }*/
00730 
00731   // Geom
00732   if ( myToCreate )
00733   {
00734     QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
00735     if ( aGeomEntry == "" )
00736     {
00737       theMess = tr( "GEOMETRY_OBJECT_IS_NOT_DEFINED" );
00738       return false;
00739     }
00740     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() );
00741     if ( !pGeom || GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() )->_is_nil() )
00742     {
00743       theMess = tr( "GEOMETRY_OBJECT_IS_NULL" );
00744       return false;
00745     }
00746 
00747     // Mesh
00748     if ( !myIsMesh ) // i.e sub-mesh creation,
00749     {
00750       QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
00751       if ( aMeshEntry == "" )
00752       {
00753         theMess = tr( "MESH_IS_NOT_DEFINED" );
00754         return false;
00755       }
00756       _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
00757       if ( !pMesh || SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() )->_is_nil() )
00758       {
00759         theMess = tr( "MESH_IS_NULL" );
00760         return false;
00761       }
00762       if ( !isSubshapeOk() )
00763       {
00764         theMess = tr( "INVALID_SUBSHAPE" );
00765         return false;
00766       }
00767     }
00768   }
00769 
00770   return true;
00771 }
00772 
00773 //================================================================================
00782 //================================================================================
00783 static bool isCompatible(const HypothesisData* theAlgoData,
00784                          const HypothesisData* theHypData,
00785                          const int             theHypType)
00786 {
00787   if ( !theAlgoData )
00788     return true;
00789 
00790   if ( theHypType == SMESHGUI_MeshOp::Algo )
00791     return SMESH::IsCompatibleAlgorithm( theAlgoData, theHypData );
00792 
00793   bool isOptional;
00794   return ( SMESH::IsAvailableHypothesis( theAlgoData, theHypData->TypeName, isOptional ));
00795 }
00796 
00797 //================================================================================
00808 //================================================================================
00809 void SMESHGUI_MeshOp::availableHyps( const int       theDim,
00810                                      const int       theHypType,
00811                                      QStringList&    theHyps,
00812                                      THypDataList&   theDataList,
00813                                      HypothesisData* theAlgoData ) const
00814 {
00815   theDataList.clear();
00816   theHyps.clear();
00817   bool isAlgo = ( theHypType == Algo );
00818   bool isAux  = ( theHypType == AddHyp );
00819   QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses( isAlgo, theDim, isAux, myIsOnGeometry );
00820 
00821   QStringList::const_iterator anIter;
00822   for ( anIter = aHypTypeNameList.begin(); anIter != aHypTypeNameList.end(); ++anIter )
00823   {
00824     HypothesisData* aData = SMESH::GetHypothesisData( *anIter );
00825     if ( isCompatible ( theAlgoData, aData, theHypType )) {
00826       theDataList.append( aData );
00827       theHyps.append( aData->Label );
00828     }
00829   }
00830 }
00831 
00832 //================================================================================
00846 //================================================================================
00847 void SMESHGUI_MeshOp::existingHyps( const int theDim,
00848                                     const int theHypType,
00849                                     _PTR(SObject) theFather,
00850                                     QStringList& theHyps,
00851                                     THypList& theHypList,
00852                                     HypothesisData* theAlgoData)
00853 {
00854   // Clear hypoheses list
00855   theHyps.clear();
00856   theHypList.clear();
00857 
00858   if ( !theFather )
00859     return;
00860 
00861   const bool isAux  = ( theHypType == AddHyp );
00862 
00863   _PTR(SObject)          aHypRoot;
00864   _PTR(GenericAttribute) anAttr;
00865   _PTR(AttributeName)    aName;
00866   _PTR(AttributeIOR)     anIOR;
00867 
00868   bool isMesh = !_CAST( SComponent, theFather );
00869   int aPart = -1;
00870   if ( isMesh )
00871     aPart = theHypType == Algo ? SMESH::Tag_RefOnAppliedAlgorithms : SMESH::Tag_RefOnAppliedHypothesis;
00872   else
00873     aPart = theHypType == Algo ? SMESH::Tag_AlgorithmsRoot : SMESH::Tag_HypothesisRoot;
00874 
00875   if ( theFather->FindSubObject( aPart, aHypRoot ) )
00876   {
00877     _PTR(ChildIterator) anIter =
00878       SMESH::GetActiveStudyDocument()->NewChildIterator( aHypRoot );
00879     for ( ; anIter->More(); anIter->Next() )
00880     {
00881       _PTR(SObject) anObj = anIter->Value();
00882       if ( isMesh ) // i.e. mesh or submesh
00883       {
00884         _PTR(SObject) aRefObj;
00885         if ( anObj->ReferencedObject( aRefObj ) )
00886           anObj = aRefObj;
00887         else
00888           continue;
00889       }
00890       if ( anObj->FindAttribute( anAttr, "AttributeName" ) )
00891       {
00892         aName = anAttr;
00893         CORBA::Object_var aVar = _CAST(SObject,anObj)->GetObject();
00894         if ( !CORBA::is_nil( aVar ) )
00895         {
00896           SMESH::SMESH_Hypothesis_var aHypVar = SMESH::SMESH_Hypothesis::_narrow( aVar );
00897           if ( !aHypVar->_is_nil() )
00898           {
00899             HypothesisData* aData = SMESH::GetHypothesisData( aHypVar->GetName() );
00900             if ( !aData) continue;
00901             if ( ( theDim == -1 || aData->Dim.contains( theDim ) ) &&
00902                  ( isCompatible ( theAlgoData, aData, theHypType )) &&
00903                  ( isAux == aData->IsAux ))
00904             {
00905               std::string aHypName = aName->Value();
00906               theHyps.append( aHypName.c_str() );
00907               theHypList.append( THypItem( aHypVar, aHypName.c_str() ) );
00908             }
00909           }
00910         }
00911       }
00912     }
00913   }
00914 }
00915 
00916 //================================================================================
00925 //================================================================================
00926 SMESH::SMESH_Hypothesis_var
00927 SMESHGUI_MeshOp::getInitParamsHypothesis( const QString& aHypType,
00928                                           const QString& aServerLib ) const
00929 {
00930   if ( aHypType.isEmpty() || aServerLib.isEmpty() )
00931     return SMESH::SMESH_Hypothesis::_nil();
00932 
00933   const int nbColonsInMeshEntry = 3;
00934   bool isSubMesh = myToCreate ?
00935     !myIsMesh :
00936     myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ).count(':') > nbColonsInMeshEntry;
00937 
00938   // get mesh and geom object
00939   SMESH::SMESH_Mesh_var aMeshVar = SMESH::SMESH_Mesh::_nil();
00940   GEOM::GEOM_Object_var aGeomVar = GEOM::GEOM_Object::_nil();
00941 
00942   QString anEntry;
00943   if ( isSubMesh )
00944   {
00945     anEntry = myDlg->selectedObject
00946       ( myToCreate ? SMESHGUI_MeshDlg::Mesh : SMESHGUI_MeshDlg::Obj );
00947     if ( _PTR(SObject) pObj = studyDS()->FindObjectID( anEntry.toLatin1().data() ))
00948     {
00949       CORBA::Object_ptr Obj = _CAST( SObject,pObj )->GetObject();
00950       if ( myToCreate ) // mesh and geom may be selected
00951       {
00952         aMeshVar = SMESH::SMESH_Mesh::_narrow( Obj );
00953         anEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
00954         if ( _PTR(SObject) pGeom = studyDS()->FindObjectID( anEntry.toLatin1().data() ))
00955           aGeomVar= GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
00956       }
00957       else // edition: sub-mesh may be selected
00958       {
00959         SMESH::SMESH_subMesh_var sm = SMESH::SMESH_subMesh::_narrow( Obj );
00960         if ( !sm->_is_nil() ) {
00961           aMeshVar = sm->GetFather();
00962           aGeomVar = sm->GetSubShape();
00963         }
00964       }
00965     }
00966   }
00967   else // mesh
00968   {
00969     if ( !myToCreate ) // mesh to edit can be selected
00970     {
00971       anEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
00972       if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( anEntry.toLatin1().data() ))
00973       {
00974         aMeshVar = SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
00975         if ( !aMeshVar->_is_nil() )
00976           aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pMesh );
00977       }
00978     }
00979     if ( aGeomVar->_is_nil() ) {
00980       anEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
00981       if ( _PTR(SObject) pGeom = studyDS()->FindObjectID( anEntry.toLatin1().data() ))
00982       {
00983         aGeomVar= GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
00984       }
00985     }
00986   }
00987 
00988   SMESH::SMESH_Hypothesis_var hyp =
00989     SMESHGUI::GetSMESHGen()->GetHypothesisParameterValues( aHypType.toLatin1().data(),
00990                                                            aServerLib.toLatin1().data(),
00991                                                            aMeshVar,
00992                                                            aGeomVar,
00993                                                            /*byMesh = */isSubMesh);
00994   if ( hyp->_is_nil() && isSubMesh )
00995     hyp = SMESHGUI::GetSMESHGen()->GetHypothesisParameterValues( aHypType.toLatin1().data(),
00996                                                                  aServerLib.toLatin1().data(),
00997                                                                  aMeshVar,
00998                                                                  aGeomVar,
00999                                                                  /*byMesh = */false);
01000   return hyp;
01001 }
01002 
01003 //================================================================================
01010 //================================================================================
01011 static int getTabDim (const QObject* tab, SMESHGUI_MeshDlg* dlg )
01012 {
01013   int aDim = -1;
01014   for (int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++)
01015     if (tab == dlg->tab(i))
01016       aDim = i;
01017   return aDim;
01018 }
01019 
01020 //================================================================================
01029 //================================================================================
01030 void SMESHGUI_MeshOp::onCreateHyp( const int theHypType, const int theIndex )
01031 {
01032   // Specifies dimension of hypothesis to be created
01033   int aDim = getTabDim( sender(), myDlg );
01034   if (aDim == -1)
01035     return;
01036 
01037   // Specifies type of hypothesis to be created
01038   THypDataList& dataList = myAvailableHypData[ aDim ][ theHypType ];
01039   if (theIndex < 0 || theIndex >= dataList.count())
01040     return;
01041   QString aHypTypeName = dataList[ theIndex ]->TypeName;
01042 
01043   // Create hypothesis
01044   createHypothesis(aDim, theHypType, aHypTypeName);
01045 }
01046 
01047 namespace
01048 {
01049   QString GetUniqueName (const QStringList& theHypNames,
01050                          const QString& theName,
01051                          size_t theIteration = 1)
01052   {
01053     QString aName = theName + "_" + QString::number( theIteration );
01054     if ( theHypNames.contains( aName ) )
01055       return GetUniqueName( theHypNames, theName, ++theIteration );
01056     return aName;
01057   }
01058 }
01059 
01060 //================================================================================
01067 //================================================================================
01068 void SMESHGUI_MeshOp::createHypothesis(const int theDim,
01069                                        const int theType,
01070                                        const QString& theTypeName)
01071 {
01072   HypothesisData* aData = SMESH::GetHypothesisData(theTypeName);
01073   if (!aData)
01074     return;
01075 
01076   myDim = theDim;
01077   myType = theType;
01078   QStringList aHypNames;
01079   TDim2Type2HypList::const_iterator aDimIter = myExistingHyps.begin();
01080   for ( ; aDimIter != myExistingHyps.end(); aDimIter++) {
01081     const TType2HypList& aType2HypList = aDimIter.value();
01082     TType2HypList::const_iterator aTypeIter = aType2HypList.begin();
01083     for ( ; aTypeIter != aType2HypList.end(); aTypeIter++) {
01084       const THypList& aHypList = aTypeIter.value();
01085       THypList::const_iterator anIter = aHypList.begin();
01086       for ( ; anIter != aHypList.end(); anIter++) {
01087         const THypItem& aHypItem = *anIter;
01088         const QString& aHypName = aHypItem.second;
01089         aHypNames.append(aHypName);
01090       }
01091     }
01092   }
01093   QString aHypName = GetUniqueName( aHypNames, aData->Label);
01094 
01095   // existing hypos
01096   bool dialog = false;
01097 
01098   QString aClientLibName = aData->ClientLibName;
01099   if (aClientLibName == "") {
01100     // Call hypothesis creation server method (without GUI)
01101     SMESH::SMESH_Hypothesis_var aHyp =
01102       SMESH::CreateHypothesis(theTypeName, aHypName, false);
01103 #ifdef WITHGENERICOBJ
01104     if (!CORBA::is_nil(aHyp))
01105       aHyp->UnRegister();
01106 #endif
01107   } else {
01108     // Get hypotheses creator client (GUI)
01109     // BUG 0020378
01110     //SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(theTypeName);
01111     SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(theTypeName);
01112 
01113     // Create hypothesis
01114     if (aCreator) {
01115       // Get parameters appropriate to initialize a new hypothesis
01116       SMESH::SMESH_Hypothesis_var initParamHyp =
01117         getInitParamsHypothesis(theTypeName, aData->ServerLibName);
01118 
01119       removeCustomFilters(); // Issue 0020170
01120 
01121       // Get Entry of the Geom object
01122       QString aGeomEntry = "";
01123       QString aMeshEntry = "";
01124       QString anObjEntry = "";
01125       aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
01126       aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
01127       anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
01128 
01129       if ( myToCreate && myIsMesh )
01130         aMeshEntry = aGeomEntry;
01131 
01132       if ( aMeshEntry != aGeomEntry ) { // Get Geom object from Mesh of a sub-mesh being edited
01133         _PTR(SObject) pObj = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
01134         GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
01135         aMeshEntry = ( aGeomVar->_is_nil() ) ? "" : aMeshEntry = aGeomVar->GetStudyEntry();
01136       }
01137 
01138       if ( aMeshEntry == "" && aGeomEntry == "" ) { // get geom of an object being edited
01139         _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
01140         bool isMesh;
01141         GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj, &isMesh );
01142         if ( !aGeomVar->_is_nil() )
01143         {
01144           aGeomEntry = aGeomVar->GetStudyEntry();
01145           if ( isMesh )
01146             aMeshEntry = aGeomEntry;
01147         }
01148       }
01149 
01150       if ( anObjEntry != "" && aGeomEntry != "" && aMeshEntry == "" ) {
01151         // take geometry from submesh being created
01152         _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
01153         if ( pObj ) {
01154           // if current object is sub-mesh
01155           SMESH::SMESH_subMesh_var aSubMeshVar =
01156             SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
01157           if ( !aSubMeshVar->_is_nil() ) {
01158             SMESH::SMESH_Mesh_var aMeshVar =  aSubMeshVar->GetFather();
01159             if ( !aMeshVar->_is_nil() ) {
01160               _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar );
01161               GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( aMeshSO );
01162               if ( !aGeomVar->_is_nil() )
01163                 aMeshEntry = aGeomVar->GetStudyEntry();
01164             }
01165           }
01166         }
01167       }
01168 
01169       aCreator->setShapeEntry( aGeomEntry );
01170       if ( aMeshEntry != "" )
01171         aCreator->setMainShapeEntry( aMeshEntry );
01172       myDlg->setEnabled( false );
01173       aCreator->create(initParamHyp, aHypName, myDlg, this, SLOT( onHypoCreated( int ) ) );
01174       dialog = true;
01175     }
01176     else {
01177      SMESH::SMESH_Hypothesis_var aHyp =
01178        SMESH::CreateHypothesis(theTypeName, aHypName, false);
01179 #ifdef WITHGENERICOBJ
01180      if (!CORBA::is_nil(aHyp))
01181        aHyp->UnRegister();
01182 #endif
01183     }
01184   }
01185 
01186   if( !dialog )
01187     onHypoCreated(2);
01188 }
01189 
01190 //================================================================================
01198 //================================================================================
01199 void SMESHGUI_MeshOp::onHypoCreated( int result )
01200 {
01201   if( result != 2 )
01202   {
01203     int obj = myDlg->getActiveObject();
01204     onActivateObject( obj ); // Issue 0020170. Restore filters
01205     myDlg->setEnabled( true );
01206   }
01207 
01208   _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
01209 
01210   int nbHyp = myExistingHyps[myDim][myType].count();
01211   HypothesisData* algoData = hypData( myDim, Algo, currentHyp( myDim, Algo ));
01212   QStringList aNewHyps;
01213   existingHyps(myDim, myType, aFather, aNewHyps, myExistingHyps[myDim][myType], algoData);
01214   if (aNewHyps.count() > nbHyp)
01215   {
01216     for (int i = nbHyp; i < aNewHyps.count(); i++)
01217       myDlg->tab(myDim)->addHyp(myType, aNewHyps[i]);
01218   }
01219 
01220   if( result!=2 && myHypoSet )
01221     processSet();
01222 }
01223 
01224 //================================================================================
01233 //================================================================================
01234 void SMESHGUI_MeshOp::onEditHyp( const int theHypType, const int theIndex )
01235 {
01236   // Speicfies dimension of hypothesis to be created
01237   int aDim = getTabDim( sender(), myDlg );
01238   if (aDim == -1)
01239     return;
01240 
01241   const THypList& aList = myExistingHyps[ aDim ][ theHypType ];
01242   if ( theIndex < 0 || theIndex >= aList.count() )
01243     return;
01244   const THypItem& aHypItem = aList[ theIndex ];
01245   SMESH::SMESH_Hypothesis_var aHyp = aHypItem.first;
01246   if ( aHyp->_is_nil() )
01247     return;
01248 
01249   SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(aHyp->GetName());
01250   if ( aCreator )
01251   {
01252     // Get initial parameters
01253     SMESH::SMESH_Hypothesis_var initParamHyp =
01254       getInitParamsHypothesis( aHyp->GetName(), aHyp->GetLibName());
01255     aCreator->setInitParamsHypothesis( initParamHyp );
01256 
01257     // Get Entry of the Geom object
01258     QString aGeomEntry = "";
01259     QString aMeshEntry = "";
01260     QString anObjEntry = "";
01261     aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
01262     aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
01263     anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
01264 
01265     if ( myToCreate && myIsMesh )
01266       aMeshEntry = aGeomEntry;
01267 
01268     if ( aMeshEntry != aGeomEntry ) { // Get Geom object from Mesh of a sub-mesh being edited
01269       _PTR(SObject) pObj = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
01270       GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
01271       aMeshEntry = ( aGeomVar->_is_nil() ) ? "" : aMeshEntry = aGeomVar->GetStudyEntry();
01272     }
01273 
01274     if ( aMeshEntry == "" && aGeomEntry == "" ) { // get geom of an object being edited
01275       _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
01276       bool isMesh;
01277       GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj, &isMesh );
01278       if ( !aGeomVar->_is_nil() )
01279       {
01280         aGeomEntry = aGeomVar->GetStudyEntry();
01281         if ( isMesh )
01282           aMeshEntry = aGeomEntry;
01283       }
01284     }
01285 
01286     if ( anObjEntry != "" && aGeomEntry != "" && aMeshEntry == "" ) {
01287       // take geometry from submesh being created
01288       _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
01289       if ( pObj ) {
01290         // if current object is sub-mesh
01291         SMESH::SMESH_subMesh_var aSubMeshVar =
01292           SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
01293         if ( !aSubMeshVar->_is_nil() ) {
01294           SMESH::SMESH_Mesh_var aMeshVar =  aSubMeshVar->GetFather();
01295           if ( !aMeshVar->_is_nil() ) {
01296             _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar );
01297             GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( aMeshSO );
01298             if ( !aGeomVar->_is_nil() )
01299               aMeshEntry = aGeomVar->GetStudyEntry();
01300           }
01301         }
01302       }
01303     }
01304 
01305     aCreator->setShapeEntry( aGeomEntry );
01306     if ( aMeshEntry != "" )
01307       aCreator->setMainShapeEntry( aMeshEntry );
01308     removeCustomFilters(); // Issue 0020170
01309     myDlg->setEnabled( false );
01310     aCreator->edit( aHyp.in(), aHypItem.second, dlg(), this, SLOT( onHypoEdited( int ) ) );
01311   }
01312 }
01313 
01314 //================================================================================
01321 //================================================================================
01322 void SMESHGUI_MeshOp::onHypoEdited( int result )
01323 {
01324   int obj = myDlg->getActiveObject();
01325   onActivateObject( obj ); // Issue 0020170. Restore filters
01326   myDlg->setEnabled( true );
01327 }
01328 
01329 //================================================================================
01337 //================================================================================
01338 HypothesisData* SMESHGUI_MeshOp::hypData( const int theDim,
01339                                           const int theHypType,
01340                                           const int theIndex)
01341 {
01342   if ( theDim     > -1 && theDim    <= SMESH::DIM_3D &&
01343        theHypType > -1 && theHypType < NbHypTypes &&
01344        theIndex   > -1 && theIndex   < myAvailableHypData[ theDim ][ theHypType ].count() )
01345     return myAvailableHypData[ theDim ][ theHypType ][ theIndex ];
01346   return 0;
01347 }
01348 
01349 //================================================================================
01354 //================================================================================
01355 void SMESHGUI_MeshOp::onAlgoSelected( const int theIndex,
01356                                       const int theDim )
01357 {
01358   if ( myIgnoreAlgoSelection )
01359     return;
01360 
01361   int aDim = theDim < 0 ? getTabDim( sender(), myDlg ): theDim;
01362   if (aDim == -1)
01363     return;
01364 
01365   // find highest available dimension, all algos of this dimension are available for choice
01366   int aTopDim = -1;
01367   for (int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++)
01368     if (isAccessibleDim( i ))
01369       aTopDim = i;
01370   if (aTopDim == -1)
01371     return;
01372 
01373   const bool isSubmesh = ( myToCreate ? !myIsMesh : myDlg->isObjectShown( SMESHGUI_MeshDlg::Mesh ));
01374 
01375   HypothesisData* algoData = hypData( aDim, Algo, theIndex );
01376   HypothesisData* algoByDim[4];
01377   algoByDim[ aDim ] = algoData;
01378 
01379   QStringList anAvailable;
01380   if ( !algoData ) { // all algos becomes available
01381     availableHyps( aDim, Algo, anAvailable, myAvailableHypData[ aDim ][ Algo ]);
01382     myDlg->tab( aDim )->setAvailableHyps( Algo, anAvailable );
01383   }
01384 
01385   // check that algorithms of other dimentions are compatible with
01386   // the selected one
01387 
01388    // 2 loops: backward and forward from algo dimension
01389   for ( int forward = false; forward <= true; ++forward )
01390   {
01391     int dim = aDim + 1, lastDim = SMESH::DIM_3D, dir = 1;
01392     if ( !forward ) {
01393       dim = aDim - 1; lastDim = SMESH::DIM_0D; dir = -1;
01394     }
01395     HypothesisData* prevAlgo = algoData;
01396     bool noCompatible = false;
01397     for ( ; dim * dir <= lastDim * dir; dim += dir)
01398     {
01399       if ( !isAccessibleDim( dim ))
01400         continue;
01401       if ( noCompatible ) { // the selected algo has no compatible ones
01402         anAvailable.clear();
01403         myDlg->tab( dim )->setAvailableHyps( Algo, anAvailable );
01404         myAvailableHypData[dim][Algo].clear();
01405         algoByDim[ dim ] = 0;
01406         continue;
01407       }
01408       // get currently selected algo
01409       int algoIndex = currentHyp( dim, Algo );
01410       HypothesisData* curAlgo = hypData( dim, Algo, algoIndex );
01411       if ( curAlgo ) { // some algo selected
01412         if ( !isCompatible( prevAlgo, curAlgo, Algo ))
01413           curAlgo = 0;
01414       }
01415       // set new available algoritms
01416       availableHyps( dim, Algo, anAvailable, myAvailableHypData[dim][Algo], prevAlgo );
01417       HypothesisData* soleCompatible = 0;
01418       if ( anAvailable.count() == 1 )
01419         soleCompatible = myAvailableHypData[dim][Algo][0];
01420       if ( dim == aTopDim && prevAlgo ) // all available algoritms should be selectable any way
01421         availableHyps( dim, Algo, anAvailable, myAvailableHypData[dim][Algo], 0 );
01422       myDlg->tab( dim )->setAvailableHyps( Algo, anAvailable );
01423       noCompatible = anAvailable.isEmpty();
01424 
01425       // restore previously selected algo
01426       algoIndex = myAvailableHypData[dim][Algo].indexOf( curAlgo );
01427       if ( !isSubmesh && algoIndex < 0 && soleCompatible && !forward && dim != SMESH::DIM_0D)
01428         // select the sole compatible algo
01429         algoIndex = myAvailableHypData[dim][Algo].indexOf( soleCompatible );
01430       setCurrentHyp( dim, Algo, algoIndex );
01431 
01432       // remember current algo
01433       prevAlgo = algoByDim[ dim ] = hypData( dim, Algo, algoIndex );
01434     }
01435   }
01436 
01437   // set hypotheses corresponding to the found algoritms
01438 
01439   _PTR(SObject) pObj = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
01440 
01441   for ( int dim = SMESH::DIM_0D; dim <= SMESH::DIM_3D; dim++ )
01442   {
01443     if ( !isAccessibleDim( dim ))
01444       continue;
01445     for ( int type = MainHyp; type < NbHypTypes; type++ )
01446     {
01447       myAvailableHypData[ dim ][ type ].clear();
01448       QStringList anAvailable, anExisting;
01449 
01450       HypothesisData* curAlgo = algoByDim[ dim ];
01451       int hypIndex = currentHyp( dim, type );
01452 
01453       SMESH::SMESH_Hypothesis_var curHyp;
01454       if ( hypIndex >= 0 && hypIndex < myExistingHyps[ dim ][ type ].count() )
01455         curHyp = myExistingHyps[ dim ][ type ][ hypIndex ].first;
01456 
01457       if ( !myToCreate && !curAlgo && !curHyp->_is_nil() ) { // edition, algo not selected
01458         // try to find algo by selected hypothesis in order to keep it selected
01459         bool algoDeselectedByUser = ( theDim < 0 && aDim == dim );
01460         CORBA::String_var curHypType = curHyp->GetName();
01461         if ( !algoDeselectedByUser &&
01462              myObjHyps[ dim ][ type ].count() > 0 &&
01463              !strcmp( curHypType, myObjHyps[ dim ][ type ].first().first->GetName()) )
01464         {
01465           HypothesisData* hypData = SMESH::GetHypothesisData( curHyp->GetName() );
01466           for (int i = 0; i < myAvailableHypData[ dim ][ Algo ].count(); ++i) {
01467             curAlgo = myAvailableHypData[ dim ][ Algo ][ i ];
01468             if (curAlgo && hypData && isCompatible(curAlgo, hypData, type))
01469               break;
01470             else
01471               curAlgo = 0;
01472           }
01473         }
01474       }
01475       // get hyps compatible with curAlgo
01476       if ( curAlgo )
01477       {
01478         // check if a selected hyp is compatible with the curAlgo
01479         if ( !curHyp->_is_nil() ) {
01480           HypothesisData* hypData = SMESH::GetHypothesisData( curHyp->GetName() );
01481           if ( !isCompatible( curAlgo, hypData, type ))
01482             curHyp = SMESH::SMESH_Hypothesis::_nil();
01483         }
01484         existingHyps( dim, type, pObj, anExisting, myExistingHyps[ dim ][ type ], curAlgo);
01485         availableHyps( dim, type, anAvailable, myAvailableHypData[ dim ][ type ], curAlgo);
01486       }
01487       // set list of hypotheses
01488       myDlg->tab( dim )->setAvailableHyps( type, anAvailable );
01489       myDlg->tab( dim )->setExistingHyps( type, anExisting );
01490 
01491       // set current existing hypothesis
01492       if ( !curHyp->_is_nil() && !anExisting.isEmpty() )
01493         hypIndex = this->find( curHyp, myExistingHyps[ dim ][ type ]);
01494       else
01495         hypIndex = -1;
01496       if ( !isSubmesh && hypIndex < 0 && anExisting.count() == 1 ) {
01497         // none is yet selected => select the sole existing if it is not optional
01498         CORBA::String_var hypTypeName = myExistingHyps[ dim ][ type ].first().first->GetName();
01499         bool isOptional = true;
01500         if ( algoByDim[ dim ] &&
01501              SMESH::IsAvailableHypothesis( algoByDim[ dim ], hypTypeName.in(), isOptional ) &&
01502              !isOptional )
01503           hypIndex = 0;
01504       }
01505       setCurrentHyp( dim, type, hypIndex );
01506     }
01507   }
01508 }
01509 
01510 //================================================================================
01515 //================================================================================
01516 void SMESHGUI_MeshOp::onHypoSet( const QString& theSetName )
01517 {
01518   myHypoSet = SMESH::GetHypothesesSet(theSetName);
01519   if (!myHypoSet)
01520     return;
01521 
01522   // clear all hyps
01523   for (int dim = SMESH::DIM_0D; dim <= SMESH::DIM_3D; dim++) {
01524     setCurrentHyp(dim, Algo, -1);
01525     setCurrentHyp(dim, AddHyp, -1);
01526     setCurrentHyp(dim, MainHyp, -1);
01527   }
01528 
01529   myHypoSet->init(true); //algorithms
01530   processSet();
01531   myHypoSet->init(false); //hypotheses
01532   processSet();
01533   myHypoSet = 0;
01534 }
01535 
01536 //================================================================================
01542 //================================================================================
01543 void SMESHGUI_MeshOp::processSet()
01544 {
01545   myHypoSet->next();
01546   if( !myHypoSet->more() )
01547     return;
01548 
01549   bool isAlgo = myHypoSet->isAlgo();
01550   QString aHypoTypeName = myHypoSet->current();
01551   HypothesisData* aHypData = SMESH::GetHypothesisData(aHypoTypeName);
01552   if (!aHypData)
01553   {
01554     processSet();
01555     return;
01556   }
01557 
01558   int aDim = aHypData->Dim[0];
01559   // create or/and set
01560   if (isAlgo)
01561   {
01562     int index = myAvailableHypData[aDim][Algo].indexOf( aHypData );
01563     if ( index < 0 )
01564     {
01565       QStringList anAvailable;
01566       availableHyps( aDim, Algo, anAvailable, myAvailableHypData[aDim][Algo] );
01567       myDlg->tab( aDim )->setAvailableHyps( Algo, anAvailable );
01568       index = myAvailableHypData[aDim][Algo].indexOf( aHypData );
01569     }
01570     setCurrentHyp( aDim, Algo, index );
01571     onAlgoSelected( index, aDim );
01572     processSet();
01573   }
01574   else
01575   {
01576     bool mainHyp = true;
01577     QStringList anAvailable;
01578     availableHyps( aDim, MainHyp, anAvailable, myAvailableHypData[aDim][MainHyp] );
01579     myDlg->tab( aDim )->setAvailableHyps( MainHyp, anAvailable );
01580     int index = myAvailableHypData[aDim][MainHyp].indexOf( aHypData );
01581     if ( index < 0 )
01582     {
01583       mainHyp = false;
01584       index = myAvailableHypData[aDim][AddHyp].indexOf( aHypData );
01585     }
01586     if (index >= 0)
01587       createHypothesis(aDim, mainHyp ? MainHyp : AddHyp, aHypoTypeName);
01588     else
01589       processSet();
01590   }
01591 }
01592 
01593 //================================================================================
01602 //================================================================================
01603 bool SMESHGUI_MeshOp::createMesh( QString& theMess, QStringList& theEntryList )
01604 {
01605   theMess = "";
01606 
01607   //QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
01608   //QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
01609 
01610   QStringList aList;
01611   myDlg->selectedObject( SMESHGUI_MeshDlg::Geom, aList );
01612   QStringList::Iterator it = aList.begin();
01613   for ( ; it!=aList.end(); it++)
01614   {
01615     QString aGeomEntry = *it;
01616     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() );
01617     GEOM::GEOM_Object_var aGeomVar =
01618       GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
01619 
01620     SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
01621     if ( aSMESHGen->_is_nil() )
01622       return false;
01623 
01624     SUIT_OverrideCursor aWaitCursor;
01625 
01626     // create mesh
01627     SMESH::SMESH_Mesh_var aMeshVar = aSMESHGen->CreateMesh( aGeomVar );
01628     if ( aMeshVar->_is_nil() )
01629       return false;
01630     _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar.in() );
01631     if ( aMeshSO ) {
01632       SMESH::SetName( aMeshSO, myDlg->objectText( SMESHGUI_MeshDlg::Obj ) );
01633       theEntryList.append( aMeshSO->GetID().c_str() );
01634     }
01635 
01636     for ( int aDim = SMESH::DIM_0D; aDim <= SMESH::DIM_3D; aDim++ ) {
01637       if ( !isAccessibleDim( aDim )) continue;
01638 
01639       // assign hypotheses
01640       for ( int aHypType = MainHyp; aHypType <= AddHyp; aHypType++ ) {
01641         int aHypIndex = currentHyp( aDim, aHypType );
01642         if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() ) {
01643           SMESH::SMESH_Hypothesis_var aHypVar = myExistingHyps[ aDim ][ aHypType ][ aHypIndex ].first;
01644           if ( !aHypVar->_is_nil() )
01645             SMESH::AddHypothesisOnMesh( aMeshVar, aHypVar );
01646         }
01647       }
01648       // find or create algorithm
01649       SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
01650       if ( !anAlgoVar->_is_nil() )
01651         SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
01652     }
01653 #ifdef WITHGENERICOBJ
01654     // obj has been published in study. Its refcount has been incremented.
01655     // It is safe to decrement its refcount
01656     // so that it will be destroyed when the entry in study will be removed
01657     if (aMeshSO)
01658       aMeshVar->UnRegister();
01659 #endif
01660   }
01661   return true;
01662 }
01663 
01664 //================================================================================
01673 //================================================================================
01674 bool SMESHGUI_MeshOp::createSubMesh( QString& theMess, QStringList& theEntryList )
01675 {
01676   theMess = "";
01677 
01678   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
01679   if ( aSMESHGen->_is_nil() )
01680     return false;
01681 
01682   // get mesh object
01683   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
01684   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
01685   SMESH::SMESH_Mesh_var aMeshVar =
01686     SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
01687   if (aMeshVar->_is_nil())
01688     return false;
01689 
01690   // GEOM shape of the main mesh
01691   GEOM::GEOM_Object_var mainGeom = aMeshVar->GetShapeToMesh();
01692 
01693   // Name for the new sub-mesh
01694   QString aName = myDlg->objectText(SMESHGUI_MeshDlg::Obj);
01695 
01696   // get geom object
01697   GEOM::GEOM_Object_var aGeomVar;
01698   QStringList aGEOMs;
01699   myDlg->selectedObject(SMESHGUI_MeshDlg::Geom, aGEOMs);
01700   if (aGEOMs.count() == 1)
01701   {
01702     //QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
01703     QString aGeomEntry = aGEOMs.first();
01704     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() );
01705     aGeomVar = GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
01706   }
01707   else if (aGEOMs.count() > 1)
01708   {
01709     // create a GEOM group
01710     GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
01711     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
01712     if (!geomGen->_is_nil() && aStudy) {
01713       GEOM::GEOM_IGroupOperations_var op =
01714         geomGen->GetIGroupOperations(aStudy->StudyId());
01715       if (!op->_is_nil()) {
01716         // check and add all selected GEOM objects: they must be
01717         // a sub-shapes of the main GEOM and must be of one type
01718         int iSubSh = 0;
01719         TopAbs_ShapeEnum aGroupType = TopAbs_SHAPE;
01720         GEOM::ListOfGO_var aSeq = new GEOM::ListOfGO;
01721         aSeq->length(aGEOMs.count());
01722         QStringList::const_iterator aSubShapesIter = aGEOMs.begin();
01723         for ( ; aSubShapesIter != aGEOMs.end(); aSubShapesIter++, iSubSh++) {
01724           QString aSubGeomEntry = (*aSubShapesIter);
01725           _PTR(SObject) pSubGeom = studyDS()->FindObjectID(aSubGeomEntry.toLatin1().data());
01726           GEOM::GEOM_Object_var aSubGeomVar =
01727             GEOM::GEOM_Object::_narrow(_CAST(SObject,pSubGeom)->GetObject());
01728           TopAbs_ShapeEnum aSubShapeType = (TopAbs_ShapeEnum)aSubGeomVar->GetShapeType();
01729           if (iSubSh == 0) {
01730             aGroupType = aSubShapeType;
01731           } else {
01732             if (aSubShapeType != aGroupType)
01733               aGroupType = TopAbs_SHAPE;
01734           }
01735           aSeq[iSubSh] = aSubGeomVar;
01736         }
01737         // create a group
01738         GEOM::GEOM_Object_var aGroupVar = op->CreateGroup(mainGeom, aGroupType);
01739         op->UnionList(aGroupVar, aSeq);
01740 
01741         if (op->IsDone()) {
01742           aGeomVar = aGroupVar;
01743 
01744           // publish the GEOM group in study
01745           QString aNewGeomGroupName ("Auto_group_for_");
01746           aNewGeomGroupName += aName;
01747           SALOMEDS::SObject_var aNewGroupSO =
01748             geomGen->AddInStudy(aSMESHGen->GetCurrentStudy(), aGeomVar,
01749                                 aNewGeomGroupName.toLatin1().data(), mainGeom);
01750         }
01751       }
01752     }
01753   }
01754   else {
01755   }
01756   if (aGeomVar->_is_nil())
01757     return false;
01758 
01759   SUIT_OverrideCursor aWaitCursor;
01760 
01761   // create sub-mesh
01762   SMESH::SMESH_subMesh_var aSubMeshVar = aMeshVar->GetSubMesh( aGeomVar, aName.toLatin1().data() );
01763   _PTR(SObject) aSubMeshSO = SMESH::FindSObject( aSubMeshVar.in() );
01764   if ( aSubMeshSO ) {
01765     SMESH::SetName( aSubMeshSO, aName.toLatin1().data() );
01766     theEntryList.append( aSubMeshSO->GetID().c_str() );
01767   }
01768 
01769   for ( int aDim = SMESH::DIM_0D; aDim <= SMESH::DIM_3D; aDim++ )
01770   {
01771     if ( !isAccessibleDim( aDim )) continue;
01772 
01773     // find or create algorithm
01774     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
01775     if ( !anAlgoVar->_is_nil() )
01776       SMESH::AddHypothesisOnSubMesh( aSubMeshVar, anAlgoVar );
01777     // assign hypotheses
01778     for ( int aHypType = MainHyp; aHypType <= AddHyp; aHypType++ )
01779     {
01780       int aHypIndex = currentHyp( aDim, aHypType );
01781       if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() )
01782       {
01783         SMESH::SMESH_Hypothesis_var aHypVar =
01784           myExistingHyps[ aDim ][ aHypType ][ aHypIndex ].first;
01785         if ( !aHypVar->_is_nil() )
01786           SMESH::AddHypothesisOnSubMesh( aSubMeshVar, aHypVar );
01787       }
01788     }
01789   }
01790 
01791   // deselect geometry: next submesh should be created on other sub-shape
01792   myDlg->clearSelection( SMESHGUI_MeshDlg::Geom );
01793   selectObject( _PTR(SObject)() );
01794   selectionDone();
01795 
01796   return true;
01797 }
01798 
01799 //================================================================================
01808 //================================================================================
01809 int SMESHGUI_MeshOp::currentHyp( const int theDim, const int theHypType ) const
01810 {
01811   return myDlg->tab( theDim )->currentHyp( theHypType ) - 1;
01812 }
01813 
01814 //================================================================================
01820 //================================================================================
01821 bool SMESHGUI_MeshOp::isAccessibleDim( const int theDim ) const
01822 {
01823   return myDlg->isTabEnabled( theDim );
01824 }
01825 
01826 //================================================================================
01835 //================================================================================
01836 void SMESHGUI_MeshOp::setCurrentHyp( const int theDim,
01837                                      const int theHypType,
01838                                      const int theIndex )
01839 {
01840   myIgnoreAlgoSelection = true;
01841   myDlg->tab( theDim )->setCurrentHyp( theHypType, theIndex + 1 );
01842   myIgnoreAlgoSelection = false;
01843 }
01844 
01845 //================================================================================
01851 //================================================================================
01852 void SMESHGUI_MeshOp::setDefaultName() const
01853 {
01854   QString aResName;
01855 
01856   _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
01857   int i = 1;
01858   QString aPrefix = tr( myIsMesh ? "SMESH_OBJECT_MESH" : "SMESH_SUBMESH" ) + "_";
01859   _PTR(SObject) anObj;
01860   do
01861   {
01862     aResName = aPrefix + QString::number( i++ );
01863     anObj = aStudy->FindObject( aResName.toLatin1().data() );
01864   }
01865   while ( anObj );
01866 
01867   QLineEdit* aControl = ( QLineEdit* )myDlg->objectWg(
01868     SMESHGUI_MeshDlg::Obj, SMESHGUI_MeshDlg::Control );
01869   aControl->setText( aResName );
01870 }
01871 
01872 //================================================================================
01880 //================================================================================
01881 SMESH::SMESH_Hypothesis_var SMESHGUI_MeshOp::getAlgo( const int theDim )
01882 {
01883   SMESH::SMESH_Hypothesis_var anAlgoVar;
01884 
01885   // get type of the selected algo
01886   int aHypIndex = currentHyp( theDim, Algo );
01887   THypDataList& dataList = myAvailableHypData[ theDim ][ Algo ];
01888   if ( aHypIndex < 0 || aHypIndex >= dataList.count())
01889     return anAlgoVar;
01890   QString aHypName = dataList[ aHypIndex ]->TypeName;
01891 
01892   // get existing algoritms
01893   _PTR(SObject) pObj = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
01894   QStringList tmp;
01895   existingHyps( theDim, Algo, pObj, tmp, myExistingHyps[ theDim ][ Algo ]);
01896 
01897   // look for an existing algo of such a type
01898   THypList& aHypVarList = myExistingHyps[ theDim ][ Algo ];
01899   THypList::iterator anIter = aHypVarList.begin();
01900   for ( ; anIter != aHypVarList.end(); anIter++)
01901   {
01902     SMESH::SMESH_Hypothesis_var aHypVar = (*anIter).first;
01903     CORBA::String_var aName = aHypVar->GetName();
01904     if ( !aHypVar->_is_nil() && !strcmp(aHypName.toLatin1().data(), aName) )
01905     {
01906       anAlgoVar = aHypVar;
01907       break;
01908     }
01909   }
01910 
01911   if (anAlgoVar->_is_nil())
01912   {
01913     HypothesisData* aHypData = SMESH::GetHypothesisData( aHypName );
01914     if (aHypData)
01915     {
01916       QString aClientLibName = aHypData->ClientLibName;
01917       if (aClientLibName == "")
01918       {
01919         // Call hypothesis creation server method (without GUI)
01920         SMESH::SMESH_Hypothesis_var aHyp =
01921           SMESH::CreateHypothesis(aHypName, aHypName, true);
01922 #ifdef WITHGENERICOBJ
01923         if (!CORBA::is_nil(aHyp))
01924           aHyp->UnRegister();
01925 #endif
01926       }
01927       else
01928       {
01929         // Get hypotheses creator client (GUI)
01930         // BUG 0020378
01931         SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(aHypName);
01932 
01933         // Create algorithm
01934         if (aCreator)
01935           aCreator->create(true, aHypName, myDlg, 0, QString::null );
01936         else {
01937           SMESH::SMESH_Hypothesis_var aHyp =
01938             SMESH::CreateHypothesis(aHypName, aHypName, true);
01939 #ifdef WITHGENERICOBJ
01940           if (!CORBA::is_nil(aHyp))
01941             aHyp->UnRegister();
01942 #endif
01943         }
01944       }
01945       QStringList tmpList;
01946       _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
01947       existingHyps( theDim, Algo, aFather, tmpList, myExistingHyps[ theDim ][ Algo ] );
01948     }
01949 
01950     THypList& aNewHypVarList = myExistingHyps[ theDim ][ Algo ];
01951     for ( anIter = aNewHypVarList.begin(); anIter != aNewHypVarList.end(); ++anIter )
01952     {
01953       SMESH::SMESH_Hypothesis_var aHypVar = (*anIter).first;
01954       CORBA::String_var aName = aHypVar->GetName();
01955       if ( !aHypVar->_is_nil() && !strcmp(aHypName.toLatin1().data(), aName) )
01956       {
01957         anAlgoVar = aHypVar;
01958         break;
01959       }
01960     }
01961   }
01962 
01963   return anAlgoVar._retn();
01964 }
01965 
01966 //================================================================================
01973 //================================================================================
01974 void SMESHGUI_MeshOp::readMesh()
01975 {
01976   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
01977   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
01978   if ( !pObj )
01979     return;
01980 
01981   if (myIsOnGeometry) {
01982     // Get name of mesh if current object is sub-mesh
01983     SMESH::SMESH_subMesh_var aSubMeshVar =
01984       SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
01985     if ( !aSubMeshVar->_is_nil() )
01986     {
01987       SMESH::SMESH_Mesh_var aMeshVar =  aSubMeshVar->GetFather();
01988       if ( !aMeshVar->_is_nil() )
01989       {
01990         _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar );
01991         QString aMeshName = name( aMeshSO );
01992         myDlg->setObjectText( SMESHGUI_MeshDlg::Mesh, aMeshName );
01993       }
01994     }
01995 
01996     // Get name of geometry object
01997     GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
01998     if ( !aGeomVar->_is_nil() )
01999     {
02000       _PTR(SObject) aGeomSO = studyDS()->FindObjectID( aGeomVar->GetStudyEntry() );
02001       QString aShapeName = name( aGeomSO );
02002       myDlg->setObjectText( SMESHGUI_MeshDlg::Geom, aShapeName );
02003     }
02004   }
02005 
02006   // Get hypotheses and algorithms assigned to the mesh/sub-mesh
02007   QStringList anExisting;
02008   const int aDim = ( myIsOnGeometry ) ? SMESH::DIM_0D : SMESH::DIM_3D;
02009   for ( int dim = aDim; dim <= SMESH::DIM_3D; dim++ )
02010   {
02011     // get algorithm
02012     existingHyps( dim, Algo, pObj, anExisting, myObjHyps[ dim ][ Algo ] );
02013     // find algo index among available ones
02014     int aHypIndex = -1;
02015     if ( myObjHyps[ dim ][ Algo ].count() > 0 )
02016     {
02017       SMESH::SMESH_Hypothesis_var aVar = myObjHyps[ dim ][ Algo ].first().first;
02018       HypothesisData* algoData = SMESH::GetHypothesisData( aVar->GetName() );
02019       aHypIndex = myAvailableHypData[ dim ][ Algo ].indexOf ( algoData );
02020 //       if ( aHypIndex < 0 && algoData ) {
02021 //         // assigned algo is incompatible with other algorithms
02022 //         myAvailableHypData[ dim ][ Algo ].push_back( algoData );
02023 //         aHypIndex = myAvailableHypData[ dim ][ hypType ].count() - 1;
02024 //       }
02025     }
02026     setCurrentHyp( dim, Algo, aHypIndex );
02027     // set existing and available hypothesis according to the selected algo
02028     onAlgoSelected( aHypIndex, dim );
02029   }
02030 
02031   // get hypotheses
02032   bool hypWithoutAlgo = false;
02033   for ( int dim = aDim; dim <= SMESH::DIM_3D; dim++ )
02034   {
02035     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
02036     {
02037       // get hypotheses
02038       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
02039       // find index of requered hypothesis among existing ones for this dimension and type
02040       int aHypIndex = -1;
02041       if ( myObjHyps[ dim ][ hypType ].count() > 0 ) {
02042         aHypIndex = find( myObjHyps[ dim ][ hypType ].first().first,
02043                           myExistingHyps[ dim ][ hypType ] );
02044         if ( aHypIndex < 0 ) {
02045           // assigned hypothesis is incompatible with the algorithm
02046           if ( currentHyp( dim, Algo ) < 0 )
02047           { // none algo selected; it is edition for sure, of submesh maybe
02048             hypWithoutAlgo = true;
02049             myExistingHyps[ dim ][ hypType ].push_back( myObjHyps[ dim ][ hypType ].first() );
02050             aHypIndex = myExistingHyps[ dim ][ hypType ].count() - 1;
02051             myDlg->tab( dim )->setExistingHyps( hypType, anExisting );
02052           }
02053         }
02054       }
02055       setCurrentHyp( dim, hypType, aHypIndex );
02056     }
02057   }
02058   // make available other hyps of same type as one without algo
02059   if ( hypWithoutAlgo )
02060     onAlgoSelected( currentHyp( 0, Algo ), 0 );
02061 }
02062 
02063 //================================================================================
02071 //================================================================================
02072 QString SMESHGUI_MeshOp::name( _PTR(SObject) theSO ) const
02073 {
02074   QString aResName;
02075   if ( theSO )
02076   {
02077     _PTR(GenericAttribute) anAttr;
02078     _PTR(AttributeName)    aNameAttr;
02079     if ( theSO->FindAttribute( anAttr, "AttributeName" ) )
02080     {
02081       aNameAttr = anAttr;
02082       aResName = aNameAttr->Value().c_str();
02083     }
02084   }
02085   return aResName;
02086 }
02087 
02088 //================================================================================
02097 //================================================================================
02098 int SMESHGUI_MeshOp::find( const SMESH::SMESH_Hypothesis_var& theHyp,
02099                            const THypList& theHypList ) const
02100 {
02101   int aRes = -1;
02102   if ( !theHyp->_is_nil() )
02103   {
02104     int i = 0;
02105     THypList::const_iterator anIter = theHypList.begin();
02106     for ( ; anIter != theHypList.end(); ++ anIter)
02107     {
02108       if ( theHyp->_is_equivalent( (*anIter).first ) )
02109       {
02110         aRes = i;
02111         break;
02112       }
02113       i++;
02114     }
02115   }
02116   return aRes;
02117 }
02118 
02119 //================================================================================
02127 //================================================================================
02128 bool SMESHGUI_MeshOp::editMeshOrSubMesh( QString& theMess )
02129 {
02130   theMess = "";
02131 
02132   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
02133   if ( aSMESHGen->_is_nil() )
02134     return false;
02135 
02136   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
02137   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
02138   if ( !pObj )
02139     return false;
02140 
02141   SUIT_OverrideCursor aWaitCursor;
02142 
02143   // Set new name
02144   QString aName = myDlg->objectText( SMESHGUI_MeshDlg::Obj );
02145   SMESH::SetName( pObj, aName );
02146   int aDim = ( myIsOnGeometry ) ? SMESH::DIM_0D : SMESH::DIM_3D;
02147 
02148   // First, remove old algos in order to avoid messages on algorithm hiding
02149   for ( int dim = aDim; dim <= SMESH::DIM_3D; dim++ )
02150   {
02151     if ( isAccessibleDim( dim ) && myObjHyps[ dim ][ Algo ].count() > 0 )
02152     {
02153       SMESH::SMESH_Hypothesis_var anOldAlgo = myObjHyps[ dim ][ Algo ].first().first;
02154       SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( dim );
02155       if ( anAlgoVar->_is_nil() || // no new algo selected or
02156            strcmp(anOldAlgo->GetName(), anAlgoVar->GetName()) ) // algo change
02157       {
02158         // remove old algorithm
02159         SMESH::RemoveHypothesisOrAlgorithmOnMesh ( pObj, myObjHyps[ dim ][ Algo ].first().first );
02160         myObjHyps[ dim ][ Algo ].clear();
02161       }
02162     }
02163   }
02164 
02165   // Assign new algorithms and hypotheses
02166   for ( int dim = aDim; dim <= SMESH::DIM_3D; dim++ )
02167   {
02168     if ( !isAccessibleDim( dim )) continue;
02169 
02170     // find or create algorithm
02171     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( dim );
02172 
02173     // assign new algorithm
02174     if ( !anAlgoVar->_is_nil() && // some algo selected and
02175          myObjHyps[ dim ][ Algo ].count() == 0 ) // no algo assigned
02176     {
02177       SALOMEDS_SObject* aSObject = _CAST(SObject, pObj);
02178       CORBA::Object_var anObject = aSObject->GetObject();
02179       SMESH::SMESH_Mesh_var aMeshVar = SMESH::SMESH_Mesh::_narrow( anObject );
02180       bool isMesh = !aMeshVar->_is_nil();
02181       if ( isMesh ) {
02182         SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
02183       } else {
02184         SMESH::SMESH_subMesh_var aVar =
02185           SMESH::SMESH_subMesh::_narrow( _CAST(SObject,pObj)->GetObject() );
02186         if ( !aVar->_is_nil() )
02187           SMESH::AddHypothesisOnSubMesh( aVar, anAlgoVar );
02188       }
02189       myObjHyps[ dim ][ Algo ].append( THypItem( anAlgoVar, aName) );
02190     }
02191 
02192     // assign hypotheses
02193     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
02194     {
02195       int aNewHypIndex = currentHyp( dim, hypType );
02196       int anOldHypIndex = -1;
02197 
02198       // remove old hypotheses
02199       if ( myObjHyps[ dim ][ hypType ].count() > 0 )
02200       {
02201         anOldHypIndex = find( myObjHyps[ dim ][ hypType ].first().first,
02202                               myExistingHyps[ dim ][ hypType ] );
02203         if ( aNewHypIndex != anOldHypIndex || // different hyps
02204              anOldHypIndex == -1 )            // hyps of different algos
02205         {
02206           SMESH::RemoveHypothesisOrAlgorithmOnMesh
02207             ( pObj, myObjHyps[ dim ][ hypType ].first().first );
02208           myObjHyps[ dim ][ hypType ].clear();
02209         }
02210       }
02211 
02212       // assign new hypotheses
02213       if ( aNewHypIndex != anOldHypIndex && aNewHypIndex != -1 )
02214       {
02215         SMESH::SMESH_Mesh_var aMeshVar =
02216           SMESH::SMESH_Mesh::_narrow( _CAST(SObject,pObj)->GetObject() );
02217         bool isMesh = !aMeshVar->_is_nil();
02218         if ( isMesh )
02219         {
02220           SMESH::AddHypothesisOnMesh
02221             (aMeshVar, myExistingHyps[ dim ][ hypType ][ aNewHypIndex ].first );
02222         }
02223         else
02224         {
02225           SMESH::SMESH_subMesh_var aVar =
02226             SMESH::SMESH_subMesh::_narrow( _CAST(SObject,pObj)->GetObject() );
02227           if ( !aVar->_is_nil() )
02228             SMESH::AddHypothesisOnSubMesh
02229               ( aVar, myExistingHyps[ dim ][ hypType ][ aNewHypIndex ].first );
02230         }
02231       }
02232       // reread all hypotheses of mesh if necessary
02233       QStringList anExisting;
02234       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
02235     }
02236   }
02237 
02238   return true;
02239 }
02240 
02241 //================================================================================
02252 //================================================================================
02253 bool SMESHGUI_MeshOp::isValid( SUIT_Operation* theOp ) const
02254 {
02255   return SMESHGUI_Operation::isValid( theOp ) && !theOp->inherits( "SMESHGUI_MeshOp" );
02256 }
02257 
02258 //================================================================================
02263 //================================================================================
02264 void SMESHGUI_MeshOp::onGeomSelectionByMesh( bool theByMesh )
02265 {
02266   if ( theByMesh ) {
02267     if ( !myShapeByMeshOp ) {
02268       myShapeByMeshOp = new SMESHGUI_ShapeByMeshOp();
02269       connect(myShapeByMeshOp, SIGNAL(committed(SUIT_Operation*)),
02270               SLOT(onPublishShapeByMeshDlg(SUIT_Operation*)));
02271       connect(myShapeByMeshOp, SIGNAL(aborted(SUIT_Operation*)),
02272               SLOT(onCloseShapeByMeshDlg(SUIT_Operation*)));
02273     }
02274     // set mesh object to SMESHGUI_ShapeByMeshOp and start it
02275     QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
02276     if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() )) {
02277       SMESH::SMESH_Mesh_var aMeshVar =
02278         SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
02279       if ( !aMeshVar->_is_nil() ) {
02280         myDlg->hide(); // stop processing selection
02281         myShapeByMeshOp->setModule( getSMESHGUI() );
02282         myShapeByMeshOp->setStudy( 0 ); // it's really necessary
02283         myShapeByMeshOp->SetMesh( aMeshVar );
02284         myShapeByMeshOp->start();
02285       }
02286     }
02287   }
02288 }
02289 
02290 //================================================================================
02294 //================================================================================
02295 void SMESHGUI_MeshOp::onPublishShapeByMeshDlg(SUIT_Operation* op)
02296 {
02297   if ( myShapeByMeshOp == op ) {
02298     SMESHGUI::GetSMESHGUI()->getApp()->updateObjectBrowser(); //MZN: 24.11.2006  IPAL13980 - Object Browser update added
02299     myDlg->show();
02300     // Select a found geometry object
02301     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshOp->GetShape();
02302     if ( !aGeomVar->_is_nil() )
02303     {
02304       QString ID = aGeomVar->GetStudyEntry();
02305       if ( _PTR(SObject) aGeomSO = studyDS()->FindObjectID( ID.toLatin1().data() )) {
02306         selectObject( aGeomSO );
02307         selectionDone();
02308       }
02309     }
02310   }
02311 }
02312 
02313 //================================================================================
02317 //================================================================================
02318 void SMESHGUI_MeshOp::onCloseShapeByMeshDlg(SUIT_Operation* op)
02319 {
02320   if ( myShapeByMeshOp == op && myDlg ) {
02321     myDlg->show();
02322   }
02323 }
02324 
02325 //================================================================================
02330 //================================================================================
02331 void SMESHGUI_MeshOp::selectObject( _PTR(SObject) theSObj ) const
02332 {
02333   if ( LightApp_SelectionMgr* sm = selectionMgr() ) {
02334     SALOME_ListIO anIOList;
02335     if ( theSObj ) {
02336       Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
02337         ( theSObj->GetID().c_str(), "SMESH", theSObj->GetName().c_str() );
02338       anIOList.Append( anIO );
02339     }
02340     sm->setSelectedObjects( anIOList, false );
02341   }
02342 }