Salome HOME
0022616: [CEA 1038] Improve the quality of stl and vtk exports
[modules/geom.git] / src / BREPPlugin / BREPPlugin_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 "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_ListIteratorOfListIO.hxx>
33
34 // GEOM includes
35 #include "GeometryGUI.h"
36 #include "GEOM_Operation.h"
37 #include "GEOMBase.h"
38 #include "GEOM_Displayer.h"
39
40 #include <SALOMEconfig.h>
41 #include CORBA_SERVER_HEADER(BREPPlugin)
42
43 //=======================================================================
44 // function : BREPPlugin_GUI()
45 // purpose  : Constructor
46 //=======================================================================
47 BREPPlugin_GUI::BREPPlugin_GUI( GeometryGUI* parent ) : GEOMPluginGUI( parent )
48 {
49 }
50
51 //=======================================================================
52 // function : ~BREPPlugin_GUI
53 // purpose  : Destructor
54 //=======================================================================
55 BREPPlugin_GUI::~BREPPlugin_GUI()
56 {
57 }
58
59 //=======================================================================
60 // function : OnGUIEvent()
61 // purpose  : 
62 //=======================================================================
63 bool BREPPlugin_GUI::OnGUIEvent( int theCommandID, SUIT_Desktop* parent )
64 {
65   QString cmd;
66   switch ( theCommandID ) {
67   case 1:
68     cmd = "Export_BREP"; break;
69   case 2:
70     cmd = "Import_BREP"; break;
71   default:
72     break;
73   }
74   return OnGUIEvent( cmd, parent );
75 }
76
77 //=======================================================================
78 // function : OnGUIEvent()
79 // purpose  :
80 //=======================================================================
81 bool BREPPlugin_GUI::OnGUIEvent( const QString& theCommandID, SUIT_Desktop* parent )
82 {
83   bool result = false;
84   
85   if ( theCommandID == "Export_BREP" )
86   {
87     result = exportBREP( parent );
88   }
89   else if ( theCommandID == "Import_BREP" )
90   {
91     result = importBREP( parent );
92   }
93   else
94   {
95     getGeometryGUI()->getApp()->putInfo( tr("GEOM_PRP_COMMAND").arg( theCommandID ) );
96   }
97
98   return result;
99 }
100
101 //=======================================================================
102 // function : importBREP
103 // purpose  :
104 //=======================================================================
105 bool BREPPlugin_GUI::importBREP( SUIT_Desktop* parent )
106 {
107   SalomeApp_Application* app = getGeometryGUI()->getApp();
108   if ( !app ) return false;
109   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
110   if ( !study ) return false;
111
112   SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
113   GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "BREPPluginEngine" );
114   GEOM::IBREPOperations_var brepOp = GEOM::IBREPOperations::_narrow( op );
115   if ( CORBA::is_nil( brepOp ) ) 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.in() );
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( dsStudy,
141                                                                                 SALOMEDS::SObject::_nil(),
142                                                                                 main.in(),
143                                                                                 publishName.toUtf8().constData() );
144           
145           entryList.append( so->GetID() );
146           transaction.commit();
147           GEOM_Displayer( study ).Display( main.in() );
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( true );
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   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
182   if ( !study ) return false;
183
184   SALOMEDS::Study_var dsStudy = GeometryGUI::ClientStudyToStudy( study->studyDS() );
185   GEOM::GEOM_IOperations_var op = GeometryGUI::GetGeomGen()->GetPluginOperations( dsStudy->StudyId(), "BREPPluginEngine" );
186   GEOM::IBREPOperations_var brepOp = GEOM::IBREPOperations::_narrow( op );
187   if ( CORBA::is_nil( brepOp ) ) return false;
188
189   LightApp_SelectionMgr* sm = app->selectionMgr();
190   if ( !sm ) return false;
191   
192   SALOME_ListIO selectedObjects;
193   sm->selectedObjects( selectedObjects );
194   bool ok = false;
195   
196   SALOME_ListIteratorOfListIO it( selectedObjects );
197   for ( ; it.More(); it.Next() )
198   {
199     Handle(SALOME_InteractiveObject) io = it.Value();
200     GEOM::GEOM_Object_var obj = GEOMBase::ConvertIOinGEOMObject( io );
201     
202     if ( CORBA::is_nil( obj ) ) continue;
203     
204     QString fileName = app->getFileName( false,
205                                          QString( io->getName() ),
206                                          tr( "BREP_FILES" ),
207                                          tr( "EXPORT_TITLE" ),
208                                          parent );
209     
210     if ( fileName.isEmpty() )
211       return false;
212     
213     SUIT_OverrideCursor wc;
214     
215     GEOM_Operation transaction( app, brepOp.in() );
216     
217     try
218     {
219       app->putInfo( tr( "GEOM_PRP_EXPORT" ).arg( fileName ) );
220       transaction.start();
221       
222       brepOp->ExportBREP( obj, fileName.toUtf8().constData() );
223       
224       if ( brepOp->IsDone() )
225       {
226         transaction.commit();
227       }
228       else
229       {
230         transaction.abort();
231         SUIT_MessageBox::critical( parent,
232                                    tr( "GEOM_ERROR" ),
233                                    tr( "GEOM_PRP_ABORT" ) + "\n" + tr( brepOp->GetErrorCode() ) );
234         return false;
235       }
236     }
237     catch ( const SALOME::SALOME_Exception& e )
238     {
239       transaction.abort();
240       return false;
241     }
242     ok = true;
243   }
244   
245   if ( !ok )
246   {
247     SUIT_MessageBox::warning( parent,
248                               tr( "WRN_WARNING" ),
249                               tr( "GEOM_WRN_NO_APPROPRIATE_SELECTION" ) );
250   }
251   return ok;
252 }
253
254 //=====================================================================================
255 // EXPORTED METHODS
256 //=====================================================================================
257 extern "C"
258 {
259 #ifdef WIN32
260   __declspec( dllexport )
261 #endif
262   GEOMGUI* GetLibGUI( GeometryGUI* parent )
263   {
264     return new BREPPlugin_GUI( parent );
265   }
266 }