Salome HOME
IPAL54049: Operations on multiple study objects are very long
[modules/geom.git] / src / IGESPlugin / IGESPlugin_GUI.cxx
1 // Copyright (C) 2014-2016  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 "IGESPlugin_GUI.h"
22 #include "IGESPlugin_ExportDlg.h"
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 #include "GEOM_GenericObjPtr.h"
41
42 #include <SALOMEconfig.h>
43 #include CORBA_SERVER_HEADER(IGESPlugin)
44
45 typedef GEOM::GenericObjPtr<GEOM::IIGESOperations> IGESOpPtr;
46
47 //=======================================================================
48 // function : IGESPlugin_GUI()
49 // purpose  : Constructor
50 //=======================================================================
51 IGESPlugin_GUI::IGESPlugin_GUI( GeometryGUI* parent ) : GEOMPluginGUI( parent )
52 {
53 }
54
55 //=======================================================================
56 // function : ~IGESPlugin_GUI
57 // purpose  : Destructor
58 //=======================================================================
59 IGESPlugin_GUI::~IGESPlugin_GUI()
60 {
61 }
62
63 //=======================================================================
64 // function : OnGUIEvent()
65 // purpose  : 
66 //=======================================================================
67 bool IGESPlugin_GUI::OnGUIEvent( int theCommandID, SUIT_Desktop* parent )
68 {
69   QString cmd;
70   switch ( theCommandID ) {
71   case 1:
72     cmd = "Export_IGES"; break;
73   case 2:
74     cmd = "Import_IGES"; break;
75   default:
76     break;
77   }
78   return OnGUIEvent( cmd, parent );
79 }
80
81 //=======================================================================
82 // function : OnGUIEvent()
83 // purpose  :
84 //=======================================================================
85 bool IGESPlugin_GUI::OnGUIEvent( const QString& theCommandID, SUIT_Desktop* parent )
86 {
87   bool result = false;
88
89   if( theCommandID == "Export_IGES" ) {
90     result = exportIGES( parent );
91   }
92   else if (theCommandID == "Import_IGES") {
93     result = importIGES( parent );
94   }
95   else {
96     getGeometryGUI()->getApp()->putInfo( tr("GEOM_PRP_COMMAND").arg( theCommandID ) );
97   }
98
99   return true;
100 }
101
102 //=======================================================================
103 // function : importIGES
104 // purpose  :
105 //=======================================================================
106 bool IGESPlugin_GUI::importIGES( SUIT_Desktop* parent )
107 {
108   SalomeApp_Application* app = getGeometryGUI()->getApp();
109   if ( !app ) return false;
110   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
111   if ( !study ) return false;
112
113   SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
114   GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "IGESPluginEngine" );
115   IGESOpPtr igesOp = GEOM::IIGESOperations::_narrow( op );
116   if ( igesOp.isNull() ) return false;
117
118   QStringList fileNames = app->getOpenFileNames( SUIT_FileDlg::getLastVisitedPath().isEmpty() ? QDir::currentPath() : QString(""),
119                                                  tr( "IGES_FILES" ),
120                                                  tr( "IMPORT_TITLE" ),
121                                                  parent );
122   if ( fileNames.count() > 0 )
123   {
124     QStringList entryList;
125     QStringList errors;
126     SUIT_MessageBox::StandardButton igesAnswer = SUIT_MessageBox::NoButton;
127
128     for ( int i = 0; i < fileNames.count(); i++ )
129     {
130       QString fileName = fileNames.at( i );
131       SUIT_OverrideCursor wc;
132       GEOM_Operation transaction( app, igesOp.get() );
133       bool ignoreUnits = false;
134
135       try
136       {
137         app->putInfo( tr( "GEOM_PRP_LOADING" ).arg( SUIT_Tools::file( fileName, true ) ) );
138         transaction.start();
139
140         CORBA::String_var units = igesOp->ReadValue( fileName.toUtf8().constData(), "LEN_UNITS" );
141         QString unitsStr( units.in() );
142         bool unitsOK = unitsStr.isEmpty() || unitsStr == "M" || unitsStr.toLower() == "metre";
143
144         if ( !unitsOK )
145         {
146           if( igesAnswer == SUIT_MessageBox::NoToAll )
147           {
148             ignoreUnits = true;
149           }
150           else if( igesAnswer != SUIT_MessageBox::YesToAll )
151           {
152             SUIT_MessageBox::StandardButtons btns = SUIT_MessageBox::Yes | SUIT_MessageBox::No | SUIT_MessageBox::Cancel;
153             if ( i < fileNames.count()-1 ) btns = btns | SUIT_MessageBox::YesToAll | SUIT_MessageBox::NoToAll;
154             igesAnswer = SUIT_MessageBox::question( parent,
155                                                     tr( "WRN_WARNING" ),
156                                                     tr( "SCALE_DIMENSIONS" ).arg( unitsStr ),
157                                                     btns,
158                                                     SUIT_MessageBox::No );
159             switch ( igesAnswer )
160             {
161             case SUIT_MessageBox::Cancel:
162               return true;             // cancel (break) import operation
163             case SUIT_MessageBox::Yes:
164             case SUIT_MessageBox::YesToAll:
165               break;                   // scaling is confirmed
166             case SUIT_MessageBox::No:
167             case SUIT_MessageBox::NoAll:
168               ignoreUnits = true;      // scaling is rejected
169             default:
170               break;
171             }
172           }
173         }
174
175         GEOM::ListOfGO_var result = igesOp->ImportIGES( fileName.toUtf8().constData(), ignoreUnits );
176         if ( result->length() > 0 && igesOp->IsDone() )
177         {
178           GEOM::GEOM_Object_var main = result[0];
179           QString publishName = GEOMBase::GetDefaultName( SUIT_Tools::file( fileName, true ) );
180           SALOMEDS::SObject_var so = GeometryGUI::GetGeomGen()->PublishInStudy( dsStudy,
181                                                                                 SALOMEDS::SObject::_nil(),
182                                                                                 main.in(),
183                                                                                 publishName.toUtf8().constData() );
184
185           entryList.append( so->GetID() );
186           transaction.commit();
187           GEOM_Displayer( study ).Display( main.in() );
188           main->UnRegister();
189         }
190         else
191         {
192           transaction.abort();
193           errors.append( QString( "%1 : %2" ).arg( fileName ).arg( igesOp->GetErrorCode() ) );
194         }
195       }
196       catch( const SALOME::SALOME_Exception& e )
197       {
198         transaction.abort();
199       }
200     }
201
202     getGeometryGUI()->updateObjBrowser( true );
203     app->browseObjects( entryList );
204
205     if ( errors.count() > 0 )
206     {
207       SUIT_MessageBox::critical( parent,
208                                  tr( "GEOM_ERROR" ),
209                                  tr( "GEOM_IMPORT_ERRORS" ) + "\n" + errors.join( "\n" ) );
210     }
211   }
212   return fileNames.count() > 0;
213 }
214
215 //=======================================================================
216 // function : exportIGES
217 // purpose  :
218 //=======================================================================
219 bool IGESPlugin_GUI::exportIGES( SUIT_Desktop* parent )
220 {
221   SalomeApp_Application* app = getGeometryGUI()->getApp();
222   if ( !app ) return false;
223   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
224   if ( !study ) return false;
225
226   SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
227   GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "IGESPluginEngine" );
228   IGESOpPtr igesOp = GEOM::IIGESOperations::_narrow( op );
229   if ( igesOp.isNull() ) return false;
230
231   LightApp_SelectionMgr* sm = app->selectionMgr();
232   if ( !sm ) return false;
233   
234   SALOME_ListIO selectedObjects;
235   sm->selectedObjects( selectedObjects );
236   bool ok = false;
237   
238   SALOME_ListIteratorOfListIO it( selectedObjects );
239   for ( ; it.More(); it.Next() )
240   {
241     Handle(SALOME_InteractiveObject) io = it.Value();
242     GEOM::GEOM_Object_var obj = GEOMBase::ConvertIOinGEOMObject( io );
243     
244     if ( CORBA::is_nil( obj ) ) continue;
245
246     QString version;
247     QString fileName = IGESPlugin_ExportDlg::getFileName( QString( io->getName() ),
248                                                           tr( "IGES_FILES" ),
249                                                           tr( "EXPORT_TITLE" ),
250                                                           parent,
251                                                           version );
252     
253     if ( fileName.isEmpty() )
254       return false;
255     
256     SUIT_OverrideCursor wc;
257     
258     GEOM_Operation transaction( app, igesOp.get() );
259     
260     try
261     {
262       app->putInfo( tr( "GEOM_PRP_EXPORT" ).arg( fileName ) );
263       transaction.start();
264       
265       igesOp->ExportIGES( obj, fileName.toUtf8().constData(), version.toUtf8().constData() );
266       
267       if ( igesOp->IsDone() )
268       {
269         transaction.commit();
270       }
271       else
272       {
273         transaction.abort();
274         SUIT_MessageBox::critical( parent,
275                                    tr( "GEOM_ERROR" ),
276                                    tr( "GEOM_PRP_ABORT" ) + "\n" + tr( igesOp->GetErrorCode() ) );
277         return false;
278       }
279     }
280     catch ( const SALOME::SALOME_Exception& e )
281     {
282       transaction.abort();
283       return false;
284     }
285     ok = true;
286   }
287   
288   if ( !ok )
289   {
290     SUIT_MessageBox::warning( parent,
291                               tr( "WRN_WARNING" ),
292                               tr( "GEOM_WRN_NO_APPROPRIATE_SELECTION" ) );
293   }
294   return ok;
295 }
296
297 //=====================================================================================
298 // EXPORTED METHODS
299 //=====================================================================================
300 extern "C"
301 {
302 #ifdef WIN32
303     __declspec( dllexport )
304 #endif
305   GEOMGUI* GetLibGUI( GeometryGUI* parent )
306   {
307     return new IGESPlugin_GUI( parent );
308   }
309 }