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