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