Salome HOME
Issue 0019855 (Cannot delete objects with dependencies) : improve 'Delete objects...
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI.cxx
1 // GEOM GEOMGUI : GUI for Geometry component
2 //
3 // Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File   : GEOMBase_Tools.cxx
23 // Author : Damien COQUERET, Open CASCADE S.A.S.
24 //
25
26 #include "GEOMToolsGUI.h"
27 #include "GEOMToolsGUI_DeleteDlg.h"
28
29 #include <GeometryGUI.h>
30 #include <GEOMBase.h>
31 #include <GEOM_Operation.h>
32 #include <GEOM_Displayer.h>
33
34 #include <SUIT_Session.h>
35 #include <SUIT_OverrideCursor.h>
36 #include <SUIT_MessageBox.h>
37 #include <SUIT_Tools.h>
38 #include <SUIT_FileDlg.h>
39 #include <SUIT_Desktop.h>
40 #include <SUIT_ViewModel.h>
41 #include <SUIT_ViewManager.h>
42
43 #include <SalomeApp_Application.h>
44 #include <SalomeApp_Study.h>
45 #include <LightApp_SelectionMgr.h>
46 #include <GEOMImpl_Types.hxx>
47
48 #include <SALOME_ListIO.hxx>
49 #include <SALOME_ListIteratorOfListIO.hxx>
50 #include <SALOME_Prs.h>
51
52 // QT Includes
53 #include <QApplication>
54 #include <QMap>
55
56 // OCCT Includes
57 #include <TCollection_AsciiString.hxx>
58
59 using namespace std;
60
61 typedef QMap<QString, QString> FilterMap;
62
63 //=======================================================================
64 // function : getFileName
65 // purpose  : Selection of a file name for Import/Export. Returns also
66 //            the selected file type code through <filter> argument.
67 //=======================================================================
68 static QString getFileName( QWidget*           parent,
69                             const QString&     initial,
70                             const FilterMap&   filterMap,
71                             const QStringList& filters,
72                             const QString&     caption,
73                             bool               open,
74                             QString&           format )
75 {
76   static QString lastUsedFilter;
77   //QStringList filters;
78   QString aBrepFilter;
79   for ( FilterMap::const_iterator it = filterMap.begin(); it != filterMap.end(); ++it ) {
80     //filters.push_back( it.key() );
81     if ( it.key().contains( "BREP", Qt::CaseInsensitive ) )
82       aBrepFilter = it.key();
83   }
84
85   SUIT_FileDlg* fd = new SUIT_FileDlg( parent, open, true, true );
86   if ( !caption.isEmpty() )
87     fd->setWindowTitle( caption );
88
89   if ( !initial.isEmpty() )
90     fd->selectFile( initial );
91
92   fd->setFilters( filters );
93   
94   if ( !lastUsedFilter.isEmpty() && filterMap.contains( lastUsedFilter ) ) {
95     fd->selectFilter( lastUsedFilter );
96   }
97   else if ( !aBrepFilter.isEmpty() ) {
98     fd->selectFilter( aBrepFilter );
99   }
100
101   QString filename;
102   if ( fd->exec() == QDialog::Accepted ) {
103     filename = fd->selectedFile();
104     format = filterMap[fd->selectedFilter()];
105     lastUsedFilter = fd->selectedFilter();
106   }
107
108   delete fd;
109   qApp->processEvents();
110   return filename;
111 }
112
113 //=======================================================================
114 // function : getParentComponent
115 // purpose  : Get object's parent component entry
116 //=======================================================================
117 static QString getParentComponent( _PTR( SObject ) obj )
118 {
119   if ( obj ) {
120     _PTR(SComponent) comp = obj->GetFatherComponent();
121     if ( comp )
122       return QString( comp->GetID().c_str() );
123   }
124   return QString();
125 }
126
127 //=====================================================================================
128 // function : inUse
129 // purpose  : check if the object(s) passed as the the second arguments are used
130 //            by the other objects in the study
131 //=====================================================================================
132 static bool inUse( _PTR(Study) study, const QString& component, const QMap<QString,QString>& objects )
133 {
134   _PTR(SObject) comp = study->FindObjectID( component.toLatin1().data() );
135   if ( !comp )
136     return false;
137
138   // collect all GEOM objects being deleted
139   QMap<QString, GEOM::GEOM_Object_var> gobjects;
140   QMap<QString, QString>::ConstIterator oit;
141   for ( oit = objects.begin(); oit != objects.end(); ++oit ) {
142     _PTR(SObject) so = study->FindObjectID( oit.key().toLatin1().data() );
143     if ( !so )
144       continue;
145     CORBA::Object_var corbaObj_rem = GeometryGUI::ClientSObjectToObject( so );
146     GEOM::GEOM_Object_var geomObj_rem = GEOM::GEOM_Object::_narrow( corbaObj_rem );
147     if( CORBA::is_nil( geomObj_rem ) ) 
148       continue;
149     gobjects.insert( oit.key(), geomObj_rem );
150   }
151   
152   // browse through all GEOM data tree
153   _PTR(ChildIterator) it ( study->NewChildIterator( comp ) );
154   for ( it->InitEx( true ); it->More(); it->Next() ) {
155     _PTR(SObject) child( it->Value() );
156     CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject( child );
157     GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
158     if( CORBA::is_nil( geomObj ) ) 
159       continue;
160
161     GEOM::ListOfGO_var list = geomObj->GetDependency();
162     if( list->length() <= 1 ) 
163       continue; // ??? why 1?
164
165     for( int i = 0; i < list->length(); i++ ) {
166       bool depends = false;
167       bool deleted = false;
168       QMap<QString, GEOM::GEOM_Object_var>::Iterator git;
169       for ( git = gobjects.begin(); git != gobjects.end() && ( !depends || !deleted ); ++git ) {
170         depends = depends || list[i]->_is_equivalent( *git );
171         deleted = deleted || git.key() == child->GetID().c_str() ;//geomObj->_is_equivalent( *git );
172       }
173       if ( depends && !deleted )
174         return true;
175     }
176   }
177   return false;
178 }
179
180 //=======================================================================
181 // function : GEOMToolsGUI()
182 // purpose  : Constructor
183 //=======================================================================
184 GEOMToolsGUI::GEOMToolsGUI( GeometryGUI* parent )
185 : GEOMGUI( parent )
186 {
187 }
188
189
190 //=======================================================================
191 // function : ~GEOMToolsGUI()
192 // purpose  : Destructor
193 //=======================================================================
194 GEOMToolsGUI::~GEOMToolsGUI()
195 {
196 }
197
198
199 //=======================================================================
200 // function : OnGUIEvent()
201 // purpose  :
202 //=======================================================================
203 bool GEOMToolsGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent)
204 {
205   getGeometryGUI()->EmitSignalDeactivateDialog();
206
207   switch (theCommandID)
208     {
209     case 31: // COPY
210       {
211         OnEditCopy();
212         break;
213       }
214     case 33: // DELETE
215       {
216         OnEditDelete();
217         break;
218       }
219     case 111: // IMPORT BREP
220     case 112: // IMPORT IGES
221     case 113: // IMPORT STEP
222       {
223         Import();
224         break;
225       }
226     case 121: // EXPORT BREP
227     case 122: // EXPORT IGES
228     case 123: // EXPORT STEP
229       {
230         Export();
231         break;
232       }
233     case 2171: // POPUP VIEWER - SELECT ONLY - VERTEX
234       {
235         OnSelectOnly( GEOM_POINT );
236         break;
237       }
238     case 2172: // POPUP VIEWER - SELECT ONLY - EDGE
239       {
240         OnSelectOnly( GEOM_EDGE );
241         break;
242       }
243     case 2173: // POPUP VIEWER - SELECT ONLY - WIRE
244       {
245         OnSelectOnly( GEOM_WIRE );
246         break;
247       }
248     case 2174: // POPUP VIEWER - SELECT ONLY - FACE
249       {
250         OnSelectOnly( GEOM_FACE );
251         break;
252       }
253     case 2175: // POPUP VIEWER - SELECT ONLY - SHELL
254       {
255         OnSelectOnly( GEOM_SHELL );
256         break;
257       }
258     case 2176: // POPUP VIEWER - SELECT ONLY - SOLID
259       {
260         OnSelectOnly( GEOM_SOLID );
261         break;
262       }
263     case 2177: // POPUP VIEWER - SELECT ONLY - COMPOUND
264       {
265         OnSelectOnly( GEOM_COMPOUND );
266         break;
267       }
268     case 2178: // POPUP VIEWER - SELECT ONLY - SELECT ALL
269       {
270         OnSelectOnly( GEOM_ALLOBJECTS );
271         break;
272       }    
273     case 411: // SETTINGS - ADD IN STUDY
274       {
275         // SAN -- TO BE REMOVED !!!
276         break;
277       }
278     case 412: // SETTINGS - SHADING COLOR
279       {
280         OnSettingsColor();
281         break;
282       }
283     case 804: // ADD IN STUDY - POPUP VIEWER
284       {
285         // SAN -- TO BE REMOVED !!!!
286         break;
287       }
288     case 901: // RENAME
289       {
290         OnRename();
291         break;
292       }
293     case 5103: // CHECK GEOMETRY
294       {
295         OnCheckGeometry();
296         break;
297       }
298     case 8032: // COLOR - POPUP VIEWER
299       {
300         OnColor();
301         break;
302       }
303     case 8033: // TRANSPARENCY - POPUP VIEWER
304       {
305         OnTransparency();
306         break;
307       }
308     case 8034: // ISOS - POPUP VIEWER
309       {
310         OnNbIsos();
311         break;
312       }
313     case 8035: // AUTO COLOR - POPUP VIEWER
314       {
315         OnAutoColor();
316         break;
317       }
318     case 8036: // DISABLE AUTO COLOR - POPUP VIEWER
319       {
320         OnDisableAutoColor();
321         break;
322       }
323     case 9024 : // OPEN - OBJBROSER POPUP
324       {
325         OnOpen();
326         break;
327       }
328     default:
329       {
330         SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
331         break;
332       }
333     }
334   return true;
335 }
336
337
338
339 //===============================================================================
340 // function : OnEditDelete()
341 // purpose  :
342 //===============================================================================
343 void GEOMToolsGUI::OnEditDelete()
344 {
345   SALOME_ListIO selected;
346   SalomeApp_Application* app =
347     dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
348   if ( !app )
349     return;
350
351   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
352   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
353   if ( !aSelMgr || !appStudy )
354     return;
355
356   // get selection
357   aSelMgr->selectedObjects( selected, "ObjectBrowser", false );
358   if ( selected.IsEmpty() )
359     return;
360
361   _PTR(Study) aStudy = appStudy->studyDS();
362   
363   // check if study is locked
364   if ( _PTR(AttributeStudyProperties)( aStudy->GetProperties() )->IsLocked() ) {
365     SUIT_MessageBox::warning( app->desktop(),
366                               tr("WRN_WARNING"),
367                               tr("WRN_STUDY_LOCKED"),
368                               tr("BUT_OK") );
369     return; // study is locked
370   }
371   
372   // get GEOM component
373   CORBA::String_var geomIOR = app->orb()->object_to_string( GeometryGUI::GetGeomGen() );
374   QString geomComp = getParentComponent( aStudy->FindObjectIOR( geomIOR.in() ) );
375
376   // check each selected object: if belongs to GEOM, if not reference...
377   QMap<QString,QString> toBeDeleted;
378   QMap<QString,QString> allDeleted;
379   bool isComponentSelected = false;
380   for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
381     Handle(SALOME_InteractiveObject) anIObject = It.Value();
382     if ( !anIObject->hasEntry() )
383       continue; // invalid object
384     // ...
385     QString entry = anIObject->getEntry();
386     _PTR(SObject) obj = aStudy->FindObjectID( entry.toLatin1().data() );
387     // check parent component
388     QString parentComp = getParentComponent( obj );
389     if ( parentComp != geomComp )  {
390       SUIT_MessageBox::warning ( app->desktop(),
391                                  QObject::tr("ERR_ERROR"),
392                                  QObject::tr("NON_GEOM_OBJECTS_SELECTED").arg( getGeometryGUI()->moduleName() ),
393                                  QObject::tr("BUT_OK") );
394       return; // not GEOM object selected
395     }
396
397     ///////////////////////////////////////////////////////
398     // if GEOM component is selected, so skip other checks
399     if ( isComponentSelected ) continue; 
400     ///////////////////////////////////////////////////////
401         
402     // check if object is reference
403     _PTR(SObject) refobj;
404     if ( obj && obj->ReferencedObject( refobj ) )
405       continue; // skip references
406     // ...
407     QString aName = obj->GetName().c_str();
408     if ( entry == geomComp ) {
409       // GEOM component is selected, skip other checks
410       isComponentSelected = true;
411       continue;
412     }
413     toBeDeleted.insert( entry, aName );
414     allDeleted.insert( entry, aName ); // skip GEOM component
415     // browse through all children recursively
416     _PTR(ChildIterator) it ( aStudy->NewChildIterator( obj ) );
417     for ( it->InitEx( true ); it->More(); it->Next() ) {
418       _PTR(SObject) child( it->Value() );
419       if ( child && child->ReferencedObject( refobj ) )
420         continue; // skip references
421       aName = child->GetName().c_str();
422       if ( !aName.isEmpty() )
423         allDeleted.insert( child->GetID().c_str(), aName );
424     }
425   }
426   
427   // is there is anything to delete?
428   if ( !isComponentSelected && allDeleted.count() <= 0 )
429     return; // nothing to delete
430
431   // show confirmation dialog box
432   GEOMToolsGUI_DeleteDlg dlg( app->desktop(), allDeleted, isComponentSelected );
433   if ( !dlg.exec() )
434     return; // operation is cancelled by user
435   
436   // get currently opened views
437   QList<SALOME_View*> views;
438   SALOME_View* view;
439   ViewManagerList vmans = app->viewManagers();
440   SUIT_ViewManager* vman;
441   QListIterator<SUIT_ViewManager*> vit( vmans );
442   while ( vit.hasNext() && (vman = vit.next()) ) {
443     SUIT_ViewModel* vmod = vman->getViewModel();
444     view = dynamic_cast<SALOME_View*> ( vmod ); // must work for OCC and VTK views
445     if ( view )
446       views.append( view );
447   }
448   
449   _PTR(StudyBuilder) aStudyBuilder (aStudy->NewBuilder());
450   GEOM_Displayer* disp = new GEOM_Displayer( appStudy );
451   
452   if ( isComponentSelected ) {
453     // GEOM component is selected: delete all objects recursively
454     _PTR(SObject) comp = aStudy->FindObjectID( geomComp.toLatin1().data() );
455     if ( !comp )
456       return;
457     _PTR(ChildIterator) it ( aStudy->NewChildIterator( comp ) );
458     // remove top-level objects only
459     for ( it->InitEx( false ); it->More(); it->Next() ) {
460       _PTR(SObject) child( it->Value() );
461       // remove object from GEOM engine
462       removeObjectWithChildren( child, aStudy, views, disp );
463       // remove object from study
464       aStudyBuilder->RemoveObjectWithChildren( child );
465     }
466   }
467   else {
468     // GEOM component is not selected: check if selected objects are in use
469     if ( inUse( aStudy, geomComp, allDeleted ) ) {
470       SUIT_MessageBox::warning ( app->desktop(),
471                                  QObject::tr("WRN_WARNING"),
472                                  QObject::tr("DEP_OBJECT"),
473                                  QObject::tr("BUT_OK") );
474       return; // object(s) in use
475     }
476     // ... and then delete all objects
477     QMap<QString, QString>::Iterator it;
478     for ( it = toBeDeleted.begin(); it != toBeDeleted.end(); ++it ) {
479       _PTR(SObject) obj ( aStudy->FindObjectID( it.key().toLatin1().data() ) );
480       // remove object from GEOM engine
481       removeObjectWithChildren( obj, aStudy, views, disp );
482       // remove objects from study
483       aStudyBuilder->RemoveObjectWithChildren( obj );
484     }
485   }
486   
487   selected.Clear();
488   aSelMgr->setSelectedObjects( selected );
489   getGeometryGUI()->updateObjBrowser();
490   app->updateActions(); //SRN: To update a Save button in the toolbar
491 }
492
493
494 //==============================================================================
495 // function : OnEditCopy()
496 // purpose  :
497 //==============================================================================
498 void GEOMToolsGUI::OnEditCopy()
499 {
500 /*
501  SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection() );
502   GEOM::string_array_var listIOR = new GEOM::string_array;
503
504   const SALOME_ListIO& List = Sel->StoredIObjects();
505
506   myGeomBase->ConvertListOfIOInListOfIOR(List, listIOR);
507
508   Sel->ClearIObjects();
509
510   SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
511   int aStudyID = aStudy->StudyId();
512
513   for (unsigned int ind = 0; ind < listIOR->length();ind++) {
514     GEOM::GEOM_Object_var aShapeInit = myGeom->GetIORFromString(listIOR[ind]);
515     try {
516       GEOM::GEOM_IInsertOperations_var IOp =  myGeom->GetIInsertOperations(aStudyID);
517       GEOM::GEOM_Object_var result = IOp->MakeCopy(aShapeInit);
518       myGeomBase->Display(result);
519     }
520     catch  (const SALOME::SALOME_Exception& S_ex) {
521       QtCatchCorbaException(S_ex);
522     }
523   }
524
525   QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_READY"));
526 */
527 }
528
529
530 //=====================================================================================
531 // function : Import
532 // purpose  : BRep, Iges, Step
533 //=====================================================================================
534 bool GEOMToolsGUI::Import()
535 {
536   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( getGeometryGUI()->getApp() );
537   //SUIT_Application* app = getGeometryGUI()->getApp();
538   if (! app) return false;
539
540   SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
541   if ( !stud ) {
542     cout << "FAILED to cast active study to SalomeApp_Study" << endl;
543     return false;
544   }
545   _PTR(Study) aStudy = stud->studyDS();
546
547   bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
548   if ( aLocked ) {
549     SUIT_MessageBox::warning ( app->desktop(),
550                                QObject::tr("WRN_WARNING"),
551                                QObject::tr("WRN_STUDY_LOCKED"),
552                                QObject::tr("BUT_OK") );
553     return false;
554   }
555
556   GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
557   if ( CORBA::is_nil( eng ) ) {
558     SUIT_MessageBox::critical( app->desktop(),
559                                QObject::tr("WRN_WARNING"),
560                                QObject::tr( "GEOM Engine is not started" ),
561                                QObject::tr("BUT_OK") );
562       return false;
563     }
564
565   GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
566   if ( aInsOp->_is_nil() )
567     return false;
568
569   GEOM::GEOM_Object_var anObj;
570
571   // Obtain a list of available import formats
572   FilterMap aMap;
573   QStringList filters;
574   GEOM::string_array_var aFormats, aPatterns;
575   aInsOp->ImportTranslators( aFormats, aPatterns );
576
577   for ( int i = 0, n = aFormats->length(); i < n; i++ ) {
578     aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
579     filters.push_back( (char*)aPatterns[i] );
580   }
581
582   QString fileType;
583
584   QString fileName = getFileName(app->desktop(), "", aMap, filters,
585                                  tr("GEOM_MEN_IMPORT"), true, fileType);
586
587   if (fileName.isEmpty())
588     return false;
589
590   if (fileType.isEmpty() )
591     {
592       // Trying to detect file type
593       QFileInfo aFileInfo( fileName );
594       QString aPossibleType = (aFileInfo.suffix()).toUpper() ;
595
596       if ( (aMap.values()).contains(aPossibleType) )
597         fileType = aPossibleType;
598     }
599
600   if (fileType.isEmpty())
601     return false;
602
603   GEOM_Operation* anOp = new GEOM_Operation (app, aInsOp.in());
604   try {
605     SUIT_OverrideCursor wc;
606
607     app->putInfo(tr("GEOM_PRP_LOADING").arg(SUIT_Tools::file(fileName, /*withExten=*/true)));
608
609     anOp->start();
610
611     CORBA::String_var fileN = CORBA::string_dup(fileName.toLatin1().data());
612     CORBA::String_var fileT = CORBA::string_dup(fileType.toLatin1().data());
613     anObj = aInsOp->Import(fileN, fileT);
614
615     if ( !anObj->_is_nil() && aInsOp->IsDone() ) {
616       QString aPublishObjName =
617         GEOMBase::GetDefaultName(SUIT_Tools::file(fileName, /*withExten=*/true));
618
619       SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy(aStudy);
620       GeometryGUI::GetGeomGen()->PublishInStudy(aDSStudy,
621                                                 SALOMEDS::SObject::_nil(),
622                                                 anObj,
623                                                 aPublishObjName.toStdString().c_str());
624
625       GEOM_Displayer( stud ).Display( anObj.in() );
626
627       // update data model and object browser
628       getGeometryGUI()->updateObjBrowser( true );
629
630       anOp->commit();
631     }
632     else {
633       anOp->abort();
634       wc.suspend();
635       SUIT_MessageBox::critical( app->desktop(),
636                                  QObject::tr( "GEOM_ERROR" ),
637                                  QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
638                                  QObject::tr("BUT_OK") );
639     }
640   }
641   catch( const SALOME::SALOME_Exception& S_ex ) {
642     //QtCatchCorbaException(S_ex);
643     anOp->abort();
644     return false;
645   }
646
647   app->updateActions(); //SRN: To update a Save button in the toolbar
648
649   return true;
650 }
651
652
653 //=====================================================================================
654 // function : Export
655 // purpose  : BRep, Iges, Step
656 //=====================================================================================
657 bool GEOMToolsGUI::Export()
658 {
659   SalomeApp_Application* app = getGeometryGUI()->getApp();
660   if (!app) return false;
661
662   SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
663   if ( !stud ) {
664     cout << "FAILED to cast active study to SalomeApp_Study" << endl;
665     return false;
666   }
667   _PTR(Study) aStudy = stud->studyDS();
668
669   GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
670   if ( CORBA::is_nil( eng ) ) {
671     SUIT_MessageBox::critical( app->desktop(),
672                                QObject::tr("WRN_WARNING"),
673                                QObject::tr( "GEOM Engine is not started" ),
674                                QObject::tr("BUT_OK") );
675     return false;
676   }
677
678   GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
679   if ( aInsOp->_is_nil() )
680     return false;
681
682   // Obtain a list of available export formats
683   FilterMap aMap;
684   QStringList filters;
685   GEOM::string_array_var aFormats, aPatterns;
686   aInsOp->ExportTranslators( aFormats, aPatterns );
687   for ( int i = 0, n = aFormats->length(); i < n; i++ ) {
688     aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
689     filters.push_back( (char*)aPatterns[i] );
690   }
691
692   // Get selected objects
693   LightApp_SelectionMgr* sm = app->selectionMgr();
694   if ( !sm )
695     return false;
696
697   SALOME_ListIO selectedObjects;
698   sm->selectedObjects( selectedObjects );
699
700   SALOME_ListIteratorOfListIO It( selectedObjects );
701   for(;It.More();It.Next()) {
702     Handle(SALOME_InteractiveObject) IObject = It.Value();
703     Standard_Boolean found;
704     GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject(IObject, found);
705
706     if ( !found || anObj->_is_nil() )
707       continue;
708
709     QString fileType;
710     QString file = getFileName(app->desktop(), QString( IObject->getName() ), aMap, filters,
711                                tr("GEOM_MEN_EXPORT"), false, fileType);
712
713     // User has pressed "Cancel" --> stop the operation
714     if ( file.isEmpty() || fileType.isEmpty() )
715       return false;
716
717     GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
718     try {
719       SUIT_OverrideCursor wc;
720
721       app->putInfo( tr("GEOM_PRP_EXPORT").arg(SUIT_Tools::file( file, /*withExten=*/true )) );
722
723       anOp->start();
724
725
726       aInsOp->Export( anObj, file.toStdString().c_str(), fileType.toLatin1().constData() );
727
728       if ( aInsOp->IsDone() )
729         anOp->commit();
730       else
731         {
732           anOp->abort();
733           wc.suspend();
734           SUIT_MessageBox::critical( app->desktop(),
735                                      QObject::tr( "GEOM_ERROR" ),
736                                      QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
737                                      QObject::tr("BUT_OK") );
738           return false;
739         }
740     }
741     catch (const SALOME::SALOME_Exception& S_ex) {
742       //QtCatchCorbaException(S_ex);
743       anOp->abort();
744       return false;
745     }
746   }
747
748   return true;
749 }
750
751 //=====================================================================================
752 // function : RemoveObjectWithChildren
753 // purpose  : to be used by OnEditDelete() method
754 //=====================================================================================
755 void GEOMToolsGUI::removeObjectWithChildren(_PTR(SObject) obj,
756                                             _PTR(Study) aStudy,
757                                             QList<SALOME_View*> views,
758                                             GEOM_Displayer* disp)
759 {
760   // iterate through all children of obj
761   for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
762     _PTR(SObject) child (it->Value());
763     removeObjectWithChildren(child, aStudy, views, disp);
764   }
765
766   // erase object and remove it from engine
767   _PTR(GenericAttribute) anAttr;
768   if (obj->FindAttribute(anAttr, "AttributeIOR")) {
769     _PTR(AttributeIOR) anIOR (anAttr);
770
771     // Delete shape in Client
772     const TCollection_AsciiString ASCIor ((char*)anIOR->Value().c_str());
773     getGeometryGUI()->GetShapeReader().RemoveShapeFromBuffer(ASCIor);
774
775     CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(obj);
776     GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
777     if (!CORBA::is_nil(geomObj)) {
778       // Erase graphical object
779       QListIterator<SALOME_View*> it( views );
780       while ( it.hasNext() )
781         if ( SALOME_View* view = it.next() )
782           disp->Erase(geomObj, true, view);
783       
784       // Remove object from Engine
785       GeometryGUI::GetGeomGen()->RemoveObject( geomObj );
786     }
787   }
788 }
789
790 //=================================================================================
791 // function : deactivate()
792 // purpose  : Called when GEOM component is deactivated
793 //=================================================================================
794 void GEOMToolsGUI::deactivate()
795 {
796   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
797   if ( app ) {
798     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
799     GEOM_Displayer aDisp (appStudy);
800     aDisp.GlobalSelection();
801     getGeometryGUI()->setLocalSelectionMode(GEOM_ALLOBJECTS);
802   }
803 }
804
805 //=====================================================================================
806 // EXPORTED METHODS
807 //=====================================================================================
808 extern "C"
809 {
810 #ifdef WIN32
811   __declspec( dllexport )
812 #endif
813   GEOMGUI* GetLibGUI( GeometryGUI* parent )
814   {
815     return new GEOMToolsGUI( parent );
816   }
817 }