Salome HOME
Implementation of DXFPLUGIN as a GEOM plugin (added files)
[plugins/dxfplugin.git] / src / DXFPlugin_ImportDlg.cxx
diff --git a/src/DXFPlugin_ImportDlg.cxx b/src/DXFPlugin_ImportDlg.cxx
new file mode 100644 (file)
index 0000000..ba69e45
--- /dev/null
@@ -0,0 +1,142 @@
+// Copyright (C) 2014  CEA/DEN, EDF R&D, 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 "DXFPlugin_ImportDlg.h"
+#include "DXFPlugin_Operations_i.hh"
+
+// GUI includes
+#include <SUIT_Session.h>
+#include <SUIT_MessageBox.h>
+#include <SUIT_Desktop.h>
+#include <SUIT_Tools.h>
+#include <SalomeApp_Application.h>
+
+// GEOM includes
+#include <GEOMBase.h>
+#include <GEOM_Operation.h>
+
+//=================================================================================
+// Constructor
+//=================================================================================
+DXFPlugin_ImportDlg::DXFPlugin_ImportDlg( QWidget* parent )
+: SUIT_FileDlg( parent, true, true, true ),
+  GEOMBase_Helper( dynamic_cast<SUIT_Desktop*>( parent ) )
+{
+  setWindowTitle( tr( "DXFPLUGIN_IMPORT_TITLE" ) );
+  setAcceptMode( AcceptOpen );
+  setNameFilter( tr( "DXFPLUGIN_DXF_FILES" ) );
+  setFileMode( QFileDialog::ExistingFiles );
+}
+
+//=================================================================================
+// Destructor
+//=================================================================================
+DXFPlugin_ImportDlg::~DXFPlugin_ImportDlg()
+{
+  // no need to delete child widgets, Qt does it all for us
+}
+
+//=================================================================================
+// function : createOperation
+// purpose  :
+//=================================================================================
+GEOM::GEOM_IOperations_ptr DXFPlugin_ImportDlg::createOperation()
+{
+  return getGeomEngine()->GetPluginOperations(getStudyId(), "DXFPluginEngine");
+}
+
+//=================================================================================
+// function : isValid
+// purpose  :
+//=================================================================================
+bool DXFPlugin_ImportDlg::isValid (QString& msg)
+{
+  bool ok = true;
+  //@@ add custom validation actions here @@//
+  return ok;
+}
+
+//=================================================================================
+// function : execute
+// purpose  :
+//=================================================================================
+bool DXFPlugin_ImportDlg::execute( ObjectList& objects )
+{
+  bool res = false;
+  GEOM::GEOM_Object_var anObj;
+  DXFPlugin::GEOM_IDXFPluginOperations_var anOper = DXFPlugin::GEOM_IDXFPluginOperations::_narrow( getOperation() );
+
+  SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
+
+  QStringList fileNames = selectedFiles();
+  if ( fileNames.count() == 0 )
+    return false; // nothing selected, return
+
+  for( int i = 0; i < fileNames.size(); i++ ) {
+    QString aFileName = fileNames.at( i );
+    if ( aFileName.isEmpty() )
+      continue;
+    GEOM_Operation* anOp = new GEOM_Operation( app, anOper.in() );
+    try {
+      app->putInfo( tr( "GEOM_PRP_LOADING" ).arg( SUIT_Tools::file( aFileName.toUtf8().constData(), /*withExten=*/true ) ) );
+      anOp->start();
+      GEOM::ListOfGO_var anObj = anOper->ImportDXF( aFileName.toUtf8().constData() );
+      if( anObj->length() > 0 && anOper->IsDone() ) {
+        GEOM::GEOM_Object_ptr anObjPtr = anObj[0]._retn();
+        objects.push_back( anObjPtr );
+        QString aPublishObjName = GEOMBase::GetDefaultName( SUIT_Tools::file( fileNames.at( i ),
+                                                            /*withExten=*/true ) );
+        anObjPtr->SetName( aPublishObjName.toStdString().c_str() );
+        anOp->commit();
+      }
+      else {
+        anOp->abort();
+        SUIT_MessageBox::critical( app->desktop(),
+                                   QObject::tr("GEOM_ERROR"),
+                                   QObject::tr("GEOM_PRP_ABORT") + "\n" + QObject::tr( anOper->GetErrorCode() ) );
+      }
+    }
+    catch( const SALOME::SALOME_Exception& S_ex ) {
+      anOp->abort();
+    }
+  }
+  return true;
+}
+
+//=================================================================================
+// function : accept
+// purpose  :
+//=================================================================================
+void DXFPlugin_ImportDlg::accept()
+{
+  SUIT_FileDlg::accept();
+  onAccept();
+}
+
+//=================================================================================
+// function : getObjectName
+// purpose  :
+//=================================================================================
+QString DXFPlugin_ImportDlg::getObjectName( GEOM::GEOM_Object_ptr object ) const
+{
+  if( object->_is_nil() )
+    return QString::null;
+  return object->GetName();
+}