Salome HOME
Update copyright: 2016
[modules/geom.git] / src / VTKPlugin / VTKPlugin_GUI.cxx
1 // Copyright (C) 2014-2016  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 "VTKPlugin_GUI.h"
22 #include "VTKPlugin_ExportDlg.h"
23
24 // GUI includes
25 #include <SUIT_Desktop.h>
26 #include <SUIT_MessageBox.h>
27 #include <SUIT_OverrideCursor.h>
28 #include <LightApp_SelectionMgr.h>
29 #include <SalomeApp_Application.h>
30 #include <SalomeApp_Study.h>
31 #include <SALOME_ListIO.hxx>
32
33 // GEOM includes
34 #include "GeometryGUI.h"
35 #include "GEOM_Operation.h"
36 #include "GEOMBase.h"
37 #include "GEOM_Displayer.h"
38 #include "GEOM_GenericObjPtr.h"
39
40 #include <SALOMEconfig.h>
41 #include CORBA_SERVER_HEADER(VTKPlugin)
42
43 typedef GEOM::GenericObjPtr<GEOM::IVTKOperations> VTKOpPtr;
44
45 //=======================================================================
46 // function : VTKPlugin_GUI()
47 // purpose  : Constructor
48 //=======================================================================
49 VTKPlugin_GUI::VTKPlugin_GUI( GeometryGUI* parent ) : GEOMPluginGUI( parent )
50 {
51 }
52
53 //=======================================================================
54 // function : ~VTKPlugin_GUI
55 // purpose  : Destructor
56 //=======================================================================
57 VTKPlugin_GUI::~VTKPlugin_GUI()
58 {
59 }
60
61 //=======================================================================
62 // function : OnGUIEvent()
63 // purpose  : 
64 //=======================================================================
65 bool VTKPlugin_GUI::OnGUIEvent( int theCommandID, SUIT_Desktop* parent )
66 {
67   QString cmd;
68   switch ( theCommandID ) {
69   case 1:
70     cmd = "Export_VTK"; break;
71   default:
72     break;
73   }
74   return OnGUIEvent( cmd, parent );
75 }
76
77 //=======================================================================
78 // function : OnGUIEvent()
79 // purpose  :
80 //=======================================================================
81 bool VTKPlugin_GUI::OnGUIEvent( const QString& theCommandID, SUIT_Desktop* parent )
82 {
83   bool result = false;
84
85   if( theCommandID == "Export_VTK" ) {
86     result = exportVTK( parent );
87   }
88   else {
89     getGeometryGUI()->getApp()->putInfo( tr("GEOM_PRP_COMMAND").arg( theCommandID ) );
90   }
91
92   return true;
93 }
94
95 //=======================================================================
96 // function : exportVTK
97 // purpose  :
98 //=======================================================================
99 bool VTKPlugin_GUI::exportVTK( SUIT_Desktop* parent )
100 {
101   SalomeApp_Application* app = getGeometryGUI()->getApp();
102   if ( !app ) return false;
103   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
104   if ( !study ) return false;
105
106   SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
107   GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "VTKPluginEngine" );
108   VTKOpPtr vtkOp = GEOM::IVTKOperations::_narrow( op );
109   if ( vtkOp.isNull() ) return false;
110
111   LightApp_SelectionMgr* sm = app->selectionMgr();
112   if ( !sm ) return false;
113   
114   SALOME_ListIO selectedObjects;
115   sm->selectedObjects( selectedObjects );
116   bool ok = false;
117   
118   SALOME_ListIteratorOfListIO it( selectedObjects );
119   for ( ; it.More(); it.Next() )
120   {
121     Handle(SALOME_InteractiveObject) io = it.Value();
122     GEOM::GEOM_Object_var obj = GEOMBase::ConvertIOinGEOMObject( io );
123     
124     if ( CORBA::is_nil( obj ) ) continue;
125
126     double deflection = 0.;
127     QString fileName = VTKPlugin_ExportDlg::getFileName( io,
128                                                          tr( "VTK_FILES" ),
129                                                          tr( "EXPORT_TITLE" ),
130                                                          parent,
131                                                          deflection );
132
133     if ( fileName.isEmpty() )
134       return false;
135     
136     SUIT_OverrideCursor wc;
137     
138     GEOM_Operation transaction( app, vtkOp.get() );
139     
140     try
141     {
142       app->putInfo( tr( "GEOM_PRP_EXPORT" ).arg( fileName ) );
143       transaction.start();
144       
145       vtkOp->ExportVTK( obj, fileName.toUtf8().constData(), deflection  );
146       
147       if ( vtkOp->IsDone() )
148       {
149         transaction.commit();
150       }
151       else
152       {
153         transaction.abort();
154         SUIT_MessageBox::critical( parent,
155                                    tr( "GEOM_ERROR" ),
156                                    tr( "GEOM_PRP_ABORT" ) + "\n" + tr( vtkOp->GetErrorCode() ) );
157         return false;
158       }
159     }
160     catch ( const SALOME::SALOME_Exception& e )
161     {
162       transaction.abort();
163       return false;
164     }
165     ok = true;
166   }
167   
168   if ( !ok )
169   {
170     SUIT_MessageBox::warning( parent,
171                               tr( "WRN_WARNING" ),
172                               tr( "GEOM_WRN_NO_APPROPRIATE_SELECTION" ) );
173   }
174   return ok;
175 }
176
177 //=====================================================================================
178 // EXPORTED METHODS
179 //=====================================================================================
180 extern "C"
181 {
182 #ifdef WIN32
183     __declspec( dllexport )
184 #endif
185   GEOMGUI* GetLibGUI( GeometryGUI* parent )
186   {
187     return new VTKPlugin_GUI( parent );
188   }
189 }