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