Salome HOME
Implementation "21042: EDF 1600 ALL: Rename objects in the OB" issue.
[modules/geom.git] / src / GEOMToolsGUI / GEOMToolsGUI.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : GEOMBase_Tools.cxx
25 // Author : Damien COQUERET, Open CASCADE S.A.S.
26 //
27 #include "GEOMToolsGUI.h"
28 #include "GEOMToolsGUI_DeleteDlg.h"
29
30 #include <GeometryGUI.h>
31 #include "GeometryGUI_Operations.h"
32 #include <GEOMBase.h>
33 #include <GEOM_Operation.h>
34 #include <GEOM_Displayer.h>
35
36 #include <SUIT_Session.h>
37 #include <SUIT_OverrideCursor.h>
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_Tools.h>
40 #include <SUIT_FileDlg.h>
41 #include <SUIT_Desktop.h>
42 #include <SUIT_ViewModel.h>
43 #include <SUIT_ViewManager.h>
44
45 #include <SalomeApp_Application.h>
46 #include <SalomeApp_Study.h>
47 #include <LightApp_SelectionMgr.h>
48 #include <GEOMImpl_Types.hxx>
49
50 #include <SALOME_ListIO.hxx>
51 #include <SALOME_ListIteratorOfListIO.hxx>
52 #include <SALOME_Prs.h>
53
54 // QT Includes
55 #include <QApplication>
56 #include <QMap>
57 #include <QRegExp>
58
59 // OCCT Includes
60 #include <TCollection_AsciiString.hxx>
61
62 typedef QMap<QString, QString> FilterMap;
63 static QString lastUsedFilter;
64
65 //=======================================================================
66 // function : getFileName
67 // purpose  : Selection of a file name for Import/Export. Returns also
68 //            the selected file type code through <filter> argument.
69 //=======================================================================
70 static QString getFileName( QWidget*           parent,
71                             const QString&     initial,
72                             const FilterMap&   filterMap,
73                             const QStringList& filters,
74                             const QString&     caption,
75                             bool               open,
76                             QString&           format,
77                             bool               showCurrentDirInitially = false )
78 {
79   //QStringList filters;
80   QString aBrepFilter;
81   for ( FilterMap::const_iterator it = filterMap.begin(); it != filterMap.end(); ++it ) {
82     //filters.push_back( it.key() );
83     if ( it.key().contains( "BREP", Qt::CaseInsensitive ) )
84       aBrepFilter = it.key();
85   }
86
87   SUIT_FileDlg* fd = new SUIT_FileDlg( parent, open, true, true );
88   if ( !caption.isEmpty() )
89     fd->setWindowTitle( caption );
90
91   if ( !initial.isEmpty() )
92     fd->selectFile( initial );
93   
94   if ( showCurrentDirInitially && SUIT_FileDlg::getLastVisitedPath().isEmpty() )
95     fd->setDirectory( QDir::currentPath() );
96
97   fd->setFilters( filters );
98   
99   if ( !lastUsedFilter.isEmpty() && filterMap.contains( lastUsedFilter ) ) {
100     fd->selectFilter( lastUsedFilter );
101   }
102   else if ( !aBrepFilter.isEmpty() ) {
103     fd->selectFilter( aBrepFilter );
104   }
105
106   QString filename;
107   if ( fd->exec() == QDialog::Accepted ) {
108     filename = fd->selectedFile();
109     format = filterMap[fd->selectedFilter()];
110     lastUsedFilter = fd->selectedFilter();
111   }
112
113   delete fd;
114   qApp->processEvents();
115   return filename;
116 }
117
118 //=======================================================================
119 // function : getFileNames
120 // purpose  : Select list of files for Import operation. Returns also
121 //            the selected file type code through <format> argument.
122 //=======================================================================
123 static QStringList getFileNames( QWidget*           parent,
124                                  const QString&     initial,
125                                  const FilterMap&   filterMap,
126                                  const QString&     caption,
127                                  QString&           format,
128                                  bool               showCurrentDirInitially = false)
129 {
130   QString aBrepFilter;
131   QStringList allFilters;
132   QStringList filters;
133   QRegExp re( "\\((.*)\\)" );
134   re.setMinimal( true );
135   for ( FilterMap::const_iterator it = filterMap.begin(); it != filterMap.end(); ++it ) {
136     if ( it.value().contains( "BREP", Qt::CaseInsensitive ) && aBrepFilter.isEmpty() )
137       aBrepFilter = it.key();
138     filters.append( it.key() );
139     int pos = 0;
140     while ( re.indexIn( it.key(), pos ) >= 0 ) {
141       QString f = re.cap(1);
142       pos = re.pos() + f.length() + 2;
143       allFilters.append( f.simplified() );
144     }
145   }
146   filters.append( QObject::tr( "GEOM_ALL_IMPORT_FILES" ).arg( allFilters.join( " " ) ) );
147   
148   SUIT_FileDlg fd( parent, true, true, true );
149   fd.setFileMode( SUIT_FileDlg::ExistingFiles );     
150   if ( !caption.isEmpty() )
151     fd.setWindowTitle( caption );
152   if ( !initial.isEmpty() )
153     fd.selectFile( initial );
154   
155   if ( showCurrentDirInitially && SUIT_FileDlg::getLastVisitedPath().isEmpty() )
156     fd.setDirectory( QDir::currentPath() );
157   
158   fd.setFilters( filters );
159   
160   if ( !lastUsedFilter.isEmpty() && filterMap.contains( lastUsedFilter ) )
161     fd.selectFilter( lastUsedFilter );
162   else if ( !aBrepFilter.isEmpty() )
163     fd.selectFilter( aBrepFilter );
164
165   QStringList filenames;
166   if ( fd.exec() ) {
167     filenames = fd.selectedFiles();
168     format = filterMap.contains( fd.selectedFilter() ) ? filterMap[ fd.selectedFilter() ] : QString();
169     lastUsedFilter = fd.selectedFilter();
170   }
171   qApp->processEvents();
172   return filenames;
173 }
174
175 //=======================================================================
176 // function : getParentComponent
177 // purpose  : Get object's parent component entry
178 //=======================================================================
179 static QString getParentComponent( _PTR( SObject ) obj )
180 {
181   if ( obj ) {
182     _PTR(SComponent) comp = obj->GetFatherComponent();
183     if ( comp )
184       return QString( comp->GetID().c_str() );
185   }
186   return QString();
187 }
188
189 //=====================================================================================
190 // function : inUse
191 // purpose  : check if the object(s) passed as the the second arguments are used
192 //            by the other objects in the study
193 //=====================================================================================
194 static bool inUse( _PTR(Study) study, const QString& component, const QMap<QString,QString>& objects )
195 {
196   _PTR(SObject) comp = study->FindObjectID( component.toLatin1().data() );
197   if ( !comp )
198     return false;
199
200   // collect all GEOM objects being deleted
201   QMap<QString, GEOM::GEOM_Object_var> gobjects;
202   QMap<QString, QString>::ConstIterator oit;
203   std::list<_PTR(SObject)> aSelectedSO;
204   for ( oit = objects.begin(); oit != objects.end(); ++oit ) {
205     _PTR(SObject) so = study->FindObjectID( oit.key().toLatin1().data() );
206     if ( !so )
207       continue;
208     aSelectedSO.push_back(so);
209     CORBA::Object_var corbaObj_rem = GeometryGUI::ClientSObjectToObject( so );
210     GEOM::GEOM_Object_var geomObj_rem = GEOM::GEOM_Object::_narrow( corbaObj_rem );
211     if( CORBA::is_nil( geomObj_rem ) ) 
212       continue;
213     gobjects.insert( oit.key(), geomObj_rem );
214   }
215
216   // Search References with other Modules
217   std::list< _PTR(SObject) >::iterator itSO = aSelectedSO.begin();
218   for ( ; itSO != aSelectedSO.end(); ++itSO ) {
219     std::vector<_PTR(SObject)> aReferences = study->FindDependances( *itSO  );    
220     int aRefLength = aReferences.size();
221     if (aRefLength) {
222       for (int i = 0; i < aRefLength; i++) {
223         _PTR(SObject) firstSO( aReferences[i] );
224         _PTR(SComponent) aComponent = firstSO->GetFatherComponent();
225         QString type = aComponent->ComponentDataType().c_str();
226         if ( type == "SMESH" )
227           return true;
228       }
229     }
230   }
231
232   // browse through all GEOM data tree
233   _PTR(ChildIterator) it ( study->NewChildIterator( comp ) );
234   for ( it->InitEx( true ); it->More(); it->Next() ) {
235     _PTR(SObject) child( it->Value() );
236     CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject( child );
237     GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
238     if( CORBA::is_nil( geomObj ) ) 
239       continue;
240
241     GEOM::ListOfGO_var list = geomObj->GetDependency();
242     if( list->length() == 0 ) 
243       continue;
244
245     for( int i = 0; i < list->length(); i++ ) {
246       bool depends = false;
247       bool deleted = false;
248       QMap<QString, GEOM::GEOM_Object_var>::Iterator git;
249       for ( git = gobjects.begin(); git != gobjects.end() && ( !depends || !deleted ); ++git ) {
250         depends = depends || list[i]->_is_equivalent( *git );
251         deleted = deleted || git.key() == child->GetID().c_str() ;//geomObj->_is_equivalent( *git );
252       }
253       if ( depends && !deleted )
254         return true;
255     }
256   }
257   return false;
258 }
259
260
261 //=======================================================================
262 // function : GEOMToolsGUI()
263 // purpose  : Constructor
264 //=======================================================================
265 GEOMToolsGUI::GEOMToolsGUI( GeometryGUI* parent )
266 : GEOMGUI( parent )
267 {
268 }
269
270
271 //=======================================================================
272 // function : ~GEOMToolsGUI()
273 // purpose  : Destructor
274 //=======================================================================
275 GEOMToolsGUI::~GEOMToolsGUI()
276 {
277 }
278
279
280 //=======================================================================
281 // function : OnGUIEvent()
282 // purpose  :
283 //=======================================================================
284 bool GEOMToolsGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent)
285 {
286   getGeometryGUI()->EmitSignalDeactivateDialog();
287
288   switch ( theCommandID ) {
289   case GEOMOp::OpDelete:         // EDIT - DELETE
290     OnEditDelete();
291     break;
292   case GEOMOp::OpImport:         // FILE - IMPORT
293     Import();
294     break;
295   case GEOMOp::OpExport:         // FILE - EXPORT
296     Export();
297     break;
298   case GEOMOp::OpCheckGeom:      // TOOLS - CHECK GEOMETRY
299     OnCheckGeometry();
300     break;
301   case GEOMOp::OpSelectVertex:   // POPUP - SELECT ONLY - VERTEX
302     OnSelectOnly( GEOM_POINT );
303     break;
304   case GEOMOp::OpSelectEdge:     // POPUP - SELECT ONLY - EDGE
305     OnSelectOnly( GEOM_EDGE );
306     break;
307   case GEOMOp::OpSelectWire:     // POPUP - SELECT ONLY - WIRE
308     OnSelectOnly( GEOM_WIRE );
309     break;
310   case GEOMOp::OpSelectFace:     // POPUP - SELECT ONLY - FACE
311     OnSelectOnly( GEOM_FACE );
312     break;
313   case GEOMOp::OpSelectShell:    // POPUP - SELECT ONLY - SHELL
314     OnSelectOnly( GEOM_SHELL );
315     break;
316   case GEOMOp::OpSelectSolid:    // POPUP - SELECT ONLY - SOLID
317     OnSelectOnly( GEOM_SOLID );
318     break;
319   case GEOMOp::OpSelectCompound: // POPUP - SELECT ONLY - COMPOUND
320     OnSelectOnly( GEOM_COMPOUND );
321     break;
322   case GEOMOp::OpSelectAll:      // POPUP - SELECT ONLY - SELECT ALL
323     OnSelectOnly( GEOM_ALLOBJECTS );
324     break;
325   case GEOMOp::OpDeflection:     // POPUP - DEFLECTION ANGLE
326     OnDeflection();
327     break;
328   case GEOMOp::OpColor:          // POPUP - COLOR
329     OnColor();
330     break;
331   case GEOMOp::OpTransparency:   // POPUP - TRANSPARENCY
332     OnTransparency();
333     break;
334   case GEOMOp::OpIncrTransparency: // SHORTCUT   - INCREASE TRANSPARENCY
335     OnChangeTransparency( true );
336     break;
337   case GEOMOp::OpDecrTransparency: // SHORTCUT   - DECREASE TRANSPARENCY
338     OnChangeTransparency( false );
339     break;
340   case GEOMOp::OpIsos:           // POPUP - ISOS
341     OnNbIsos();
342     break;
343   case GEOMOp::OpIncrNbIsos:     // SHORTCUT   - INCREASE NB ISOLINES
344     OnNbIsos( INCR );
345     break;
346   case GEOMOp::OpDecrNbIsos:     // SHORTCUT   - DECREASE NB ISOLINES
347     OnNbIsos( DECR );
348     break;
349   case GEOMOp::OpAutoColor:      // POPUP - AUTO COLOR
350     OnAutoColor();
351     break;
352   case GEOMOp::OpNoAutoColor:    // POPUP - DISABLE AUTO COLOR
353     OnDisableAutoColor();
354     break;
355   case GEOMOp::OpShowChildren:   // POPUP - SHOW CHILDREN
356   case GEOMOp::OpHideChildren:   // POPUP - HIDE CHILDREN
357     OnShowHideChildren( theCommandID == GEOMOp::OpShowChildren );
358     break;
359   case GEOMOp::OpPointMarker:    // POPUP - POINT MARKER
360     OnPointMarker();
361     break;
362   case GEOMOp::OpUnpublishObject:// POPUP - UNPUBLISH
363     OnUnpublishObject();
364     break;
365   case GEOMOp::OpPublishObject:// GEOM ROOT OBJECT - POPUP - PUBLISH
366     OnPublishObject();
367     break;
368   default:
369     SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
370     break;
371   }
372   return true;
373 }
374
375
376 //===============================================================================
377 // function : OnEditDelete()
378 // purpose  :
379 //===============================================================================
380 void GEOMToolsGUI::OnEditDelete()
381 {
382   SALOME_ListIO selected;
383   SalomeApp_Application* app =
384     dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
385   if ( !app )
386     return;
387
388   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
389   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
390   if ( !aSelMgr || !appStudy )
391     return;
392
393   // get selection
394   aSelMgr->selectedObjects( selected, "ObjectBrowser", false );
395   if ( selected.IsEmpty() )
396     return;
397
398   _PTR(Study) aStudy = appStudy->studyDS();
399   
400   // check if study is locked
401   if ( _PTR(AttributeStudyProperties)( aStudy->GetProperties() )->IsLocked() ) {
402     SUIT_MessageBox::warning( app->desktop(),
403                               tr("WRN_WARNING"),
404                               tr("WRN_STUDY_LOCKED") );
405     return; // study is locked
406   }
407   
408   // get GEOM component
409   CORBA::String_var geomIOR = app->orb()->object_to_string( GeometryGUI::GetGeomGen() );
410   QString geomComp = getParentComponent( aStudy->FindObjectIOR( geomIOR.in() ) );
411
412   // check each selected object: if belongs to GEOM, if not reference...
413   QMap<QString,QString> toBeDeleted;
414   QMap<QString,QString> allDeleted;
415   bool isComponentSelected = false;
416   
417   for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
418     Handle(SALOME_InteractiveObject) anIObject = It.Value();
419     if ( !anIObject->hasEntry() )
420       continue; // invalid object
421     // ...
422     QString entry = anIObject->getEntry();
423     _PTR(SObject) obj = aStudy->FindObjectID( entry.toLatin1().data() );
424     // check parent component
425     QString parentComp = getParentComponent( obj );
426     if ( parentComp != geomComp )  {
427       SUIT_MessageBox::warning( app->desktop(),
428                                 QObject::tr("ERR_ERROR"),
429                                 QObject::tr("NON_GEOM_OBJECTS_SELECTED").arg( getGeometryGUI()->moduleName() ) );
430       return; // not GEOM object selected
431     }
432
433     ///////////////////////////////////////////////////////
434     // if GEOM component is selected, so skip other checks
435     if ( isComponentSelected ) continue; 
436     ///////////////////////////////////////////////////////
437         
438     // check if object is reference
439     _PTR(SObject) refobj;
440     if ( obj && obj->ReferencedObject( refobj ) ) {
441       // get the main object by reference IPAL 21354
442       obj = refobj;
443       entry = obj->GetID().c_str();
444     }
445     // ...
446     QString aName = obj->GetName().c_str();
447     if ( entry == geomComp ) {
448       // GEOM component is selected, skip other checks
449       isComponentSelected = true;
450       continue;
451     }
452     toBeDeleted.insert( entry, aName );
453     allDeleted.insert( entry, aName ); // skip GEOM component
454     // browse through all children recursively
455     _PTR(ChildIterator) it ( aStudy->NewChildIterator( obj ) );
456     for ( it->InitEx( true ); it->More(); it->Next() ) {
457       _PTR(SObject) child( it->Value() );
458       if ( child && child->ReferencedObject( refobj ) )
459         continue; // skip references
460       aName = child->GetName().c_str();
461       if ( !aName.isEmpty() )
462         allDeleted.insert( child->GetID().c_str(), aName );
463     }
464   }
465   
466   // is there is anything to delete?
467   if ( !isComponentSelected && allDeleted.count() <= 0 )
468     return; // nothing to delete
469
470   // show confirmation dialog box
471   GEOMToolsGUI_DeleteDlg dlg( app->desktop(), allDeleted, isComponentSelected );
472   if ( !dlg.exec() )
473     return; // operation is cancelled by user
474   
475   // get currently opened views
476   QList<SALOME_View*> views;
477   SALOME_View* view;
478   ViewManagerList vmans = app->viewManagers();
479   SUIT_ViewManager* vman;
480   foreach ( vman, vmans ) {
481     SUIT_ViewModel* vmod = vman->getViewModel();
482     view = dynamic_cast<SALOME_View*> ( vmod ); // must work for OCC and VTK views
483     if ( view )
484       views.append( view );
485   }
486   
487   _PTR(StudyBuilder) aStudyBuilder (aStudy->NewBuilder());
488   GEOM_Displayer* disp = new GEOM_Displayer( appStudy );
489   
490   if ( isComponentSelected ) {
491     // GEOM component is selected: delete all objects recursively
492     _PTR(SObject) comp = aStudy->FindObjectID( geomComp.toLatin1().data() );
493     if ( !comp )
494       return;
495     _PTR(ChildIterator) it ( aStudy->NewChildIterator( comp ) );
496     // remove top-level objects only
497     for ( it->InitEx( false ); it->More(); it->Next() ) {
498       _PTR(SObject) child( it->Value() );
499       // remove object from GEOM engine
500       removeObjectWithChildren( child, aStudy, views, disp );
501       // remove object from study
502       aStudyBuilder->RemoveObjectWithChildren( child );
503     }
504   }
505   else {
506     // GEOM component is not selected: check if selected objects are in use
507     if ( inUse( aStudy, geomComp, allDeleted ) ) {
508       SUIT_MessageBox::warning( app->desktop(),
509                                 QObject::tr("WRN_WARNING"),
510                                 QObject::tr("DEP_OBJECT") );
511       return; // object(s) in use
512     }
513     // ... and then delete all objects
514     QMap<QString, QString>::Iterator it;
515     for ( it = toBeDeleted.begin(); it != toBeDeleted.end(); ++it ) {
516       _PTR(SObject) obj ( aStudy->FindObjectID( it.key().toLatin1().data() ) );
517       // remove object from GEOM engine
518       removeObjectWithChildren( obj, aStudy, views, disp );
519       // remove objects from study
520       aStudyBuilder->RemoveObjectWithChildren( obj );      
521     }
522   }
523
524   
525   
526   selected.Clear();
527   aSelMgr->setSelectedObjects( selected );
528   getGeometryGUI()->updateObjBrowser();
529   app->updateActions(); //SRN: To update a Save button in the toolbar
530 }
531
532 //=====================================================================================
533 // function : Import
534 // purpose  : BRep, Iges, Step, ...
535 //=====================================================================================
536 bool GEOMToolsGUI::Import()
537 {
538   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( getGeometryGUI()->getApp() );
539   if ( !app ) return false;
540
541   SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
542   if ( !stud ) {
543     MESSAGE ( "FAILED to cast active study to SalomeApp_Study" );
544     return false;
545   }
546   _PTR(Study) aStudy = stud->studyDS();
547
548   // check if study is locked
549   bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
550   if ( aLocked ) {
551     SUIT_MessageBox::warning( app->desktop(),
552                               QObject::tr("WRN_WARNING"),
553                               QObject::tr("WRN_STUDY_LOCKED") );
554     return false;
555   }
556
557   // check if GEOM engine is available
558   GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
559   if ( CORBA::is_nil( eng ) ) {
560     SUIT_MessageBox::critical( app->desktop(),
561                                QObject::tr("WRN_WARNING"),
562                                QObject::tr( "GEOM Engine is not started" ) );
563     return false;
564   }
565
566   GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
567   if ( aInsOp->_is_nil() )
568     return false;
569
570   // obtain a list of available import formats
571   FilterMap aMap;
572   GEOM::string_array_var aFormats, aPatterns;
573   aInsOp->ImportTranslators( aFormats, aPatterns );
574
575   for ( int i = 0, n = aFormats->length(); i < n; i++ )
576     aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
577
578   // select files to be imported
579   QString fileType;
580   QStringList fileNames = getFileNames( app->desktop(), "", aMap,
581                                         tr( "GEOM_MEN_IMPORT" ), fileType, true );
582
583   // set Wait cursor
584   SUIT_OverrideCursor wc;
585
586   if ( fileNames.count() == 0 )
587     return false; // nothing selected, return
588
589   QStringList errors;
590
591   QList< GEOM::GEOM_Object_var > objsForDisplay;
592
593   QStringList anEntryList;
594
595   // iterate through all selected files
596
597   SUIT_MessageBox::StandardButton igesAnswer = SUIT_MessageBox::NoButton;
598   SUIT_MessageBox::StandardButton acisAnswer = SUIT_MessageBox::NoButton;
599
600   for ( int i = 0; i < fileNames.count(); i++ ) {
601     QString fileName = fileNames[i];
602
603     if ( fileName.isEmpty() )
604       continue;
605
606     QString aCurrentType;
607     if ( fileType.isEmpty() ) {
608       // file type is not defined, try to detect
609       QString ext = QFileInfo( fileName ).suffix().toUpper();
610       QRegExp re( "\\*\\.(\\w+)" );
611       for ( FilterMap::const_iterator it = aMap.begin(); 
612             it != aMap.end() && aCurrentType.isEmpty(); ++it ) {
613         int pos = 0;
614         while ( re.indexIn( it.key(), pos ) >= 0 ) {
615           QString f = re.cap(1).trimmed().toUpper();
616           if ( ext == f ) { aCurrentType = it.value(); break; }
617           pos = re.pos() + re.cap(1).length() + 2;
618         }
619       }
620     }
621     else {
622       aCurrentType = fileType;
623     }
624
625     if ( aCurrentType.isEmpty() ) {
626       errors.append( QString( "%1 : %2" ).arg( fileName ).arg( tr( "GEOM_UNSUPPORTED_TYPE" ) ) );
627       continue;
628     }
629
630     GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
631     try {
632       app->putInfo( tr( "GEOM_PRP_LOADING" ).arg( SUIT_Tools::file( fileName, /*withExten=*/true ) ) );
633       anOp->start();
634
635       CORBA::String_var fileN = fileName.toLatin1().constData();
636       CORBA::String_var fileT = aCurrentType.toLatin1().constData();
637
638       // skl 29.05.2009
639       if ( aCurrentType == "IGES" ) {
640         GEOM::GEOM_Object_var anObj = aInsOp->ImportFile( fileN, "IGES_UNIT" );
641         bool needConvert = false;
642         TCollection_AsciiString aUnitName = aInsOp->GetErrorCode();
643         if ( aUnitName.SubString( 1, 4 ) == "UNIT" )
644           needConvert = aUnitName.SubString( 6, aUnitName.Length() ) != "M";
645
646         if ( needConvert ) {
647           if ( igesAnswer == SUIT_MessageBox::NoToAll ) {
648             // converting for all files is already approved
649             fileT = "IGES_SCALE";
650           }
651           else if ( igesAnswer != SUIT_MessageBox::YesToAll ) {
652             SUIT_MessageBox::StandardButtons btns = SUIT_MessageBox::Yes | SUIT_MessageBox::No;
653             if ( i < fileNames.count()-1 ) btns = btns | SUIT_MessageBox::YesToAll | SUIT_MessageBox::NoToAll;
654             igesAnswer = SUIT_MessageBox::question( app->desktop(),
655                                                     "Question",//tr("WRN_WARNING"),
656                                                     tr("GEOM_SCALE_DIMENSIONS"),
657                                                     btns | SUIT_MessageBox::Cancel,
658                                                     SUIT_MessageBox::No );
659             switch ( igesAnswer ) {
660             case SUIT_MessageBox::Cancel:
661               return false;                // cancel (break) import operation
662             case SUIT_MessageBox::Yes:
663             case SUIT_MessageBox::YesToAll:
664               break;                       // scaling is confirmed
665             case SUIT_MessageBox::No:
666             case SUIT_MessageBox::NoAll:
667               fileT = "IGES_SCALE";
668             default:
669               break;                       // scaling is rejected
670             } // switch ( igesAnswer )
671           } // if ( igeAnswer != NoToAll )
672         } // if ( needConvert )
673       } // if ( aCurrentType == "IGES" )
674       else if ( aCurrentType == "ACIS" ) {
675         if ( acisAnswer != SUIT_MessageBox::YesToAll && acisAnswer != SUIT_MessageBox::NoToAll ) {
676           SUIT_MessageBox::StandardButtons btns = SUIT_MessageBox::Yes | SUIT_MessageBox::No;
677           if ( i < fileNames.count()-1 ) btns = btns | SUIT_MessageBox::YesToAll | SUIT_MessageBox::NoToAll;
678           acisAnswer = SUIT_MessageBox::question( app->desktop(),
679                                                   "Question",//tr("WRN_WARNING"),
680                                                   tr("GEOM_PUBLISH_NAMED_SHAPES"),
681                                                   btns | SUIT_MessageBox::Cancel,
682                                                   SUIT_MessageBox::No );
683           if ( acisAnswer == SUIT_MessageBox::Cancel )
684             return false; // cancel (break) import operation
685         } // if ( acisAnswer != YesToAll && acisAnswer != NoToAll )
686       } // else if ( aCurrentType == "ACIS" )
687       
688       GEOM::GEOM_Object_var anObj = aInsOp->ImportFile( fileN, fileT );
689
690       if ( !anObj->_is_nil() && aInsOp->IsDone() ) {
691         QString aPublishObjName = 
692           GEOMBase::GetDefaultName( SUIT_Tools::file( fileName, /*withExten=*/true ) );
693         
694         SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy( aStudy );
695         SALOMEDS::SObject_var aSO = GeometryGUI::GetGeomGen()->PublishInStudy( aDSStudy,
696                                                                                SALOMEDS::SObject::_nil(),
697                                                                                anObj,
698                                                                                aPublishObjName.toLatin1().constData() );
699         if( ( !aSO->_is_nil() ) )
700           anEntryList.append( aSO->GetID() );
701         
702         objsForDisplay.append( anObj );
703         
704         if ( aCurrentType == "ACIS" ) {
705           if ( acisAnswer == SUIT_MessageBox::Yes || acisAnswer == SUIT_MessageBox::YesToAll )
706             GeometryGUI::GetGeomGen()->PublishNamedShapesInStudy( aDSStudy, anObj );
707         }
708
709         anOp->commit();
710       }
711       else {
712         anOp->abort();
713         errors.append( QString( "%1 : %2" ).arg( fileName ).arg( aInsOp->GetErrorCode() ) );
714       }
715     }
716     catch( const SALOME::SALOME_Exception& S_ex ) {
717       anOp->abort();
718       errors.append( QString( "%1 : %2" ).arg( fileName ).arg( tr( "GEOM_UNKNOWN_IMPORT_ERROR" ) ) );
719     }
720   }
721
722   // update object browser
723   getGeometryGUI()->updateObjBrowser( true );
724
725   // browse published objects
726   app->browseObjects( anEntryList );
727
728   // display imported model (if only one file is selected)
729   if ( objsForDisplay.count() == 1 )
730     GEOM_Displayer( stud ).Display( objsForDisplay[0].in() );
731
732   if ( errors.count() > 0 ) {
733     SUIT_MessageBox::critical( app->desktop(),
734                                QObject::tr( "GEOM_ERROR" ),
735                                QObject::tr( "GEOM_IMPORT_ERRORS" ) + "\n" + errors.join( "\n" ) );
736   }
737
738   app->updateActions(); //SRN: To update a Save button in the toolbar
739
740   return objsForDisplay.count() > 0;
741 }
742
743
744 //=====================================================================================
745 // function : Export
746 // purpose  : BRep, Iges, Step
747 //=====================================================================================
748 bool GEOMToolsGUI::Export()
749 {
750   SalomeApp_Application* app = getGeometryGUI()->getApp();
751   if (!app) return false;
752
753   SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
754   if ( !stud ) {
755     MESSAGE ( "FAILED to cast active study to SalomeApp_Study" );
756     return false;
757   }
758   _PTR(Study) aStudy = stud->studyDS();
759
760   GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
761   if ( CORBA::is_nil( eng ) ) {
762     SUIT_MessageBox::critical( app->desktop(),
763                                QObject::tr("WRN_WARNING"),
764                                QObject::tr( "GEOM Engine is not started" ) );
765     return false;
766   }
767
768   GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
769   if ( aInsOp->_is_nil() )
770     return false;
771
772   // Obtain a list of available export formats
773   FilterMap aMap;
774   QStringList filters;
775   GEOM::string_array_var aFormats, aPatterns;
776   aInsOp->ExportTranslators( aFormats, aPatterns );
777   for ( int i = 0, n = aFormats->length(); i < n; i++ ) {
778     aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
779     filters.push_back( (char*)aPatterns[i] );
780   }
781
782   // Get selected objects
783   LightApp_SelectionMgr* sm = app->selectionMgr();
784   if ( !sm )
785     return false;
786
787   SALOME_ListIO selectedObjects;
788   sm->selectedObjects( selectedObjects );
789   bool appropriateObj = false;
790
791   SALOME_ListIteratorOfListIO It( selectedObjects );
792   for(;It.More();It.Next()) {
793     Handle(SALOME_InteractiveObject) IObject = It.Value();
794     GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject( IObject );
795
796     if ( anObj->_is_nil() )
797       continue;
798
799     QString fileType;
800     QString file = getFileName(app->desktop(), QString( IObject->getName() ), aMap, filters,
801                                tr("GEOM_MEN_EXPORT"), false, fileType, true);
802
803     // User has pressed "Cancel" --> stop the operation
804     if ( file.isEmpty() || fileType.isEmpty() )
805       return false;
806
807     GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
808     try {
809       SUIT_OverrideCursor wc;
810
811       app->putInfo( tr("GEOM_PRP_EXPORT").arg(SUIT_Tools::file( file, /*withExten=*/true )) );
812
813       anOp->start();
814
815
816       aInsOp->Export( anObj, file.toStdString().c_str(), fileType.toLatin1().constData() );
817
818       if ( aInsOp->IsDone() )
819         anOp->commit();
820       else
821         {
822           anOp->abort();
823           wc.suspend();
824           SUIT_MessageBox::critical( app->desktop(),
825                                      QObject::tr( "GEOM_ERROR" ),
826                                      QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ) );
827           return false;
828         }
829     }
830     catch (const SALOME::SALOME_Exception& S_ex) {
831       //QtCatchCorbaException(S_ex);
832       anOp->abort();
833       return false;
834     }
835     appropriateObj = true;
836   }
837
838   if ( !appropriateObj )
839     SUIT_MessageBox::warning( app->desktop(),
840                               QObject::tr("WRN_WARNING"),
841                               QObject::tr("GEOM_WRN_NO_APPROPRIATE_SELECTION") );
842   return true;
843 }
844
845 //=====================================================================================
846 // function : RemoveObjectWithChildren
847 // purpose  : used by OnEditDelete() method
848 //=====================================================================================
849 void GEOMToolsGUI::removeObjectWithChildren(_PTR(SObject) obj,
850                                             _PTR(Study) aStudy,
851                                             QList<SALOME_View*> views,
852                                             GEOM_Displayer* disp)
853 {
854   // iterate through all children of obj
855   for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
856     _PTR(SObject) child (it->Value());
857     removeObjectWithChildren(child, aStudy, views, disp);
858   }
859
860   // erase object and remove it from engine
861   _PTR(GenericAttribute) anAttr;
862   if (obj->FindAttribute(anAttr, "AttributeIOR")) {
863     _PTR(AttributeIOR) anIOR (anAttr);
864     
865     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
866
867     // Delete shape in Client
868     const TCollection_AsciiString ASCIor ((char*)anIOR->Value().c_str());
869     getGeometryGUI()->GetShapeReader().RemoveShapeFromBuffer(ASCIor);
870
871     CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(obj);
872     GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
873     if (!CORBA::is_nil(geomObj)) {
874       
875       //Remove visual properties of the object
876       appStudy->removeObjectFromAll(obj->GetID().c_str());
877       
878       // Erase graphical object
879       QListIterator<SALOME_View*> it( views );
880       while ( it.hasNext() )
881         if ( SALOME_View* view = it.next() )
882           disp->Erase(geomObj, true, view);
883       
884       // Remove object from Engine
885       // We can't directly remove object from engine. All we can do is to unpublish the object
886       // from the study. Another client could be using the object.
887       // Unpublishing is done just after in aStudyBuilder->RemoveObjectWithChildren( child );
888       //GeometryGUI::GetGeomGen()->RemoveObject( geomObj );
889     }
890   }
891 }
892
893 //=================================================================================
894 // function : deactivate()
895 // purpose  : Called when GEOM component is deactivated
896 //=================================================================================
897 void GEOMToolsGUI::deactivate()
898 {
899   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
900   if ( app ) {
901     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
902     GEOM_Displayer aDisp (appStudy);
903     aDisp.GlobalSelection();
904     getGeometryGUI()->setLocalSelectionMode(GEOM_ALLOBJECTS);
905   }
906 }
907
908 //=====================================================================================
909 // EXPORTED METHODS
910 //=====================================================================================
911 extern "C"
912 {
913 #ifdef WIN32
914   __declspec( dllexport )
915 #endif
916   GEOMGUI* GetLibGUI( GeometryGUI* parent )
917   {
918     return new GEOMToolsGUI( parent );
919   }
920 }