Salome HOME
0023216: [CEA 1691] Import the compounds with their names from a STEP file
[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 #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   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
115   if ( !study ) return false;
116
117   SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
118   GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "STEPPluginEngine" );
119   STEPOpPtr stepOp = GEOM::ISTEPOperations::_narrow( op );
120   if ( stepOp.isNull() ) return false;
121
122   bool        isCreateAssemblies = true;
123   QStringList fileNames          = STEPPlugin_ImportDlg::getOpenFileNames
124         (SUIT_FileDlg::getLastVisitedPath().isEmpty() ? QDir::currentPath() : QString(""),
125          tr("STEP_FILES"), tr("IMPORT_TITLE"), parent, isCreateAssemblies);
126
127   if ( fileNames.count() > 0 )
128   {
129     QStringList entryList;
130     QStringList errors;
131     SUIT_MessageBox::StandardButton stepAnswer = SUIT_MessageBox::NoButton;
132     
133     for ( int i = 0; i < fileNames.count(); i++ )
134     {
135       QString fileName = fileNames.at( i );
136       SUIT_OverrideCursor wc;
137       GEOM_Operation transaction( app, stepOp.get() );
138       bool ignoreUnits = false;
139       
140       try
141       {
142         app->putInfo( tr( "GEOM_PRP_LOADING" ).arg( SUIT_Tools::file( fileName, true ) ) );
143         transaction.start();
144         
145         CORBA::String_var units = stepOp->ReadValue( fileName.toUtf8().constData(), "LEN_UNITS" );
146         QString unitsStr( units.in() );
147         bool unitsOK = unitsStr.isEmpty() || unitsStr == "M" || unitsStr.toLower() == "metre";
148         
149         if ( !unitsOK )
150         {
151           if( stepAnswer == SUIT_MessageBox::NoToAll )
152           {
153             ignoreUnits = true;
154           }
155           else if( stepAnswer != SUIT_MessageBox::YesToAll )
156           {
157             SUIT_MessageBox::StandardButtons btns = SUIT_MessageBox::Yes | SUIT_MessageBox::No | SUIT_MessageBox::Cancel;
158             if ( i < fileNames.count()-1 ) btns = btns | SUIT_MessageBox::YesToAll | SUIT_MessageBox::NoToAll;
159             stepAnswer = SUIT_MessageBox::question( parent,
160                                                     tr( "WRN_WARNING" ),
161                                                     tr( "SCALE_DIMENSIONS" ).arg( unitsStr ),
162                                                     btns,
163                                                     SUIT_MessageBox::No );
164             switch ( stepAnswer )
165             {
166             case SUIT_MessageBox::Cancel:
167               return true;             // cancel (break) import operation
168             case SUIT_MessageBox::Yes:
169             case SUIT_MessageBox::YesToAll:
170               break;                   // scaling is confirmed
171             case SUIT_MessageBox::No:
172             case SUIT_MessageBox::NoAll:
173               ignoreUnits = true;      // scaling is rejected
174             default:
175               break;
176             }
177           }
178         }
179         
180         GEOM::ListOfGO_var result = stepOp->ImportSTEP
181           (fileName.toUtf8().constData(), ignoreUnits, isCreateAssemblies);
182         if ( result->length() > 0 && stepOp->IsDone() )
183         {
184           GEOM::GEOM_Object_var main = result[0];
185           QString publishName = GEOMBase::GetDefaultName( SUIT_Tools::file( fileName, true ) );
186           SALOMEDS::SObject_var so = GeometryGUI::GetGeomGen()->PublishInStudy( dsStudy,
187                                                                                 SALOMEDS::SObject::_nil(),
188                                                                                 main.in(),
189                                                                                 publishName.toUtf8().constData() );
190           
191           entryList.append( so->GetID() );
192           for ( int i = 1, n = result->length(); i < n; i++ ) {
193             GEOM::GEOM_Object_ptr group = result[i];
194             CORBA::String_var grpName = group->GetName();
195             GeometryGUI::GetGeomGen()->AddInStudy( dsStudy, group, grpName.in(), main );
196           }
197           transaction.commit();
198           GEOM_Displayer( study ).Display( main.in() );
199           main->UnRegister();
200         }
201         else
202         {
203           transaction.abort();
204           errors.append( QString( "%1 : %2" ).arg( fileName ).arg( stepOp->GetErrorCode() ) );
205         }
206       }
207       catch( const SALOME::SALOME_Exception& e )
208       {
209         transaction.abort();
210       }
211     }
212
213     getGeometryGUI()->updateObjBrowser( true );
214     app->browseObjects( entryList );
215           
216     if ( errors.count() > 0 )
217     {
218       SUIT_MessageBox::critical( parent,
219                                  tr( "GEOM_ERROR" ),
220                                  tr( "GEOM_IMPORT_ERRORS" ) + "\n" + errors.join( "\n" ) );
221     }
222   }
223   return fileNames.count() > 0;
224 }
225
226 //=======================================================================
227 // function : exportSTEP
228 // purpose  :
229 //=======================================================================
230 bool STEPPlugin_GUI::exportSTEP( SUIT_Desktop* parent )
231 {
232   SalomeApp_Application* app = getGeometryGUI()->getApp();
233   if ( !app ) return false;
234   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
235   if ( !study ) return false;
236
237   SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
238   GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "STEPPluginEngine" );
239   STEPOpPtr stepOp = GEOM::ISTEPOperations::_narrow( op );
240   if ( stepOp.isNull() ) return false;
241
242   LightApp_SelectionMgr* sm = app->selectionMgr();
243   if ( !sm ) return false;
244   
245   SALOME_ListIO selectedObjects;
246   sm->selectedObjects( selectedObjects );
247   bool ok = false;
248   
249   SALOME_ListIteratorOfListIO it( selectedObjects );
250   for ( ; it.More(); it.Next() )
251   {
252     Handle(SALOME_InteractiveObject) io = it.Value();
253     GEOM::GEOM_Object_var obj = GEOMBase::ConvertIOinGEOMObject( io );
254     
255     if ( CORBA::is_nil( obj ) ) continue;
256     
257     GEOM::length_unit anUnit;
258     QString fileName = STEPPlugin_ExportDlg::getFileName
259                                         (QString( io->getName() ),
260                                          tr( "STEP_FILES" ),
261                                          tr( "EXPORT_TITLE" ),
262                                          parent, anUnit);
263     
264     if ( fileName.isEmpty() )
265       return false;
266     
267     SUIT_OverrideCursor wc;
268     
269     GEOM_Operation transaction( app, stepOp.get() );
270     
271     try
272     {
273       app->putInfo( tr( "GEOM_PRP_EXPORT" ).arg( fileName ) );
274       transaction.start();
275       
276       stepOp->ExportSTEP( obj, fileName.toUtf8().constData(), anUnit);
277       
278       if ( stepOp->IsDone() )
279       {
280         transaction.commit();
281       }
282       else
283       {
284         transaction.abort();
285         SUIT_MessageBox::critical( parent,
286                                    tr( "GEOM_ERROR" ),
287                                    tr( "GEOM_PRP_ABORT" ) + "\n" + tr( stepOp->GetErrorCode() ) );
288         return false;
289       }
290     }
291     catch ( const SALOME::SALOME_Exception& e )
292     {
293       transaction.abort();
294       return false;
295     }
296     ok = true;
297   }
298   
299   if ( !ok )
300   {
301     SUIT_MessageBox::warning( parent,
302                               tr( "WRN_WARNING" ),
303                               tr( "GEOM_WRN_NO_APPROPRIATE_SELECTION" ) );
304   }
305   return ok;
306 }
307
308 //=====================================================================================
309 // EXPORTED METHODS
310 //=====================================================================================
311 extern "C"
312 {
313 #ifdef WIN32
314     __declspec( dllexport )
315 #endif
316   GEOMGUI* GetLibGUI( GeometryGUI* parent )
317   {
318     return new STEPPlugin_GUI( parent );
319   }
320 }