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
22 // File : GEOMBase_Tools.cxx
23 // Author : Damien COQUERET, Open CASCADE S.A.S.
26 #include "GEOMToolsGUI.h"
28 #include <GeometryGUI.h>
30 #include <GEOM_Operation.h>
31 #include <GEOM_Displayer.h>
33 #include <SUIT_Session.h>
34 #include <SUIT_OverrideCursor.h>
35 #include <SUIT_MessageBox.h>
36 #include <SUIT_Tools.h>
37 #include <SUIT_FileDlg.h>
38 #include <SUIT_Desktop.h>
39 #include <SUIT_ViewModel.h>
40 #include <SUIT_ViewManager.h>
42 #include <SalomeApp_Application.h>
43 #include <SalomeApp_Study.h>
44 #include <LightApp_SelectionMgr.h>
45 #include <GEOMImpl_Types.hxx>
47 #include <SALOME_ListIO.hxx>
48 #include <SALOME_ListIteratorOfListIO.hxx>
49 #include <SALOME_Prs.h>
52 #include <QApplication>
56 #include <TCollection_AsciiString.hxx>
60 typedef QMap<QString, QString> FilterMap;
62 //=======================================================================
63 // function : getFileName
64 // purpose : Selection of a file name for Import/Export. Returns also
65 // the selected file type code through <filter> argument.
66 //=======================================================================
67 static QString getFileName( QWidget* parent,
68 const QString& initial,
69 const FilterMap& filterMap,
70 const QString& caption,
74 static QString lastUsedFilter;
76 for ( FilterMap::const_iterator it = filterMap.begin(); it != filterMap.end(); ++it )
77 filters.push_back( it.key() );
79 SUIT_FileDlg* fd = new SUIT_FileDlg( parent, open, true, true );
80 if ( !caption.isEmpty() )
81 fd->setWindowTitle( caption );
83 if ( !initial.isEmpty() )
84 fd->selectFile( initial );
86 if ( !lastUsedFilter.isEmpty() && filterMap.contains( lastUsedFilter ) )
87 fd->selectFilter( lastUsedFilter );
89 fd->setFilters( filters );
92 QString filename = fd->selectedFile();
93 format = filterMap[fd->selectedFilter()];
94 lastUsedFilter = fd->selectedFilter();
96 qApp->processEvents();
100 //=======================================================================
101 // function : GEOMToolsGUI()
102 // purpose : Constructor
103 //=======================================================================
104 GEOMToolsGUI::GEOMToolsGUI( GeometryGUI* parent )
110 //=======================================================================
111 // function : ~GEOMToolsGUI()
112 // purpose : Destructor
113 //=======================================================================
114 GEOMToolsGUI::~GEOMToolsGUI()
119 //=======================================================================
120 // function : OnGUIEvent()
122 //=======================================================================
123 bool GEOMToolsGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent)
125 getGeometryGUI()->EmitSignalDeactivateDialog();
127 switch (theCommandID)
139 case 111: // IMPORT BREP
140 case 112: // IMPORT IGES
141 case 113: // IMPORT STEP
146 case 121: // EXPORT BREP
147 case 122: // EXPORT IGES
148 case 123: // EXPORT STEP
153 case 2171: // POPUP VIEWER - SELECT ONLY - VERTEX
155 OnSelectOnly( GEOM_POINT );
158 case 2172: // POPUP VIEWER - SELECT ONLY - EDGE
160 OnSelectOnly( GEOM_EDGE );
163 case 2173: // POPUP VIEWER - SELECT ONLY - WIRE
165 OnSelectOnly( GEOM_WIRE );
168 case 2174: // POPUP VIEWER - SELECT ONLY - FACE
170 OnSelectOnly( GEOM_FACE );
173 case 2175: // POPUP VIEWER - SELECT ONLY - SHELL
175 OnSelectOnly( GEOM_SHELL );
178 case 2176: // POPUP VIEWER - SELECT ONLY - SOLID
180 OnSelectOnly( GEOM_SOLID );
183 case 2177: // POPUP VIEWER - SELECT ONLY - COMPOUND
185 OnSelectOnly( GEOM_COMPOUND );
188 case 2178: // POPUP VIEWER - SELECT ONLY - SELECT ALL
190 OnSelectOnly( GEOM_ALLOBJECTS );
193 case 411: // SETTINGS - ADD IN STUDY
195 // SAN -- TO BE REMOVED !!!
198 case 412: // SETTINGS - SHADING COLOR
203 case 413: // SETTINGS - ISOS
208 case 414: // SETTINGS : STEP VALUE FOR SPIN BOXES
213 case 804: // ADD IN STUDY - POPUP VIEWER
215 // SAN -- TO BE REMOVED !!!!
223 case 5103: // CHECK GEOMETRY
228 case 8032: // COLOR - POPUP VIEWER
233 case 8033: // TRANSPARENCY - POPUP VIEWER
238 case 8034: // ISOS - POPUP VIEWER
243 case 9024 : // OPEN - OBJBROSER POPUP
250 SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
259 //===============================================================================
260 // function : OnEditDelete()
262 //===============================================================================
263 void GEOMToolsGUI::OnEditDelete()
265 SALOME_ListIO selected;
266 SalomeApp_Application* app =
267 dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
269 LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
270 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
271 if ( aSelMgr && appStudy ) {
272 aSelMgr->selectedObjects( selected, QString::null, false );
273 if ( !selected.IsEmpty() ) {
274 _PTR(Study) aStudy = appStudy->studyDS();
276 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
278 SUIT_MessageBox::warning ( app->desktop(),
279 QObject::tr("WRN_WARNING"),
280 QObject::tr("WRN_STUDY_LOCKED"),
281 QObject::tr("BUT_OK") );
285 // VSR 17/11/04: check if all objects selected belong to GEOM component --> start
286 // modifications of ASV 01.06.05
287 QString parentComp = getParentComponent( aStudy, selected );
288 CORBA::String_var geomIOR = app->orb()->object_to_string( GeometryGUI::GetGeomGen() );
289 QString geomComp = getParentComponent( aStudy->FindObjectIOR( geomIOR.in() ) );
291 if ( parentComp != geomComp ) {
292 SUIT_MessageBox::warning ( app->desktop(),
293 QObject::tr("ERR_ERROR"),
294 QObject::tr("NON_GEOM_OBJECTS_SELECTED").arg( getGeometryGUI()->moduleName() ),
295 QObject::tr("BUT_OK") );
298 // VSR 17/11/04: check if all objects selected belong to GEOM component <-- finish
300 if ( SUIT_MessageBox::warning( app->desktop(),
301 QObject::tr( "GEOM_WRN_WARNING" ),
302 QObject::tr( "GEOM_REALLY_DELETE" ),
303 QObject::tr( "GEOM_BUT_YES" ),
304 QObject::tr( "GEOM_BUT_NO" ), 1 ) != 0 )
307 // QAD_Operation* op = new SALOMEGUI_ImportOperation(.....);
310 // prepare list of SALOME_Views
311 QList<SALOME_View*> views;
314 ViewManagerList vmans = app->viewManagers();
315 SUIT_ViewManager* vman;
316 QListIterator<SUIT_ViewManager*> it( vmans );
317 while ( it.hasNext() && (vman = it.next()) ) {
318 SUIT_ViewModel* vmod = vman->getViewModel();
319 view = dynamic_cast<SALOME_View*> ( vmod ); // must work for OCC and VTK views
321 views.append( view );
324 _PTR(StudyBuilder) aStudyBuilder (aStudy->NewBuilder());
325 _PTR(GenericAttribute) anAttr;
326 GEOM_Displayer* disp = new GEOM_Displayer( appStudy );
328 _PTR(SComponent) aGeom ( aStudy->FindComponent("GEOM") );
332 // MAIN LOOP OF SELECTED OBJECTS
333 for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
335 Handle(SALOME_InteractiveObject) io = It.Value();
336 if ( !io->hasEntry() )
339 _PTR(SObject) obj ( aStudy->FindObjectID( io->getEntry() ) );
341 // disable removal of "Geometry" component object
342 if ( !strcmp( obj->GetIOR().c_str(), geomIOR ) )
345 //If the object has been used to create another one,then it can't be deleted
346 _PTR(ChildIterator) it (aStudy->NewChildIterator(aGeom));
347 for ( it->InitEx( true ); it->More(); it->Next() ) {
348 _PTR(SObject) chobj (it->Value());
349 if(CheckSubObjectInUse(chobj, obj, aStudy)) return;
351 for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
352 _PTR(SObject) child (it->Value());
353 if(CheckSubObjectInUse( chobj, child, aStudy)) return;
357 RemoveObjectWithChildren(obj, aStudy, views, disp);
359 // Remove objects from Study
360 aStudyBuilder->RemoveObject( obj );
363 } // MAIN LOOP of selected
366 aSelMgr->setSelectedObjects( selected );
367 getGeometryGUI()->updateObjBrowser();
368 } // if ( selected not empty )
369 } // if ( selMgr && appStudy )
371 app->updateActions(); //SRN: To update a Save button in the toolbar
383 //==============================================================================
384 // function : OnEditCopy()
386 //==============================================================================
387 void GEOMToolsGUI::OnEditCopy()
390 SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection() );
391 GEOM::string_array_var listIOR = new GEOM::string_array;
393 const SALOME_ListIO& List = Sel->StoredIObjects();
395 myGeomBase->ConvertListOfIOInListOfIOR(List, listIOR);
397 Sel->ClearIObjects();
399 SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
400 int aStudyID = aStudy->StudyId();
402 for (unsigned int ind = 0; ind < listIOR->length();ind++) {
403 GEOM::GEOM_Object_var aShapeInit = myGeom->GetIORFromString(listIOR[ind]);
405 GEOM::GEOM_IInsertOperations_var IOp = myGeom->GetIInsertOperations(aStudyID);
406 GEOM::GEOM_Object_var result = IOp->MakeCopy(aShapeInit);
407 myGeomBase->Display(result);
409 catch (const SALOME::SALOME_Exception& S_ex) {
410 QtCatchCorbaException(S_ex);
414 QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_READY"));
419 //=====================================================================================
421 // purpose : BRep, Iges, Step
422 //=====================================================================================
423 bool GEOMToolsGUI::Import()
425 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( getGeometryGUI()->getApp() );
426 //SUIT_Application* app = getGeometryGUI()->getApp();
427 if (! app) return false;
429 SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
431 cout << "FAILED to cast active study to SalomeApp_Study" << endl;
434 _PTR(Study) aStudy = stud->studyDS();
436 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
438 SUIT_MessageBox::warning ( app->desktop(),
439 QObject::tr("WRN_WARNING"),
440 QObject::tr("WRN_STUDY_LOCKED"),
441 QObject::tr("BUT_OK") );
445 GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
446 if ( CORBA::is_nil( eng ) ) {
447 SUIT_MessageBox::critical( app->desktop(),
448 QObject::tr("WRN_WARNING"),
449 QObject::tr( "GEOM Engine is not started" ),
450 QObject::tr("BUT_OK") );
454 GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
455 if ( aInsOp->_is_nil() )
458 GEOM::GEOM_Object_var anObj;
460 // Obtain a list of available import formats
462 GEOM::string_array_var aFormats, aPatterns;
463 aInsOp->ImportTranslators( aFormats, aPatterns );
465 for ( int i = 0, n = aFormats->length(); i < n; i++ )
466 aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
470 QString fileName = getFileName(app->desktop(), "", aMap,
471 tr("GEOM_MEN_IMPORT"), true, fileType);
473 if (fileType.isEmpty() )
475 // Trying to detect file type
476 QFileInfo aFileInfo( fileName );
477 QString aPossibleType = (aFileInfo.suffix()).toUpper() ;
479 if ( (aMap.values()).contains(aPossibleType) )
480 fileType = aPossibleType;
483 if (fileName.isEmpty() || fileType.isEmpty())
486 GEOM_Operation* anOp = new GEOM_Operation (app, aInsOp.in());
488 SUIT_OverrideCursor wc;
490 app->putInfo(tr("GEOM_PRP_LOADING").arg(SUIT_Tools::file(fileName, /*withExten=*/true)));
494 CORBA::String_var fileN = fileName.toLatin1().data();
495 CORBA::String_var fileT = fileType.toLatin1().data();
496 anObj = aInsOp->Import(fileN, fileT);
498 if ( !anObj->_is_nil() && aInsOp->IsDone() ) {
499 QString aPublishObjName =
500 GEOMBase::GetDefaultName(SUIT_Tools::file(fileName, /*withExten=*/true));
502 SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy(aStudy);
503 GeometryGUI::GetGeomGen()->PublishInStudy(aDSStudy,
504 SALOMEDS::SObject::_nil(),
506 aPublishObjName.toStdString().c_str());
508 GEOM_Displayer( stud ).Display( anObj.in() );
510 // update data model and object browser
511 getGeometryGUI()->updateObjBrowser( true );
518 SUIT_MessageBox::critical( app->desktop(),
519 QObject::tr( "GEOM_ERROR" ),
520 QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
521 QObject::tr("BUT_OK") );
524 catch( const SALOME::SALOME_Exception& S_ex ) {
525 //QtCatchCorbaException(S_ex);
530 app->updateActions(); //SRN: To update a Save button in the toolbar
536 //=====================================================================================
538 // purpose : BRep, Iges, Step
539 //=====================================================================================
540 bool GEOMToolsGUI::Export()
542 SalomeApp_Application* app = getGeometryGUI()->getApp();
543 if (!app) return false;
545 SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
547 cout << "FAILED to cast active study to SalomeApp_Study" << endl;
550 _PTR(Study) aStudy = stud->studyDS();
552 GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
553 if ( CORBA::is_nil( eng ) ) {
554 SUIT_MessageBox::critical( app->desktop(),
555 QObject::tr("WRN_WARNING"),
556 QObject::tr( "GEOM Engine is not started" ),
557 QObject::tr("BUT_OK") );
561 GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
562 if ( aInsOp->_is_nil() )
565 // Obtain a list of available export formats
567 GEOM::string_array_var aFormats, aPatterns;
568 aInsOp->ExportTranslators( aFormats, aPatterns );
569 for ( int i = 0, n = aFormats->length(); i < n; i++ )
570 aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
572 // Get selected objects
573 LightApp_SelectionMgr* sm = app->selectionMgr();
577 SALOME_ListIO selectedObjects;
578 sm->selectedObjects( selectedObjects );
580 SALOME_ListIteratorOfListIO It( selectedObjects );
581 for(;It.More();It.Next()) {
582 Handle(SALOME_InteractiveObject) IObject = It.Value();
583 Standard_Boolean found;
584 GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject(IObject, found);
586 if ( !found || anObj->_is_nil() )
590 QString file = getFileName(app->desktop(), QString( IObject->getName() ), aMap,
591 tr("GEOM_MEN_EXPORT"), false, fileType);
593 // User has pressed "Cancel" --> stop the operation
594 if ( file.isEmpty() || fileType.isEmpty() )
597 GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
599 SUIT_OverrideCursor wc;
601 app->putInfo( tr("GEOM_PRP_EXPORT").arg(SUIT_Tools::file( file, /*withExten=*/true )) );
606 aInsOp->Export( anObj, file.toStdString().c_str(), fileType.toLatin1().constData() );
608 if ( aInsOp->IsDone() )
614 SUIT_MessageBox::critical( app->desktop(),
615 QObject::tr( "GEOM_ERROR" ),
616 QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
617 QObject::tr("BUT_OK") );
621 catch (const SALOME::SALOME_Exception& S_ex) {
622 //QtCatchCorbaException(S_ex);
632 QString GEOMToolsGUI::getParentComponent( _PTR( Study ) study, const SALOME_ListIO& iobjs )
636 for ( SALOME_ListIteratorOfListIO it( iobjs ); it.More(); it.Next() ) {
638 Handle(SALOME_InteractiveObject) io = it.Value();
639 if ( !io->hasEntry() )
642 QString compName = getParentComponent( study->FindObjectID( io->getEntry() ) );
644 if ( parentComp.isNull() )
645 parentComp = compName;
646 else if ( parentComp.compare( compName) != 0 ) { // objects belonging to different components are selected
647 parentComp = QString::null;
655 QString GEOMToolsGUI::getParentComponent( _PTR( SObject ) obj )
658 _PTR(SComponent) comp = obj->GetFatherComponent();
660 _PTR(GenericAttribute) anAttr;
661 if ( comp->FindAttribute( anAttr, "AttributeName") ) {
662 _PTR(AttributeName) aName( anAttr );
663 return QString( aName->Value().c_str() );
670 //=====================================================================================
671 // function : RemoveObjectWithChildren
672 // purpose : to be used by OnEditDelete() method
673 //=====================================================================================
674 void GEOMToolsGUI::RemoveObjectWithChildren(_PTR(SObject) obj,
676 QList<SALOME_View*> views,
677 GEOM_Displayer* disp)
679 // iterate through all children of obj
680 for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
681 _PTR(SObject) child (it->Value());
682 RemoveObjectWithChildren(child, aStudy, views, disp);
685 // erase object and remove it from engine
686 _PTR(GenericAttribute) anAttr;
687 if (obj->FindAttribute(anAttr, "AttributeIOR")) {
688 _PTR(AttributeIOR) anIOR (anAttr);
690 // Delete shape in Client
691 const TCollection_AsciiString ASCIor ((char*)anIOR->Value().c_str());
692 getGeometryGUI()->GetShapeReader().RemoveShapeFromBuffer(ASCIor);
694 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(obj);
695 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
696 if (!CORBA::is_nil(geomObj)) {
697 // Erase graphical object
698 QListIterator<SALOME_View*> it( views );
699 while ( it.hasNext() )
700 if ( SALOME_View* view = it.next() )
701 disp->Erase(geomObj, true, view);
703 // Remove object from Engine
704 GeometryGUI::GetGeomGen()->RemoveObject( geomObj );
709 //=====================================================================================
710 // function : CheckSubObjectInUse
711 // purpose : to be used by OnEditDelete() method
712 //=====================================================================================
713 bool GEOMToolsGUI::CheckSubObjectInUse(_PTR(SObject) checkobj,
714 _PTR(SObject) remobj,
717 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(checkobj);
718 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
719 if( CORBA::is_nil(geomObj) )
722 GEOM::ListOfGO_var list = geomObj->GetDependency();
723 if( list->length() > 1 )
724 for(int i = 0; i < list->length(); i++ ){
725 CORBA::Object_var corbaObj_rem = GeometryGUI::ClientSObjectToObject(remobj);
726 GEOM::GEOM_Object_var geomObj_rem = GEOM::GEOM_Object::_narrow( corbaObj_rem );
727 if( list[i]->_is_equivalent( geomObj_rem ) ){
728 SalomeApp_Application* app =
729 dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
731 SUIT_MessageBox::warning ( app->desktop(),
732 QObject::tr("WRN_WARNING"),
733 QObject::tr("DEP_OBJECT"),
734 QObject::tr("BUT_OK") );
742 //=================================================================================
743 // function : deactivate()
744 // purpose : Called when GEOM component is deactivated
745 //=================================================================================
746 void GEOMToolsGUI::deactivate()
748 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
750 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
751 GEOM_Displayer aDisp (appStudy);
752 aDisp.GlobalSelection();
753 getGeometryGUI()->setLocalSelectionMode(GEOM_ALLOBJECTS);
757 //=====================================================================================
759 //=====================================================================================
763 GEOMGUI* GetLibGUI( GeometryGUI* parent )
765 return new GEOMToolsGUI( parent );