Salome HOME
Merge from rnv/geom_plugin_imp branch
[plugins/acisplugin.git] / src / ACISPlugin_GUI.cxx
diff --git a/src/ACISPlugin_GUI.cxx b/src/ACISPlugin_GUI.cxx
new file mode 100644 (file)
index 0000000..f36893c
--- /dev/null
@@ -0,0 +1,284 @@
+// Copyright (C) 2014-2015  OPEN CASCADE
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+
+// internal includes
+#include "ACISPlugin_GUI.h"
+#include "ACISPlugin_IOperations_i.hh"
+
+// GUI includes
+#include <SUIT_Desktop.h>
+#include <SUIT_FileDlg.h>
+#include <SUIT_MessageBox.h>
+#include <SUIT_OverrideCursor.h>
+#include <SUIT_Tools.h>
+#include <LightApp_SelectionMgr.h>
+#include <SalomeApp_Application.h>
+#include <SalomeApp_Study.h>
+#include <SALOME_ListIO.hxx>
+
+// GEOM includes
+#include "GeometryGUI.h"
+#include "GEOM_Operation.h"
+#include "GEOMBase.h"
+#include "GEOM_Displayer.h"
+
+#include <SALOMEconfig.h>
+#include CORBA_SERVER_HEADER(ACISPlugin)
+
+//=======================================================================
+// function : ACISPlugin_GUI()
+// purpose  : Constructor
+//=======================================================================
+ACISPlugin_GUI::ACISPlugin_GUI( GeometryGUI* parent ) : GEOMPluginGUI( parent )
+{
+}
+
+//=======================================================================
+// function : ~ACISPlugin_GUI
+// purpose  : Destructor
+//=======================================================================
+ACISPlugin_GUI::~ACISPlugin_GUI()
+{
+}
+
+//=======================================================================
+// function : OnGUIEvent()
+// purpose  : 
+//=======================================================================
+bool ACISPlugin_GUI::OnGUIEvent( int theCommandID, SUIT_Desktop* parent )
+{
+  QString cmd;
+  switch ( theCommandID ) {
+  case 1:
+    cmd = "Export_ACIS";
+  case 2:
+    cmd = "Import_ACIS";
+  default:
+    break;
+  }
+  return OnGUIEvent( cmd, parent );
+}
+
+//=======================================================================
+// function : OnGUIEvent()
+// purpose  :
+//=======================================================================
+bool ACISPlugin_GUI::OnGUIEvent( const QString& theCommandID, SUIT_Desktop* parent )
+{
+  bool result = false;
+
+  if ( theCommandID == "Export_ACIS" )
+  {
+    result = exportACIS( parent );
+  }
+  else if ( theCommandID == "Import_ACIS" )
+  {
+    result = importACIS( parent );
+  }
+  else
+  {
+    getGeometryGUI()->getApp()->putInfo( tr("GEOM_PRP_COMMAND").arg( theCommandID ) );
+  }
+  return result;
+}
+
+//=======================================================================
+// function : importACIS
+// purpose  :
+//=======================================================================
+bool ACISPlugin_GUI::importACIS( SUIT_Desktop* parent )
+{
+  SalomeApp_Application* app = getGeometryGUI()->getApp();
+  if ( !app ) return false;
+  SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
+  if ( !study ) return false;
+
+  SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
+  GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "ACISPluginEngine" );
+  ACISPlugin::IACISOperations_var acisOp = ACISPlugin::IACISOperations::_narrow( op );
+  if ( CORBA::is_nil( acisOp ) ) return false;
+
+  QStringList fileNames = app->getOpenFileNames( SUIT_FileDlg::getLastVisitedPath().isEmpty() ? QDir::currentPath() : QString(""),
+                                                 tr( "ACIS_FILES" ),
+                                                 tr( "IMPORT_TITLE" ),
+                                                 parent );
+  if ( fileNames.count() > 0 )
+  {
+    QStringList entryList;
+    QStringList errors;
+
+    SUIT_MessageBox::StandardButton acisAnswer = SUIT_MessageBox::NoButton;
+
+    foreach( QString fileName, fileNames )
+    {
+      SUIT_OverrideCursor wc;
+      GEOM_Operation transaction( app, acisOp.in() );
+
+      try
+      {
+        app->putInfo( tr( "GEOM_PRP_LOADING" ).arg( fileName ) );
+        transaction.start();
+
+        if ( acisAnswer != SUIT_MessageBox::YesToAll && acisAnswer != SUIT_MessageBox::NoToAll ) {
+          SUIT_MessageBox::StandardButtons btns = SUIT_MessageBox::Yes | SUIT_MessageBox::No;
+          btns = btns | SUIT_MessageBox::YesToAll | SUIT_MessageBox::NoToAll;
+          acisAnswer = SUIT_MessageBox::question( app->desktop(),
+                                                  "Question",
+                                                  tr("GEOM_PUBLISH_NAMED_SHAPES"),
+                                                  btns | SUIT_MessageBox::Cancel,
+                                                  SUIT_MessageBox::No );
+          if ( acisAnswer == SUIT_MessageBox::Cancel ) {
+            acisOp->SetErrorCode(OK);
+            return true; // cancel (break) import operation
+          }
+        }
+        GEOM::ListOfGO_var result = acisOp->ImportACIS( fileName.toUtf8().constData() );
+
+        if ( result->length() > 0 && acisOp->IsDone() ) {
+          GEOM::GEOM_Object_var main = result[0];
+          QString publishName = GEOMBase::GetDefaultName( SUIT_Tools::file( fileName, true ) );
+          main.in()->SetName( publishName.toStdString().c_str() );
+          SALOMEDS::SObject_var so = GeometryGUI::GetGeomGen()->PublishInStudy( dsStudy,
+                                                                SALOMEDS::SObject::_nil(),
+                                                                main.in(),
+                                                                publishName.toUtf8().constData() );
+          if ( acisAnswer == SUIT_MessageBox::Yes || acisAnswer == SUIT_MessageBox::YesToAll )
+            GeometryGUI::GetGeomGen()->PublishNamedShapesInStudy( dsStudy, main.in() );
+
+          entryList.append( so->GetID() );
+          transaction.commit();
+        }
+        else
+        {
+          transaction.abort();
+          errors.append( QString( "%1 : %2" ).arg( fileName ).arg( acisOp->GetErrorCode() ) );
+        }
+      }
+      catch( const SALOME::SALOME_Exception& e )
+      {
+        transaction.abort();
+      }
+    }
+    getGeometryGUI()->updateObjBrowser( true );
+    app->browseObjects( entryList );
+
+    if ( errors.count() > 0 )
+    {
+      SUIT_MessageBox::critical( parent,
+                                tr( "GEOM_ERROR" ),
+                                tr( "GEOM_IMPORT_ERRORS" ) + "\n" + errors.join( "\n" ) );
+    }
+  }
+  return fileNames.count() > 0;
+}
+
+//=======================================================================
+// function : exportACIS
+// purpose  :
+//=======================================================================
+bool ACISPlugin_GUI::exportACIS( SUIT_Desktop* parent )
+{
+  SalomeApp_Application* app = getGeometryGUI()->getApp();
+  if ( !app ) return false;
+  SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
+  if ( !study ) return false;
+
+  SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
+  GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "ACISPluginEngine" );
+  ACISPlugin::IACISOperations_var acisOp = ACISPlugin::IACISOperations::_narrow( op );
+  if ( CORBA::is_nil( acisOp ) ) return false;
+
+  LightApp_SelectionMgr* sm = app->selectionMgr();
+  if ( !sm ) return false;
+
+  SALOME_ListIO selectedObjects;
+  sm->selectedObjects( selectedObjects );
+  bool ok = false;
+
+  SALOME_ListIteratorOfListIO it( selectedObjects );
+  for ( ; it.More(); it.Next() )
+  {
+    Handle(SALOME_InteractiveObject) io = it.Value();
+    GEOM::GEOM_Object_var obj = GEOMBase::ConvertIOinGEOMObject( io );
+
+    if ( CORBA::is_nil( obj ) ) continue;
+
+    QString fileName = app->getFileName( false,
+                                         QString( io->getName() ),
+                                         tr( "ACIS_FILES" ),
+                                         tr( "EXPORT_TITLE" ),
+                                         parent );
+
+    if ( fileName.isEmpty() )
+      return false;
+
+    SUIT_OverrideCursor wc;
+
+    GEOM_Operation transaction( app, acisOp.in() );
+
+    try
+    {
+      app->putInfo( tr( "GEOM_PRP_EXPORT" ).arg( fileName ) );
+      transaction.start();
+
+      acisOp->ExportACIS( obj, fileName.toUtf8().constData() );
+
+      if ( acisOp->IsDone() )
+      {
+        transaction.commit();
+      }
+      else
+      {
+        transaction.abort();
+        SUIT_MessageBox::critical( parent,
+                                   tr( "GEOM_ERROR" ),
+                                   tr( "GEOM_PRP_ABORT" ) + "\n" + tr( acisOp->GetErrorCode() ) );
+        return false;
+      }
+    }
+    catch ( const SALOME::SALOME_Exception& e )
+    {
+      transaction.abort();
+      return false;
+    }
+    ok = true;
+  }
+
+  if ( !ok )
+  {
+    SUIT_MessageBox::warning( parent,
+                              tr( "WRN_WARNING" ),
+                              tr( "GEOM_WRN_NO_APPROPRIATE_SELECTION" ) );
+  }
+  return ok;
+}
+
+//=====================================================================================
+// EXPORTED METHODS
+//=====================================================================================
+extern "C"
+{
+#ifdef WIN32
+    __declspec( dllexport )
+#endif
+  GEOMGUI* GetLibGUI( GeometryGUI* parent )
+  {
+    return new ACISPlugin_GUI( parent );
+  }
+}