Salome HOME
Implementation of ACISPLUGIN as a GEOM plugin (added files)
[plugins/acisplugin.git] / src / ACISPlugin_ExportDlg.cxx
1 // Copyright (C) 2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // internal includes
21 #include "ACISPlugin_ExportDlg.h"
22 #include "ACISPlugin_Operations_i.hh"
23
24 // GUI includes
25 #include <SUIT_Session.h>
26 #include <SUIT_Desktop.h>
27 #include <SUIT_Tools.h>
28 #include <SUIT_MessageBox.h>
29 #include <SUIT_OverrideCursor.h>
30
31 #include <SalomeApp_Application.h>
32
33 // GEOM includes
34 #include <GEOMBase.h>
35 #include <GEOM_Operation.h>
36 #include <GEOM_Constants.h>
37
38 //=================================================================================
39 // Constructor
40 //=================================================================================
41 ACISPlugin_ExportDlg::ACISPlugin_ExportDlg( Handle(SALOME_InteractiveObject)& object, QWidget* parent )
42 : SUIT_FileDlg( parent, false, true, true ),
43   GEOMBase_Helper( dynamic_cast<SUIT_Desktop*>( parent ) )
44 {
45   setWindowTitle( tr( "ACISPLUGIN_EXPORT_TITLE" ) );
46   setNameFilter( tr( "ACISPLUGIN_ACIS_FILES" ) );
47
48   myObject = object;
49   const char* objectName = myObject->getName();
50   selectFile( QString( objectName ) );
51 }
52
53 //=================================================================================
54 // Destructor
55 //=================================================================================
56 ACISPlugin_ExportDlg::~ACISPlugin_ExportDlg()
57 {
58   // no need to delete child widgets, Qt does it all for us
59 }
60
61 //=================================================================================
62 // function : createOperation
63 // purpose  :
64 //=================================================================================
65 GEOM::GEOM_IOperations_ptr ACISPlugin_ExportDlg::createOperation()
66 {
67   return getGeomEngine()->GetPluginOperations( getStudyId(), "ACISPluginEngine" );
68 }
69
70 //=================================================================================
71 // function : isValid
72 // purpose  :
73 //=================================================================================
74 bool ACISPlugin_ExportDlg::isValid( QString& msg )
75 {
76   bool ok = true;
77   //@@ add custom validation actions here @@//
78   return ok;
79 }
80
81 //=================================================================================
82 // function : execute
83 // purpose  :
84 //=================================================================================
85 bool ACISPlugin_ExportDlg::execute( ObjectList& objects )
86 {
87   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
88   if (!app) return false;
89
90   ACISPlugin::GEOM_IACISPluginOperations_var aACISOp = ACISPlugin::GEOM_IACISPluginOperations::_narrow( getOperation() );
91   if ( aACISOp->_is_nil() ) return false;
92
93   GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject( myObject );
94   if ( anObj->_is_nil() ) return false;
95
96   QString aFile = selectedFile();
97
98   // User has pressed "Cancel" --> stop the operation
99   if ( aFile.isEmpty() )
100     return false;
101
102   GEOM_Operation* anOp = new GEOM_Operation( app, aACISOp.in() );
103   try {
104     SUIT_OverrideCursor wc;
105     app->putInfo( tr("GEOM_PRP_EXPORT").arg(SUIT_Tools::file( aFile, /*withExten=*/true )) );
106     anOp->start();
107     aACISOp->ExportACIS( anObj, aFile.toUtf8().constData() );
108     if( aACISOp->IsDone() )
109       anOp->commit();
110     else {
111       anOp->abort();
112       wc.suspend();
113       SUIT_MessageBox::critical( app->desktop(),
114                                  QObject::tr("GEOM_ERROR"),
115                                  QObject::tr("GEOM_PRP_ABORT") + "\n" + QObject::tr( aACISOp->GetErrorCode() ) );
116       return false;
117     }
118   }
119   catch( const SALOME::SALOME_Exception& S_ex ) {
120     anOp->abort();
121     return false;
122   }
123
124   objects.push_back( anObj._retn() );
125   return true;
126 }
127
128 void ACISPlugin_ExportDlg::accept()
129 {
130   SUIT_FileDlg::accept();
131   onAccept(false);
132 }