1 // GEOM GEOMGUI : GUI for Geometry component
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 // File : GEOMBase_Tools.cxx
25 // Author : Damien COQUERET
29 #include "GEOMToolsGUI.h"
31 #include "GeometryGUI.h"
33 #include "GEOM_Operation.h"
34 #include "GEOM_Displayer.h"
36 #include <SUIT_Session.h>
37 //#include <SUIT_Application.h>
38 #include <SUIT_OverrideCursor.h>
39 #include <SUIT_MessageBox.h>
40 #include <SUIT_Tools.h>
41 #include <SUIT_FileDlg.h>
42 #include <SUIT_Desktop.h>
43 #include <SUIT_ViewModel.h>
44 #include <SUIT_ViewManager.h>
46 #include <SalomeApp_Application.h>
47 #include <SalomeApp_Study.h>
48 #include <LightApp_SelectionMgr.h>
49 #include <GEOMImpl_Types.hxx>
51 #include <SALOME_ListIO.hxx>
52 #include <SALOME_ListIteratorOfListIO.hxx>
53 #include <SALOME_Prs.h>
56 #include <QApplication>
60 #include <TCollection_AsciiString.hxx>
64 typedef QMap<QString, QString> FilterMap;
66 //=======================================================================
67 // function : getFileName
68 // purpose : Selection of a file name for Import/Export. Returns also
69 // the selected file type code through <filter> argument.
70 //=======================================================================
71 static QString getFileName( QWidget* parent,
72 const QString& initial,
73 const FilterMap& filterMap,
74 const QString& caption,
78 static QString lastUsedFilter;
80 for ( FilterMap::const_iterator it = filterMap.begin(); it != filterMap.end(); ++it )
81 filters.push_back( it.key() );
83 SUIT_FileDlg* fd = new SUIT_FileDlg( parent, open, true, true );
84 if ( !caption.isEmpty() )
85 fd->setWindowTitle( caption );
87 if ( !initial.isEmpty() )
88 fd->selectFile( initial );
90 if ( !lastUsedFilter.isEmpty() && filterMap.contains( lastUsedFilter ) )
91 fd->selectFilter( lastUsedFilter );
93 fd->setFilters( filters );
96 QString filename = fd->selectedFile();
97 format = filterMap[fd->selectedFilter()];
98 lastUsedFilter = fd->selectedFilter();
100 qApp->processEvents();
104 //=======================================================================
105 // function : GEOMToolsGUI()
106 // purpose : Constructor
107 //=======================================================================
108 GEOMToolsGUI::GEOMToolsGUI( GeometryGUI* parent )
114 //=======================================================================
115 // function : ~GEOMToolsGUI()
116 // purpose : Destructor
117 //=======================================================================
118 GEOMToolsGUI::~GEOMToolsGUI()
123 //=======================================================================
124 // function : OnGUIEvent()
126 //=======================================================================
127 bool GEOMToolsGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent)
129 getGeometryGUI()->EmitSignalDeactivateDialog();
131 switch (theCommandID)
143 case 111: // IMPORT BREP
144 case 112: // IMPORT IGES
145 case 113: // IMPORT STEP
150 case 121: // EXPORT BREP
151 case 122: // EXPORT IGES
152 case 123: // EXPORT STEP
157 case 2171: // POPUP VIEWER - SELECT ONLY - VERTEX
159 OnSelectOnly( GEOM_POINT );
162 case 2172: // POPUP VIEWER - SELECT ONLY - EDGE
164 OnSelectOnly( GEOM_EDGE );
167 case 2173: // POPUP VIEWER - SELECT ONLY - WIRE
169 OnSelectOnly( GEOM_WIRE );
172 case 2174: // POPUP VIEWER - SELECT ONLY - FACE
174 OnSelectOnly( GEOM_FACE );
177 case 2175: // POPUP VIEWER - SELECT ONLY - SHELL
179 OnSelectOnly( GEOM_SHELL );
182 case 2176: // POPUP VIEWER - SELECT ONLY - SOLID
184 OnSelectOnly( GEOM_SOLID );
187 case 2177: // POPUP VIEWER - SELECT ONLY - COMPOUND
189 OnSelectOnly( GEOM_COMPOUND );
192 case 2178: // POPUP VIEWER - SELECT ONLY - SELECT ALL
194 OnSelectOnly( GEOM_ALLOBJECTS );
197 case 411: // SETTINGS - ADD IN STUDY
199 // SAN -- TO BE REMOVED !!!
202 case 412: // SETTINGS - SHADING COLOR
207 case 413: // SETTINGS - ISOS
212 case 414: // SETTINGS : STEP VALUE FOR SPIN BOXES
217 case 804: // ADD IN STUDY - POPUP VIEWER
219 // SAN -- TO BE REMOVED !!!!
227 case 5103: // CHECK GEOMETRY
232 case 8032: // COLOR - POPUP VIEWER
237 case 8033: // TRANSPARENCY - POPUP VIEWER
242 case 8034: // ISOS - POPUP VIEWER
247 case 9024 : // OPEN - OBJBROSER POPUP
254 SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
263 //===============================================================================
264 // function : OnEditDelete()
266 //===============================================================================
267 void GEOMToolsGUI::OnEditDelete()
269 SALOME_ListIO selected;
270 SalomeApp_Application* app =
271 dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
273 LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
274 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
275 if ( aSelMgr && appStudy ) {
276 aSelMgr->selectedObjects( selected, QString::null, false );
277 if ( !selected.IsEmpty() ) {
278 _PTR(Study) aStudy = appStudy->studyDS();
280 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
282 SUIT_MessageBox::warning ( app->desktop(),
283 QObject::tr("WRN_WARNING"),
284 QObject::tr("WRN_STUDY_LOCKED"),
285 QObject::tr("BUT_OK") );
289 // VSR 17/11/04: check if all objects selected belong to GEOM component --> start
290 // modifications of ASV 01.06.05
291 QString parentComp = getParentComponent( aStudy, selected );
292 CORBA::String_var geomIOR = app->orb()->object_to_string( GeometryGUI::GetGeomGen() );
293 QString geomComp = getParentComponent( aStudy->FindObjectIOR( geomIOR.in() ) );
295 if ( parentComp != geomComp ) {
296 SUIT_MessageBox::warning ( app->desktop(),
297 QObject::tr("ERR_ERROR"),
298 QObject::tr("NON_GEOM_OBJECTS_SELECTED").arg( getGeometryGUI()->moduleName() ),
299 QObject::tr("BUT_OK") );
302 // VSR 17/11/04: check if all objects selected belong to GEOM component <-- finish
304 if ( SUIT_MessageBox::warning( app->desktop(),
305 QObject::tr( "GEOM_WRN_WARNING" ),
306 QObject::tr( "GEOM_REALLY_DELETE" ),
307 QObject::tr( "GEOM_BUT_YES" ),
308 QObject::tr( "GEOM_BUT_NO" ), 1 ) != 0 )
311 // QAD_Operation* op = new SALOMEGUI_ImportOperation(.....);
314 // prepare list of SALOME_Views
315 QList<SALOME_View*> views;
318 ViewManagerList vmans = app->viewManagers();
319 SUIT_ViewManager* vman;
320 QListIterator<SUIT_ViewManager*> it( vmans );
321 while ( it.hasNext() && (vman = it.next()) ) {
322 SUIT_ViewModel* vmod = vman->getViewModel();
323 view = dynamic_cast<SALOME_View*> ( vmod ); // must work for OCC and VTK views
325 views.append( view );
328 _PTR(StudyBuilder) aStudyBuilder (aStudy->NewBuilder());
329 _PTR(GenericAttribute) anAttr;
330 GEOM_Displayer* disp = new GEOM_Displayer( appStudy );
332 _PTR(SComponent) aGeom ( aStudy->FindComponent("GEOM") );
336 // MAIN LOOP OF SELECTED OBJECTS
337 for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
339 Handle(SALOME_InteractiveObject) io = It.Value();
340 if ( !io->hasEntry() )
343 _PTR(SObject) obj ( aStudy->FindObjectID( io->getEntry() ) );
345 // disable removal of "Geometry" component object
346 if ( !strcmp( obj->GetIOR().c_str(), geomIOR ) )
349 //If the object has been used to create another one,then it can't be deleted
350 _PTR(ChildIterator) it (aStudy->NewChildIterator(aGeom));
351 for ( it->InitEx( true ); it->More(); it->Next() ) {
352 _PTR(SObject) chobj (it->Value());
353 if(CheckSubObjectInUse(chobj, obj, aStudy)) return;
355 for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
356 _PTR(SObject) child (it->Value());
357 if(CheckSubObjectInUse( chobj, child, aStudy)) return;
361 RemoveObjectWithChildren(obj, aStudy, views, disp);
363 // Remove objects from Study
364 aStudyBuilder->RemoveObject( obj );
367 } // MAIN LOOP of selected
370 aSelMgr->setSelectedObjects( selected );
371 getGeometryGUI()->updateObjBrowser();
372 } // if ( selected not empty )
373 } // if ( selMgr && appStudy )
375 app->updateActions(); //SRN: To update a Save button in the toolbar
387 //==============================================================================
388 // function : OnEditCopy()
390 //==============================================================================
391 void GEOMToolsGUI::OnEditCopy()
394 SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection() );
395 GEOM::string_array_var listIOR = new GEOM::string_array;
397 const SALOME_ListIO& List = Sel->StoredIObjects();
399 myGeomBase->ConvertListOfIOInListOfIOR(List, listIOR);
401 Sel->ClearIObjects();
403 SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
404 int aStudyID = aStudy->StudyId();
406 for (unsigned int ind = 0; ind < listIOR->length();ind++) {
407 GEOM::GEOM_Object_var aShapeInit = myGeom->GetIORFromString(listIOR[ind]);
409 GEOM::GEOM_IInsertOperations_var IOp = myGeom->GetIInsertOperations(aStudyID);
410 GEOM::GEOM_Object_var result = IOp->MakeCopy(aShapeInit);
411 myGeomBase->Display(result);
413 catch (const SALOME::SALOME_Exception& S_ex) {
414 QtCatchCorbaException(S_ex);
418 QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_READY"));
423 //=====================================================================================
425 // purpose : BRep, Iges, Step
426 //=====================================================================================
427 bool GEOMToolsGUI::Import()
429 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( getGeometryGUI()->getApp() );
430 //SUIT_Application* app = getGeometryGUI()->getApp();
431 if (! app) return false;
433 SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
435 cout << "FAILED to cast active study to SalomeApp_Study" << endl;
438 _PTR(Study) aStudy = stud->studyDS();
440 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
442 SUIT_MessageBox::warning ( app->desktop(),
443 QObject::tr("WRN_WARNING"),
444 QObject::tr("WRN_STUDY_LOCKED"),
445 QObject::tr("BUT_OK") );
449 GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
450 if ( CORBA::is_nil( eng ) ) {
451 SUIT_MessageBox::critical( app->desktop(),
452 QObject::tr("WRN_WARNING"),
453 QObject::tr( "GEOM Engine is not started" ),
454 QObject::tr("BUT_OK") );
458 GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
459 if ( aInsOp->_is_nil() )
462 GEOM::GEOM_Object_var anObj;
464 // Obtain a list of available import formats
466 GEOM::string_array_var aFormats, aPatterns;
467 aInsOp->ImportTranslators( aFormats, aPatterns );
469 for ( int i = 0, n = aFormats->length(); i < n; i++ )
470 aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
474 QString fileName = getFileName(app->desktop(), "", aMap,
475 tr("GEOM_MEN_IMPORT"), true, fileType);
477 if (fileType.isEmpty() )
479 // Trying to detect file type
480 QFileInfo aFileInfo( fileName );
481 QString aPossibleType = (aFileInfo.suffix()).toUpper() ;
483 if ( (aMap.values()).contains(aPossibleType) )
484 fileType = aPossibleType;
487 if (fileName.isEmpty() || fileType.isEmpty())
490 GEOM_Operation* anOp = new GEOM_Operation (app, aInsOp.in());
492 SUIT_OverrideCursor wc;
494 app->putInfo(tr("GEOM_PRP_LOADING").arg(SUIT_Tools::file(fileName, /*withExten=*/true)));
498 CORBA::String_var fileN = fileName.toLatin1().data();
499 CORBA::String_var fileT = fileType.toLatin1().data();
500 anObj = aInsOp->Import(fileN, fileT);
502 if ( !anObj->_is_nil() && aInsOp->IsDone() ) {
503 QString aPublishObjName =
504 GEOMBase::GetDefaultName(SUIT_Tools::file(fileName, /*withExten=*/true));
506 SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy(aStudy);
507 GeometryGUI::GetGeomGen()->PublishInStudy(aDSStudy,
508 SALOMEDS::SObject::_nil(),
510 aPublishObjName.toStdString().c_str());
512 GEOM_Displayer( stud ).Display( anObj.in() );
514 // update data model and object browser
515 getGeometryGUI()->updateObjBrowser( true );
522 SUIT_MessageBox::critical( app->desktop(),
523 QObject::tr( "GEOM_ERROR" ),
524 QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
525 QObject::tr("BUT_OK") );
528 catch( const SALOME::SALOME_Exception& S_ex ) {
529 //QtCatchCorbaException(S_ex);
534 app->updateActions(); //SRN: To update a Save button in the toolbar
540 //=====================================================================================
542 // purpose : BRep, Iges, Step
543 //=====================================================================================
544 bool GEOMToolsGUI::Export()
546 SalomeApp_Application* app = getGeometryGUI()->getApp();
547 if (!app) return false;
549 SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
551 cout << "FAILED to cast active study to SalomeApp_Study" << endl;
554 _PTR(Study) aStudy = stud->studyDS();
556 GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
557 if ( CORBA::is_nil( eng ) ) {
558 SUIT_MessageBox::critical( app->desktop(),
559 QObject::tr("WRN_WARNING"),
560 QObject::tr( "GEOM Engine is not started" ),
561 QObject::tr("BUT_OK") );
565 GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
566 if ( aInsOp->_is_nil() )
569 // Obtain a list of available export formats
571 GEOM::string_array_var aFormats, aPatterns;
572 aInsOp->ExportTranslators( aFormats, aPatterns );
573 for ( int i = 0, n = aFormats->length(); i < n; i++ )
574 aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
576 // Get selected objects
577 LightApp_SelectionMgr* sm = app->selectionMgr();
581 SALOME_ListIO selectedObjects;
582 sm->selectedObjects( selectedObjects );
584 SALOME_ListIteratorOfListIO It( selectedObjects );
585 for(;It.More();It.Next()) {
586 Handle(SALOME_InteractiveObject) IObject = It.Value();
587 Standard_Boolean found;
588 GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject(IObject, found);
590 if ( !found || anObj->_is_nil() )
594 QString file = getFileName(app->desktop(), QString( IObject->getName() ), aMap,
595 tr("GEOM_MEN_EXPORT"), false, fileType);
597 // User has pressed "Cancel" --> stop the operation
598 if ( file.isEmpty() || fileType.isEmpty() )
601 GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
603 SUIT_OverrideCursor wc;
605 app->putInfo( tr("GEOM_PRP_EXPORT").arg(SUIT_Tools::file( file, /*withExten=*/true )) );
610 aInsOp->Export( anObj, file.toStdString().c_str(), fileType.toLatin1().constData() );
612 if ( aInsOp->IsDone() )
618 SUIT_MessageBox::critical( app->desktop(),
619 QObject::tr( "GEOM_ERROR" ),
620 QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
621 QObject::tr("BUT_OK") );
625 catch (const SALOME::SALOME_Exception& S_ex) {
626 //QtCatchCorbaException(S_ex);
636 QString GEOMToolsGUI::getParentComponent( _PTR( Study ) study, const SALOME_ListIO& iobjs )
640 for ( SALOME_ListIteratorOfListIO it( iobjs ); it.More(); it.Next() ) {
642 Handle(SALOME_InteractiveObject) io = it.Value();
643 if ( !io->hasEntry() )
646 QString compName = getParentComponent( study->FindObjectID( io->getEntry() ) );
648 if ( parentComp.isNull() )
649 parentComp = compName;
650 else if ( parentComp.compare( compName) != 0 ) { // objects belonging to different components are selected
651 parentComp = QString::null;
659 QString GEOMToolsGUI::getParentComponent( _PTR( SObject ) obj )
662 _PTR(SComponent) comp = obj->GetFatherComponent();
664 _PTR(GenericAttribute) anAttr;
665 if ( comp->FindAttribute( anAttr, "AttributeName") ) {
666 _PTR(AttributeName) aName( anAttr );
667 return QString( aName->Value().c_str() );
674 //=====================================================================================
675 // function : RemoveObjectWithChildren
676 // purpose : to be used by OnEditDelete() method
677 //=====================================================================================
678 void GEOMToolsGUI::RemoveObjectWithChildren(_PTR(SObject) obj,
680 QList<SALOME_View*> views,
681 GEOM_Displayer* disp)
683 // iterate through all children of obj
684 for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
685 _PTR(SObject) child (it->Value());
686 RemoveObjectWithChildren(child, aStudy, views, disp);
689 // erase object and remove it from engine
690 _PTR(GenericAttribute) anAttr;
691 if (obj->FindAttribute(anAttr, "AttributeIOR")) {
692 _PTR(AttributeIOR) anIOR (anAttr);
694 // Delete shape in Client
695 const TCollection_AsciiString ASCIor ((char*)anIOR->Value().c_str());
696 getGeometryGUI()->GetShapeReader().RemoveShapeFromBuffer(ASCIor);
698 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(obj);
699 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
700 if (!CORBA::is_nil(geomObj)) {
701 // Erase graphical object
702 QListIterator<SALOME_View*> it( views );
703 while ( it.hasNext() )
704 if ( SALOME_View* view = it.next() )
705 disp->Erase(geomObj, true, view);
707 // Remove object from Engine
708 GeometryGUI::GetGeomGen()->RemoveObject( geomObj );
713 //=====================================================================================
714 // function : CheckSubObjectInUse
715 // purpose : to be used by OnEditDelete() method
716 //=====================================================================================
717 bool GEOMToolsGUI::CheckSubObjectInUse(_PTR(SObject) checkobj,
718 _PTR(SObject) remobj,
721 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(checkobj);
722 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
723 if( CORBA::is_nil(geomObj) )
726 GEOM::ListOfGO_var list = geomObj->GetDependency();
727 if( list->length() > 1 )
728 for(int i = 0; i < list->length(); i++ ){
729 CORBA::Object_var corbaObj_rem = GeometryGUI::ClientSObjectToObject(remobj);
730 GEOM::GEOM_Object_var geomObj_rem = GEOM::GEOM_Object::_narrow( corbaObj_rem );
731 if( list[i]->_is_equivalent( geomObj_rem ) ){
732 SalomeApp_Application* app =
733 dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
735 SUIT_MessageBox::warning ( app->desktop(),
736 QObject::tr("WRN_WARNING"),
737 QObject::tr("DEP_OBJECT"),
738 QObject::tr("BUT_OK") );
746 //=================================================================================
747 // function : deactivate()
748 // purpose : Called when GEOM component is deactivated
749 //=================================================================================
750 void GEOMToolsGUI::deactivate()
752 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
754 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
755 GEOM_Displayer aDisp (appStudy);
756 aDisp.GlobalSelection();
757 getGeometryGUI()->setLocalSelectionMode(GEOM_ALLOBJECTS);
761 //=====================================================================================
763 //=====================================================================================
767 GEOMGUI* GetLibGUI( GeometryGUI* parent )
769 return new GEOMToolsGUI( parent );