Salome HOME
IMP 001989: multi selection in "Import" dialog boxes
[modules/smesh.git] / src / SMESHGUI / SMESHGUI.cxx
index 73d08b61f2050757eb6f3d567b3186de37ba8ae3..78f79cd15915d1fbed2ae7d50896a241507a0023 100644 (file)
@@ -68,6 +68,7 @@
 #include "SMESHGUI_MakeNodeAtPointDlg.h"
 #include "SMESHGUI_BuildCompoundDlg.h"
 #include "SMESHGUI_ComputeDlg.h"
+#include "SMESHGUI_FileInfoDlg.h"
 
 #include "SMESHGUI_Utils.h"
 #include "SMESHGUI_GEOMGenUtils.h"
 
 #include "SALOMEconfig.h"
 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
-#include CORBA_SERVER_HEADER(SMESH_MeshEditor)
+#include CORBA_CLIENT_HEADER(SMESH_MeshEditor)
 
 // QT Includes
 #define         INCLUDE_MENUITEM_DEF
@@ -168,93 +169,113 @@ using namespace std;
 
   // Definitions
   //=============================================================
-  void ImportMeshesFromFile(SMESH::SMESH_Gen_ptr theComponentMesh,
-                           int theCommandID)
+  void ImportMeshesFromFile( SMESH::SMESH_Gen_ptr theComponentMesh,
+                            int theCommandID )
   {
     QStringList filter;
     string myExtension;
 
-    if(theCommandID == 113){
-      filter.append(QObject::tr("MED files (*.med)"));
-      filter.append(QObject::tr("All files (*)"));
-    }else if (theCommandID == 112){
-      filter.append(QObject::tr("IDEAS files (*.unv)"));
-    }else if (theCommandID == 111){
-      filter.append(QObject::tr("DAT files (*.dat)"));
+    if ( theCommandID == 113 ) {
+      filter.append( QObject::tr( "MED files (*.med)" ) );
+      filter.append( QObject::tr( "All files (*)" ) );
+    }
+    else if ( theCommandID == 112 ) {
+      filter.append( QObject::tr( "IDEAS files (*.unv)" ) );
+    }
+    else if ( theCommandID == 111 ) {
+      filter.append( QObject::tr( "DAT files (*.dat)" ) );
     }
 
     QString anInitialPath = "";
-    //if ( SUIT_FileDlg::getLastVisitedPath().isEmpty() )
-    //  anInitialPath = QDir::currentDirPath();
-
-    QString filename = SUIT_FileDlg::getFileName(SMESHGUI::desktop(),
-                                                anInitialPath,
-                                                filter,
-                                                QObject::tr("Import mesh"),
-                                                true);
-    if(!filename.isEmpty()) {
+    if ( SUIT_FileDlg::getLastVisitedPath().isEmpty() )
+      anInitialPath = QDir::currentDirPath();
+
+    QStringList filenames = SUIT_FileDlg::getOpenFileNames( SMESHGUI::desktop(),
+                                                           anInitialPath,
+                                                           filter,
+                                                           QObject::tr( "SMESH_IMPORT_MESH" ) );
+    if ( filenames.count() > 0 ) {
       SUIT_OverrideCursor wc;
       _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
 
-      try {
+      QStringList errors;
+      bool isEmpty = false;
+      for ( QStringList::ConstIterator it = filenames.begin(); it != filenames.end(); ++it ) {
+       QString filename = *it;
        SMESH::mesh_array_var aMeshes = new SMESH::mesh_array;
-       switch ( theCommandID ) {
-       case 112:
-         {
-           aMeshes->length( 1 );
-           aMeshes[0] = theComponentMesh->CreateMeshesFromUNV(filename.latin1());
-           break;
-         }
-       case 113:
-         {
-           SMESH::DriverMED_ReadStatus res;
-           aMeshes = theComponentMesh->CreateMeshesFromMED(filename.latin1(),res);
-           if ( res != SMESH::DRS_OK ) {
-             wc.suspend();
-             SUIT_MessageBox::warn1(SMESHGUI::desktop(),
-                                     QObject::tr("SMESH_WRN_WARNING"),
-                                     QObject::tr(QString("SMESH_DRS_%1").arg(res)),
-                                     QObject::tr("SMESH_BUT_OK"));
-             aMeshes->length( 0 );
-             wc.resume();
+       try {
+         switch ( theCommandID ) {
+         case 111:
+           {
+             // DAT format (currently unsupported)
+             errors.append( QString( "%1 :\n\t%2" ).arg( filename ).
+                            arg( QObject::tr( "SMESH_ERR_NOT_SUPPORTED_FORMAT" ) ) );
+             break;
+           }
+         case 112:
+           {
+             // UNV format
+             aMeshes->length( 1 );
+             aMeshes[0] = theComponentMesh->CreateMeshesFromUNV( filename.latin1() );
+             if ( aMeshes[0]->_is_nil() )
+               errors.append( QString( "%1 :\n\t%2" ).arg( filename ).
+                              arg( QObject::tr( "SMESH_ERR_UNKNOWN_IMPORT_ERROR" ) ) );
+             break;
+           }
+         case 113:
+           {
+             // MED format
+             SMESH::DriverMED_ReadStatus res;
+             aMeshes = theComponentMesh->CreateMeshesFromMED( filename.latin1(), res );
+             if ( res != SMESH::DRS_OK ) {
+               errors.append( QString( "%1 :\n\t%2" ).arg( filename ).
+                              arg( QObject::tr( QString( "SMESH_DRS_%1" ).arg( res ) ) ) );
+             }
+             break;
            }
-           break;
          }
        }
+       catch ( const SALOME::SALOME_Exception& S_ex ) {
+         errors.append( QString( "%1 :\n\t%2" ).arg( filename ).
+                        arg( QObject::tr( "SMESH_ERR_UNKNOWN_IMPORT_ERROR" ) ) );
+       }
 
-       bool isEmpty = false;
        for ( int i = 0, iEnd = aMeshes->length(); i < iEnd; i++ ) {
          _PTR(SObject) aMeshSO = SMESH::FindSObject( aMeshes[i] );
          if ( aMeshSO ) {
            _PTR(StudyBuilder) aBuilder = aStudy->NewBuilder();
            _PTR(AttributePixMap) aPixmap = aBuilder->FindOrCreateAttribute( aMeshSO, "AttributePixMap" );
-           aPixmap->SetPixMap("ICON_SMESH_TREE_MESH_IMPORTED");
+           aPixmap->SetPixMap( "ICON_SMESH_TREE_MESH_IMPORTED" );
            if ( theCommandID == 112 ) // mesh names aren't taken from the file for UNV import
              SMESH::SetName( aMeshSO, QFileInfo(filename).fileName() );
-         } else
+         }
+         else {
            isEmpty = true;
+         }
        }
+      }
 
-       if ( isEmpty ) {
-         wc.suspend();
-         SUIT_MessageBox::warn1(SMESHGUI::desktop(),
-                                 QObject::tr("SMESH_WRN_WARNING"),
-                                 QObject::tr("SMESH_DRS_EMPTY"),
-                                 QObject::tr("SMESH_BUT_OK"));
-         wc.resume();
-       }
-
-       SMESHGUI::GetSMESHGUI()->updateObjBrowser();
+      // update Object browser
+      SMESHGUI::GetSMESHGUI()->updateObjBrowser();
+      
+      // show Error message box if there were errors
+      if ( errors.count() > 0 ) {
+       SUIT_MessageBox::error1( SMESHGUI::desktop(),
+                                QObject::tr( "SMESH_ERROR" ),
+                                QObject::tr( "SMESH_IMPORT_ERRORS" ) + "\n" + errors.join( "\n" ),
+                                QObject::tr( "SMESH_BUT_OK" ) );
       }
-      catch (const SALOME::SALOME_Exception& S_ex){
-       wc.suspend();
-       SalomeApp_Tools::QtCatchCorbaException(S_ex);
-       wc.resume();
+      
+      // show warning message box, if some imported mesh is empty
+      if ( isEmpty ) {
+         SUIT_MessageBox::warn1( SMESHGUI::desktop(),
+                                 QObject::tr( "SMESH_WRN_WARNING" ),
+                                 QObject::tr( "SMESH_DRS_SOME_EMPTY" ),
+                                 QObject::tr( "SMESH_BUT_OK" ) );
       }
     }
   }
 
-
   void ExportMeshToFile( int theCommandID )
   {
     LightApp_SelectionMgr *aSel = SMESHGUI::selectionMgr();
@@ -353,10 +374,13 @@ using namespace std;
        if ( resMgr )
          toCreateGroups = resMgr->booleanValue( "SMESH", "auto_groups", false );
 
-       if ( theCommandID != 122 && theCommandID != 125 && theCommandID != 140 && theCommandID != 141)
-
-         aFilename = SUIT_FileDlg::getFileName(SMESHGUI::desktop(), "", aFilter, aTitle, false);
+       QString anInitialPath = "";
+       if ( SUIT_FileDlg::getLastVisitedPath().isEmpty() )
+         anInitialPath = QDir::currentDirPath();
 
+       if ( theCommandID != 122 && theCommandID != 125 && theCommandID != 140 && theCommandID != 141)
+         aFilename = SUIT_FileDlg::getFileName(SMESHGUI::desktop(), anInitialPath + QString("/") + anIObject->getName(),
+                                               aFilter, aTitle, false);
        else if(theCommandID == 140 || theCommandID == 141) { // Export to STL
          QStringList filters;
           QMap<QString, int>::const_iterator it = aFilterMapSTL.begin();
@@ -367,6 +391,9 @@ using namespace std;
           fd->setCaption( aTitle );
           fd->setFilters( filters );
           fd->setSelectedFilter( QObject::tr("STL ASCII  (*.stl)") );
+         if ( !anInitialPath.isEmpty() )
+           fd->setDir( anInitialPath );
+         fd->setSelection(anIObject->getName());
           bool is_ok = false;
           while (!is_ok) {
             fd->exec();
@@ -394,6 +421,9 @@ using namespace std;
           //fd->setSelectedFilter( QObject::tr("MED 2.2 (*.med)") );
           fd->setSelectedFilter(aDefaultFilter);
           fd->SetChecked(toCreateGroups);
+         if ( !anInitialPath.isEmpty() )
+           fd->setDir( anInitialPath );
+         fd->setSelection(anIObject->getName());
           bool is_ok = false;
           while (!is_ok) {
             fd->exec();
@@ -876,9 +906,23 @@ using namespace std;
     LightApp_SelectionMgr* aSel = SMESHGUI::selectionMgr();
     SALOME_ListIO selected; aSel->selectedObjects( selected, QString::null, false );
 
+    _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
+    _PTR(StudyBuilder) aStudyBuilder = aStudy->NewBuilder();
+    _PTR(GenericAttribute) anAttr;
+    _PTR(AttributeIOR) anIOR;
+
+    int objectCount = 0;
     QString aParentComponent = QString::null;
     for( SALOME_ListIteratorOfListIO anIt( selected ); anIt.More(); anIt.Next() )
     {
+      _PTR(SObject) aSO = aStudy->FindObjectID(anIt.Value()->getEntry());
+      if (aSO) {
+       // check if object is not reference
+       _PTR(SObject) refobj;
+       if ( !aSO->ReferencedObject( refobj ) )
+         objectCount++;
+      }
+
       QString cur = anIt.Value()->getComponentDataType();
       if( aParentComponent.isNull() )
         aParentComponent = cur;
@@ -886,6 +930,9 @@ using namespace std;
         aParentComponent = "";
     }
 
+    if ( objectCount == 0 )
+      return; // No Valid Objects Selected
+
     if ( aParentComponent != SMESHGUI::GetSMESHGUI()->name() )  {
       SUIT_MessageBox::warn1 ( SMESHGUI::desktop(),
                              QObject::tr("ERR_ERROR"),
@@ -905,11 +952,6 @@ using namespace std;
     SUIT_ViewManager* vm = anApp->activeViewManager();
     int nbSf = vm->getViewsCount();
 
-    _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
-    _PTR(StudyBuilder) aStudyBuilder = aStudy->NewBuilder();
-    _PTR(GenericAttribute) anAttr;
-    _PTR(AttributeIOR) anIOR;
-
     SALOME_ListIteratorOfListIO It(selected);
 
     aStudyBuilder->NewCommand();  // There is a transaction
@@ -925,6 +967,10 @@ using namespace std;
            continue;
        }
 
+       _PTR(SObject) refobj;
+       if ( aSO && aSO->ReferencedObject( refobj ) )
+         continue; // skip references 
+
         // put the whole hierarchy of sub-objects of the selected SO into a list and
         // then treat them all starting from the deepest objects (at list back)
 
@@ -1302,6 +1348,24 @@ bool SMESHGUI::OnGUIEvent( int theCommandID )
       break;
     }
 
+  case 150:    //MED FILE INFORMATION
+    {
+      SALOME_ListIO selected;
+      LightApp_SelectionMgr *aSel = SMESHGUI::selectionMgr();
+      if( aSel )
+        aSel->selectedObjects( selected );
+      if( selected.Extent() )
+      {
+        Handle(SALOME_InteractiveObject) anIObject = selected.First();
+        SMESH::SMESH_Mesh_var aMesh = SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(anIObject);
+        if ( !aMesh->_is_nil() )
+        {
+          SMESHGUI_FileInfoDlg dlg( desktop(), aMesh->GetMEDFileInfo() );
+          dlg.exec();
+        }
+      }
+      break;
+    }
   case 122:                                    // EXPORT MED
   case 121:
   case 123:
@@ -1513,6 +1577,7 @@ bool SMESHGUI::OnGUIEvent( int theCommandID )
       new SMESHGUI_BuildCompoundDlg( this );
     }
     break;
+
   case 407: // DIAGONAL INVERSION
   case 408: // Delete diagonal
     {
@@ -2143,6 +2208,46 @@ bool SMESHGUI::OnGUIEvent( int theCommandID )
        }
       break;
     }
+  case 4043: {                                // CLEAR_MESH
+
+    if(checkLock(aStudy)) break;
+    
+    SALOME_ListIO selected;
+    if( LightApp_SelectionMgr *aSel = SMESHGUI::selectionMgr() )
+      aSel->selectedObjects( selected );
+
+    SUIT_OverrideCursor wc;
+    SALOME_ListIteratorOfListIO It (selected);
+    for ( ; It.More(); It.Next() )
+    {
+      Handle(SALOME_InteractiveObject) IOS = It.Value();
+      SMESH::SMESH_Mesh_var aMesh =
+        SMESH::IObjectToInterface<SMESH::SMESH_Mesh>(IOS);
+      if ( aMesh->_is_nil()) continue;
+      try {
+        SMESH::UpdateView(SMESH::eErase, IOS->getEntry());
+        aMesh->Clear();
+        _PTR(SObject) aMeshSObj = SMESH::FindSObject(aMesh);
+        SMESH::ModifiedMesh( aMeshSObj, false, true);
+        // hide groups and submeshes
+        _PTR(ChildIterator) anIter =
+          SMESH::GetActiveStudyDocument()->NewChildIterator( aMeshSObj );
+        for ( anIter->InitEx(true); anIter->More(); anIter->Next() )
+        {
+          _PTR(SObject) so = anIter->Value();
+          SMESH::UpdateView(SMESH::eErase, so->GetID().c_str());
+        }
+      }
+      catch (const SALOME::SALOME_Exception& S_ex){
+       wc.suspend();
+       SalomeApp_Tools::QtCatchCorbaException(S_ex);
+       wc.resume();
+      }
+    }
+    SMESH::UpdateView();
+    updateObjBrowser();
+    break;
+  }
   case 4051:                                   // RENUMBERING NODES
     {
       if(checkLock(aStudy)) break;
@@ -2491,6 +2596,7 @@ void SMESHGUI::initialize( CAM_Application* app )
   createSMESHAction(  125, "EXPORT_MED" );
   createSMESHAction(  126, "EXPORT_UNV" );
   createSMESHAction(  141, "EXPORT_STL" );
+  createSMESHAction(  150, "FILE_INFO" );
   createSMESHAction(   33, "DELETE",          "ICON_DELETE", Key_Delete );
   createSMESHAction( 5105, "SEL_FILTER_LIB" );
   createSMESHAction(  701, "COMPUTE",         "ICON_COMPUTE" );
@@ -2533,6 +2639,7 @@ void SMESHGUI::initialize( CAM_Application* app )
   createSMESHAction( 4032, "HEXA",            "ICON_DLG_HEXAS" );
   createSMESHAction( 4041, "REMOVE_NODES",    "ICON_DLG_REM_NODE" );
   createSMESHAction( 4042, "REMOVE_ELEMENTS", "ICON_DLG_REM_ELEMENT" );
+  createSMESHAction( 4043, "CLEAR_MESH"    ,  "ICON_CLEAR_MESH" );
   createSMESHAction( 4051, "RENUM_NODES",     "ICON_DLG_RENUMBERING_NODES" );
   createSMESHAction( 4052, "RENUM_ELEMENTS",  "ICON_DLG_RENUMBERING_ELEMENTS" );
   createSMESHAction( 4061, "TRANS",           "ICON_SMESH_TRANSLATION_VECTOR" );
@@ -2624,7 +2731,7 @@ void SMESHGUI::initialize( CAM_Application* app )
 
   createMenu( 5105, toolsId, -1 );
 
-  createMenu( 702, meshId, -1 );
+  createMenu( 702, meshId, -1 ); // "Mesh" menu
   createMenu( 703, meshId, -1 );
   createMenu( 704, meshId, -1 );
   createMenu( 710, meshId, -1 );
@@ -2683,6 +2790,7 @@ void SMESHGUI::initialize( CAM_Application* app )
 
   createMenu( 4041, removeId, -1 );
   createMenu( 4042, removeId, -1 );
+  createMenu( 4043, removeId, -1 );
 
   createMenu( 4051, renumId, -1 );
   createMenu( 4052, renumId, -1 );
@@ -2770,6 +2878,7 @@ void SMESHGUI::initialize( CAM_Application* app )
   createTool( separator(), addRemTb );
   createTool( 4041, addRemTb );
   createTool( 4042, addRemTb );
+  createTool( 4043, addRemTb );
   createTool( separator(), addRemTb );
   createTool( 4051, addRemTb );
   createTool( 4052, addRemTb );
@@ -2820,6 +2929,8 @@ void SMESHGUI::initialize( CAM_Application* app )
 
   // popup for object browser
 
+  createPopupItem( 150, OB, mesh, "&& selcount=1 && isImported" );      // FILE INFORMATION
+  
   createPopupItem( 704, OB, mesh, "&& isComputable");      // EDIT_MESHSUBMESH
   createPopupItem( 704, OB, subMesh, "&& isComputable" );  // EDIT_MESHSUBMESH
   createPopupItem( 803, OB, group );                       // EDIT_GROUP
@@ -2833,12 +2944,16 @@ void SMESHGUI::initialize( CAM_Application* app )
   createPopupItem( 801, OB, mesh );                        // CREATE_GROUP
   createPopupItem( 802, OB, subMesh );                     // CONSTRUCT_GROUP
   popupMgr()->insert( separator(), -1, 0 );
-  createPopupItem( 1100, OB, hypo, "" /*"&& $hasReference={false}"*/ );   // EDIT HYPOTHESIS
+  createPopupItem( 1100, OB, hypo);                        // EDIT HYPOTHESIS
   createPopupItem( 1102, OB, hyp_alg ); // REMOVE HYPOTHESIS / ALGORITHMS
-  createPopupItem( 1101, OB, mesh_group + " " + hyp_alg, "" /*"&& $hasReference={false}"*/ ); // RENAME
+  createPopupItem( 1101, OB, mesh_group + " " + hyp_alg ); // RENAME
+  popupMgr()->insert( separator(), -1, 0 );
+  createPopupItem( 4043, OB, mesh );                       // CLEAR_MESH
   popupMgr()->insert( separator(), -1, 0 );
 
-  QString only_one_non_empty = QString( " && %1=1 && numberOfNodes>0" ).arg( QtxPopupMgr::Selection::defSelCountParam() );
+  QString         nbSelected = QtxPopupMgr::Selection::defSelCountParam();
+  QString only_one_non_empty = QString( " && %1=1 && numberOfNodes>0" ).arg( nbSelected );
+
   createPopupItem( 125, OB, mesh, only_one_non_empty );   // EXPORT_MED
   createPopupItem( 126, OB, mesh, only_one_non_empty );   // EXPORT_UNV
   createPopupItem( 141, OB, mesh, only_one_non_empty );   // EXPORT_STL
@@ -3180,8 +3295,12 @@ void SMESHGUI::contextMenuPopup( const QString& client, QPopupMenu* menu, QStrin
     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( application()->activeStudy() );
     _PTR(Study) study = appStudy->studyDS();
     _PTR(SObject) obj = study->FindObjectID( io->getEntry() );
-    if ( obj )
-      title = QString( obj->GetName().c_str() );
+    if ( obj ) {
+      QString aName = QString( obj->GetName().c_str() );
+      while ( aName.at( aName.length() - 1 ) == ' ' ) // Remove extraspaces in Name of Popup
+         aName.remove( (aName.length() - 1), 1 );
+      title = aName;
+    }
   }
 }