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