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