Salome HOME
d868adb9720aad1cae434e80cd90d48607beb3c3
[modules/geom.git] / src / STLPlugin / STLPlugin_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 "STLPlugin_GUI.h"
22 #include "STLPlugin_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(STLPlugin)
44
45 typedef GEOM::GenericObjPtr<GEOM::ISTLOperations> STLOpPtr;
46
47 //=======================================================================
48 // function : STLPlugin_GUI()
49 // purpose  : Constructor
50 //=======================================================================
51 STLPlugin_GUI::STLPlugin_GUI( GeometryGUI* parent ) : GEOMPluginGUI( parent )
52 {
53 }
54
55 //=======================================================================
56 // function : ~STLPlugin_GUI
57 // purpose  : Destructor
58 //=======================================================================
59 STLPlugin_GUI::~STLPlugin_GUI()
60 {
61 }
62
63 //=======================================================================
64 // function : OnGUIEvent()
65 // purpose  : 
66 //=======================================================================
67 bool STLPlugin_GUI::OnGUIEvent( int theCommandID, SUIT_Desktop* parent )
68 {
69   QString cmd;
70   switch ( theCommandID ) {
71   case 1:
72     cmd = "Export_STL"; break;
73   case 2:
74     cmd = "Import_STL"; break;
75   default:
76     break;
77   }
78   return OnGUIEvent( cmd, parent );
79 }
80
81 //=======================================================================
82 // function : OnGUIEvent()
83 // purpose  :
84 //=======================================================================
85 bool STLPlugin_GUI::OnGUIEvent( const QString& theCommandID, SUIT_Desktop* parent )
86 {
87   bool result = false;
88
89   if( theCommandID == "Export_STL" ) {
90     result = exportSTL( parent );
91   }
92   else if (theCommandID == "Import_STL") {
93     result = importSTL( parent );
94   }
95   else {
96     getGeometryGUI()->getApp()->putInfo( tr("GEOM_PRP_COMMAND").arg( theCommandID ) );
97   }
98
99   return true;
100 }
101
102 //=======================================================================
103 // function : importSTL
104 // purpose  :
105 //=======================================================================
106 bool STLPlugin_GUI::importSTL( SUIT_Desktop* parent )
107 {
108   SalomeApp_Application* app = getGeometryGUI()->getApp();
109   if ( !app ) return false;
110   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
111   if ( !study ) return false;
112
113   SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
114   GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "STLPluginEngine" );
115   STLOpPtr stlOp = GEOM::ISTLOperations::_narrow( op );
116   if ( stlOp.isNull() ) return false;
117   
118   QStringList fileNames = app->getOpenFileNames( SUIT_FileDlg::getLastVisitedPath().isEmpty() ? QDir::currentPath() : QString(""),
119                                                  tr( "STL_FILES" ),
120                                                  tr( "IMPORT_TITLE" ),
121                                                  parent );
122   if ( fileNames.count() > 0 )
123   {
124     QStringList entryList;
125     QStringList errors;
126     
127     foreach( QString fileName, fileNames )
128     {
129       SUIT_OverrideCursor wc;
130       GEOM_Operation transaction( app, stlOp.get() );
131       
132       try
133       {
134         app->putInfo( tr( "GEOM_PRP_LOADING" ).arg( fileName ) );
135         transaction.start();
136         GEOM::ListOfGO_var result = stlOp->ImportSTL( fileName.toUtf8().constData() );
137         if ( result->length() > 0 && stlOp->IsDone() )
138         {
139           GEOM::GEOM_Object_var main = result[0];
140           QString publishName = GEOMBase::GetDefaultName( SUIT_Tools::file( fileName, true ) );
141           SALOMEDS::SObject_var so = GeometryGUI::GetGeomGen()->PublishInStudy( dsStudy,
142                                                                                 SALOMEDS::SObject::_nil(),
143                                                                                 main.in(),
144                                                                                 publishName.toUtf8().constData() );
145           
146           entryList.append( so->GetID() );
147           transaction.commit();
148           GEOM_Displayer( study ).Display( main.in() );
149           main->UnRegister();
150         }
151         else
152         {
153           transaction.abort();
154           errors.append( QString( "%1 : %2" ).arg( fileName ).arg( stlOp->GetErrorCode() ) );
155         }
156       }
157       catch( const SALOME::SALOME_Exception& e )
158       {
159         transaction.abort();
160       }
161     }
162     getGeometryGUI()->updateObjBrowser( true );
163     app->browseObjects( entryList );
164     
165     if ( errors.count() > 0 )
166     {
167       SUIT_MessageBox::critical( parent,
168                                  tr( "GEOM_ERROR" ),
169                                  tr( "GEOM_IMPORT_ERRORS" ) + "\n" + errors.join( "\n" ) );
170     }
171   }
172   return fileNames.count() > 0;
173 }
174
175 //=======================================================================
176 // function : exportSTL
177 // purpose  :
178 //=======================================================================
179 bool STLPlugin_GUI::exportSTL( SUIT_Desktop* parent )
180 {
181   SalomeApp_Application* app = getGeometryGUI()->getApp();
182   if ( !app ) return false;
183   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
184   if ( !study ) return false;
185
186   SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
187   GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "STLPluginEngine" );
188   STLOpPtr stlOp = GEOM::ISTLOperations::_narrow( op );
189   if ( stlOp.isNull() ) return false;
190
191   LightApp_SelectionMgr* sm = app->selectionMgr();
192   if ( !sm ) return false;
193   
194   SALOME_ListIO selectedObjects;
195   sm->selectedObjects( selectedObjects );
196   bool ok = false;
197   
198   SALOME_ListIteratorOfListIO it( selectedObjects );
199   for ( ; it.More(); it.Next() )
200   {
201     Handle(SALOME_InteractiveObject) io = it.Value();
202     GEOM::GEOM_Object_var obj = GEOMBase::ConvertIOinGEOMObject( io );
203     
204     if ( CORBA::is_nil( obj ) ) continue;
205
206     bool isASCII = false;
207     double deflection = 0.;
208     bool isRelative = false;
209     QString fileName = STLPlugin_ExportDlg::getFileName( io,
210                                                          tr( "STL_FILES" ),
211                                                          tr( "EXPORT_TITLE" ),
212                                                          parent,
213                                                          isASCII,
214                                                          deflection,
215                                                          isRelative );
216
217     if ( fileName.isEmpty() )
218       return false;
219     
220     SUIT_OverrideCursor wc;
221     
222     GEOM_Operation transaction( app, stlOp.get() );
223     
224     try
225     {
226       app->putInfo( tr( "GEOM_PRP_EXPORT" ).arg( fileName ) );
227       transaction.start();
228       
229       stlOp->ExportSTL( obj, fileName.toUtf8().constData(), isASCII, deflection, isRelative  );
230       
231       if ( stlOp->IsDone() )
232       {
233         transaction.commit();
234       }
235       else
236       {
237         transaction.abort();
238         SUIT_MessageBox::critical( parent,
239                                    tr( "GEOM_ERROR" ),
240                                    tr( "GEOM_PRP_ABORT" ) + "\n" + tr( stlOp->GetErrorCode() ) );
241         return false;
242       }
243     }
244     catch ( const SALOME::SALOME_Exception& e )
245     {
246       transaction.abort();
247       return false;
248     }
249     ok = true;
250   }
251   
252   if ( !ok )
253   {
254     SUIT_MessageBox::warning( parent,
255                               tr( "WRN_WARNING" ),
256                               tr( "GEOM_WRN_NO_APPROPRIATE_SELECTION" ) );
257   }
258   return ok;
259 }
260
261 //=====================================================================================
262 // EXPORTED METHODS
263 //=====================================================================================
264 extern "C"
265 {
266 #ifdef WIN32
267     __declspec( dllexport )
268 #endif
269   GEOMGUI* GetLibGUI( GeometryGUI* parent )
270   {
271     return new STLPlugin_GUI( parent );
272   }
273 }