]> SALOME platform Git repositories - plugins/acisplugin.git/blob - src/ACISPlugin_GUI.cxx
Salome HOME
Some corrections of import/export operations
[plugins/acisplugin.git] / src / ACISPlugin_GUI.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_GUI.h"
22 #include "ACISPlugin_IOperations_i.hh"
23
24 // GUI includes
25 #include <SUIT_Desktop.h>
26 #include <SUIT_FileDlg.h>
27 #include <SUIT_MessageBox.h>
28 #include <SUIT_OverrideCursor.h>
29 #include <SUIT_Tools.h>
30 #include <LightApp_SelectionMgr.h>
31 #include <SalomeApp_Application.h>
32 #include <SalomeApp_Study.h>
33 #include <SALOME_ListIO.hxx>
34
35 // GEOM includes
36 #include "GeometryGUI.h"
37 #include "GEOM_Operation.h"
38 #include "GEOMBase.h"
39 #include "GEOM_Displayer.h"
40
41 #include <SALOMEconfig.h>
42 #include CORBA_SERVER_HEADER(ACISPlugin)
43
44 //=======================================================================
45 // function : ACISPlugin_GUI()
46 // purpose  : Constructor
47 //=======================================================================
48 ACISPlugin_GUI::ACISPlugin_GUI( GeometryGUI* parent ) : GEOMPluginGUI( parent )
49 {
50 }
51
52 //=======================================================================
53 // function : ~ACISPlugin_GUI
54 // purpose  : Destructor
55 //=======================================================================
56 ACISPlugin_GUI::~ACISPlugin_GUI()
57 {
58 }
59
60 //=======================================================================
61 // function : OnGUIEvent()
62 // purpose  : 
63 //=======================================================================
64 bool ACISPlugin_GUI::OnGUIEvent( int theCommandID, SUIT_Desktop* parent )
65 {
66   QString cmd;
67   switch ( theCommandID ) {
68   case 1:
69     cmd = "Export_ACIS";
70   case 2:
71     cmd = "Import_ACIS";
72   default:
73     break;
74   }
75   return OnGUIEvent( cmd, parent );
76 }
77
78 //=======================================================================
79 // function : OnGUIEvent()
80 // purpose  :
81 //=======================================================================
82 bool ACISPlugin_GUI::OnGUIEvent( const QString& theCommandID, SUIT_Desktop* parent )
83 {
84   bool result = false;
85
86   if ( theCommandID == "Export_ACIS" )
87   {
88     result = exportACIS( parent );
89   }
90   else if ( theCommandID == "Import_ACIS" )
91   {
92     result = importACIS( parent );
93   }
94   else
95   {
96     getGeometryGUI()->getApp()->putInfo( tr("GEOM_PRP_COMMAND").arg( theCommandID ) );
97   }
98   return result;
99 }
100
101 //=======================================================================
102 // function : importACIS
103 // purpose  :
104 //=======================================================================
105 bool ACISPlugin_GUI::importACIS( SUIT_Desktop* parent )
106 {
107   SalomeApp_Application* app = getGeometryGUI()->getApp();
108   if ( !app ) return false;
109   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
110   if ( !study ) return false;
111
112   SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
113   GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "ACISPluginEngine" );
114   ACISPlugin::IACISOperations_var acisOp = ACISPlugin::IACISOperations::_narrow( op );
115   if ( CORBA::is_nil( acisOp ) ) return false;
116
117   QStringList fileNames = app->getOpenFileNames( SUIT_FileDlg::getLastVisitedPath().isEmpty() ? QDir::currentPath() : QString(""),
118                                                  tr( "ACIS_FILES" ),
119                                                  tr( "IMPORT_TITLE" ),
120                                                  parent );
121   if ( fileNames.count() > 0 )
122   {
123     QStringList entryList;
124     QStringList errors;
125
126     SUIT_MessageBox::StandardButton acisAnswer = SUIT_MessageBox::NoButton;
127
128     foreach( QString fileName, fileNames )
129     {
130       SUIT_OverrideCursor wc;
131       GEOM_Operation transaction( app, acisOp.in() );
132
133       try
134       {
135         app->putInfo( tr( "GEOM_PRP_LOADING" ).arg( fileName ) );
136         transaction.start();
137
138         if ( acisAnswer != SUIT_MessageBox::YesToAll && acisAnswer != SUIT_MessageBox::NoToAll ) {
139           SUIT_MessageBox::StandardButtons btns = SUIT_MessageBox::Yes | SUIT_MessageBox::No;
140           btns = btns | SUIT_MessageBox::YesToAll | SUIT_MessageBox::NoToAll;
141           acisAnswer = SUIT_MessageBox::question( app->desktop(),
142                                                   "Question",
143                                                   tr("GEOM_PUBLISH_NAMED_SHAPES"),
144                                                   btns | SUIT_MessageBox::Cancel,
145                                                   SUIT_MessageBox::No );
146           if ( acisAnswer == SUIT_MessageBox::Cancel ) {
147             acisOp->SetErrorCode(OK);
148             return true; // cancel (break) import operation
149           }
150         }
151         GEOM::ListOfGO_var result = acisOp->ImportACIS( fileName.toUtf8().constData() );
152
153         if ( result->length() > 0 && acisOp->IsDone() ) {
154           GEOM::GEOM_Object_var main = result[0];
155           QString publishName = GEOMBase::GetDefaultName( SUIT_Tools::file( fileName, true ) );
156           main.in()->SetName( publishName.toStdString().c_str() );
157           SALOMEDS::SObject_var so = GeometryGUI::GetGeomGen()->PublishInStudy( dsStudy,
158                                                                 SALOMEDS::SObject::_nil(),
159                                                                 main.in(),
160                                                                 publishName.toUtf8().constData() );
161           if ( acisAnswer == SUIT_MessageBox::Yes || acisAnswer == SUIT_MessageBox::YesToAll )
162             GeometryGUI::GetGeomGen()->PublishNamedShapesInStudy( dsStudy, main.in() );
163
164           entryList.append( so->GetID() );
165           transaction.commit();
166         }
167         else
168         {
169           transaction.abort();
170           errors.append( QString( "%1 : %2" ).arg( fileName ).arg( acisOp->GetErrorCode() ) );
171         }
172       }
173       catch( const SALOME::SALOME_Exception& e )
174       {
175         transaction.abort();
176       }
177     }
178     getGeometryGUI()->updateObjBrowser( true );
179     app->browseObjects( entryList );
180
181     if ( errors.count() > 0 )
182     {
183       SUIT_MessageBox::critical( parent,
184                                  tr( "GEOM_ERROR" ),
185                                  tr( "GEOM_IMPORT_ERRORS" ) + "\n" + errors.join( "\n" ) );
186     }
187   }
188   return fileNames.count() > 0;
189 }
190
191 //=======================================================================
192 // function : exportACIS
193 // purpose  :
194 //=======================================================================
195 bool ACISPlugin_GUI::exportACIS( SUIT_Desktop* parent )
196 {
197   SalomeApp_Application* app = getGeometryGUI()->getApp();
198   if ( !app ) return false;
199   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
200   if ( !study ) return false;
201
202   SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
203   GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "ACISPluginEngine" );
204   ACISPlugin::IACISOperations_var acisOp = ACISPlugin::IACISOperations::_narrow( op );
205   if ( CORBA::is_nil( acisOp ) ) return false;
206
207   LightApp_SelectionMgr* sm = app->selectionMgr();
208   if ( !sm ) return false;
209
210   SALOME_ListIO selectedObjects;
211   sm->selectedObjects( selectedObjects );
212   bool ok = false;
213
214   SALOME_ListIteratorOfListIO it( selectedObjects );
215   for ( ; it.More(); it.Next() )
216   {
217     Handle(SALOME_InteractiveObject) io = it.Value();
218     GEOM::GEOM_Object_var obj = GEOMBase::ConvertIOinGEOMObject( io );
219
220     if ( CORBA::is_nil( obj ) ) continue;
221
222     QString fileName = app->getFileName( false,
223                                          QString( io->getName() ),
224                                          tr( "ACIS_FILES" ),
225                                          tr( "EXPORT_TITLE" ),
226                                          parent );
227
228     if ( fileName.isEmpty() )
229       return false;
230
231     SUIT_OverrideCursor wc;
232
233     GEOM_Operation transaction( app, acisOp.in() );
234
235     try
236     {
237       app->putInfo( tr( "GEOM_PRP_EXPORT" ).arg( fileName ) );
238       transaction.start();
239
240       acisOp->ExportACIS( obj, fileName.toUtf8().constData() );
241
242       if ( acisOp->IsDone() )
243       {
244         transaction.commit();
245       }
246       else
247       {
248         transaction.abort();
249         SUIT_MessageBox::critical( parent,
250                                    tr( "GEOM_ERROR" ),
251                                    tr( "GEOM_PRP_ABORT" ) + "\n" + tr( acisOp->GetErrorCode() ) );
252         return false;
253       }
254     }
255     catch ( const SALOME::SALOME_Exception& e )
256     {
257       transaction.abort();
258       return false;
259     }
260     ok = true;
261   }
262
263   if ( !ok )
264   {
265     SUIT_MessageBox::warning( parent,
266                               tr( "WRN_WARNING" ),
267                               tr( "GEOM_WRN_NO_APPROPRIATE_SELECTION" ) );
268   }
269   return ok;
270 }
271
272 //=====================================================================================
273 // EXPORTED METHODS
274 //=====================================================================================
275 extern "C"
276 {
277 #ifdef WIN32
278     __declspec( dllexport )
279 #endif
280   GEOMGUI* GetLibGUI( GeometryGUI* parent )
281   {
282     return new ACISPlugin_GUI( parent );
283   }
284 }