Salome HOME
28a18c11ca5c1c85d74c3cc062c3d3551c64c0e0
[modules/geom.git] / src / IGESPlugin / IGESPlugin_GUI.cxx
1 // Copyright (C) 2014-2023  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 result;
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
111   GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( "IGESPluginEngine" );
112   IGESOpPtr igesOp = GEOM::IIGESOperations::_narrow( op );
113   if ( igesOp.isNull() ) 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.get() );
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( SALOMEDS::SObject::_nil(),
178                                                                                 main.in(),
179                                                                                 publishName.toUtf8().constData() );
180
181           entryList.append( so->GetID() );
182           transaction.commit();
183           GEOM_Displayer().Display( main.in() );
184           main->UnRegister();
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();
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   GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( "IGESPluginEngine" );
223   IGESOpPtr igesOp = GEOM::IIGESOperations::_narrow( op );
224   if ( igesOp.isNull() ) return false;
225
226   LightApp_SelectionMgr* sm = app->selectionMgr();
227   if ( !sm ) return false;
228   
229   SALOME_ListIO selectedObjects;
230   sm->selectedObjects( selectedObjects );
231   bool ok = false;
232   
233   SALOME_ListIteratorOfListIO it( selectedObjects );
234   for ( ; it.More(); it.Next() )
235   {
236     Handle(SALOME_InteractiveObject) io = it.Value();
237     GEOM::GEOM_Object_var obj = GEOMBase::ConvertIOinGEOMObject( io );
238     
239     if ( CORBA::is_nil( obj ) ) continue;
240
241     QString version;
242     QString fileName = IGESPlugin_ExportDlg::getFileName( QString( io->getName() ),
243                                                           tr( "IGES_FILES" ),
244                                                           tr( "EXPORT_TITLE" ),
245                                                           parent,
246                                                           version );
247     
248     if ( fileName.isEmpty() )
249       return false;
250     
251     SUIT_OverrideCursor wc;
252     
253     GEOM_Operation transaction( app, igesOp.get() );
254     
255     try
256     {
257       app->putInfo( tr( "GEOM_PRP_EXPORT" ).arg( fileName ) );
258       transaction.start();
259       
260       igesOp->ExportIGES( obj, fileName.toUtf8().constData(), version.toUtf8().constData() );
261       
262       if ( igesOp->IsDone() )
263       {
264         transaction.commit();
265       }
266       else
267       {
268         transaction.abort();
269         SUIT_MessageBox::critical( parent,
270                                    tr( "GEOM_ERROR" ),
271                                    tr( "GEOM_PRP_ABORT" ) + "\n" + tr( igesOp->GetErrorCode() ) );
272         return false;
273       }
274     }
275     catch ( const SALOME::SALOME_Exception& e )
276     {
277       transaction.abort();
278       return false;
279     }
280     ok = true;
281   }
282   
283   if ( !ok )
284   {
285     SUIT_MessageBox::warning( parent,
286                               tr( "WRN_WARNING" ),
287                               tr( "GEOM_WRN_NO_APPROPRIATE_SELECTION" ) );
288   }
289   return ok;
290 }
291
292 //=====================================================================================
293 // EXPORTED METHODS
294 //=====================================================================================
295 extern "C"
296 {
297 #ifdef WIN32
298     __declspec( dllexport )
299 #endif
300   GEOMGUI* GetLibGUI( GeometryGUI* parent )
301   {
302     return new IGESPlugin_GUI( parent );
303   }
304 }