Back to index

salome-geom  6.5.0
GEOMToolsGUI.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // GEOM GEOMGUI : GUI for Geometry component
00024 // File   : GEOMBase_Tools.cxx
00025 // Author : Damien COQUERET, Open CASCADE S.A.S.
00026 
00027 #include "GEOMToolsGUI.h"
00028 #include "GEOMToolsGUI_DeleteDlg.h"
00029 
00030 #include <GeometryGUI.h>
00031 #include "GeometryGUI_Operations.h"
00032 #include <GEOMBase.h>
00033 #include <GEOM_Operation.h>
00034 #include <GEOM_Displayer.h>
00035 
00036 #include <SUIT_Session.h>
00037 #include <SUIT_OverrideCursor.h>
00038 #include <SUIT_MessageBox.h>
00039 #include <SUIT_Tools.h>
00040 #include <SUIT_FileDlg.h>
00041 #include <SUIT_Desktop.h>
00042 #include <SUIT_ViewModel.h>
00043 #include <SUIT_ViewManager.h>
00044 
00045 #include <SalomeApp_Application.h>
00046 #include <SalomeApp_Study.h>
00047 #include <LightApp_SelectionMgr.h>
00048 #include <GEOMImpl_Types.hxx>
00049 
00050 #include <SALOME_ListIO.hxx>
00051 #include <SALOME_ListIteratorOfListIO.hxx>
00052 #include <SALOME_Prs.h>
00053 
00054 // QT Includes
00055 #include <QApplication>
00056 #include <QMap>
00057 #include <QRegExp>
00058 
00059 // OCCT Includes
00060 #include <TCollection_AsciiString.hxx>
00061 
00062 typedef QMap<QString, QString> FilterMap;
00063 static QString lastUsedFilter;
00064 
00065 //=======================================================================
00066 // function : getFileName
00067 // purpose  : Selection of a file name for Import/Export. Returns also
00068 //            the selected file type code through <filter> argument.
00069 //=======================================================================
00070 static QString getFileName( QWidget*           parent,
00071                             const QString&     initial,
00072                             const FilterMap&   filterMap,
00073                             const QStringList& filters,
00074                             const QString&     caption,
00075                             bool               open,
00076                             QString&           format,
00077                             bool               showCurrentDirInitially = false )
00078 {
00079   //QStringList filters;
00080   QString aBrepFilter;
00081   for (FilterMap::const_iterator it = filterMap.begin(); it != filterMap.end(); ++it) {
00082     //filters.push_back( it.key() );
00083     if (it.key().contains( "BREP", Qt::CaseInsensitive ))
00084       aBrepFilter = it.key();
00085   }
00086 
00087   SUIT_FileDlg* fd = new SUIT_FileDlg( parent, open, true, true );
00088   if ( !caption.isEmpty() )
00089     fd->setWindowTitle( caption );
00090 
00091   if ( !initial.isEmpty() )
00092     fd->selectFile( initial );
00093 
00094   if ( showCurrentDirInitially && SUIT_FileDlg::getLastVisitedPath().isEmpty() )
00095     fd->setDirectory( QDir::currentPath() );
00096 
00097   fd->setFilters( filters );
00098 
00099   if ( !lastUsedFilter.isEmpty() && filterMap.contains( lastUsedFilter ) ) {
00100     fd->selectFilter( lastUsedFilter );
00101   }
00102   else if ( !aBrepFilter.isEmpty() ) {
00103     fd->selectFilter( aBrepFilter );
00104   }
00105 
00106   QString filename;
00107   if ( fd->exec() == QDialog::Accepted ) {
00108     filename = fd->selectedFile();
00109     format = filterMap[fd->selectedFilter()];
00110     lastUsedFilter = fd->selectedFilter();
00111   }
00112 
00113   delete fd;
00114   qApp->processEvents();
00115   return filename;
00116 }
00117 
00118 //=======================================================================
00119 // function : getFileNames
00120 // purpose  : Select list of files for Import operation. Returns also
00121 //            the selected file type code through <format> argument.
00122 //=======================================================================
00123 static QStringList getFileNames( QWidget*           parent,
00124                                  const QString&     initial,
00125                                  const FilterMap&   filterMap,
00126                                  const QString&     caption,
00127                                  QString&           format,
00128                                  bool               showCurrentDirInitially = false)
00129 {
00130   QString aBrepFilter;
00131   QStringList allFilters;
00132   QStringList filters;
00133   QRegExp re( "\\((.*)\\)" );
00134   re.setMinimal( true );
00135   for ( FilterMap::const_iterator it = filterMap.begin(); it != filterMap.end(); ++it ) {
00136     if ( it.value().contains( "BREP", Qt::CaseInsensitive ) && aBrepFilter.isEmpty() )
00137       aBrepFilter = it.key();
00138     filters.append( it.key() );
00139     int pos = 0;
00140     while ( re.indexIn( it.key(), pos ) >= 0 ) {
00141       QString f = re.cap(1);
00142       pos = re.pos() + f.length() + 2;
00143       allFilters.append( f.simplified() );
00144     }
00145   }
00146   filters.append( QObject::tr( "GEOM_ALL_IMPORT_FILES" ).arg( allFilters.join( " " ) ) );
00147 
00148   SUIT_FileDlg fd( parent, true, true, true );
00149   fd.setFileMode( SUIT_FileDlg::ExistingFiles );
00150   if ( !caption.isEmpty() )
00151     fd.setWindowTitle( caption );
00152   if ( !initial.isEmpty() )
00153     fd.selectFile( initial );
00154 
00155   if ( showCurrentDirInitially && SUIT_FileDlg::getLastVisitedPath().isEmpty() )
00156     fd.setDirectory( QDir::currentPath() );
00157 
00158   fd.setFilters( filters );
00159 
00160   if ( !lastUsedFilter.isEmpty() && filterMap.contains( lastUsedFilter ) )
00161     fd.selectFilter( lastUsedFilter );
00162   else if ( !aBrepFilter.isEmpty() )
00163     fd.selectFilter( aBrepFilter );
00164 
00165   QStringList filenames;
00166   if ( fd.exec() ) {
00167     filenames = fd.selectedFiles();
00168     format = filterMap.contains( fd.selectedFilter() ) ? filterMap[ fd.selectedFilter() ] : QString();
00169     lastUsedFilter = fd.selectedFilter();
00170   }
00171   qApp->processEvents();
00172   return filenames;
00173 }
00174 
00175 //=======================================================================
00176 // function : getParentComponent
00177 // purpose  : Get object's parent component entry
00178 //=======================================================================
00179 static QString getParentComponent( _PTR( SObject ) obj )
00180 {
00181   if ( obj ) {
00182     _PTR(SComponent) comp = obj->GetFatherComponent();
00183     if ( comp )
00184       return QString( comp->GetID().c_str() );
00185   }
00186   return QString();
00187 }
00188 
00189 //=====================================================================================
00190 // function : inUse
00191 // purpose  : check if the object(s) passed as the the second arguments are used
00192 //            by the other objects in the study
00193 //=====================================================================================
00194 static bool inUse( _PTR(Study) study, const QString& component, const QMap<QString,QString>& objects )
00195 {
00196   _PTR(SObject) comp = study->FindObjectID( component.toLatin1().data() );
00197   if ( !comp )
00198     return false;
00199 
00200   // collect all GEOM objects being deleted
00201   QMap<QString, GEOM::GEOM_Object_var> gobjects;
00202   QMap<QString, QString>::ConstIterator oit;
00203   std::list<_PTR(SObject)> aSelectedSO;
00204   for ( oit = objects.begin(); oit != objects.end(); ++oit ) {
00205     _PTR(SObject) so = study->FindObjectID( oit.key().toLatin1().data() );
00206     if ( !so )
00207       continue;
00208     aSelectedSO.push_back(so);
00209     CORBA::Object_var corbaObj_rem = GeometryGUI::ClientSObjectToObject( so );
00210     GEOM::GEOM_Object_var geomObj_rem = GEOM::GEOM_Object::_narrow( corbaObj_rem );
00211     if( CORBA::is_nil( geomObj_rem ) )
00212       continue;
00213     gobjects.insert( oit.key(), geomObj_rem );
00214   }
00215 
00216   // Search References with other Modules
00217   std::list< _PTR(SObject) >::iterator itSO = aSelectedSO.begin();
00218   for ( ; itSO != aSelectedSO.end(); ++itSO ) {
00219     std::vector<_PTR(SObject)> aReferences = study->FindDependances( *itSO  );
00220     int aRefLength = aReferences.size();
00221     if (aRefLength) {
00222       for (int i = 0; i < aRefLength; i++) {
00223         _PTR(SObject) firstSO( aReferences[i] );
00224         _PTR(SComponent) aComponent = firstSO->GetFatherComponent();
00225         QString type = aComponent->ComponentDataType().c_str();
00226         if ( type == "SMESH" )
00227           return true;
00228       }
00229     }
00230   }
00231 
00232   // browse through all GEOM data tree
00233   _PTR(ChildIterator) it ( study->NewChildIterator( comp ) );
00234   for ( it->InitEx( true ); it->More(); it->Next() ) {
00235     _PTR(SObject) child( it->Value() );
00236     CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject( child );
00237     GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
00238     if( CORBA::is_nil( geomObj ) )
00239       continue;
00240 
00241     GEOM::ListOfGO_var list = geomObj->GetDependency();
00242     if( list->length() == 0 )
00243       continue;
00244 
00245     for( int i = 0; i < list->length(); i++ ) {
00246       bool depends = false;
00247       bool deleted = false;
00248       QMap<QString, GEOM::GEOM_Object_var>::Iterator git;
00249       for ( git = gobjects.begin(); git != gobjects.end() && ( !depends || !deleted ); ++git ) {
00250         depends = depends || list[i]->_is_equivalent( *git );
00251         deleted = deleted || git.key() == child->GetID().c_str() ;//geomObj->_is_equivalent( *git );
00252       }
00253       if ( depends && !deleted )
00254         return true;
00255     }
00256   }
00257   return false;
00258 }
00259 
00260 //=======================================================================
00261 // function : GEOMToolsGUI()
00262 // purpose  : Constructor
00263 //=======================================================================
00264 GEOMToolsGUI::GEOMToolsGUI( GeometryGUI* parent )
00265 : GEOMGUI( parent )
00266 {
00267 }
00268 
00269 //=======================================================================
00270 // function : ~GEOMToolsGUI()
00271 // purpose  : Destructor
00272 //=======================================================================
00273 GEOMToolsGUI::~GEOMToolsGUI()
00274 {
00275 }
00276 
00277 //=======================================================================
00278 // function : OnGUIEvent()
00279 // purpose  :
00280 //=======================================================================
00281 bool GEOMToolsGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent)
00282 {
00283   getGeometryGUI()->EmitSignalDeactivateDialog();
00284 
00285   switch ( theCommandID ) {
00286   case GEOMOp::OpDelete:         // EDIT - DELETE
00287     OnEditDelete();
00288     break;
00289   case GEOMOp::OpImport:         // FILE - IMPORT
00290     Import();
00291     break;
00292   case GEOMOp::OpExport:         // FILE - EXPORT
00293     Export();
00294     break;
00295   case GEOMOp::OpCheckGeom:      // TOOLS - CHECK GEOMETRY
00296     OnCheckGeometry();
00297     break;
00298   case GEOMOp::OpSelectVertex:   // POPUP - SELECT ONLY - VERTEX
00299     OnSelectOnly( GEOM_POINT );
00300     break;
00301   case GEOMOp::OpSelectEdge:     // POPUP - SELECT ONLY - EDGE
00302     OnSelectOnly( GEOM_EDGE );
00303     break;
00304   case GEOMOp::OpSelectWire:     // POPUP - SELECT ONLY - WIRE
00305     OnSelectOnly( GEOM_WIRE );
00306     break;
00307   case GEOMOp::OpSelectFace:     // POPUP - SELECT ONLY - FACE
00308     OnSelectOnly( GEOM_FACE );
00309     break;
00310   case GEOMOp::OpSelectShell:    // POPUP - SELECT ONLY - SHELL
00311     OnSelectOnly( GEOM_SHELL );
00312     break;
00313   case GEOMOp::OpSelectSolid:    // POPUP - SELECT ONLY - SOLID
00314     OnSelectOnly( GEOM_SOLID );
00315     break;
00316   case GEOMOp::OpSelectCompound: // POPUP - SELECT ONLY - COMPOUND
00317     OnSelectOnly( GEOM_COMPOUND );
00318     break;
00319   case GEOMOp::OpSelectAll:      // POPUP - SELECT ONLY - SELECT ALL
00320     OnSelectOnly( GEOM_ALLOBJECTS );
00321     break;
00322   case GEOMOp::OpDeflection:     // POPUP - DEFLECTION ANGLE
00323     OnDeflection();
00324     break;
00325   case GEOMOp::OpColor:          // POPUP - COLOR
00326     OnColor();
00327     break;
00328   case GEOMOp::OpSetTexture:        // POPUP - TEXTURE
00329     OnTexture();
00330     break;
00331   case GEOMOp::OpTransparency:   // POPUP - TRANSPARENCY
00332     OnTransparency();
00333     break;
00334   case GEOMOp::OpIncrTransparency: // SHORTCUT   - INCREASE TRANSPARENCY
00335     OnChangeTransparency( true );
00336     break;
00337   case GEOMOp::OpDecrTransparency: // SHORTCUT   - DECREASE TRANSPARENCY
00338     OnChangeTransparency( false );
00339     break;
00340   case GEOMOp::OpIsos:           // POPUP - ISOS
00341     OnNbIsos();
00342     break;
00343   case GEOMOp::OpIncrNbIsos:     // SHORTCUT   - INCREASE NB ISOLINES
00344     OnNbIsos( INCR );
00345     break;
00346   case GEOMOp::OpDecrNbIsos:     // SHORTCUT   - DECREASE NB ISOLINES
00347     OnNbIsos( DECR );
00348     break;
00349   case GEOMOp::OpMaterialProperties: // POPUP - MATERIAL PROPERTIES
00350     OnMaterialProperties();
00351     break;
00352   case GEOMOp::OpAutoColor:      // POPUP - AUTO COLOR
00353     OnAutoColor();
00354     break;
00355   case GEOMOp::OpNoAutoColor:    // POPUP - DISABLE AUTO COLOR
00356     OnDisableAutoColor();
00357     break;
00358   case GEOMOp::OpShowChildren:   // POPUP - SHOW CHILDREN
00359   case GEOMOp::OpHideChildren:   // POPUP - HIDE CHILDREN
00360     OnShowHideChildren( theCommandID == GEOMOp::OpShowChildren );
00361     break;
00362   case GEOMOp::OpPointMarker:    // POPUP - POINT MARKER
00363     OnPointMarker();
00364     break;
00365   case GEOMOp::OpUnpublishObject:// POPUP - UNPUBLISH
00366     OnUnpublishObject();
00367     break;
00368   case GEOMOp::OpPublishObject:// GEOM ROOT OBJECT - POPUP - PUBLISH
00369     OnPublishObject();
00370     break;
00371   case GEOMOp::OpEdgeWidth:
00372     OnEdgeWidth();
00373     break;
00374   case GEOMOp::OpIsosWidth:
00375     OnIsosWidth();
00376     break;
00377   case GEOMOp::OpBringToFront:
00378     OnBringToFront();
00379     break;
00380   case GEOMOp::OpClsBringToFront:
00381     OnClsBringToFront();
00382      break;
00383   default:
00384     SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
00385     break;
00386   }
00387   return true;
00388 }
00389 
00390 //===============================================================================
00391 // function : OnEditDelete()
00392 // purpose  :
00393 //===============================================================================
00394 void GEOMToolsGUI::OnEditDelete()
00395 {
00396   SALOME_ListIO selected;
00397   SalomeApp_Application* app =
00398     dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
00399   if ( !app )
00400     return;
00401 
00402   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
00403   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
00404   if ( !aSelMgr || !appStudy )
00405     return;
00406 
00407   // get selection
00408   aSelMgr->selectedObjects( selected, "ObjectBrowser", false );
00409   if ( selected.IsEmpty() )
00410     return;
00411 
00412   _PTR(Study) aStudy = appStudy->studyDS();
00413 
00414   // check if study is locked
00415   if ( _PTR(AttributeStudyProperties)( aStudy->GetProperties() )->IsLocked() ) {
00416     SUIT_MessageBox::warning( app->desktop(),
00417                               tr("WRN_WARNING"),
00418                               tr("WRN_STUDY_LOCKED") );
00419     return; // study is locked
00420   }
00421 
00422   // get GEOM component
00423   CORBA::String_var geomIOR = app->orb()->object_to_string( GeometryGUI::GetGeomGen() );
00424   QString geomComp = getParentComponent( aStudy->FindObjectIOR( geomIOR.in() ) );
00425 
00426   // check each selected object: if belongs to GEOM, if not reference...
00427   QMap<QString,QString> toBeDeleted;
00428   QMap<QString,QString> allDeleted;
00429   bool isComponentSelected = false;
00430 
00431   for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
00432     Handle(SALOME_InteractiveObject) anIObject = It.Value();
00433     if ( !anIObject->hasEntry() )
00434       continue; // invalid object
00435     // ...
00436     QString entry = anIObject->getEntry();
00437     _PTR(SObject) obj = aStudy->FindObjectID( entry.toLatin1().data() );
00438     // check parent component
00439     QString parentComp = getParentComponent( obj );
00440     if ( parentComp != geomComp )  {
00441       SUIT_MessageBox::warning( app->desktop(),
00442                                 QObject::tr("ERR_ERROR"),
00443                                 QObject::tr("NON_GEOM_OBJECTS_SELECTED").arg( getGeometryGUI()->moduleName() ) );
00444       return; // not GEOM object selected
00445     }
00446 
00448     // if GEOM component is selected, so skip other checks
00449     if ( isComponentSelected ) continue;
00451 
00452     // check if object is reference
00453     _PTR(SObject) refobj;
00454     if ( obj && obj->ReferencedObject( refobj ) ) {
00455       // get the main object by reference IPAL 21354
00456       obj = refobj;
00457       entry = obj->GetID().c_str();
00458     }
00459     // ...
00460     QString aName = obj->GetName().c_str();
00461     if ( entry == geomComp ) {
00462       // GEOM component is selected, skip other checks
00463       isComponentSelected = true;
00464       continue;
00465     }
00466     toBeDeleted.insert( entry, aName );
00467     allDeleted.insert( entry, aName ); // skip GEOM component
00468     // browse through all children recursively
00469     _PTR(ChildIterator) it ( aStudy->NewChildIterator( obj ) );
00470     for ( it->InitEx( true ); it->More(); it->Next() ) {
00471       _PTR(SObject) child( it->Value() );
00472       if ( child && child->ReferencedObject( refobj ) )
00473         continue; // skip references
00474       aName = child->GetName().c_str();
00475       if ( !aName.isEmpty() )
00476         allDeleted.insert( child->GetID().c_str(), aName );
00477     }
00478   }
00479 
00480   // is there is anything to delete?
00481   if ( !isComponentSelected && allDeleted.count() <= 0 )
00482     return; // nothing to delete
00483 
00484   // show confirmation dialog box
00485   GEOMToolsGUI_DeleteDlg dlg( app->desktop(), allDeleted, isComponentSelected );
00486   if ( !dlg.exec() )
00487     return; // operation is cancelled by user
00488 
00489   // get currently opened views
00490   QList<SALOME_View*> views;
00491   SALOME_View* view;
00492   ViewManagerList vmans = app->viewManagers();
00493   SUIT_ViewManager* vman;
00494   foreach ( vman, vmans ) {
00495     SUIT_ViewModel* vmod = vman->getViewModel();
00496     view = dynamic_cast<SALOME_View*> ( vmod ); // must work for OCC and VTK views
00497     if ( view )
00498       views.append( view );
00499   }
00500 
00501   _PTR(StudyBuilder) aStudyBuilder (aStudy->NewBuilder());
00502   GEOM_Displayer* disp = new GEOM_Displayer( appStudy );
00503 
00504   if ( isComponentSelected ) {
00505     // GEOM component is selected: delete all objects recursively
00506     _PTR(SObject) comp = aStudy->FindObjectID( geomComp.toLatin1().data() );
00507     if ( !comp )
00508       return;
00509     _PTR(ChildIterator) it ( aStudy->NewChildIterator( comp ) );
00510     // remove top-level objects only
00511     for ( it->InitEx( false ); it->More(); it->Next() ) {
00512       _PTR(SObject) child( it->Value() );
00513       // remove object from GEOM engine
00514       removeObjectWithChildren( child, aStudy, views, disp );
00515       // remove object from study
00516       aStudyBuilder->RemoveObjectWithChildren( child );
00517     }
00518   }
00519   else {
00520     // GEOM component is not selected: check if selected objects are in use
00521     if ( inUse( aStudy, geomComp, allDeleted ) ) {
00522       SUIT_MessageBox::warning( app->desktop(),
00523                                 QObject::tr("WRN_WARNING"),
00524                                 QObject::tr("DEP_OBJECT") );
00525       return; // object(s) in use
00526     }
00527     // ... and then delete all objects
00528     QMap<QString, QString>::Iterator it;
00529     for ( it = toBeDeleted.begin(); it != toBeDeleted.end(); ++it ) {
00530       _PTR(SObject) obj ( aStudy->FindObjectID( it.key().toLatin1().data() ) );
00531       // remove object from GEOM engine
00532       removeObjectWithChildren( obj, aStudy, views, disp );
00533       // remove objects from study
00534       aStudyBuilder->RemoveObjectWithChildren( obj );
00535     }
00536   }
00537 
00538   selected.Clear();
00539   aSelMgr->setSelectedObjects( selected );
00540   getGeometryGUI()->updateObjBrowser();
00541   app->updateActions(); //SRN: To update a Save button in the toolbar
00542 }
00543 
00544 //=====================================================================================
00545 // function : Import
00546 // purpose  : BRep, Iges, Step, ...
00547 //=====================================================================================
00548 bool GEOMToolsGUI::Import()
00549 {
00550   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( getGeometryGUI()->getApp() );
00551   if ( !app ) return false;
00552 
00553   SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
00554   if ( !stud ) {
00555     MESSAGE ( "FAILED to cast active study to SalomeApp_Study" );
00556     return false;
00557   }
00558   _PTR(Study) aStudy = stud->studyDS();
00559 
00560   // check if study is locked
00561   bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
00562   if ( aLocked ) {
00563     SUIT_MessageBox::warning( app->desktop(),
00564                               QObject::tr("WRN_WARNING"),
00565                               QObject::tr("WRN_STUDY_LOCKED") );
00566     return false;
00567   }
00568 
00569   // check if GEOM engine is available
00570   GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
00571   if ( CORBA::is_nil( eng ) ) {
00572     SUIT_MessageBox::critical( app->desktop(),
00573                                QObject::tr("WRN_WARNING"),
00574                                QObject::tr( "GEOM Engine is not started" ) );
00575     return false;
00576   }
00577 
00578   GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
00579   if ( aInsOp->_is_nil() )
00580     return false;
00581 
00582   // obtain a list of available import formats
00583   FilterMap aMap;
00584   GEOM::string_array_var aFormats, aPatterns;
00585   aInsOp->ImportTranslators( aFormats, aPatterns );
00586 
00587   for ( int i = 0, n = aFormats->length(); i < n; i++ )
00588     aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
00589 
00590   // select files to be imported
00591   QString fileType;
00592   QStringList fileNames = getFileNames( app->desktop(), "", aMap,
00593                                         tr( "GEOM_MEN_IMPORT" ), fileType, true );
00594 
00595   // set Wait cursor
00596   SUIT_OverrideCursor wc;
00597 
00598   if ( fileNames.count() == 0 )
00599     return false; // nothing selected, return
00600 
00601   QStringList errors;
00602 
00603   QList< GEOM::GEOM_Object_var > objsForDisplay;
00604 
00605   QStringList anEntryList;
00606 
00607   // iterate through all selected files
00608 
00609   SUIT_MessageBox::StandardButton igesAnswer = SUIT_MessageBox::NoButton;
00610   SUIT_MessageBox::StandardButton acisAnswer = SUIT_MessageBox::NoButton;
00611 
00612   for ( int i = 0; i < fileNames.count(); i++ ) {
00613     QString fileName = fileNames[i];
00614 
00615     if ( fileName.isEmpty() )
00616       continue;
00617 
00618     QString aCurrentType;
00619     if ( fileType.isEmpty() ) {
00620       // file type is not defined, try to detect
00621       QString ext = QFileInfo( fileName ).suffix().toUpper();
00622       QRegExp re( "\\*\\.(\\w+)" );
00623       for ( FilterMap::const_iterator it = aMap.begin();
00624             it != aMap.end() && aCurrentType.isEmpty(); ++it ) {
00625         int pos = 0;
00626         while ( re.indexIn( it.key(), pos ) >= 0 ) {
00627           QString f = re.cap(1).trimmed().toUpper();
00628           if ( ext == f ) { aCurrentType = it.value(); break; }
00629           pos = re.pos() + re.cap(1).length() + 2;
00630         }
00631       }
00632     }
00633     else {
00634       aCurrentType = fileType;
00635     }
00636 
00637     if ( aCurrentType.isEmpty() ) {
00638       errors.append( QString( "%1 : %2" ).arg( fileName ).arg( tr( "GEOM_UNSUPPORTED_TYPE" ) ) );
00639       continue;
00640     }
00641 
00642     GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
00643     try {
00644       app->putInfo( tr( "GEOM_PRP_LOADING" ).arg( SUIT_Tools::file( fileName, /*withExten=*/true ) ) );
00645       anOp->start();
00646 
00647       CORBA::String_var fileN = fileName.toLatin1().constData();
00648       CORBA::String_var fileT = aCurrentType.toLatin1().constData();
00649 
00650       // skl 29.05.2009
00651       if ( aCurrentType == "IGES" ) {
00652         GEOM::GEOM_Object_var anObj = aInsOp->ImportFile( fileN, "IGES_UNIT" );
00653         bool needConvert = false;
00654         TCollection_AsciiString aUnitName = aInsOp->GetErrorCode();
00655         if ( aUnitName.SubString( 1, 4 ) == "UNIT" )
00656           needConvert = aUnitName.SubString( 6, aUnitName.Length() ) != "M";
00657 
00658         if ( needConvert ) {
00659           if ( igesAnswer == SUIT_MessageBox::NoToAll ) {
00660             // converting for all files is already approved
00661             fileT = "IGES_SCALE";
00662           }
00663           else if ( igesAnswer != SUIT_MessageBox::YesToAll ) {
00664             SUIT_MessageBox::StandardButtons btns = SUIT_MessageBox::Yes | SUIT_MessageBox::No;
00665             if ( i < fileNames.count()-1 ) btns = btns | SUIT_MessageBox::YesToAll | SUIT_MessageBox::NoToAll;
00666             igesAnswer = SUIT_MessageBox::question( app->desktop(),
00667                                                     "Question",//tr("WRN_WARNING"),
00668                                                     tr("GEOM_SCALE_DIMENSIONS"),
00669                                                     btns | SUIT_MessageBox::Cancel,
00670                                                     SUIT_MessageBox::No );
00671             switch ( igesAnswer ) {
00672             case SUIT_MessageBox::Cancel:
00673               return false;                // cancel (break) import operation
00674             case SUIT_MessageBox::Yes:
00675             case SUIT_MessageBox::YesToAll:
00676               break;                       // scaling is confirmed
00677             case SUIT_MessageBox::No:
00678             case SUIT_MessageBox::NoAll:
00679               fileT = "IGES_SCALE";
00680             default:
00681               break;                       // scaling is rejected
00682             } // switch ( igesAnswer )
00683           } // if ( igeAnswer != NoToAll )
00684         } // if ( needConvert )
00685       } // if ( aCurrentType == "IGES" )
00686       else if ( aCurrentType == "ACIS" ) {
00687         if ( acisAnswer != SUIT_MessageBox::YesToAll && acisAnswer != SUIT_MessageBox::NoToAll ) {
00688           SUIT_MessageBox::StandardButtons btns = SUIT_MessageBox::Yes | SUIT_MessageBox::No;
00689           if ( i < fileNames.count()-1 ) btns = btns | SUIT_MessageBox::YesToAll | SUIT_MessageBox::NoToAll;
00690           acisAnswer = SUIT_MessageBox::question( app->desktop(),
00691                                                   "Question",//tr("WRN_WARNING"),
00692                                                   tr("GEOM_PUBLISH_NAMED_SHAPES"),
00693                                                   btns | SUIT_MessageBox::Cancel,
00694                                                   SUIT_MessageBox::No );
00695           if ( acisAnswer == SUIT_MessageBox::Cancel )
00696             return false; // cancel (break) import operation
00697         } // if ( acisAnswer != YesToAll && acisAnswer != NoToAll )
00698       } // else if ( aCurrentType == "ACIS" )
00699 
00700       GEOM::GEOM_Object_var anObj = aInsOp->ImportFile( fileN, fileT );
00701 
00702       if ( !anObj->_is_nil() && aInsOp->IsDone() ) {
00703         QString aPublishObjName =
00704           GEOMBase::GetDefaultName( SUIT_Tools::file( fileName, /*withExten=*/true ) );
00705 
00706         SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy( aStudy );
00707         SALOMEDS::SObject_var aSO =
00708           GeometryGUI::GetGeomGen()->PublishInStudy( aDSStudy,
00709                                                      SALOMEDS::SObject::_nil(),
00710                                                      anObj,
00711                                                      aPublishObjName.toLatin1().constData() );
00712         if ( ( !aSO->_is_nil() ) )
00713           anEntryList.append( aSO->GetID() );
00714 
00715         objsForDisplay.append( anObj );
00716 
00717         if ( aCurrentType == "ACIS" ) {
00718           if ( acisAnswer == SUIT_MessageBox::Yes || acisAnswer == SUIT_MessageBox::YesToAll )
00719             GeometryGUI::GetGeomGen()->PublishNamedShapesInStudy( aDSStudy, anObj );
00720         }
00721 
00722         anOp->commit();
00723       }
00724       else {
00725         anOp->abort();
00726         errors.append( QString( "%1 : %2" ).arg( fileName ).arg( aInsOp->GetErrorCode() ) );
00727       }
00728     }
00729     catch( const SALOME::SALOME_Exception& S_ex ) {
00730       anOp->abort();
00731       errors.append( QString( "%1 : %2" ).arg( fileName ).arg( tr( "GEOM_UNKNOWN_IMPORT_ERROR" ) ) );
00732     }
00733   }
00734 
00735   // update object browser
00736   getGeometryGUI()->updateObjBrowser( true );
00737 
00738   // browse published objects
00739   app->browseObjects( anEntryList );
00740 
00741   // display imported model (if only one file is selected)
00742   if ( objsForDisplay.count() == 1 )
00743     GEOM_Displayer( stud ).Display( objsForDisplay[0].in() );
00744 
00745   if ( errors.count() > 0 ) {
00746     SUIT_MessageBox::critical( app->desktop(),
00747                                QObject::tr( "GEOM_ERROR" ),
00748                                QObject::tr( "GEOM_IMPORT_ERRORS" ) + "\n" + errors.join( "\n" ) );
00749   }
00750 
00751   app->updateActions(); //SRN: To update a Save button in the toolbar
00752 
00753   return objsForDisplay.count() > 0;
00754 }
00755 
00756 //=====================================================================================
00757 // function : Export
00758 // purpose  : BRep, Iges, Step
00759 //=====================================================================================
00760 bool GEOMToolsGUI::Export()
00761 {
00762   SalomeApp_Application* app = getGeometryGUI()->getApp();
00763   if (!app) return false;
00764 
00765   SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
00766   if ( !stud ) {
00767     MESSAGE ( "FAILED to cast active study to SalomeApp_Study" );
00768     return false;
00769   }
00770   _PTR(Study) aStudy = stud->studyDS();
00771 
00772   GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
00773   if ( CORBA::is_nil( eng ) ) {
00774     SUIT_MessageBox::critical( app->desktop(),
00775                                QObject::tr("WRN_WARNING"),
00776                                QObject::tr( "GEOM Engine is not started" ) );
00777     return false;
00778   }
00779 
00780   GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
00781   if ( aInsOp->_is_nil() )
00782     return false;
00783 
00784   // Obtain a list of available export formats
00785   FilterMap aMap;
00786   QStringList filters;
00787   GEOM::string_array_var aFormats, aPatterns;
00788   aInsOp->ExportTranslators( aFormats, aPatterns );
00789   for ( int i = 0, n = aFormats->length(); i < n; i++ ) {
00790     aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
00791     filters.push_back( (char*)aPatterns[i] );
00792   }
00793 
00794   // Get selected objects
00795   LightApp_SelectionMgr* sm = app->selectionMgr();
00796   if ( !sm )
00797     return false;
00798 
00799   SALOME_ListIO selectedObjects;
00800   sm->selectedObjects( selectedObjects );
00801   bool appropriateObj = false;
00802 
00803   SALOME_ListIteratorOfListIO It( selectedObjects );
00804   for (; It.More(); It.Next()) {
00805     Handle(SALOME_InteractiveObject) IObject = It.Value();
00806     GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject( IObject );
00807 
00808     if ( anObj->_is_nil() )
00809       continue;
00810 
00811     QString fileType;
00812     QString file = getFileName(app->desktop(), QString( IObject->getName() ), aMap, filters,
00813                                tr("GEOM_MEN_EXPORT"), false, fileType, true);
00814 
00815     // User has pressed "Cancel" --> stop the operation
00816     if ( file.isEmpty() || fileType.isEmpty() )
00817       return false;
00818 
00819     GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
00820     try {
00821       SUIT_OverrideCursor wc;
00822 
00823       app->putInfo( tr("GEOM_PRP_EXPORT").arg(SUIT_Tools::file( file, /*withExten=*/true )) );
00824 
00825       anOp->start();
00826 
00827       aInsOp->Export( anObj, file.toStdString().c_str(), fileType.toLatin1().constData() );
00828 
00829       if (aInsOp->IsDone())
00830         anOp->commit();
00831       else {
00832         anOp->abort();
00833         wc.suspend();
00834         SUIT_MessageBox::critical(app->desktop(),
00835                                   QObject::tr("GEOM_ERROR"),
00836                                   QObject::tr("GEOM_PRP_ABORT") + "\n" + QObject::tr(aInsOp->GetErrorCode()));
00837         return false;
00838       }
00839     }
00840     catch (const SALOME::SALOME_Exception& S_ex) {
00841       //QtCatchCorbaException(S_ex);
00842       anOp->abort();
00843       return false;
00844     }
00845     appropriateObj = true;
00846   }
00847 
00848   if ( !appropriateObj )
00849     SUIT_MessageBox::warning( app->desktop(),
00850                               QObject::tr("WRN_WARNING"),
00851                               QObject::tr("GEOM_WRN_NO_APPROPRIATE_SELECTION") );
00852   return true;
00853 }
00854 
00855 //=====================================================================================
00856 // function : RemoveObjectWithChildren
00857 // purpose  : used by OnEditDelete() method
00858 //=====================================================================================
00859 void GEOMToolsGUI::removeObjectWithChildren(_PTR(SObject) obj,
00860                                             _PTR(Study) aStudy,
00861                                             QList<SALOME_View*> views,
00862                                             GEOM_Displayer* disp)
00863 {
00864   // iterate through all children of obj
00865   for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
00866     _PTR(SObject) child (it->Value());
00867     removeObjectWithChildren(child, aStudy, views, disp);
00868   }
00869 
00870   // erase object and remove it from engine
00871   _PTR(GenericAttribute) anAttr;
00872   if (obj->FindAttribute(anAttr, "AttributeIOR")) {
00873     _PTR(AttributeIOR) anIOR (anAttr);
00874 
00875     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
00876 
00877     // Delete shape in Client
00878     const TCollection_AsciiString ASCIor ((char*)anIOR->Value().c_str());
00879     getGeometryGUI()->GetShapeReader().RemoveShapeFromBuffer(ASCIor);
00880 
00881     CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(obj);
00882     GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
00883     if (!CORBA::is_nil(geomObj)) {
00884 
00885       //Remove visual properties of the object
00886       appStudy->removeObjectFromAll(obj->GetID().c_str());
00887 
00888       // Erase graphical object
00889       QListIterator<SALOME_View*> it( views );
00890       while ( it.hasNext() )
00891         if ( SALOME_View* view = it.next() )
00892           disp->Erase(geomObj, true, view);
00893 
00894       // Remove object from Engine
00895       // We can't directly remove object from engine. All we can do is to unpublish the object
00896       // from the study. Another client could be using the object.
00897       // Unpublishing is done just after in aStudyBuilder->RemoveObjectWithChildren( child );
00898       //GeometryGUI::GetGeomGen()->RemoveObject( geomObj );
00899     }
00900   }
00901 }
00902 
00903 //=================================================================================
00904 // function : deactivate()
00905 // purpose  : Called when GEOM component is deactivated
00906 //=================================================================================
00907 void GEOMToolsGUI::deactivate()
00908 {
00909   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
00910   if ( app ) {
00911     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
00912     GEOM_Displayer aDisp (appStudy);
00913     aDisp.GlobalSelection();
00914     getGeometryGUI()->setLocalSelectionMode(GEOM_ALLOBJECTS);
00915   }
00916 }
00917 
00918 //=====================================================================================
00919 // EXPORTED METHODS
00920 //=====================================================================================
00921 extern "C"
00922 {
00923 #ifdef WIN32
00924   __declspec( dllexport )
00925 #endif
00926   GEOMGUI* GetLibGUI( GeometryGUI* parent )
00927   {
00928     return new GEOMToolsGUI( parent );
00929   }
00930 }