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"
32 #include "GEOM_Actor.h"
34 #include "GEOM_Operation.h"
35 #include "GEOM_Displayer.h"
37 #include <SUIT_Session.h>
38 #include <SUIT_Application.h>
39 #include <SUIT_OverrideCursor.h>
40 #include <SUIT_MessageBox.h>
41 #include <SUIT_Tools.h>
42 #include <SUIT_FileDlg.h>
43 #include <SUIT_Desktop.h>
44 #include <SUIT_ViewModel.h>
46 #include <SalomeApp_Application.h>
47 #include <SalomeApp_Study.h>
48 #include <LightApp_SelectionMgr.h>
49 #include <GEOMImpl_Types.hxx>
51 #include <SALOME_ListIteratorOfListIO.hxx>
52 #include <SALOME_Prs.h>
54 #include "utilities.h"
57 #include <qapplication.h>
61 #include <TCollection_AsciiString.hxx>
65 typedef QMap<QString, QString> FilterMap;
67 //=======================================================================
68 // function : getFileName
69 // purpose : Selection of a file name for Import/Export. Returns also
70 // the selected file type code through <filter> argument.
71 //=======================================================================
72 static QString getFileName( QWidget* parent,
73 const QString& initial,
74 const FilterMap& filterMap,
75 const QStringList filters,
76 const QString& caption,
80 static QString lastUsedFilter;
81 //QStringList filters;
83 for ( FilterMap::const_iterator it = filterMap.begin(); it != filterMap.end(); ++it ) {
84 //filters.push_back( it.key() );
86 if (it.key().contains("BREP", false)) {
87 aBrepFilter = it.key();
91 SUIT_FileDlg* fd = new SUIT_FileDlg( parent, open, true, true );
92 if ( !caption.isEmpty() )
93 fd->setCaption( caption );
95 if ( !initial.isEmpty() )
96 fd->setSelection( initial );
98 fd->setFilters( filters );
100 if ( !lastUsedFilter.isEmpty() && filterMap.contains( lastUsedFilter ) )
101 fd->setSelectedFilter( lastUsedFilter );
103 if (!aBrepFilter.isEmpty()) {
104 fd->setSelectedFilter(aBrepFilter);
109 QString filename = fd->selectedFile();
110 format = filterMap[fd->selectedFilter()];
111 lastUsedFilter = fd->selectedFilter();
113 qApp->processEvents();
117 //=======================================================================
118 // function : GEOMToolsGUI()
119 // purpose : Constructor
120 //=======================================================================
121 GEOMToolsGUI::GEOMToolsGUI( GeometryGUI* parent )
127 //=======================================================================
128 // function : ~GEOMToolsGUI()
129 // purpose : Destructor
130 //=======================================================================
131 GEOMToolsGUI::~GEOMToolsGUI()
136 //=======================================================================
137 // function : OnGUIEvent()
139 //=======================================================================
140 bool GEOMToolsGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent)
142 getGeometryGUI()->EmitSignalDeactivateDialog();
144 switch (theCommandID)
156 case 111: // IMPORT BREP
157 case 112: // IMPORT IGES
158 case 113: // IMPORT STEP
163 case 121: // EXPORT BREP
164 case 122: // EXPORT IGES
165 case 123: // EXPORT STEP
170 case 2171: // POPUP VIEWER - SELECT ONLY - VERTEX
172 OnSelectOnly( GEOM_POINT );
175 case 2172: // POPUP VIEWER - SELECT ONLY - EDGE
177 OnSelectOnly( GEOM_EDGE );
180 case 2173: // POPUP VIEWER - SELECT ONLY - WIRE
182 OnSelectOnly( GEOM_WIRE );
185 case 2174: // POPUP VIEWER - SELECT ONLY - FACE
187 OnSelectOnly( GEOM_FACE );
190 case 2175: // POPUP VIEWER - SELECT ONLY - SHELL
192 OnSelectOnly( GEOM_SHELL );
195 case 2176: // POPUP VIEWER - SELECT ONLY - SOLID
197 OnSelectOnly( GEOM_SOLID );
200 case 2177: // POPUP VIEWER - SELECT ONLY - COMPOUND
202 OnSelectOnly( GEOM_COMPOUND );
205 case 2178: // POPUP VIEWER - SELECT ONLY - SELECT ALL
207 OnSelectOnly( GEOM_ALLOBJECTS );
210 case 411: // SETTINGS - ADD IN STUDY
212 // SAN -- TO BE REMOVED !!!
215 case 412: // SETTINGS - SHADING COLOR
220 case 413: // SETTINGS - ISOS
225 case 414: // SETTINGS : STEP VALUE FOR SPIN BOXES
230 case 804: // ADD IN STUDY - POPUP VIEWER
232 // SAN -- TO BE REMOVED !!!!
240 case 5103: // CHECK GEOMETRY
245 case 8032: // COLOR - POPUP VIEWER
250 case 8033: // TRANSPARENCY - POPUP VIEWER
255 case 8034: // ISOS - POPUP VIEWER
260 case 8035: // AUTO COLOR - POPUP VIEWER
265 case 8036: // DISABLE AUTO COLOR - POPUP VIEWER
267 OnDisableAutoColor();
270 case 9024 : // OPEN - OBJBROSER POPUP
277 SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
286 //===============================================================================
287 // function : OnEditDelete()
289 //===============================================================================
290 void GEOMToolsGUI::OnEditDelete()
292 SALOME_ListIO selected;
293 SalomeApp_Application* app =
294 dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
296 LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
297 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
298 if ( aSelMgr && appStudy ) {
299 aSelMgr->selectedObjects( selected, QString::null, false );
300 if ( !selected.IsEmpty() ) {
301 _PTR(Study) aStudy = appStudy->studyDS();
303 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
305 SUIT_MessageBox::warn1 ( app->desktop(),
306 QObject::tr("WRN_WARNING"),
307 QObject::tr("WRN_STUDY_LOCKED"),
308 QObject::tr("BUT_OK") );
312 // VSR 17/11/04: check if all objects selected belong to GEOM component --> start
313 // modifications of ASV 01.06.05
314 QString parentComp = getParentComponent( aStudy, selected );
315 CORBA::String_var geomIOR = app->orb()->object_to_string( GeometryGUI::GetGeomGen() );
316 QString geomComp = getParentComponent( aStudy->FindObjectIOR( geomIOR.in() ) );
318 if ( parentComp != geomComp ) {
319 SUIT_MessageBox::warn1 ( app->desktop(),
320 QObject::tr("ERR_ERROR"),
321 QObject::tr("NON_GEOM_OBJECTS_SELECTED").arg( getGeometryGUI()->moduleName() ),
322 QObject::tr("BUT_OK") );
325 // VSR 17/11/04: check if all objects selected belong to GEOM component <-- finish
327 if ( SUIT_MessageBox::warn2( app->desktop(),
328 QObject::tr( "GEOM_WRN_WARNING" ),
329 QObject::tr( "GEOM_REALLY_DELETE" ),
330 QObject::tr( "GEOM_BUT_YES" ),
331 QObject::tr( "GEOM_BUT_NO" ), 1, 0, 0 ) != 1 )
334 // QAD_Operation* op = new SALOMEGUI_ImportOperation(.....);
337 // prepare list of SALOME_Views
338 QPtrList<SALOME_View> views;
341 ViewManagerList vmans = app->viewManagers();
342 SUIT_ViewManager* vman;
343 for ( vman = vmans.first(); vman; vman = vmans.next() ) {
344 SUIT_ViewModel* vmod = vman->getViewModel();
345 view = dynamic_cast<SALOME_View*> ( vmod ); // must work for OCC and VTK views
347 views.append( view );
350 _PTR(StudyBuilder) aStudyBuilder (aStudy->NewBuilder());
351 _PTR(GenericAttribute) anAttr;
352 GEOM_Displayer* disp = new GEOM_Displayer( appStudy );
354 _PTR(SComponent) aGeom ( aStudy->FindComponent("GEOM") );
358 // MAIN LOOP OF SELECTED OBJECTS
359 for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
361 Handle(SALOME_InteractiveObject) io = It.Value();
362 if ( !io->hasEntry() )
365 _PTR(SObject) obj ( aStudy->FindObjectID( io->getEntry() ) );
367 // disable removal of "Geometry" component object
368 if ( !strcmp( obj->GetIOR().c_str(), geomIOR ) )
371 //If the object has been used to create another one,then it can't be deleted
372 _PTR(ChildIterator) it (aStudy->NewChildIterator(aGeom));
373 for ( it->InitEx( true ); it->More(); it->Next() ) {
374 _PTR(SObject) chobj (it->Value());
375 if(CheckSubObjectInUse(chobj, obj, aStudy)) return;
377 for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
378 _PTR(SObject) child (it->Value());
379 if(CheckSubObjectInUse( chobj, child, aStudy)) return;
383 RemoveObjectWithChildren(obj, aStudy, views, disp);
385 // Remove objects from Study
386 aStudyBuilder->RemoveObject( obj );
389 } // MAIN LOOP of selected
392 aSelMgr->setSelectedObjects( selected );
393 getGeometryGUI()->updateObjBrowser();
394 } // if ( selected not empty )
395 } // if ( selMgr && appStudy )
397 app->updateActions(); //SRN: To update a Save button in the toolbar
409 //==============================================================================
410 // function : OnEditCopy()
412 //==============================================================================
413 void GEOMToolsGUI::OnEditCopy()
416 SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection() );
417 GEOM::string_array_var listIOR = new GEOM::string_array;
419 const SALOME_ListIO& List = Sel->StoredIObjects();
421 myGeomBase->ConvertListOfIOInListOfIOR(List, listIOR);
423 Sel->ClearIObjects();
425 SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
426 int aStudyID = aStudy->StudyId();
428 for (unsigned int ind = 0; ind < listIOR->length();ind++) {
429 GEOM::GEOM_Object_var aShapeInit = myGeom->GetIORFromString(listIOR[ind]);
431 GEOM::GEOM_IInsertOperations_var IOp = myGeom->GetIInsertOperations(aStudyID);
432 GEOM::GEOM_Object_var result = IOp->MakeCopy(aShapeInit);
433 myGeomBase->Display(result);
435 catch (const SALOME::SALOME_Exception& S_ex) {
436 QtCatchCorbaException(S_ex);
440 QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_READY"));
445 //=====================================================================================
447 // purpose : BRep, Iges, Step
448 //=====================================================================================
449 bool GEOMToolsGUI::Import()
451 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( getGeometryGUI()->getApp() );
452 //SUIT_Application* app = getGeometryGUI()->getApp();
453 if (! app) return false;
455 SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
457 cout << "FAILED to cast active study to SalomeApp_Study" << endl;
460 _PTR(Study) aStudy = stud->studyDS();
462 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
464 SUIT_MessageBox::warn1 ( app->desktop(),
465 QObject::tr("WRN_WARNING"),
466 QObject::tr("WRN_STUDY_LOCKED"),
467 QObject::tr("BUT_OK") );
471 GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
472 if ( CORBA::is_nil( eng ) ) {
473 SUIT_MessageBox::error1( app->desktop(),
474 QObject::tr("WRN_WARNING"),
475 QObject::tr( "GEOM Engine is not started" ),
476 QObject::tr("BUT_OK") );
480 GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
481 if ( aInsOp->_is_nil() )
484 GEOM::GEOM_Object_var anObj;
486 // Obtain a list of available import formats
489 GEOM::string_array_var aFormats, aPatterns;
490 aInsOp->ImportTranslators( aFormats, aPatterns );
492 for ( int i = 0, n = aFormats->length(); i < n; i++ ) {
493 aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
494 filters.push_back( (char*)aPatterns[i] );
499 QString fileName = getFileName(app->desktop(), "", aMap, filters,
500 tr("GEOM_MEN_IMPORT"), true, fileType);
502 if (fileType.isEmpty() )
504 // Trying to detect file type
505 QFileInfo aFileInfo( fileName );
506 QString aPossibleType = (aFileInfo.extension(false)).upper() ;
508 if ( (aMap.values()).contains(aPossibleType) )
509 fileType = aPossibleType;
512 if (fileName.isEmpty() || fileType.isEmpty())
515 GEOM_Operation* anOp = new GEOM_Operation (app, aInsOp.in());
517 SUIT_OverrideCursor wc;
519 app->putInfo(tr("GEOM_PRP_LOADING").arg(SUIT_Tools::file(fileName, /*withExten=*/true)));
523 CORBA::String_var fileN = fileName.latin1();
524 CORBA::String_var fileT = fileType.latin1();
525 anObj = aInsOp->Import(fileN, fileT);
527 if ( !anObj->_is_nil() && aInsOp->IsDone() ) {
528 QString aPublishObjName =
529 GEOMBase::GetDefaultName(SUIT_Tools::file(fileName, /*withExten=*/true));
531 SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy(aStudy);
532 GeometryGUI::GetGeomGen()->PublishInStudy(aDSStudy,
533 SALOMEDS::SObject::_nil(),
537 GEOM_Displayer( stud ).Display( anObj.in() );
539 // update data model and object browser
540 getGeometryGUI()->updateObjBrowser( true );
547 SUIT_MessageBox::error1( app->desktop(),
548 QObject::tr( "GEOM_ERROR" ),
549 QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
550 QObject::tr("BUT_OK") );
553 catch( const SALOME::SALOME_Exception& S_ex ) {
554 //QtCatchCorbaException(S_ex);
559 app->updateActions(); //SRN: To update a Save button in the toolbar
565 //=====================================================================================
567 // purpose : BRep, Iges, Step
568 //=====================================================================================
569 bool GEOMToolsGUI::Export()
571 SalomeApp_Application* app = getGeometryGUI()->getApp();
572 if (!app) return false;
574 SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
576 cout << "FAILED to cast active study to SalomeApp_Study" << endl;
579 _PTR(Study) aStudy = stud->studyDS();
581 GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
582 if ( CORBA::is_nil( eng ) ) {
583 SUIT_MessageBox::error1( app->desktop(),
584 QObject::tr("WRN_WARNING"),
585 QObject::tr( "GEOM Engine is not started" ),
586 QObject::tr("BUT_OK") );
590 GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
591 if ( aInsOp->_is_nil() )
594 // Obtain a list of available export formats
597 GEOM::string_array_var aFormats, aPatterns;
598 aInsOp->ExportTranslators( aFormats, aPatterns );
599 for ( int i = 0, n = aFormats->length(); i < n; i++ ) {
600 aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
601 filters.push_back( (char*)aPatterns[i] );
604 // Get selected objects
605 LightApp_SelectionMgr* sm = app->selectionMgr();
609 SALOME_ListIO selectedObjects;
610 sm->selectedObjects( selectedObjects );
612 SALOME_ListIteratorOfListIO It( selectedObjects );
613 for(;It.More();It.Next()) {
614 Handle(SALOME_InteractiveObject) IObject = It.Value();
615 Standard_Boolean found;
616 GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject(IObject, found);
618 if ( !found || anObj->_is_nil() )
622 QString file = getFileName(app->desktop(), QString( IObject->getName() ), aMap, filters,
623 tr("GEOM_MEN_EXPORT"), false, fileType);
625 // User has pressed "Cancel" --> stop the operation
626 if ( file.isEmpty() || fileType.isEmpty() )
629 GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
631 SUIT_OverrideCursor wc;
633 app->putInfo( tr("GEOM_PRP_EXPORT").arg(SUIT_Tools::file( file, /*withExten=*/true )) );
638 aInsOp->Export( anObj, file, fileType.latin1() );
640 if ( aInsOp->IsDone() )
646 SUIT_MessageBox::error1( app->desktop(),
647 QObject::tr( "GEOM_ERROR" ),
648 QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
649 QObject::tr("BUT_OK") );
653 catch (const SALOME::SALOME_Exception& S_ex) {
654 //QtCatchCorbaException(S_ex);
664 QString GEOMToolsGUI::getParentComponent( _PTR( Study ) study, const SALOME_ListIO& iobjs )
668 for ( SALOME_ListIteratorOfListIO it( iobjs ); it.More(); it.Next() ) {
670 Handle(SALOME_InteractiveObject) io = it.Value();
671 if ( !io->hasEntry() )
674 QString compName = getParentComponent( study->FindObjectID( io->getEntry() ) );
676 if ( parentComp.isNull() )
677 parentComp = compName;
678 else if ( parentComp.compare( compName) != 0 ) { // objects belonging to different components are selected
679 parentComp = QString::null;
687 QString GEOMToolsGUI::getParentComponent( _PTR( SObject ) obj )
690 _PTR(SComponent) comp = obj->GetFatherComponent();
692 _PTR(GenericAttribute) anAttr;
693 if ( comp->FindAttribute( anAttr, "AttributeName") ) {
694 _PTR(AttributeName) aName( anAttr );
695 return QString( aName->Value().c_str() );
702 //=====================================================================================
703 // function : RemoveObjectWithChildren
704 // purpose : to be used by OnEditDelete() method
705 //=====================================================================================
706 void GEOMToolsGUI::RemoveObjectWithChildren(_PTR(SObject) obj,
708 QPtrList<SALOME_View> views,
709 GEOM_Displayer* disp)
711 // iterate through all children of obj
712 for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
713 _PTR(SObject) child (it->Value());
714 RemoveObjectWithChildren(child, aStudy, views, disp);
717 // erase object and remove it from engine
718 _PTR(GenericAttribute) anAttr;
719 if (obj->FindAttribute(anAttr, "AttributeIOR")) {
720 _PTR(AttributeIOR) anIOR (anAttr);
722 // Delete shape in Client
723 const TCollection_AsciiString ASCIor ((char*)anIOR->Value().c_str());
724 getGeometryGUI()->GetShapeReader().RemoveShapeFromBuffer(ASCIor);
726 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(obj);
727 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
728 if (!CORBA::is_nil(geomObj)) {
729 // Erase graphical object
730 SALOME_View* view = views.first();
731 for (; view; view = views.next()) {
732 disp->Erase(geomObj, true, view);
735 // Remove object from Engine
736 GeometryGUI::GetGeomGen()->RemoveObject( geomObj );
741 //=====================================================================================
742 // function : CheckSubObjectInUse
743 // purpose : to be used by OnEditDelete() method
744 //=====================================================================================
745 bool GEOMToolsGUI::CheckSubObjectInUse(_PTR(SObject) checkobj,
746 _PTR(SObject) remobj,
749 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(checkobj);
750 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
751 if( CORBA::is_nil(geomObj) )
754 GEOM::ListOfGO_var list = geomObj->GetDependency();
755 if( list->length() > 1 )
756 for(int i = 0; i < list->length(); i++ ){
757 CORBA::Object_var corbaObj_rem = GeometryGUI::ClientSObjectToObject(remobj);
758 GEOM::GEOM_Object_var geomObj_rem = GEOM::GEOM_Object::_narrow( corbaObj_rem );
759 if( list[i]->_is_equivalent( geomObj_rem ) ){
760 SalomeApp_Application* app =
761 dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
763 SUIT_MessageBox::warn1 ( app->desktop(),
764 QObject::tr("WRN_WARNING"),
765 QObject::tr("DEP_OBJECT"),
766 QObject::tr("BUT_OK") );
774 //=================================================================================
775 // function : deactivate()
776 // purpose : Called when GEOM component is deactivated
777 //=================================================================================
778 void GEOMToolsGUI::deactivate()
780 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
782 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
783 GEOM_Displayer aDisp (appStudy);
784 aDisp.GlobalSelection();
785 getGeometryGUI()->setLocalSelectionMode(GEOM_ALLOBJECTS);
789 //=====================================================================================
791 //=====================================================================================
795 GEOMGUI* GetLibGUI( GeometryGUI* parent )
797 return new GEOMToolsGUI( parent );