Salome HOME
Copyright update 2022
[modules/geom.git] / src / BREPPlugin / BREPPlugin_GUI.cxx
1 // Copyright (C) 2014-2022  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
113   GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( "BREPPluginEngine" );
114   BREPOpPtr brepOp = GEOM::IBREPOperations::_narrow( op );
115   if ( brepOp.isNull() ) return false;
116   
117   QStringList fileNames = app->getOpenFileNames( SUIT_FileDlg::getLastVisitedPath().isEmpty() ? QDir::currentPath() : QString(""),
118                                                  tr( "BREP_FILES" ),
119                                                  tr( "IMPORT_TITLE" ),
120                                                  parent );
121   if ( fileNames.count() > 0 )
122   {
123     QStringList entryList;
124     QStringList errors;
125     
126     foreach( QString fileName, fileNames )
127     {
128       SUIT_OverrideCursor wc;
129       GEOM_Operation transaction( app, brepOp.get() );
130       
131       try
132       {
133         app->putInfo( tr( "GEOM_PRP_LOADING" ).arg( fileName ) );
134         transaction.start();
135         GEOM::ListOfGO_var result = brepOp->ImportBREP( fileName.toUtf8().constData() );
136         if ( result->length() > 0 && brepOp->IsDone() )
137         {
138           GEOM::GEOM_Object_var main = result[0];
139           QString publishName = GEOMBase::GetDefaultName( SUIT_Tools::file( fileName, true ) );
140           SALOMEDS::SObject_var so = GeometryGUI::GetGeomGen()->PublishInStudy( SALOMEDS::SObject::_nil(),
141                                                                                                                         main.in(),
142                                                                                                                         publishName.toUtf8().constData() );
143           
144           entryList.append( so->GetID() );
145           transaction.commit();
146           GEOM_Displayer().Display( main.in() );
147           main->UnRegister();
148         }
149         else
150         {
151           transaction.abort();
152           errors.append( QString( "%1 : %2" ).arg( fileName ).arg( brepOp->GetErrorCode() ) );
153         }
154       }
155       catch( const SALOME::SALOME_Exception& e )
156       {
157         transaction.abort();
158       }
159     }
160     getGeometryGUI()->updateObjBrowser();
161     app->browseObjects( entryList );
162     
163     if ( errors.count() > 0 )
164     {
165       SUIT_MessageBox::critical( parent,
166                                  tr( "GEOM_ERROR" ),
167                                  tr( "GEOM_IMPORT_ERRORS" ) + "\n" + errors.join( "\n" ) );
168     }
169   }
170   return fileNames.count() > 0;
171 }
172
173 //=======================================================================
174 // function : exportBREP
175 // purpose  :
176 //=======================================================================
177 bool BREPPlugin_GUI::exportBREP( SUIT_Desktop* parent )
178 {
179   SalomeApp_Application* app = getGeometryGUI()->getApp();
180   if ( !app ) return false;
181
182   GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( "BREPPluginEngine" );
183   BREPOpPtr brepOp = GEOM::IBREPOperations::_narrow( op );
184   if ( brepOp.isNull() ) return false;
185
186   LightApp_SelectionMgr* sm = app->selectionMgr();
187   if ( !sm ) return false;
188   
189   SALOME_ListIO selectedObjects;
190   sm->selectedObjects( selectedObjects );
191   bool ok = false;
192   
193   SALOME_ListIteratorOfListIO it( selectedObjects );
194   for ( ; it.More(); it.Next() )
195   {
196     Handle(SALOME_InteractiveObject) io = it.Value();
197     GEOM::GEOM_Object_var obj = GEOMBase::ConvertIOinGEOMObject( io );
198     
199     if ( CORBA::is_nil( obj ) ) continue;
200     
201     QString fileName = app->getFileName( false,
202                                          QString( io->getName() ),
203                                          tr( "BREP_FILES" ),
204                                          tr( "EXPORT_TITLE" ),
205                                          parent );
206     
207     if ( fileName.isEmpty() )
208       return false;
209     
210     SUIT_OverrideCursor wc;
211     
212     GEOM_Operation transaction( app, brepOp.get() );
213     
214     try
215     {
216       app->putInfo( tr( "GEOM_PRP_EXPORT" ).arg( fileName ) );
217       transaction.start();
218       
219       brepOp->ExportBREP( obj, fileName.toUtf8().constData() );
220       
221       if ( brepOp->IsDone() )
222       {
223         transaction.commit();
224       }
225       else
226       {
227         transaction.abort();
228         SUIT_MessageBox::critical( parent,
229                                    tr( "GEOM_ERROR" ),
230                                    tr( "GEOM_PRP_ABORT" ) + "\n" + tr( brepOp->GetErrorCode() ) );
231         return false;
232       }
233     }
234     catch ( const SALOME::SALOME_Exception& e )
235     {
236       transaction.abort();
237       return false;
238     }
239     ok = true;
240   }
241   
242   if ( !ok )
243   {
244     SUIT_MessageBox::warning( parent,
245                               tr( "WRN_WARNING" ),
246                               tr( "GEOM_WRN_NO_APPROPRIATE_SELECTION" ) );
247   }
248   return ok;
249 }
250
251 //=====================================================================================
252 // EXPORTED METHODS
253 //=====================================================================================
254 extern "C"
255 {
256 #ifdef WIN32
257   __declspec( dllexport )
258 #endif
259   GEOMGUI* GetLibGUI( GeometryGUI* parent )
260   {
261     return new BREPPlugin_GUI( parent );
262   }
263 }