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