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