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