Version: 6.3.1

src/SMESHGUI/SMESHGUI_MeshOp.cxx

Go to the documentation of this file.
00001 // Copyright (C) 2007-2011  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             {
00541               //shapeDim = 3; // Bug 0016155: EDF PAL 447: If the shape is a Shell, disable 3D tab
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                 TopExp_Explorer exp (aShape, TopAbs_SHELL);
00556                 if (exp.More()) {
00557                   //shapeDim = 3; // Bug 0016155: EDF PAL 447: If the shape is a Shell, disable 3D tab
00558                   shapeDim = qMax(2, shapeDim);
00559                   for (; exp.More() && shapeDim == 2; exp.Next()) {
00560                     if (/*exp.Current().Closed()*/BRep_Tool::IsClosed(exp.Current()))
00561                       shapeDim = 3;
00562                   }
00563                 }
00564                 else if ( exp.Init( aShape, TopAbs_FACE ), exp.More() )
00565                   shapeDim = qMax(2, shapeDim);
00566                 else if ( exp.Init( aShape, TopAbs_EDGE ), exp.More() )
00567                   shapeDim = qMax(1, shapeDim);
00568                 else if ( exp.Init( aShape, TopAbs_VERTEX ), exp.More() )
00569                   shapeDim = qMax(0, shapeDim);
00570               }
00571             }
00572           }
00573         }
00574       }
00575       for (int i = SMESH::DIM_3D; i > shapeDim; i--) {
00576         // reset algos before disabling tabs (0020138)
00577         onAlgoSelected(-1, i);
00578       }
00579       myDlg->setMaxHypoDim( shapeDim );
00580       myDlg->setHypoSets( SMESH::GetHypothesesSets( shapeDim ));
00581 
00582       if (!myToCreate) // edition: read hypotheses
00583       {
00584         if (pObj != 0)
00585         {
00586           SMESH::SMESH_subMesh_var aVar =
00587             SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
00588           myDlg->setObjectShown( SMESHGUI_MeshDlg::Mesh, !aVar->_is_nil() );
00589           myDlg->setObjectShown( SMESHGUI_MeshDlg::Geom, true );
00590           myDlg->objectWg( SMESHGUI_MeshDlg::Mesh, SMESHGUI_MeshDlg::Btn )->hide();
00591           myDlg->objectWg( SMESHGUI_MeshDlg::Geom, SMESHGUI_MeshDlg::Btn )->hide();
00592           myDlg->updateGeometry();
00593           myDlg->adjustSize();
00594           readMesh();
00595         }
00596         else
00597           myDlg->reset();
00598       }
00599       else if ( !myIsMesh ) // submesh creation
00600       {
00601         // if a submesh on the selected shape already exist, pass to submesh edition mode
00602         if ( _PTR(SObject) pSubmesh = getSubmeshByGeom() ) {
00603           SMESH::SMESH_subMesh_var sm =
00604             SMESH::SObjectToInterface<SMESH::SMESH_subMesh>( pSubmesh );
00605           bool editSubmesh = ( !sm->_is_nil() &&
00606                                SUIT_MessageBox::question( myDlg, tr( "SMESH_WARNING" ),
00607                                                           tr( "EDIT_SUBMESH_QUESTION"),
00608                                                           SUIT_MessageBox::Yes |
00609                                                           SUIT_MessageBox::No,
00610                                                           SUIT_MessageBox::No )
00611                                == SUIT_MessageBox::Yes );
00612           if ( editSubmesh )
00613           {
00614             selectionMgr()->clearFilters();
00615             selectObject( pSubmesh );
00616             SMESHGUI::GetSMESHGUI()->switchToOperation(704);
00617             return;
00618           }
00619           else
00620           {
00621             myDlg->selectObject( "", SMESHGUI_MeshDlg::Geom, "" );
00622             selectObject( _PTR(SObject)() );
00623             selectionDone();
00624             return;
00625           }
00626         }
00627         // discard selected mesh if submesh creation not allowed because of
00628         // a global algorithm that does not support submeshes
00629         if ( char* algoName = isSubmeshIgnored() ) {
00630           SUIT_MessageBox::warning( myDlg, tr( "SMESH_ERROR" ),
00631                                     tr("SUBMESH_NOT_ALLOWED").arg(algoName));
00632           CORBA::string_free( algoName );
00633           myDlg->selectObject( "", SMESHGUI_MeshDlg::Mesh, "" );
00634           selectObject( _PTR(SObject)() );
00635           selectionDone();
00636           return;
00637         }
00638 
00639         // enable/disable popup for choice of geom selection way
00640         bool enable = false;
00641         QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
00642         if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() )) {
00643           SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
00644           if ( !mesh->_is_nil() ) {
00645             //rnv: issue 21056: EDF 1608 SMESH: Dialog Box "Create Sub Mesh": focus should automatically switch to geometry
00646             QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
00647             _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() );
00648             if ( !pGeom || GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() )->_is_nil() )
00649               myDlg->activateObject(SMESHGUI_MeshDlg::Geom);
00650             enable = ( shapeDim > 1 ) && ( mesh->NbEdges() > 0 );
00651           }
00652         }
00653         myDlg->setGeomPopupEnabled( enable );
00654       }
00655     }
00656     else { // no geometry defined
00657       myDlg->enableTab( SMESH::DIM_3D );
00658       QStringList hypList;
00659       availableHyps( SMESH::DIM_3D, Algo, hypList,
00660                      myAvailableHypData[SMESH::DIM_3D][Algo]);
00661 
00662       SMESHGUI_MeshTab* aTab = myDlg->tab( SMESH::DIM_3D );
00663       aTab->setAvailableHyps( Algo, hypList );
00664       for (int i = SMESH::DIM_0D;i < SMESH::DIM_3D; ++i) {
00665         myDlg->disableTab(i);
00666       }
00667       //Hide labels and fields (Mesh ang Geometry)
00668       myDlg->setObjectShown( SMESHGUI_MeshDlg::Mesh, false );
00669       myDlg->setObjectShown( SMESHGUI_MeshDlg::Geom, false );
00670       myDlg->adjustSize();
00671       readMesh();
00672     }
00673   }
00674   catch ( const SALOME::SALOME_Exception& S_ex )
00675   {
00676     SalomeApp_Tools::QtCatchCorbaException( S_ex );
00677   }
00678   catch ( ... )
00679   {
00680   }
00681 }
00682 
00683 //================================================================================
00692 //================================================================================
00693 bool SMESHGUI_MeshOp::isValid( QString& theMess ) const
00694 {
00695   // Selected object to be  edited
00696   if ( !myToCreate && myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ) == "" )
00697   {
00698     theMess = tr( "THERE_IS_NO_OBJECT_FOR_EDITING" );
00699     return false;
00700   }
00701 
00702   // Name
00703   QString aMeshName = myDlg->objectText( SMESHGUI_MeshDlg::Obj ).trimmed();
00704   if ( aMeshName.isEmpty() )
00705   {
00706     theMess = myIsMesh ? tr( "NAME_OF_MESH_IS_EMPTY" ) : tr( "NAME_OF_SUBMESH_IS_EMPTY" );
00707     return false;
00708   }
00709 
00710 /*  // Imported mesh, if create sub-mesh or edit mesh
00711   if ( !myToCreate || ( myToCreate && !myIsMesh ))
00712   {
00713     QString aMeshEntry = myDlg->selectedObject
00714       ( myToCreate ? SMESHGUI_MeshDlg::Mesh : SMESHGUI_MeshDlg::Obj );
00715     if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() )) {
00716       SMESH::SMESH_Mesh_var mesh = SMESH::SObjectToInterface<SMESH::SMESH_Mesh>( pMesh );
00717       if ( !mesh->_is_nil() && CORBA::is_nil( mesh->GetShapeToMesh() )) {
00718         theMess = tr( "IMPORTED_MESH" );
00719         return false;
00720       }
00721     }
00722   }*/
00723 
00724   // Geom
00725   if ( myToCreate )
00726   {
00727     QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
00728     if ( aGeomEntry == "" )
00729     {
00730       theMess = tr( "GEOMETRY_OBJECT_IS_NOT_DEFINED" );
00731       return false;
00732     }
00733     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() );
00734     if ( !pGeom || GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() )->_is_nil() )
00735     {
00736       theMess = tr( "GEOMETRY_OBJECT_IS_NULL" );
00737       return false;
00738     }
00739 
00740     // Mesh
00741     if ( !myIsMesh ) // i.e sub-mesh creation,
00742     {
00743       QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
00744       if ( aMeshEntry == "" )
00745       {
00746         theMess = tr( "MESH_IS_NOT_DEFINED" );
00747         return false;
00748       }
00749       _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
00750       if ( !pMesh || SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() )->_is_nil() )
00751       {
00752         theMess = tr( "MESH_IS_NULL" );
00753         return false;
00754       }
00755       if ( !isSubshapeOk() )
00756       {
00757         theMess = tr( "INVALID_SUBSHAPE" );
00758         return false;
00759       }
00760     }
00761   }
00762 
00763   return true;
00764 }
00765 
00766 //================================================================================
00775 //================================================================================
00776 static bool isCompatible(const HypothesisData* theAlgoData,
00777                          const HypothesisData* theHypData,
00778                          const int             theHypType)
00779 {
00780   if ( !theAlgoData )
00781     return true;
00782 
00783   if ( theHypType == SMESHGUI_MeshOp::Algo )
00784     return SMESH::IsCompatibleAlgorithm( theAlgoData, theHypData );
00785 
00786   bool isOptional;
00787   return ( SMESH::IsAvailableHypothesis( theAlgoData, theHypData->TypeName, isOptional ));
00788 }
00789 
00790 //================================================================================
00801 //================================================================================
00802 void SMESHGUI_MeshOp::availableHyps( const int       theDim,
00803                                      const int       theHypType,
00804                                      QStringList&    theHyps,
00805                                      THypDataList&   theDataList,
00806                                      HypothesisData* theAlgoData ) const
00807 {
00808   theDataList.clear();
00809   theHyps.clear();
00810   bool isAlgo = ( theHypType == Algo );
00811   bool isAux  = ( theHypType == AddHyp );
00812   QStringList aHypTypeNameList = SMESH::GetAvailableHypotheses( isAlgo, theDim, isAux, myIsOnGeometry );
00813 
00814   QStringList::const_iterator anIter;
00815   for ( anIter = aHypTypeNameList.begin(); anIter != aHypTypeNameList.end(); ++anIter )
00816   {
00817     HypothesisData* aData = SMESH::GetHypothesisData( *anIter );
00818     if ( isCompatible ( theAlgoData, aData, theHypType )) {
00819       theDataList.append( aData );
00820       theHyps.append( aData->Label );
00821     }
00822   }
00823 }
00824 
00825 //================================================================================
00839 //================================================================================
00840 void SMESHGUI_MeshOp::existingHyps( const int theDim,
00841                                     const int theHypType,
00842                                     _PTR(SObject) theFather,
00843                                     QStringList& theHyps,
00844                                     THypList& theHypList,
00845                                     HypothesisData* theAlgoData)
00846 {
00847   // Clear hypoheses list
00848   theHyps.clear();
00849   theHypList.clear();
00850 
00851   if ( !theFather )
00852     return;
00853 
00854   const bool isAux  = ( theHypType == AddHyp );
00855 
00856   _PTR(SObject)          aHypRoot;
00857   _PTR(GenericAttribute) anAttr;
00858   _PTR(AttributeName)    aName;
00859   _PTR(AttributeIOR)     anIOR;
00860 
00861   bool isMesh = !_CAST( SComponent, theFather );
00862   int aPart = -1;
00863   if ( isMesh )
00864     aPart = theHypType == Algo ? SMESH::Tag_RefOnAppliedAlgorithms : SMESH::Tag_RefOnAppliedHypothesis;
00865   else
00866     aPart = theHypType == Algo ? SMESH::Tag_AlgorithmsRoot : SMESH::Tag_HypothesisRoot;
00867 
00868   if ( theFather->FindSubObject( aPart, aHypRoot ) )
00869   {
00870     _PTR(ChildIterator) anIter =
00871       SMESH::GetActiveStudyDocument()->NewChildIterator( aHypRoot );
00872     for ( ; anIter->More(); anIter->Next() )
00873     {
00874       _PTR(SObject) anObj = anIter->Value();
00875       if ( isMesh ) // i.e. mesh or submesh
00876       {
00877         _PTR(SObject) aRefObj;
00878         if ( anObj->ReferencedObject( aRefObj ) )
00879           anObj = aRefObj;
00880         else
00881           continue;
00882       }
00883       if ( anObj->FindAttribute( anAttr, "AttributeName" ) )
00884       {
00885         aName = anAttr;
00886         CORBA::Object_var aVar = _CAST(SObject,anObj)->GetObject();
00887         if ( !CORBA::is_nil( aVar ) )
00888         {
00889           SMESH::SMESH_Hypothesis_var aHypVar = SMESH::SMESH_Hypothesis::_narrow( aVar );
00890           if ( !aHypVar->_is_nil() )
00891           {
00892             HypothesisData* aData = SMESH::GetHypothesisData( aHypVar->GetName() );
00893             if ( !aData) continue;
00894             if ( ( theDim == -1 || aData->Dim.contains( theDim ) ) &&
00895                  ( isCompatible ( theAlgoData, aData, theHypType )) &&
00896                  ( isAux == aData->IsAux ))
00897             {
00898               std::string aHypName = aName->Value();
00899               theHyps.append( aHypName.c_str() );
00900               theHypList.append( THypItem( aHypVar, aHypName.c_str() ) );
00901             }
00902           }
00903         }
00904       }
00905     }
00906   }
00907 }
00908 
00909 //================================================================================
00918 //================================================================================
00919 SMESH::SMESH_Hypothesis_var
00920 SMESHGUI_MeshOp::getInitParamsHypothesis( const QString& aHypType,
00921                                           const QString& aServerLib ) const
00922 {
00923   if ( aHypType.isEmpty() || aServerLib.isEmpty() )
00924     return SMESH::SMESH_Hypothesis::_nil();
00925 
00926   const int nbColonsInMeshEntry = 3;
00927   bool isSubMesh = myToCreate ?
00928     !myIsMesh :
00929     myDlg->selectedObject( SMESHGUI_MeshDlg::Obj ).count(':') > nbColonsInMeshEntry;
00930 
00931   // get mesh and geom object
00932   SMESH::SMESH_Mesh_var aMeshVar = SMESH::SMESH_Mesh::_nil();
00933   GEOM::GEOM_Object_var aGeomVar = GEOM::GEOM_Object::_nil();
00934 
00935   QString anEntry;
00936   if ( isSubMesh )
00937   {
00938     anEntry = myDlg->selectedObject
00939       ( myToCreate ? SMESHGUI_MeshDlg::Mesh : SMESHGUI_MeshDlg::Obj );
00940     if ( _PTR(SObject) pObj = studyDS()->FindObjectID( anEntry.toLatin1().data() ))
00941     {
00942       CORBA::Object_ptr Obj = _CAST( SObject,pObj )->GetObject();
00943       if ( myToCreate ) // mesh and geom may be selected
00944       {
00945         aMeshVar = SMESH::SMESH_Mesh::_narrow( Obj );
00946         anEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
00947         if ( _PTR(SObject) pGeom = studyDS()->FindObjectID( anEntry.toLatin1().data() ))
00948           aGeomVar= GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
00949       }
00950       else // edition: sub-mesh may be selected
00951       {
00952         SMESH::SMESH_subMesh_var sm = SMESH::SMESH_subMesh::_narrow( Obj );
00953         if ( !sm->_is_nil() ) {
00954           aMeshVar = sm->GetFather();
00955           aGeomVar = sm->GetSubShape();
00956         }
00957       }
00958     }
00959   }
00960   else // mesh
00961   {
00962     if ( !myToCreate ) // mesh to edit can be selected
00963     {
00964       anEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
00965       if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( anEntry.toLatin1().data() ))
00966       {
00967         aMeshVar = SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
00968         if ( !aMeshVar->_is_nil() )
00969           aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pMesh );
00970       }
00971     }
00972     if ( aGeomVar->_is_nil() ) {
00973       anEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
00974       if ( _PTR(SObject) pGeom = studyDS()->FindObjectID( anEntry.toLatin1().data() ))
00975       {
00976         aGeomVar= GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
00977       }
00978     }
00979   }
00980 
00981   SMESH::SMESH_Hypothesis_var hyp =
00982     SMESHGUI::GetSMESHGen()->GetHypothesisParameterValues( aHypType.toLatin1().data(),
00983                                                            aServerLib.toLatin1().data(),
00984                                                            aMeshVar,
00985                                                            aGeomVar,
00986                                                            /*byMesh = */isSubMesh);
00987   if ( hyp->_is_nil() && isSubMesh )
00988     hyp = SMESHGUI::GetSMESHGen()->GetHypothesisParameterValues( aHypType.toLatin1().data(),
00989                                                                  aServerLib.toLatin1().data(),
00990                                                                  aMeshVar,
00991                                                                  aGeomVar,
00992                                                                  /*byMesh = */false);
00993   return hyp;
00994 }
00995 
00996 //================================================================================
01003 //================================================================================
01004 static int getTabDim (const QObject* tab, SMESHGUI_MeshDlg* dlg )
01005 {
01006   int aDim = -1;
01007   for (int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++)
01008     if (tab == dlg->tab(i))
01009       aDim = i;
01010   return aDim;
01011 }
01012 
01013 //================================================================================
01022 //================================================================================
01023 void SMESHGUI_MeshOp::onCreateHyp( const int theHypType, const int theIndex )
01024 {
01025   // Specifies dimension of hypothesis to be created
01026   int aDim = getTabDim( sender(), myDlg );
01027   if (aDim == -1)
01028     return;
01029 
01030   // Specifies type of hypothesis to be created
01031   THypDataList& dataList = myAvailableHypData[ aDim ][ theHypType ];
01032   if (theIndex < 0 || theIndex >= dataList.count())
01033     return;
01034   QString aHypTypeName = dataList[ theIndex ]->TypeName;
01035 
01036   // Create hypothesis
01037   createHypothesis(aDim, theHypType, aHypTypeName);
01038 }
01039 
01040 namespace
01041 {
01042   QString GetUniqueName (const QStringList& theHypNames,
01043                          const QString& theName,
01044                          size_t theIteration = 1)
01045   {
01046     QString aName = theName + "_" + QString::number( theIteration );
01047     if ( theHypNames.contains( aName ) )
01048       return GetUniqueName( theHypNames, theName, ++theIteration );
01049     return aName;
01050   }
01051 }
01052 
01053 //================================================================================
01060 //================================================================================
01061 void SMESHGUI_MeshOp::createHypothesis(const int theDim,
01062                                        const int theType,
01063                                        const QString& theTypeName)
01064 {
01065   HypothesisData* aData = SMESH::GetHypothesisData(theTypeName);
01066   if (!aData)
01067     return;
01068 
01069   myDim = theDim;
01070   myType = theType;
01071   QStringList aHypNames;
01072   TDim2Type2HypList::const_iterator aDimIter = myExistingHyps.begin();
01073   for ( ; aDimIter != myExistingHyps.end(); aDimIter++) {
01074     const TType2HypList& aType2HypList = aDimIter.value();
01075     TType2HypList::const_iterator aTypeIter = aType2HypList.begin();
01076     for ( ; aTypeIter != aType2HypList.end(); aTypeIter++) {
01077       const THypList& aHypList = aTypeIter.value();
01078       THypList::const_iterator anIter = aHypList.begin();
01079       for ( ; anIter != aHypList.end(); anIter++) {
01080         const THypItem& aHypItem = *anIter;
01081         const QString& aHypName = aHypItem.second;
01082         aHypNames.append(aHypName);
01083       }
01084     }
01085   }
01086   QString aHypName = GetUniqueName( aHypNames, aData->Label);
01087 
01088   // existing hypos
01089   bool dialog = false;
01090 
01091   QString aClientLibName = aData->ClientLibName;
01092   if (aClientLibName == "") {
01093     // Call hypothesis creation server method (without GUI)
01094     SMESH::SMESH_Hypothesis_var aHyp =
01095       SMESH::CreateHypothesis(theTypeName, aHypName, false);
01096 #ifdef WITHGENERICOBJ
01097     if (!CORBA::is_nil(aHyp))
01098       aHyp->UnRegister();
01099 #endif
01100   } else {
01101     // Get hypotheses creator client (GUI)
01102     // BUG 0020378
01103     //SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(theTypeName);
01104     SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(theTypeName);
01105 
01106     // Create hypothesis
01107     if (aCreator) {
01108       // Get parameters appropriate to initialize a new hypothesis
01109       SMESH::SMESH_Hypothesis_var initParamHyp =
01110         getInitParamsHypothesis(theTypeName, aData->ServerLibName);
01111 
01112       removeCustomFilters(); // Issue 0020170
01113 
01114       // Get Entry of the Geom object
01115       QString aGeomEntry = "";
01116       QString aMeshEntry = "";
01117       QString anObjEntry = "";
01118       aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
01119       aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
01120       anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
01121 
01122       if ( myToCreate && myIsMesh )
01123         aMeshEntry = aGeomEntry;
01124 
01125       if ( aMeshEntry != aGeomEntry ) { // Get Geom object from Mesh of a sub-mesh being edited
01126         _PTR(SObject) pObj = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
01127         GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
01128         aMeshEntry = ( aGeomVar->_is_nil() ) ? "" : aMeshEntry = aGeomVar->GetStudyEntry();
01129       }
01130 
01131       if ( aMeshEntry == "" && aGeomEntry == "" ) { // get geom of an object being edited
01132         _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
01133         bool isMesh;
01134         GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj, &isMesh );
01135         if ( !aGeomVar->_is_nil() )
01136         {
01137           aGeomEntry = aGeomVar->GetStudyEntry();
01138           if ( isMesh )
01139             aMeshEntry = aGeomEntry;
01140         }
01141       }
01142 
01143       if ( anObjEntry != "" && aGeomEntry != "" && aMeshEntry == "" ) {
01144         // take geometry from submesh being created
01145         _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
01146         if ( pObj ) {
01147           // if current object is sub-mesh
01148           SMESH::SMESH_subMesh_var aSubMeshVar =
01149             SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
01150           if ( !aSubMeshVar->_is_nil() ) {
01151             SMESH::SMESH_Mesh_var aMeshVar =  aSubMeshVar->GetFather();
01152             if ( !aMeshVar->_is_nil() ) {
01153               _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar );
01154               GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( aMeshSO );
01155               if ( !aGeomVar->_is_nil() )
01156                 aMeshEntry = aGeomVar->GetStudyEntry();
01157             }
01158           }
01159         }
01160       }
01161 
01162       aCreator->setShapeEntry( aGeomEntry );
01163       if ( aMeshEntry != "" )
01164         aCreator->setMainShapeEntry( aMeshEntry );
01165       myDlg->setEnabled( false );
01166       aCreator->create(initParamHyp, aHypName, myDlg, this, SLOT( onHypoCreated( int ) ) );
01167       dialog = true;
01168     }
01169     else {
01170      SMESH::SMESH_Hypothesis_var aHyp =
01171        SMESH::CreateHypothesis(theTypeName, aHypName, false);
01172 #ifdef WITHGENERICOBJ
01173      if (!CORBA::is_nil(aHyp))
01174        aHyp->UnRegister();
01175 #endif
01176     }
01177   }
01178 
01179   if( !dialog )
01180     onHypoCreated(2);
01181 }
01182 
01183 //================================================================================
01191 //================================================================================
01192 void SMESHGUI_MeshOp::onHypoCreated( int result )
01193 {
01194   if( result != 2 )
01195   {
01196     int obj = myDlg->getActiveObject();
01197     onActivateObject( obj ); // Issue 0020170. Restore filters
01198     myDlg->setEnabled( true );
01199   }
01200 
01201   _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
01202 
01203   int nbHyp = myExistingHyps[myDim][myType].count();
01204   HypothesisData* algoData = hypData( myDim, Algo, currentHyp( myDim, Algo ));
01205   QStringList aNewHyps;
01206   existingHyps(myDim, myType, aFather, aNewHyps, myExistingHyps[myDim][myType], algoData);
01207   if (aNewHyps.count() > nbHyp)
01208   {
01209     for (int i = nbHyp; i < aNewHyps.count(); i++)
01210       myDlg->tab(myDim)->addHyp(myType, aNewHyps[i]);
01211   }
01212 
01213   if( result!=2 && myHypoSet )
01214     processSet();
01215 }
01216 
01217 //================================================================================
01226 //================================================================================
01227 void SMESHGUI_MeshOp::onEditHyp( const int theHypType, const int theIndex )
01228 {
01229   // Speicfies dimension of hypothesis to be created
01230   int aDim = getTabDim( sender(), myDlg );
01231   if (aDim == -1)
01232     return;
01233 
01234   const THypList& aList = myExistingHyps[ aDim ][ theHypType ];
01235   if ( theIndex < 0 || theIndex >= aList.count() )
01236     return;
01237   const THypItem& aHypItem = aList[ theIndex ];
01238   SMESH::SMESH_Hypothesis_var aHyp = aHypItem.first;
01239   if ( aHyp->_is_nil() )
01240     return;
01241 
01242   SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(aHyp->GetName());
01243   if ( aCreator )
01244   {
01245     // Get initial parameters
01246     SMESH::SMESH_Hypothesis_var initParamHyp =
01247       getInitParamsHypothesis( aHyp->GetName(), aHyp->GetLibName());
01248     aCreator->setInitParamsHypothesis( initParamHyp );
01249 
01250     // Get Entry of the Geom object
01251     QString aGeomEntry = "";
01252     QString aMeshEntry = "";
01253     QString anObjEntry = "";
01254     aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
01255     aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
01256     anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
01257 
01258     if ( myToCreate && myIsMesh )
01259       aMeshEntry = aGeomEntry;
01260 
01261     if ( aMeshEntry != aGeomEntry ) { // Get Geom object from Mesh of a sub-mesh being edited
01262       _PTR(SObject) pObj = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
01263       GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
01264       aMeshEntry = ( aGeomVar->_is_nil() ) ? "" : aMeshEntry = aGeomVar->GetStudyEntry();
01265     }
01266 
01267     if ( aMeshEntry == "" && aGeomEntry == "" ) { // get geom of an object being edited
01268       _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
01269       bool isMesh;
01270       GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj, &isMesh );
01271       if ( !aGeomVar->_is_nil() )
01272       {
01273         aGeomEntry = aGeomVar->GetStudyEntry();
01274         if ( isMesh )
01275           aMeshEntry = aGeomEntry;
01276       }
01277     }
01278 
01279     if ( anObjEntry != "" && aGeomEntry != "" && aMeshEntry == "" ) {
01280       // take geometry from submesh being created
01281       _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
01282       if ( pObj ) {
01283         // if current object is sub-mesh
01284         SMESH::SMESH_subMesh_var aSubMeshVar =
01285           SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
01286         if ( !aSubMeshVar->_is_nil() ) {
01287           SMESH::SMESH_Mesh_var aMeshVar =  aSubMeshVar->GetFather();
01288           if ( !aMeshVar->_is_nil() ) {
01289             _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar );
01290             GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( aMeshSO );
01291             if ( !aGeomVar->_is_nil() )
01292               aMeshEntry = aGeomVar->GetStudyEntry();
01293           }
01294         }
01295       }
01296     }
01297 
01298     aCreator->setShapeEntry( aGeomEntry );
01299     if ( aMeshEntry != "" )
01300       aCreator->setMainShapeEntry( aMeshEntry );
01301     removeCustomFilters(); // Issue 0020170
01302     myDlg->setEnabled( false );
01303     aCreator->edit( aHyp.in(), aHypItem.second, dlg(), this, SLOT( onHypoEdited( int ) ) );
01304   }
01305 }
01306 
01307 //================================================================================
01314 //================================================================================
01315 void SMESHGUI_MeshOp::onHypoEdited( int result )
01316 {
01317   int obj = myDlg->getActiveObject();
01318   onActivateObject( obj ); // Issue 0020170. Restore filters
01319   myDlg->setEnabled( true );
01320 }
01321 
01322 //================================================================================
01330 //================================================================================
01331 HypothesisData* SMESHGUI_MeshOp::hypData( const int theDim,
01332                                           const int theHypType,
01333                                           const int theIndex)
01334 {
01335   if ( theDim     > -1 && theDim    <= SMESH::DIM_3D &&
01336        theHypType > -1 && theHypType < NbHypTypes &&
01337        theIndex   > -1 && theIndex   < myAvailableHypData[ theDim ][ theHypType ].count() )
01338     return myAvailableHypData[ theDim ][ theHypType ][ theIndex ];
01339   return 0;
01340 }
01341 
01342 //================================================================================
01347 //================================================================================
01348 void SMESHGUI_MeshOp::onAlgoSelected( const int theIndex,
01349                                       const int theDim )
01350 {
01351   if ( myIgnoreAlgoSelection )
01352     return;
01353 
01354   int aDim = theDim < 0 ? getTabDim( sender(), myDlg ): theDim;
01355   if (aDim == -1)
01356     return;
01357 
01358   // find highest available dimension, all algos of this dimension are available for choice
01359   int aTopDim = -1;
01360   for (int i = SMESH::DIM_0D; i <= SMESH::DIM_3D; i++)
01361     if (isAccessibleDim( i ))
01362       aTopDim = i;
01363   if (aTopDim == -1)
01364     return;
01365 
01366   const bool isSubmesh = ( myToCreate ? !myIsMesh : myDlg->isObjectShown( SMESHGUI_MeshDlg::Mesh ));
01367 
01368   HypothesisData* algoData = hypData( aDim, Algo, theIndex );
01369   HypothesisData* algoByDim[4];
01370   algoByDim[ aDim ] = algoData;
01371 
01372   QStringList anAvailable;
01373   if ( !algoData ) { // all algos becomes available
01374     availableHyps( aDim, Algo, anAvailable, myAvailableHypData[ aDim ][ Algo ]);
01375     myDlg->tab( aDim )->setAvailableHyps( Algo, anAvailable );
01376   }
01377 
01378   // check that algorithms of other dimentions are compatible with
01379   // the selected one
01380 
01381    // 2 loops: backward and forward from algo dimension
01382   for ( int forward = false; forward <= true; ++forward )
01383   {
01384     int dim = aDim + 1, lastDim = SMESH::DIM_3D, dir = 1;
01385     if ( !forward ) {
01386       dim = aDim - 1; lastDim = SMESH::DIM_0D; dir = -1;
01387     }
01388     HypothesisData* prevAlgo = algoData;
01389     bool noCompatible = false;
01390     for ( ; dim * dir <= lastDim * dir; dim += dir)
01391     {
01392       if ( !isAccessibleDim( dim ))
01393         continue;
01394       if ( noCompatible ) { // the selected algo has no compatible ones
01395         anAvailable.clear();
01396         myDlg->tab( dim )->setAvailableHyps( Algo, anAvailable );
01397         myAvailableHypData[dim][Algo].clear();
01398         algoByDim[ dim ] = 0;
01399         continue;
01400       }
01401       // get currently selected algo
01402       int algoIndex = currentHyp( dim, Algo );
01403       HypothesisData* curAlgo = hypData( dim, Algo, algoIndex );
01404       if ( curAlgo ) { // some algo selected
01405         if ( !isCompatible( prevAlgo, curAlgo, Algo ))
01406           curAlgo = 0;
01407       }
01408       // set new available algoritms
01409       availableHyps( dim, Algo, anAvailable, myAvailableHypData[dim][Algo], prevAlgo );
01410       HypothesisData* soleCompatible = 0;
01411       if ( anAvailable.count() == 1 )
01412         soleCompatible = myAvailableHypData[dim][Algo][0];
01413       if ( dim == aTopDim && prevAlgo ) // all available algoritms should be selectable any way
01414         availableHyps( dim, Algo, anAvailable, myAvailableHypData[dim][Algo], 0 );
01415       myDlg->tab( dim )->setAvailableHyps( Algo, anAvailable );
01416       noCompatible = anAvailable.isEmpty();
01417 
01418       // restore previously selected algo
01419       algoIndex = myAvailableHypData[dim][Algo].indexOf( curAlgo );
01420       if ( !isSubmesh && algoIndex < 0 && soleCompatible && !forward && dim != SMESH::DIM_0D)
01421         // select the sole compatible algo
01422         algoIndex = myAvailableHypData[dim][Algo].indexOf( soleCompatible );
01423       setCurrentHyp( dim, Algo, algoIndex );
01424 
01425       // remember current algo
01426       prevAlgo = algoByDim[ dim ] = hypData( dim, Algo, algoIndex );
01427     }
01428   }
01429 
01430   // set hypotheses corresponding to the found algoritms
01431 
01432   _PTR(SObject) pObj = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
01433 
01434   for ( int dim = SMESH::DIM_0D; dim <= SMESH::DIM_3D; dim++ )
01435   {
01436     if ( !isAccessibleDim( dim ))
01437       continue;
01438     for ( int type = MainHyp; type < NbHypTypes; type++ )
01439     {
01440       myAvailableHypData[ dim ][ type ].clear();
01441       QStringList anAvailable, anExisting;
01442 
01443       HypothesisData* curAlgo = algoByDim[ dim ];
01444       int hypIndex = currentHyp( dim, type );
01445 
01446       SMESH::SMESH_Hypothesis_var curHyp;
01447       if ( hypIndex >= 0 && hypIndex < myExistingHyps[ dim ][ type ].count() )
01448         curHyp = myExistingHyps[ dim ][ type ][ hypIndex ].first;
01449 
01450       if ( !myToCreate && !curAlgo && !curHyp->_is_nil() ) { // edition, algo not selected
01451         // try to find algo by selected hypothesis in order to keep it selected
01452         bool algoDeselectedByUser = ( theDim < 0 && aDim == dim );
01453         CORBA::String_var curHypType = curHyp->GetName();
01454         if ( !algoDeselectedByUser &&
01455              myObjHyps[ dim ][ type ].count() > 0 &&
01456              !strcmp( curHypType, myObjHyps[ dim ][ type ].first().first->GetName()) )
01457         {
01458           HypothesisData* hypData = SMESH::GetHypothesisData( curHyp->GetName() );
01459           for (int i = 0; i < myAvailableHypData[ dim ][ Algo ].count(); ++i) {
01460             curAlgo = myAvailableHypData[ dim ][ Algo ][ i ];
01461             if (curAlgo && hypData && isCompatible(curAlgo, hypData, type))
01462               break;
01463             else
01464               curAlgo = 0;
01465           }
01466         }
01467       }
01468       // get hyps compatible with curAlgo
01469       if ( curAlgo )
01470       {
01471         // check if a selected hyp is compatible with the curAlgo
01472         if ( !curHyp->_is_nil() ) {
01473           HypothesisData* hypData = SMESH::GetHypothesisData( curHyp->GetName() );
01474           if ( !isCompatible( curAlgo, hypData, type ))
01475             curHyp = SMESH::SMESH_Hypothesis::_nil();
01476         }
01477         existingHyps( dim, type, pObj, anExisting, myExistingHyps[ dim ][ type ], curAlgo);
01478         availableHyps( dim, type, anAvailable, myAvailableHypData[ dim ][ type ], curAlgo);
01479       }
01480       // set list of hypotheses
01481       myDlg->tab( dim )->setAvailableHyps( type, anAvailable );
01482       myDlg->tab( dim )->setExistingHyps( type, anExisting );
01483 
01484       // set current existing hypothesis
01485       if ( !curHyp->_is_nil() && !anExisting.isEmpty() )
01486         hypIndex = this->find( curHyp, myExistingHyps[ dim ][ type ]);
01487       else
01488         hypIndex = -1;
01489       if ( !isSubmesh && hypIndex < 0 && anExisting.count() == 1 ) {
01490         // none is yet selected => select the sole existing if it is not optional
01491         CORBA::String_var hypTypeName = myExistingHyps[ dim ][ type ].first().first->GetName();
01492         bool isOptional = true;
01493         if ( algoByDim[ dim ] &&
01494              SMESH::IsAvailableHypothesis( algoByDim[ dim ], hypTypeName.in(), isOptional ) &&
01495              !isOptional )
01496           hypIndex = 0;
01497       }
01498       setCurrentHyp( dim, type, hypIndex );
01499     }
01500   }
01501 }
01502 
01503 //================================================================================
01508 //================================================================================
01509 void SMESHGUI_MeshOp::onHypoSet( const QString& theSetName )
01510 {
01511   myHypoSet = SMESH::GetHypothesesSet(theSetName);
01512   if (!myHypoSet)
01513     return;
01514 
01515   // clear all hyps
01516   for (int dim = SMESH::DIM_0D; dim <= SMESH::DIM_3D; dim++) {
01517     setCurrentHyp(dim, Algo, -1);
01518     setCurrentHyp(dim, AddHyp, -1);
01519     setCurrentHyp(dim, MainHyp, -1);
01520   }
01521 
01522   myHypoSet->init(true); //algorithms
01523   processSet();
01524   myHypoSet->init(false); //hypotheses
01525   processSet();
01526   myHypoSet = 0;
01527 }
01528 
01529 //================================================================================
01535 //================================================================================
01536 void SMESHGUI_MeshOp::processSet()
01537 {
01538   myHypoSet->next();
01539   if( !myHypoSet->more() )
01540     return;
01541 
01542   bool isAlgo = myHypoSet->isAlgo();
01543   QString aHypoTypeName = myHypoSet->current();
01544   HypothesisData* aHypData = SMESH::GetHypothesisData(aHypoTypeName);
01545   if (!aHypData)
01546   {
01547     processSet();
01548     return;
01549   }
01550 
01551   int aDim = aHypData->Dim[0];
01552   // create or/and set
01553   if (isAlgo)
01554   {
01555     int index = myAvailableHypData[aDim][Algo].indexOf( aHypData );
01556     if ( index < 0 )
01557     {
01558       QStringList anAvailable;
01559       availableHyps( aDim, Algo, anAvailable, myAvailableHypData[aDim][Algo] );
01560       myDlg->tab( aDim )->setAvailableHyps( Algo, anAvailable );
01561       index = myAvailableHypData[aDim][Algo].indexOf( aHypData );
01562     }
01563     setCurrentHyp( aDim, Algo, index );
01564     onAlgoSelected( index, aDim );
01565     processSet();
01566   }
01567   else
01568   {
01569     bool mainHyp = true;
01570     QStringList anAvailable;
01571     availableHyps( aDim, MainHyp, anAvailable, myAvailableHypData[aDim][MainHyp] );
01572     myDlg->tab( aDim )->setAvailableHyps( MainHyp, anAvailable );
01573     int index = myAvailableHypData[aDim][MainHyp].indexOf( aHypData );
01574     if ( index < 0 )
01575     {
01576       mainHyp = false;
01577       index = myAvailableHypData[aDim][AddHyp].indexOf( aHypData );
01578     }
01579     if (index >= 0)
01580       createHypothesis(aDim, mainHyp ? MainHyp : AddHyp, aHypoTypeName);
01581     else
01582       processSet();
01583   }
01584 }
01585 
01586 //================================================================================
01595 //================================================================================
01596 bool SMESHGUI_MeshOp::createMesh( QString& theMess, QStringList& theEntryList )
01597 {
01598   theMess = "";
01599 
01600   //QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
01601   //QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
01602 
01603   QStringList aList;
01604   myDlg->selectedObject( SMESHGUI_MeshDlg::Geom, aList );
01605   QStringList::Iterator it = aList.begin();
01606   for ( ; it!=aList.end(); it++)
01607   {
01608     QString aGeomEntry = *it;
01609     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() );
01610     GEOM::GEOM_Object_var aGeomVar =
01611       GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
01612 
01613     SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
01614     if ( aSMESHGen->_is_nil() )
01615       return false;
01616 
01617     SUIT_OverrideCursor aWaitCursor;
01618 
01619     // create mesh
01620     SMESH::SMESH_Mesh_var aMeshVar = aSMESHGen->CreateMesh( aGeomVar );
01621     if ( aMeshVar->_is_nil() )
01622       return false;
01623     _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar.in() );
01624     if ( aMeshSO ) {
01625       SMESH::SetName( aMeshSO, myDlg->objectText( SMESHGUI_MeshDlg::Obj ) );
01626       theEntryList.append( aMeshSO->GetID().c_str() );
01627     }
01628 
01629     for ( int aDim = SMESH::DIM_0D; aDim <= SMESH::DIM_3D; aDim++ ) {
01630       if ( !isAccessibleDim( aDim )) continue;
01631 
01632       // assign hypotheses
01633       for ( int aHypType = MainHyp; aHypType <= AddHyp; aHypType++ ) {
01634         int aHypIndex = currentHyp( aDim, aHypType );
01635         if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() ) {
01636           SMESH::SMESH_Hypothesis_var aHypVar = myExistingHyps[ aDim ][ aHypType ][ aHypIndex ].first;
01637           if ( !aHypVar->_is_nil() )
01638             SMESH::AddHypothesisOnMesh( aMeshVar, aHypVar );
01639         }
01640       }
01641       // find or create algorithm
01642       SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
01643       if ( !anAlgoVar->_is_nil() )
01644         SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
01645     }
01646 #ifdef WITHGENERICOBJ
01647     // obj has been published in study. Its refcount has been incremented.
01648     // It is safe to decrement its refcount
01649     // so that it will be destroyed when the entry in study will be removed
01650     if (aMeshSO)
01651       aMeshVar->UnRegister();
01652 #endif
01653   }
01654   return true;
01655 }
01656 
01657 //================================================================================
01666 //================================================================================
01667 bool SMESHGUI_MeshOp::createSubMesh( QString& theMess, QStringList& theEntryList )
01668 {
01669   theMess = "";
01670 
01671   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
01672   if ( aSMESHGen->_is_nil() )
01673     return false;
01674 
01675   // get mesh object
01676   QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
01677   _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() );
01678   SMESH::SMESH_Mesh_var aMeshVar =
01679     SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
01680   if (aMeshVar->_is_nil())
01681     return false;
01682 
01683   // GEOM shape of the main mesh
01684   GEOM::GEOM_Object_var mainGeom = aMeshVar->GetShapeToMesh();
01685 
01686   // Name for the new sub-mesh
01687   QString aName = myDlg->objectText(SMESHGUI_MeshDlg::Obj);
01688 
01689   // get geom object
01690   GEOM::GEOM_Object_var aGeomVar;
01691   QStringList aGEOMs;
01692   myDlg->selectedObject(SMESHGUI_MeshDlg::Geom, aGEOMs);
01693   if (aGEOMs.count() == 1)
01694   {
01695     //QString aGeomEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Geom );
01696     QString aGeomEntry = aGEOMs.first();
01697     _PTR(SObject) pGeom = studyDS()->FindObjectID( aGeomEntry.toLatin1().data() );
01698     aGeomVar = GEOM::GEOM_Object::_narrow( _CAST( SObject,pGeom )->GetObject() );
01699   }
01700   else if (aGEOMs.count() > 1)
01701   {
01702     // create a GEOM group
01703     GEOM::GEOM_Gen_var geomGen = SMESH::GetGEOMGen();
01704     _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
01705     if (!geomGen->_is_nil() && aStudy) {
01706       GEOM::GEOM_IGroupOperations_var op =
01707         geomGen->GetIGroupOperations(aStudy->StudyId());
01708       if (!op->_is_nil()) {
01709         // check and add all selected GEOM objects: they must be
01710         // a sub-shapes of the main GEOM and must be of one type
01711         int iSubSh = 0;
01712         TopAbs_ShapeEnum aGroupType = TopAbs_SHAPE;
01713         GEOM::ListOfGO_var aSeq = new GEOM::ListOfGO;
01714         aSeq->length(aGEOMs.count());
01715         QStringList::const_iterator aSubShapesIter = aGEOMs.begin();
01716         for ( ; aSubShapesIter != aGEOMs.end(); aSubShapesIter++, iSubSh++) {
01717           QString aSubGeomEntry = (*aSubShapesIter);
01718           _PTR(SObject) pSubGeom = studyDS()->FindObjectID(aSubGeomEntry.toLatin1().data());
01719           GEOM::GEOM_Object_var aSubGeomVar =
01720             GEOM::GEOM_Object::_narrow(_CAST(SObject,pSubGeom)->GetObject());
01721           TopAbs_ShapeEnum aSubShapeType = (TopAbs_ShapeEnum)aSubGeomVar->GetShapeType();
01722           if (iSubSh == 0) {
01723             aGroupType = aSubShapeType;
01724           } else {
01725             if (aSubShapeType != aGroupType)
01726               aGroupType = TopAbs_SHAPE;
01727           }
01728           aSeq[iSubSh] = aSubGeomVar;
01729         }
01730         // create a group
01731         GEOM::GEOM_Object_var aGroupVar = op->CreateGroup(mainGeom, aGroupType);
01732         op->UnionList(aGroupVar, aSeq);
01733 
01734         if (op->IsDone()) {
01735           aGeomVar = aGroupVar;
01736 
01737           // publish the GEOM group in study
01738           QString aNewGeomGroupName ("Auto_group_for_");
01739           aNewGeomGroupName += aName;
01740           SALOMEDS::SObject_var aNewGroupSO =
01741             geomGen->AddInStudy(aSMESHGen->GetCurrentStudy(), aGeomVar,
01742                                 aNewGeomGroupName.toLatin1().data(), mainGeom);
01743         }
01744       }
01745     }
01746   }
01747   else {
01748   }
01749   if (aGeomVar->_is_nil())
01750     return false;
01751 
01752   SUIT_OverrideCursor aWaitCursor;
01753 
01754   // create sub-mesh
01755   SMESH::SMESH_subMesh_var aSubMeshVar = aMeshVar->GetSubMesh( aGeomVar, aName.toLatin1().data() );
01756   _PTR(SObject) aSubMeshSO = SMESH::FindSObject( aSubMeshVar.in() );
01757   if ( aSubMeshSO ) {
01758     SMESH::SetName( aSubMeshSO, aName.toLatin1().data() );
01759     theEntryList.append( aSubMeshSO->GetID().c_str() );
01760   }
01761 
01762   for ( int aDim = SMESH::DIM_0D; aDim <= SMESH::DIM_3D; aDim++ )
01763   {
01764     if ( !isAccessibleDim( aDim )) continue;
01765 
01766     // find or create algorithm
01767     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( aDim );
01768     if ( !anAlgoVar->_is_nil() )
01769       SMESH::AddHypothesisOnSubMesh( aSubMeshVar, anAlgoVar );
01770     // assign hypotheses
01771     for ( int aHypType = MainHyp; aHypType <= AddHyp; aHypType++ )
01772     {
01773       int aHypIndex = currentHyp( aDim, aHypType );
01774       if ( aHypIndex >= 0 && aHypIndex < myExistingHyps[ aDim ][ aHypType ].count() )
01775       {
01776         SMESH::SMESH_Hypothesis_var aHypVar =
01777           myExistingHyps[ aDim ][ aHypType ][ aHypIndex ].first;
01778         if ( !aHypVar->_is_nil() )
01779           SMESH::AddHypothesisOnSubMesh( aSubMeshVar, aHypVar );
01780       }
01781     }
01782   }
01783 
01784   // deselect geometry: next submesh should be created on other subshape
01785   myDlg->clearSelection( SMESHGUI_MeshDlg::Geom );
01786   selectObject( _PTR(SObject)() );
01787   selectionDone();
01788 
01789   return true;
01790 }
01791 
01792 //================================================================================
01801 //================================================================================
01802 int SMESHGUI_MeshOp::currentHyp( const int theDim, const int theHypType ) const
01803 {
01804   return myDlg->tab( theDim )->currentHyp( theHypType ) - 1;
01805 }
01806 
01807 //================================================================================
01813 //================================================================================
01814 bool SMESHGUI_MeshOp::isAccessibleDim( const int theDim ) const
01815 {
01816   return myDlg->isTabEnabled( theDim );
01817 }
01818 
01819 //================================================================================
01828 //================================================================================
01829 void SMESHGUI_MeshOp::setCurrentHyp( const int theDim,
01830                                      const int theHypType,
01831                                      const int theIndex )
01832 {
01833   myIgnoreAlgoSelection = true;
01834   myDlg->tab( theDim )->setCurrentHyp( theHypType, theIndex + 1 );
01835   myIgnoreAlgoSelection = false;
01836 }
01837 
01838 //================================================================================
01844 //================================================================================
01845 void SMESHGUI_MeshOp::setDefaultName() const
01846 {
01847   QString aResName;
01848 
01849   _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
01850   int i = 1;
01851   QString aPrefix = tr( myIsMesh ? "SMESH_OBJECT_MESH" : "SMESH_SUBMESH" ) + "_";
01852   _PTR(SObject) anObj;
01853   do
01854   {
01855     aResName = aPrefix + QString::number( i++ );
01856     anObj = aStudy->FindObject( aResName.toLatin1().data() );
01857   }
01858   while ( anObj );
01859 
01860   QLineEdit* aControl = ( QLineEdit* )myDlg->objectWg(
01861     SMESHGUI_MeshDlg::Obj, SMESHGUI_MeshDlg::Control );
01862   aControl->setText( aResName );
01863 }
01864 
01865 //================================================================================
01873 //================================================================================
01874 SMESH::SMESH_Hypothesis_var SMESHGUI_MeshOp::getAlgo( const int theDim )
01875 {
01876   SMESH::SMESH_Hypothesis_var anAlgoVar;
01877 
01878   // get type of the selected algo
01879   int aHypIndex = currentHyp( theDim, Algo );
01880   THypDataList& dataList = myAvailableHypData[ theDim ][ Algo ];
01881   if ( aHypIndex < 0 || aHypIndex >= dataList.count())
01882     return anAlgoVar;
01883   QString aHypName = dataList[ aHypIndex ]->TypeName;
01884 
01885   // get existing algoritms
01886   _PTR(SObject) pObj = SMESH::GetActiveStudyDocument()->FindComponent("SMESH");
01887   QStringList tmp;
01888   existingHyps( theDim, Algo, pObj, tmp, myExistingHyps[ theDim ][ Algo ]);
01889 
01890   // look for an existing algo of such a type
01891   THypList& aHypVarList = myExistingHyps[ theDim ][ Algo ];
01892   THypList::iterator anIter = aHypVarList.begin();
01893   for ( ; anIter != aHypVarList.end(); anIter++)
01894   {
01895     SMESH::SMESH_Hypothesis_var aHypVar = (*anIter).first;
01896     CORBA::String_var aName = aHypVar->GetName();
01897     if ( !aHypVar->_is_nil() && !strcmp(aHypName.toLatin1().data(), aName) )
01898     {
01899       anAlgoVar = aHypVar;
01900       break;
01901     }
01902   }
01903 
01904   if (anAlgoVar->_is_nil())
01905   {
01906     HypothesisData* aHypData = SMESH::GetHypothesisData( aHypName );
01907     if (aHypData)
01908     {
01909       QString aClientLibName = aHypData->ClientLibName;
01910       if (aClientLibName == "")
01911       {
01912         // Call hypothesis creation server method (without GUI)
01913         SMESH::SMESH_Hypothesis_var aHyp =
01914           SMESH::CreateHypothesis(aHypName, aHypName, true);
01915 #ifdef WITHGENERICOBJ
01916         if (!CORBA::is_nil(aHyp))
01917           aHyp->UnRegister();
01918 #endif
01919       }
01920       else
01921       {
01922         // Get hypotheses creator client (GUI)
01923         // BUG 0020378
01924         SMESHGUI_GenericHypothesisCreator* aCreator = SMESH::GetHypothesisCreator(aHypName);
01925 
01926         // Create algorithm
01927         if (aCreator)
01928           aCreator->create(true, aHypName, myDlg, 0, QString::null );
01929         else {
01930           SMESH::SMESH_Hypothesis_var aHyp =
01931             SMESH::CreateHypothesis(aHypName, aHypName, true);
01932 #ifdef WITHGENERICOBJ
01933           if (!CORBA::is_nil(aHyp))
01934             aHyp->UnRegister();
01935 #endif
01936         }
01937       }
01938       QStringList tmpList;
01939       _PTR(SComponent) aFather = SMESH::GetActiveStudyDocument()->FindComponent( "SMESH" );
01940       existingHyps( theDim, Algo, aFather, tmpList, myExistingHyps[ theDim ][ Algo ] );
01941     }
01942 
01943     THypList& aNewHypVarList = myExistingHyps[ theDim ][ Algo ];
01944     for ( anIter = aNewHypVarList.begin(); anIter != aNewHypVarList.end(); ++anIter )
01945     {
01946       SMESH::SMESH_Hypothesis_var aHypVar = (*anIter).first;
01947       CORBA::String_var aName = aHypVar->GetName();
01948       if ( !aHypVar->_is_nil() && !strcmp(aHypName.toLatin1().data(), aName) )
01949       {
01950         anAlgoVar = aHypVar;
01951         break;
01952       }
01953     }
01954   }
01955 
01956   return anAlgoVar._retn();
01957 }
01958 
01959 //================================================================================
01966 //================================================================================
01967 void SMESHGUI_MeshOp::readMesh()
01968 {
01969   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
01970   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
01971   if ( !pObj )
01972     return;
01973 
01974   if (myIsOnGeometry) {
01975     // Get name of mesh if current object is sub-mesh
01976     SMESH::SMESH_subMesh_var aSubMeshVar =
01977       SMESH::SMESH_subMesh::_narrow( _CAST( SObject,pObj )->GetObject() );
01978     if ( !aSubMeshVar->_is_nil() )
01979     {
01980       SMESH::SMESH_Mesh_var aMeshVar =  aSubMeshVar->GetFather();
01981       if ( !aMeshVar->_is_nil() )
01982       {
01983         _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshVar );
01984         QString aMeshName = name( aMeshSO );
01985         myDlg->setObjectText( SMESHGUI_MeshDlg::Mesh, aMeshName );
01986       }
01987     }
01988 
01989     // Get name of geometry object
01990     GEOM::GEOM_Object_var aGeomVar = SMESH::GetShapeOnMeshOrSubMesh( pObj );
01991     if ( !aGeomVar->_is_nil() )
01992     {
01993       _PTR(SObject) aGeomSO = studyDS()->FindObjectID( aGeomVar->GetStudyEntry() );
01994       QString aShapeName = name( aGeomSO );
01995       myDlg->setObjectText( SMESHGUI_MeshDlg::Geom, aShapeName );
01996     }
01997   }
01998 
01999   // Get hypotheses and algorithms assigned to the mesh/sub-mesh
02000   QStringList anExisting;
02001   const int aDim = ( myIsOnGeometry ) ? SMESH::DIM_0D : SMESH::DIM_3D;
02002   for ( int dim = aDim; dim <= SMESH::DIM_3D; dim++ )
02003   {
02004     // get algorithm
02005     existingHyps( dim, Algo, pObj, anExisting, myObjHyps[ dim ][ Algo ] );
02006     // find algo index among available ones
02007     int aHypIndex = -1;
02008     if ( myObjHyps[ dim ][ Algo ].count() > 0 )
02009     {
02010       SMESH::SMESH_Hypothesis_var aVar = myObjHyps[ dim ][ Algo ].first().first;
02011       HypothesisData* algoData = SMESH::GetHypothesisData( aVar->GetName() );
02012       aHypIndex = myAvailableHypData[ dim ][ Algo ].indexOf ( algoData );
02013 //       if ( aHypIndex < 0 && algoData ) {
02014 //         // assigned algo is incompatible with other algorithms
02015 //         myAvailableHypData[ dim ][ Algo ].push_back( algoData );
02016 //         aHypIndex = myAvailableHypData[ dim ][ hypType ].count() - 1;
02017 //       }
02018     }
02019     setCurrentHyp( dim, Algo, aHypIndex );
02020     // set existing and available hypothesis according to the selected algo
02021     onAlgoSelected( aHypIndex, dim );
02022   }
02023 
02024   // get hypotheses
02025   bool hypWithoutAlgo = false;
02026   for ( int dim = aDim; dim <= SMESH::DIM_3D; dim++ )
02027   {
02028     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
02029     {
02030       // get hypotheses
02031       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
02032       // find index of requered hypothesis among existing ones for this dimension and type
02033       int aHypIndex = -1;
02034       if ( myObjHyps[ dim ][ hypType ].count() > 0 ) {
02035         aHypIndex = find( myObjHyps[ dim ][ hypType ].first().first,
02036                           myExistingHyps[ dim ][ hypType ] );
02037         if ( aHypIndex < 0 ) {
02038           // assigned hypothesis is incompatible with the algorithm
02039           if ( currentHyp( dim, Algo ) < 0 )
02040           { // none algo selected; it is edition for sure, of submesh maybe
02041             hypWithoutAlgo = true;
02042             myExistingHyps[ dim ][ hypType ].push_back( myObjHyps[ dim ][ hypType ].first() );
02043             aHypIndex = myExistingHyps[ dim ][ hypType ].count() - 1;
02044             myDlg->tab( dim )->setExistingHyps( hypType, anExisting );
02045           }
02046         }
02047       }
02048       setCurrentHyp( dim, hypType, aHypIndex );
02049     }
02050   }
02051   // make available other hyps of same type as one without algo
02052   if ( hypWithoutAlgo )
02053     onAlgoSelected( currentHyp( 0, Algo ), 0 );
02054 }
02055 
02056 //================================================================================
02064 //================================================================================
02065 QString SMESHGUI_MeshOp::name( _PTR(SObject) theSO ) const
02066 {
02067   QString aResName;
02068   if ( theSO )
02069   {
02070     _PTR(GenericAttribute) anAttr;
02071     _PTR(AttributeName)    aNameAttr;
02072     if ( theSO->FindAttribute( anAttr, "AttributeName" ) )
02073     {
02074       aNameAttr = anAttr;
02075       aResName = aNameAttr->Value().c_str();
02076     }
02077   }
02078   return aResName;
02079 }
02080 
02081 //================================================================================
02090 //================================================================================
02091 int SMESHGUI_MeshOp::find( const SMESH::SMESH_Hypothesis_var& theHyp,
02092                            const THypList& theHypList ) const
02093 {
02094   int aRes = -1;
02095   if ( !theHyp->_is_nil() )
02096   {
02097     int i = 0;
02098     THypList::const_iterator anIter = theHypList.begin();
02099     for ( ; anIter != theHypList.end(); ++ anIter)
02100     {
02101       if ( theHyp->_is_equivalent( (*anIter).first ) )
02102       {
02103         aRes = i;
02104         break;
02105       }
02106       i++;
02107     }
02108   }
02109   return aRes;
02110 }
02111 
02112 //================================================================================
02120 //================================================================================
02121 bool SMESHGUI_MeshOp::editMeshOrSubMesh( QString& theMess )
02122 {
02123   theMess = "";
02124 
02125   SMESH::SMESH_Gen_var aSMESHGen = SMESHGUI::GetSMESHGen();
02126   if ( aSMESHGen->_is_nil() )
02127     return false;
02128 
02129   QString anObjEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Obj );
02130   _PTR(SObject) pObj = studyDS()->FindObjectID( anObjEntry.toLatin1().data() );
02131   if ( !pObj )
02132     return false;
02133 
02134   SUIT_OverrideCursor aWaitCursor;
02135 
02136   // Set new name
02137   QString aName = myDlg->objectText( SMESHGUI_MeshDlg::Obj );
02138   SMESH::SetName( pObj, aName );
02139   int aDim = ( myIsOnGeometry ) ? SMESH::DIM_0D : SMESH::DIM_3D;
02140 
02141   // First, remove old algos in order to avoid messages on algorithm hiding
02142   for ( int dim = aDim; dim <= SMESH::DIM_3D; dim++ )
02143   {
02144     if ( isAccessibleDim( dim ) && myObjHyps[ dim ][ Algo ].count() > 0 )
02145     {
02146       SMESH::SMESH_Hypothesis_var anOldAlgo = myObjHyps[ dim ][ Algo ].first().first;
02147       SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( dim );
02148       if ( anAlgoVar->_is_nil() || // no new algo selected or
02149            strcmp(anOldAlgo->GetName(), anAlgoVar->GetName()) ) // algo change
02150       {
02151         // remove old algorithm
02152         SMESH::RemoveHypothesisOrAlgorithmOnMesh ( pObj, myObjHyps[ dim ][ Algo ].first().first );
02153         myObjHyps[ dim ][ Algo ].clear();
02154       }
02155     }
02156   }
02157 
02158   // Assign new algorithms and hypotheses
02159   for ( int dim = aDim; dim <= SMESH::DIM_3D; dim++ )
02160   {
02161     if ( !isAccessibleDim( dim )) continue;
02162 
02163     // find or create algorithm
02164     SMESH::SMESH_Hypothesis_var anAlgoVar = getAlgo( dim );
02165 
02166     // assign new algorithm
02167     if ( !anAlgoVar->_is_nil() && // some algo selected and
02168          myObjHyps[ dim ][ Algo ].count() == 0 ) // no algo assigned
02169     {
02170       SALOMEDS_SObject* aSObject = _CAST(SObject, pObj);
02171       CORBA::Object_var anObject = aSObject->GetObject();
02172       SMESH::SMESH_Mesh_var aMeshVar = SMESH::SMESH_Mesh::_narrow( anObject );
02173       bool isMesh = !aMeshVar->_is_nil();
02174       if ( isMesh ) {
02175         SMESH::AddHypothesisOnMesh( aMeshVar, anAlgoVar );
02176       } else {
02177         SMESH::SMESH_subMesh_var aVar =
02178           SMESH::SMESH_subMesh::_narrow( _CAST(SObject,pObj)->GetObject() );
02179         if ( !aVar->_is_nil() )
02180           SMESH::AddHypothesisOnSubMesh( aVar, anAlgoVar );
02181       }
02182       myObjHyps[ dim ][ Algo ].append( THypItem( anAlgoVar, aName) );
02183     }
02184 
02185     // assign hypotheses
02186     for ( int hypType = MainHyp; hypType <= AddHyp; hypType++ )
02187     {
02188       int aNewHypIndex = currentHyp( dim, hypType );
02189       int anOldHypIndex = -1;
02190 
02191       // remove old hypotheses
02192       if ( myObjHyps[ dim ][ hypType ].count() > 0 )
02193       {
02194         anOldHypIndex = find( myObjHyps[ dim ][ hypType ].first().first,
02195                               myExistingHyps[ dim ][ hypType ] );
02196         if ( aNewHypIndex != anOldHypIndex || // different hyps
02197              anOldHypIndex == -1 )            // hyps of different algos
02198         {
02199           SMESH::RemoveHypothesisOrAlgorithmOnMesh
02200             ( pObj, myObjHyps[ dim ][ hypType ].first().first );
02201           myObjHyps[ dim ][ hypType ].clear();
02202         }
02203       }
02204 
02205       // assign new hypotheses
02206       if ( aNewHypIndex != anOldHypIndex && aNewHypIndex != -1 )
02207       {
02208         SMESH::SMESH_Mesh_var aMeshVar =
02209           SMESH::SMESH_Mesh::_narrow( _CAST(SObject,pObj)->GetObject() );
02210         bool isMesh = !aMeshVar->_is_nil();
02211         if ( isMesh )
02212         {
02213           SMESH::AddHypothesisOnMesh
02214             (aMeshVar, myExistingHyps[ dim ][ hypType ][ aNewHypIndex ].first );
02215         }
02216         else
02217         {
02218           SMESH::SMESH_subMesh_var aVar =
02219             SMESH::SMESH_subMesh::_narrow( _CAST(SObject,pObj)->GetObject() );
02220           if ( !aVar->_is_nil() )
02221             SMESH::AddHypothesisOnSubMesh
02222               ( aVar, myExistingHyps[ dim ][ hypType ][ aNewHypIndex ].first );
02223         }
02224       }
02225       // reread all hypotheses of mesh if necessary
02226       QStringList anExisting;
02227       existingHyps( dim, hypType, pObj, anExisting, myObjHyps[ dim ][ hypType ] );
02228     }
02229   }
02230 
02231   return true;
02232 }
02233 
02234 //================================================================================
02245 //================================================================================
02246 bool SMESHGUI_MeshOp::isValid( SUIT_Operation* theOp ) const
02247 {
02248   return SMESHGUI_Operation::isValid( theOp ) && !theOp->inherits( "SMESHGUI_MeshOp" );
02249 }
02250 
02251 //================================================================================
02256 //================================================================================
02257 void SMESHGUI_MeshOp::onGeomSelectionByMesh( bool theByMesh )
02258 {
02259   if ( theByMesh ) {
02260     if ( !myShapeByMeshOp ) {
02261       myShapeByMeshOp = new SMESHGUI_ShapeByMeshOp();
02262       connect(myShapeByMeshOp, SIGNAL(committed(SUIT_Operation*)),
02263               SLOT(onPublishShapeByMeshDlg(SUIT_Operation*)));
02264       connect(myShapeByMeshOp, SIGNAL(aborted(SUIT_Operation*)),
02265               SLOT(onCloseShapeByMeshDlg(SUIT_Operation*)));
02266     }
02267     // set mesh object to SMESHGUI_ShapeByMeshOp and start it
02268     QString aMeshEntry = myDlg->selectedObject( SMESHGUI_MeshDlg::Mesh );
02269     if ( _PTR(SObject) pMesh = studyDS()->FindObjectID( aMeshEntry.toLatin1().data() )) {
02270       SMESH::SMESH_Mesh_var aMeshVar =
02271         SMESH::SMESH_Mesh::_narrow( _CAST( SObject,pMesh )->GetObject() );
02272       if ( !aMeshVar->_is_nil() ) {
02273         myDlg->hide(); // stop processing selection
02274         myShapeByMeshOp->setModule( getSMESHGUI() );
02275         myShapeByMeshOp->setStudy( 0 ); // it's really necessary
02276         myShapeByMeshOp->SetMesh( aMeshVar );
02277         myShapeByMeshOp->start();
02278       }
02279     }
02280   }
02281 }
02282 
02283 //================================================================================
02287 //================================================================================
02288 void SMESHGUI_MeshOp::onPublishShapeByMeshDlg(SUIT_Operation* op)
02289 {
02290   if ( myShapeByMeshOp == op ) {
02291     SMESHGUI::GetSMESHGUI()->getApp()->updateObjectBrowser(); //MZN: 24.11.2006  IPAL13980 - Object Browser update added
02292     myDlg->show();
02293     // Select a found geometry object
02294     GEOM::GEOM_Object_var aGeomVar = myShapeByMeshOp->GetShape();
02295     if ( !aGeomVar->_is_nil() )
02296     {
02297       QString ID = aGeomVar->GetStudyEntry();
02298       if ( _PTR(SObject) aGeomSO = studyDS()->FindObjectID( ID.toLatin1().data() )) {
02299         selectObject( aGeomSO );
02300         selectionDone();
02301       }
02302     }
02303   }
02304 }
02305 
02306 //================================================================================
02310 //================================================================================
02311 void SMESHGUI_MeshOp::onCloseShapeByMeshDlg(SUIT_Operation* op)
02312 {
02313   if ( myShapeByMeshOp == op && myDlg ) {
02314     myDlg->show();
02315   }
02316 }
02317 
02318 //================================================================================
02323 //================================================================================
02324 void SMESHGUI_MeshOp::selectObject( _PTR(SObject) theSObj ) const
02325 {
02326   if ( LightApp_SelectionMgr* sm = selectionMgr() ) {
02327     SALOME_ListIO anIOList;
02328     if ( theSObj ) {
02329       Handle(SALOME_InteractiveObject) anIO = new SALOME_InteractiveObject
02330         ( theSObj->GetID().c_str(), "SMESH", theSObj->GetName().c_str() );
02331       anIOList.Append( anIO );
02332     }
02333     sm->setSelectedObjects( anIOList, false );
02334   }
02335 }
Copyright © 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
Copyright © 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS