Salome HOME
Additional fix for bug NPAL19028 (see remarks from Olivier Giorgis).
[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 //
23 //
24 //  File   : GEOMBase_Tools.cxx
25 //  Author : Damien COQUERET
26 //  Module : GEOM
27 //  $Header$
28
29 #include "GEOMToolsGUI.h"
30
31 #include "GeometryGUI.h"
32 #include "GEOM_Actor.h"
33 #include "GEOMBase.h"
34 #include "GEOMBase_aWarningDlg.h"
35
36 #include "GEOM_Operation.h"
37 #include "GEOM_Displayer.h"
38
39 #include <SUIT_Session.h>
40 #include <SUIT_Application.h>
41 #include <SUIT_OverrideCursor.h>
42 #include <SUIT_MessageBox.h>
43 #include <SUIT_Tools.h>
44 #include <SUIT_FileDlg.h>
45 #include <SUIT_Desktop.h>
46 #include <SUIT_ViewModel.h>
47
48 #include <SalomeApp_Application.h>
49 #include <SalomeApp_Study.h>
50 #include <LightApp_SelectionMgr.h>
51 #include <GEOMImpl_Types.hxx>
52
53 #include <SALOME_ListIteratorOfListIO.hxx>
54 #include <SALOME_Prs.h>
55
56 #include "utilities.h"
57
58 // QT Includes
59 #include <qapplication.h>
60 #include <qmap.h>
61
62 // OCCT Includes
63 #include <TCollection_AsciiString.hxx>
64
65 using namespace std;
66
67 typedef QMap<QString, QString> FilterMap;
68
69 //=======================================================================
70 // function : getFileName
71 // purpose  : Selection of a file name for Import/Export. Returns also
72 //            the selected file type code through <filter> argument.
73 //=======================================================================
74 static QString getFileName( QWidget*           parent,
75                             const QString&     initial,
76                             const FilterMap&   filterMap,
77                             const QStringList  filters,
78                             const QString&     caption,
79                             bool               open,
80                             QString&           format,
81                             bool               showCurrentDirInitially = false)
82 {
83   static QString lastUsedFilter;
84   //QStringList filters;
85   QString aBrepFilter;
86   for ( FilterMap::const_iterator it = filterMap.begin(); it != filterMap.end(); ++it ) {
87     //filters.push_back( it.key() );
88
89     if (it.key().contains("BREP", false)) {
90       aBrepFilter = it.key();
91     }
92   }
93
94   SUIT_FileDlg* fd = new SUIT_FileDlg( parent, open, true, true );
95   if ( !caption.isEmpty() )
96     fd->setCaption( caption );
97
98   if ( showCurrentDirInitially && SUIT_FileDlg::getLastVisitedPath().isEmpty() )
99     fd->setSelection( QDir::currentDirPath() );
100   else if ( !initial.isEmpty() )
101     fd->setSelection( initial );
102
103   fd->setFilters( filters );
104
105   if ( !lastUsedFilter.isEmpty() && filterMap.contains( lastUsedFilter ) )
106     fd->setSelectedFilter( lastUsedFilter );
107   else {
108     if (!aBrepFilter.isEmpty()) {
109       fd->setSelectedFilter(aBrepFilter);
110     }
111   }
112
113   fd->exec();
114   QString filename = fd->selectedFile();
115   format = filterMap[fd->selectedFilter()];
116   lastUsedFilter = fd->selectedFilter();
117   delete fd;
118   qApp->processEvents();
119   return filename;
120 }
121
122 //=======================================================================
123 // function : GEOMToolsGUI()
124 // purpose  : Constructor
125 //=======================================================================
126 GEOMToolsGUI::GEOMToolsGUI( GeometryGUI* parent )
127 : GEOMGUI( parent )
128 {
129 }
130
131
132 //=======================================================================
133 // function : ~GEOMToolsGUI()
134 // purpose  : Destructor
135 //=======================================================================
136 GEOMToolsGUI::~GEOMToolsGUI()
137 {
138 }
139
140
141 //=======================================================================
142 // function : OnGUIEvent()
143 // purpose  :
144 //=======================================================================
145 bool GEOMToolsGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent)
146 {
147   getGeometryGUI()->EmitSignalDeactivateDialog();
148
149   switch (theCommandID)
150     {
151     case 31: // COPY
152       {
153         OnEditCopy();
154         break;
155       }
156     case 33: // DELETE
157       {
158         OnEditDelete();
159         break;
160       }
161     case 111: // IMPORT BREP
162     case 112: // IMPORT IGES
163     case 113: // IMPORT STEP
164       {
165         Import();
166         break;
167       }
168     case 121: // EXPORT BREP
169     case 122: // EXPORT IGES
170     case 123: // EXPORT STEP
171       {
172         Export();
173         break;
174       }
175     case 2171: // POPUP VIEWER - SELECT ONLY - VERTEX
176       {
177         OnSelectOnly( GEOM_POINT );
178         break;
179       }
180     case 2172: // POPUP VIEWER - SELECT ONLY - EDGE
181       {
182         OnSelectOnly( GEOM_EDGE );
183         break;
184       }
185     case 2173: // POPUP VIEWER - SELECT ONLY - WIRE
186       {
187         OnSelectOnly( GEOM_WIRE );
188         break;
189       }
190     case 2174: // POPUP VIEWER - SELECT ONLY - FACE
191       {
192         OnSelectOnly( GEOM_FACE );
193         break;
194       }
195     case 2175: // POPUP VIEWER - SELECT ONLY - SHELL
196       {
197         OnSelectOnly( GEOM_SHELL );
198         break;
199       }
200     case 2176: // POPUP VIEWER - SELECT ONLY - SOLID
201       {
202         OnSelectOnly( GEOM_SOLID );
203         break;
204       }
205     case 2177: // POPUP VIEWER - SELECT ONLY - COMPOUND
206       {
207         OnSelectOnly( GEOM_COMPOUND );
208         break;
209       }
210     case 2178: // POPUP VIEWER - SELECT ONLY - SELECT ALL
211       {
212         OnSelectOnly( GEOM_ALLOBJECTS );
213         break;
214       }    
215     case 411: // SETTINGS - ADD IN STUDY
216       {
217         // SAN -- TO BE REMOVED !!!
218         break;
219       }
220     case 412: // SETTINGS - SHADING COLOR
221       {
222         OnSettingsColor();
223         break;
224       }
225     case 413: // SETTINGS - ISOS
226       {
227         OnSettingsIsos();
228         break;
229       }
230     case 414: // SETTINGS : STEP VALUE FOR SPIN BOXES
231       {
232         OnSettingsStep();
233         break;
234       }
235     case 804: // ADD IN STUDY - POPUP VIEWER
236       {
237         // SAN -- TO BE REMOVED !!!!
238         break;
239       }
240     case 901: // RENAME
241       {
242         OnRename();
243         break;
244       }
245     case 5103: // CHECK GEOMETRY
246       {
247         OnCheckGeometry();
248         break;
249       }
250     case 8032: // COLOR - POPUP VIEWER
251       {
252         OnColor();
253         break;
254       }
255     case 8033: // TRANSPARENCY - POPUP VIEWER
256       {
257         OnTransparency();
258         break;
259       }
260     case 8034: // ISOS - POPUP VIEWER
261       {
262         OnNbIsos();
263         break;
264       }
265     case 8035: // AUTO COLOR - POPUP VIEWER
266       {
267         OnAutoColor();
268         break;
269       }
270     case 8036: // DISABLE AUTO COLOR - POPUP VIEWER
271       {
272         OnDisableAutoColor();
273         break;
274       }
275     case 9024 : // OPEN - OBJBROSER POPUP
276       {
277         OnOpen();
278         break;
279       }
280     default:
281       {
282         SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
283         break;
284       }
285     }
286   return true;
287 }
288
289
290
291 //===============================================================================
292 // function : OnEditDelete()
293 // purpose  :
294 //===============================================================================
295 void GEOMToolsGUI::OnEditDelete()
296 {
297   SALOME_ListIO selected;
298   SalomeApp_Application* app =
299     dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
300   if ( app ) {
301     LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
302     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
303     if ( aSelMgr && appStudy ) {
304       aSelMgr->selectedObjects( selected, QString::null, false );
305       if ( !selected.IsEmpty() ) {
306         _PTR(Study) aStudy = appStudy->studyDS();
307
308         bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
309         if ( aLocked ) {
310           SUIT_MessageBox::warn1 ( app->desktop(),
311                                    QObject::tr("WRN_WARNING"),
312                                    QObject::tr("WRN_STUDY_LOCKED"),
313                                    QObject::tr("BUT_OK") );
314           return;
315         }
316
317         // VSR 17/11/04: check if all objects selected belong to GEOM component --> start
318         // modifications of ASV 01.06.05
319         QString parentComp = getParentComponent( aStudy, selected );
320         CORBA::String_var geomIOR = app->orb()->object_to_string( GeometryGUI::GetGeomGen() );
321         QString geomComp = getParentComponent( aStudy->FindObjectIOR( geomIOR.in() ) );
322
323         if ( parentComp != geomComp )  {
324           SUIT_MessageBox::warn1 ( app->desktop(),
325                                   QObject::tr("ERR_ERROR"),
326                                   QObject::tr("NON_GEOM_OBJECTS_SELECTED").arg( getGeometryGUI()->moduleName() ),
327                                   QObject::tr("BUT_OK") );
328           return;
329         }
330         // VSR 17/11/04: check if all objects selected belong to GEOM component <-- finish
331         QString aNameList;
332         int nbSel = 0;
333         //Get Main Objects Names
334         Handle(SALOME_InteractiveObject) anIObject;
335         for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() )
336           {
337             QString aName = It.Value()->getName();
338             if ( aName != "" && aName.ref(0) != '*') {
339               aNameList.append("    - " + aName + "\n");
340               nbSel++;
341             }
342             anIObject = It.Value();
343           }
344         // Append Child Names of Last Selected Object
345         _PTR(SObject) obj ( aStudy->FindObjectID( anIObject->getEntry() ) );
346         for (_PTR(ChildIterator) iit (aStudy->NewChildIterator(obj)); iit->More(); iit->Next()) {
347           _PTR(SObject) child (iit->Value());
348           QString aName = child->GetName();
349           if (aName != "" && aName.ref(0) != '*') {
350             aNameList.append("    - " +  aName + "\n");
351             nbSel++;
352             //append childs child
353             for (_PTR(ChildIterator) iitt(aStudy->NewChildIterator(child)); iitt->More(); iitt->Next()) {
354               _PTR(SObject) childchild(iitt->Value());
355               QString aName = childchild->GetName();
356               if (aName != "" && aName.ref(0) != '*') {
357                aNameList.append("    - " +  aName + "\n");
358                nbSel++;
359                for (_PTR(ChildIterator) itt(aStudy->NewChildIterator(childchild)); itt->More(); itt->Next())
360                  {
361                    _PTR(SObject) childs(itt->Value());
362                    QString aName = childs->GetName();
363                    if (aName != "" && aName.ref(0) != '*') {
364                      aNameList.append("    - " +  aName + "\n");
365                      nbSel++;
366                    }
367                  }
368               }
369             }
370           }
371         } //end of child append
372
373        GEOMBase_aWarningDlg* Dialog = new GEOMBase_aWarningDlg( app->desktop(),  QObject::tr( "GEOM_WRN_WARNING" ), aNameList, nbSel);
374        int r = Dialog->exec();
375
376        if (!r)
377          return;
378
379         //      QAD_Operation* op = new SALOMEGUI_ImportOperation(.....);
380         //      op->start();
381
382         // prepare list of SALOME_Views
383         QPtrList<SALOME_View> views;
384         SALOME_View* view;
385         // fill the list
386         ViewManagerList vmans = app->viewManagers();
387         SUIT_ViewManager* vman;
388         for ( vman = vmans.first(); vman; vman = vmans.next() ) {
389           SUIT_ViewModel* vmod = vman->getViewModel();
390           view = dynamic_cast<SALOME_View*> ( vmod ); // must work for OCC and VTK views
391           if ( view )
392             views.append( view );
393         }
394
395         _PTR(StudyBuilder) aStudyBuilder (aStudy->NewBuilder());
396         _PTR(GenericAttribute) anAttr;
397         GEOM_Displayer* disp = new GEOM_Displayer( appStudy );
398
399         _PTR(SComponent) aGeom ( aStudy->FindComponent("GEOM") );
400           if ( !aGeom )
401             return;     
402
403         // MAIN LOOP OF SELECTED OBJECTS
404         for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
405
406           Handle(SALOME_InteractiveObject) io = It.Value();
407           if ( !io->hasEntry() )
408             continue;
409
410           _PTR(SObject) obj ( aStudy->FindObjectID( io->getEntry() ) );
411
412           // disable removal of "Geometry" component object
413           if ( !strcmp( obj->GetIOR().c_str(), geomIOR ) )
414             continue;
415
416           //If the object has been used to create another one,then it can't be deleted 
417           _PTR(ChildIterator) it (aStudy->NewChildIterator(aGeom));
418           for ( it->InitEx( true ); it->More(); it->Next() ) {
419              _PTR(SObject) chobj (it->Value());
420              if(CheckSubObjectInUse(chobj, obj, aStudy)) return;
421              //check subobjects
422              for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
423                _PTR(SObject) child (it->Value());
424                if(CheckSubObjectInUse( chobj, child, aStudy)) return;
425              }
426            }
427
428           RemoveObjectWithChildren(obj, aStudy, views, disp);
429
430           // Remove objects from Study
431           aStudyBuilder->RemoveObjectWithChildren( obj );
432
433           //deleted = true;
434         } // MAIN LOOP of selected
435
436         selected.Clear();
437         aSelMgr->setSelectedObjects( selected );
438         getGeometryGUI()->updateObjBrowser();
439       } // if ( selected not empty )
440     } // if ( selMgr && appStudy )
441
442     app->updateActions(); //SRN: To update a Save button in the toolbar
443
444   } // if ( app )
445
446
447   //  if ( deleted )
448   //    op->finish();
449   //  else
450   //    op->abort();
451 }
452
453
454 //==============================================================================
455 // function : OnEditCopy()
456 // purpose  :
457 //==============================================================================
458 void GEOMToolsGUI::OnEditCopy()
459 {
460 /*
461  SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection() );
462   GEOM::string_array_var listIOR = new GEOM::string_array;
463
464   const SALOME_ListIO& List = Sel->StoredIObjects();
465
466   myGeomBase->ConvertListOfIOInListOfIOR(List, listIOR);
467
468   Sel->ClearIObjects();
469
470   SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
471   int aStudyID = aStudy->StudyId();
472
473   for (unsigned int ind = 0; ind < listIOR->length();ind++) {
474     GEOM::GEOM_Object_var aShapeInit = myGeom->GetIORFromString(listIOR[ind]);
475     try {
476       GEOM::GEOM_IInsertOperations_var IOp =  myGeom->GetIInsertOperations(aStudyID);
477       GEOM::GEOM_Object_var result = IOp->MakeCopy(aShapeInit);
478       myGeomBase->Display(result);
479     }
480     catch  (const SALOME::SALOME_Exception& S_ex) {
481       QtCatchCorbaException(S_ex);
482     }
483   }
484
485   QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_READY"));
486 */
487 }
488
489
490 //=====================================================================================
491 // function : Import
492 // purpose  : BRep, Iges, Step
493 //=====================================================================================
494 bool GEOMToolsGUI::Import()
495 {
496   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( getGeometryGUI()->getApp() );
497   //SUIT_Application* app = getGeometryGUI()->getApp();
498   if (! app) return false;
499
500   SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
501   if ( !stud ) {
502     cout << "FAILED to cast active study to SalomeApp_Study" << endl;
503     return false;
504   }
505   _PTR(Study) aStudy = stud->studyDS();
506
507   bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
508   if ( aLocked ) {
509     SUIT_MessageBox::warn1 ( app->desktop(),
510                             QObject::tr("WRN_WARNING"),
511                             QObject::tr("WRN_STUDY_LOCKED"),
512                             QObject::tr("BUT_OK") );
513     return false;
514   }
515
516   GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
517   if ( CORBA::is_nil( eng ) ) {
518     SUIT_MessageBox::error1( app->desktop(),
519                             QObject::tr("WRN_WARNING"),
520                             QObject::tr( "GEOM Engine is not started" ),
521                             QObject::tr("BUT_OK") );
522       return false;
523     }
524
525   GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
526   if ( aInsOp->_is_nil() )
527     return false;
528
529   GEOM::GEOM_Object_var anObj;
530
531   // Obtain a list of available import formats
532   FilterMap aMap;
533   QStringList filters;
534   GEOM::string_array_var aFormats, aPatterns;
535   aInsOp->ImportTranslators( aFormats, aPatterns );
536
537   for ( int i = 0, n = aFormats->length(); i < n; i++ ) {
538     aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
539     filters.push_back( (char*)aPatterns[i] );
540   }
541
542   QString fileType;
543   
544   QString fileName = getFileName(app->desktop(), "", aMap, filters,
545                                  tr("GEOM_MEN_IMPORT"), true, fileType, true);
546
547   if (fileType.isEmpty() )
548     {
549       // Trying to detect file type
550       QFileInfo aFileInfo( fileName );
551       QString aPossibleType = (aFileInfo.extension(false)).upper() ;
552
553       if ( (aMap.values()).contains(aPossibleType) )
554         fileType = aPossibleType;
555     }
556
557   if (fileName.isEmpty() || fileType.isEmpty())
558     return false;
559
560   GEOM_Operation* anOp = new GEOM_Operation (app, aInsOp.in());
561   try {
562     SUIT_OverrideCursor wc;
563
564     app->putInfo(tr("GEOM_PRP_LOADING").arg(SUIT_Tools::file(fileName, /*withExten=*/true)));
565
566     anOp->start();
567
568     CORBA::String_var fileN = fileName.latin1();
569     CORBA::String_var fileT = fileType.latin1();
570     anObj = aInsOp->Import(fileN, fileT);
571
572     if ( !anObj->_is_nil() && aInsOp->IsDone() ) {
573       QString aPublishObjName =
574         GEOMBase::GetDefaultName(SUIT_Tools::file(fileName, /*withExten=*/true));
575
576       SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy(aStudy);
577       GeometryGUI::GetGeomGen()->PublishInStudy(aDSStudy,
578                                                 SALOMEDS::SObject::_nil(),
579                                                 anObj,
580                                                 aPublishObjName);
581
582       GEOM_Displayer( stud ).Display( anObj.in() );
583
584       // update data model and object browser
585       getGeometryGUI()->updateObjBrowser( true );
586
587       anOp->commit();
588     }
589     else {
590       anOp->abort();
591       wc.suspend();
592       SUIT_MessageBox::error1( app->desktop(),
593                               QObject::tr( "GEOM_ERROR" ),
594                               QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
595                               QObject::tr("BUT_OK") );
596     }
597   }
598   catch( const SALOME::SALOME_Exception& S_ex ) {
599     //QtCatchCorbaException(S_ex);
600     anOp->abort();
601     return false;
602   }
603
604   app->updateActions(); //SRN: To update a Save button in the toolbar
605
606   return true;
607 }
608
609
610 //=====================================================================================
611 // function : Export
612 // purpose  : BRep, Iges, Step
613 //=====================================================================================
614 bool GEOMToolsGUI::Export()
615 {
616   SalomeApp_Application* app = getGeometryGUI()->getApp();
617   if (!app) return false;
618
619   SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
620   if ( !stud ) {
621     cout << "FAILED to cast active study to SalomeApp_Study" << endl;
622     return false;
623   }
624   _PTR(Study) aStudy = stud->studyDS();
625
626   GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
627   if ( CORBA::is_nil( eng ) ) {
628     SUIT_MessageBox::error1( app->desktop(),
629                              QObject::tr("WRN_WARNING"),
630                              QObject::tr( "GEOM Engine is not started" ),
631                              QObject::tr("BUT_OK") );
632     return false;
633   }
634
635   GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
636   if ( aInsOp->_is_nil() )
637     return false;
638
639   // Obtain a list of available export formats
640   FilterMap aMap;
641   QStringList filters;
642   GEOM::string_array_var aFormats, aPatterns;
643   aInsOp->ExportTranslators( aFormats, aPatterns );
644   for ( int i = 0, n = aFormats->length(); i < n; i++ ) {
645     aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
646     filters.push_back( (char*)aPatterns[i] );
647   }
648
649   // Get selected objects
650   LightApp_SelectionMgr* sm = app->selectionMgr();
651   if ( !sm )
652     return false;
653
654   SALOME_ListIO selectedObjects;
655   sm->selectedObjects( selectedObjects );
656
657   SALOME_ListIteratorOfListIO It( selectedObjects );
658   for(;It.More();It.Next()) {
659     Handle(SALOME_InteractiveObject) IObject = It.Value();
660     Standard_Boolean found;
661     GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject(IObject, found);
662
663     if ( !found || anObj->_is_nil() )
664       continue;
665
666     QString fileType;
667     QString file = getFileName(app->desktop(), QString( IObject->getName() ), aMap, filters,
668                                tr("GEOM_MEN_EXPORT"), false, fileType);
669
670     // User has pressed "Cancel" --> stop the operation
671     if ( file.isEmpty() || fileType.isEmpty() )
672       return false;
673
674     GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
675     try {
676       SUIT_OverrideCursor wc;
677
678       app->putInfo( tr("GEOM_PRP_EXPORT").arg(SUIT_Tools::file( file, /*withExten=*/true )) );
679
680       anOp->start();
681
682
683       aInsOp->Export( anObj, file, fileType.latin1() );
684
685       if ( aInsOp->IsDone() )
686         anOp->commit();
687       else
688         {
689           anOp->abort();
690           wc.suspend();
691           SUIT_MessageBox::error1( app->desktop(),
692                                    QObject::tr( "GEOM_ERROR" ),
693                                    QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
694                                    QObject::tr("BUT_OK") );
695           return false;
696         }
697     }
698     catch (const SALOME::SALOME_Exception& S_ex) {
699       //QtCatchCorbaException(S_ex);
700       anOp->abort();
701       return false;
702     }
703   }
704
705   return true;
706 }
707
708
709 QString GEOMToolsGUI::getParentComponent( _PTR( Study ) study, const SALOME_ListIO& iobjs )
710 {
711   QString parentComp;
712
713   for ( SALOME_ListIteratorOfListIO it( iobjs ); it.More(); it.Next() ) {
714
715     Handle(SALOME_InteractiveObject) io = it.Value();
716     if ( !io->hasEntry() )
717       continue;
718
719     QString compName = getParentComponent( study->FindObjectID( io->getEntry() ) );
720
721     if ( parentComp.isNull() )
722       parentComp = compName;
723     else if ( parentComp.compare( compName) != 0 ) { // objects belonging to different components are selected
724       parentComp = QString::null;
725       break;
726     }
727   }
728
729   return parentComp;
730 }
731
732 QString GEOMToolsGUI::getParentComponent( _PTR( SObject ) obj )
733 {
734   if ( obj ) {
735     _PTR(SComponent) comp = obj->GetFatherComponent();
736     if ( comp ) {
737       _PTR(GenericAttribute) anAttr;
738       if ( comp->FindAttribute( anAttr, "AttributeName") ) {
739         _PTR(AttributeName) aName( anAttr );
740         return QString( aName->Value().c_str() );
741       }
742     }
743   }
744   return QString();
745 }
746
747 //=====================================================================================
748 // function : RemoveObjectWithChildren
749 // purpose  : to be used by OnEditDelete() method
750 //=====================================================================================
751 void GEOMToolsGUI::RemoveObjectWithChildren(_PTR(SObject) obj,
752                                             _PTR(Study) aStudy,
753                                             QPtrList<SALOME_View> views,
754                                             GEOM_Displayer* disp)
755 {
756   // iterate through all children of obj
757   for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
758     _PTR(SObject) child (it->Value());
759     RemoveObjectWithChildren(child, aStudy, views, disp);
760   }
761
762   // erase object and remove it from engine
763   _PTR(GenericAttribute) anAttr;
764   if (obj->FindAttribute(anAttr, "AttributeIOR")) {
765     _PTR(AttributeIOR) anIOR (anAttr);
766
767     // Delete shape in Client
768     const TCollection_AsciiString ASCIor ((char*)anIOR->Value().c_str());
769     getGeometryGUI()->GetShapeReader().RemoveShapeFromBuffer(ASCIor);
770
771     CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(obj);
772     GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
773     if (!CORBA::is_nil(geomObj)) {
774       // Erase graphical object
775       SALOME_View* view = views.first();
776       for (; view; view = views.next()) {
777         disp->Erase(geomObj, true, view);
778       }
779
780       // Remove object from Engine
781       GeometryGUI::GetGeomGen()->RemoveObject( geomObj );
782     }
783   }
784 }
785
786 //=====================================================================================
787 // function : CheckSubObjectInUse
788 // purpose  : to be used by OnEditDelete() method
789 //=====================================================================================
790 bool GEOMToolsGUI::CheckSubObjectInUse(_PTR(SObject) checkobj,
791                                        _PTR(SObject) remobj,
792                                        _PTR(Study) aStudy)
793 {
794   CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(checkobj);
795   GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
796   if( CORBA::is_nil(geomObj) ) 
797     return false;
798
799   GEOM::ListOfGO_var list = geomObj->GetDependency();
800   if( list->length() > 1 )
801     for(int i = 0; i < list->length(); i++ ){
802       CORBA::Object_var corbaObj_rem = GeometryGUI::ClientSObjectToObject(remobj);
803       GEOM::GEOM_Object_var geomObj_rem = GEOM::GEOM_Object::_narrow( corbaObj_rem );
804       if( list[i]->_is_equivalent( geomObj_rem ) ){
805         SalomeApp_Application* app =
806           dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
807
808         SUIT_MessageBox::warn1 ( app->desktop(),
809                                  QObject::tr("WRN_WARNING"),
810                                  QObject::tr("DEP_OBJECT"),
811                                  QObject::tr("BUT_OK") );
812         return true;
813       }
814     }
815
816   return false;
817 }
818
819 //=================================================================================
820 // function : deactivate()
821 // purpose  : Called when GEOM component is deactivated
822 //=================================================================================
823 void GEOMToolsGUI::deactivate()
824 {
825   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
826   if ( app ) {
827     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
828     GEOM_Displayer aDisp (appStudy);
829     aDisp.GlobalSelection();
830     getGeometryGUI()->setLocalSelectionMode(GEOM_ALLOBJECTS);
831   }
832 }
833
834 //=====================================================================================
835 // EXPORTED METHODS
836 //=====================================================================================
837 extern "C"
838 {
839 GEOMTOOLSGUI_EXPORT
840   GEOMGUI* GetLibGUI( GeometryGUI* parent )
841   {
842     return new GEOMToolsGUI( parent );
843   }
844 }