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"
27 #include "GEOMToolsGUI_DeleteDlg.h"
29 #include <GeometryGUI.h>
31 #include <GEOM_Operation.h>
32 #include <GEOM_Displayer.h>
34 #include <SUIT_Session.h>
35 #include <SUIT_OverrideCursor.h>
36 #include <SUIT_MessageBox.h>
37 #include <SUIT_Tools.h>
38 #include <SUIT_FileDlg.h>
39 #include <SUIT_Desktop.h>
40 #include <SUIT_ViewModel.h>
41 #include <SUIT_ViewManager.h>
43 #include <SalomeApp_Application.h>
44 #include <SalomeApp_Study.h>
45 #include <LightApp_SelectionMgr.h>
46 #include <GEOMImpl_Types.hxx>
48 #include <SALOME_ListIO.hxx>
49 #include <SALOME_ListIteratorOfListIO.hxx>
50 #include <SALOME_Prs.h>
53 #include <QApplication>
57 #include <TCollection_AsciiString.hxx>
61 typedef QMap<QString, QString> FilterMap;
63 //=======================================================================
64 // function : getFileName
65 // purpose : Selection of a file name for Import/Export. Returns also
66 // the selected file type code through <filter> argument.
67 //=======================================================================
68 static QString getFileName( QWidget* parent,
69 const QString& initial,
70 const FilterMap& filterMap,
71 const QStringList& filters,
72 const QString& caption,
76 static QString lastUsedFilter;
77 //QStringList filters;
79 for ( FilterMap::const_iterator it = filterMap.begin(); it != filterMap.end(); ++it ) {
80 //filters.push_back( it.key() );
81 if ( it.key().contains( "BREP", Qt::CaseInsensitive ) )
82 aBrepFilter = it.key();
85 SUIT_FileDlg* fd = new SUIT_FileDlg( parent, open, true, true );
86 if ( !caption.isEmpty() )
87 fd->setWindowTitle( caption );
89 if ( !initial.isEmpty() )
90 fd->selectFile( initial );
92 fd->setFilters( filters );
94 if ( !lastUsedFilter.isEmpty() && filterMap.contains( lastUsedFilter ) ) {
95 fd->selectFilter( lastUsedFilter );
97 else if ( !aBrepFilter.isEmpty() ) {
98 fd->selectFilter( aBrepFilter );
102 if ( fd->exec() == QDialog::Accepted ) {
103 filename = fd->selectedFile();
104 format = filterMap[fd->selectedFilter()];
105 lastUsedFilter = fd->selectedFilter();
109 qApp->processEvents();
113 //=======================================================================
114 // function : GEOMToolsGUI()
115 // purpose : Constructor
116 //=======================================================================
117 GEOMToolsGUI::GEOMToolsGUI( GeometryGUI* parent )
123 //=======================================================================
124 // function : ~GEOMToolsGUI()
125 // purpose : Destructor
126 //=======================================================================
127 GEOMToolsGUI::~GEOMToolsGUI()
132 //=======================================================================
133 // function : OnGUIEvent()
135 //=======================================================================
136 bool GEOMToolsGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent)
138 getGeometryGUI()->EmitSignalDeactivateDialog();
140 switch (theCommandID)
152 case 111: // IMPORT BREP
153 case 112: // IMPORT IGES
154 case 113: // IMPORT STEP
159 case 121: // EXPORT BREP
160 case 122: // EXPORT IGES
161 case 123: // EXPORT STEP
166 case 2171: // POPUP VIEWER - SELECT ONLY - VERTEX
168 OnSelectOnly( GEOM_POINT );
171 case 2172: // POPUP VIEWER - SELECT ONLY - EDGE
173 OnSelectOnly( GEOM_EDGE );
176 case 2173: // POPUP VIEWER - SELECT ONLY - WIRE
178 OnSelectOnly( GEOM_WIRE );
181 case 2174: // POPUP VIEWER - SELECT ONLY - FACE
183 OnSelectOnly( GEOM_FACE );
186 case 2175: // POPUP VIEWER - SELECT ONLY - SHELL
188 OnSelectOnly( GEOM_SHELL );
191 case 2176: // POPUP VIEWER - SELECT ONLY - SOLID
193 OnSelectOnly( GEOM_SOLID );
196 case 2177: // POPUP VIEWER - SELECT ONLY - COMPOUND
198 OnSelectOnly( GEOM_COMPOUND );
201 case 2178: // POPUP VIEWER - SELECT ONLY - SELECT ALL
203 OnSelectOnly( GEOM_ALLOBJECTS );
206 case 411: // SETTINGS - ADD IN STUDY
208 // SAN -- TO BE REMOVED !!!
211 case 412: // SETTINGS - SHADING COLOR
216 case 804: // ADD IN STUDY - POPUP VIEWER
218 // SAN -- TO BE REMOVED !!!!
226 case 5103: // CHECK GEOMETRY
231 case 8032: // COLOR - POPUP VIEWER
236 case 8033: // TRANSPARENCY - POPUP VIEWER
241 case 8034: // ISOS - POPUP VIEWER
246 case 8035: // AUTO COLOR - POPUP VIEWER
251 case 8036: // DISABLE AUTO COLOR - POPUP VIEWER
253 OnDisableAutoColor();
256 case 9024 : // OPEN - OBJBROSER POPUP
263 SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
272 //===============================================================================
273 // function : OnEditDelete()
275 //===============================================================================
276 void GEOMToolsGUI::OnEditDelete()
278 SALOME_ListIO selected;
279 SalomeApp_Application* app =
280 dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
282 LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
283 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
284 if ( aSelMgr && appStudy ) {
285 aSelMgr->selectedObjects( selected, QString::null, false );
286 if ( !selected.IsEmpty() ) {
287 _PTR(Study) aStudy = appStudy->studyDS();
289 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
291 SUIT_MessageBox::warning ( app->desktop(),
292 QObject::tr("WRN_WARNING"),
293 QObject::tr("WRN_STUDY_LOCKED"),
294 QObject::tr("BUT_OK") );
298 // VSR 17/11/04: check if all objects selected belong to GEOM component --> start
299 // modifications of ASV 01.06.05
300 QString parentComp = getParentComponent( aStudy, selected );
301 CORBA::String_var geomIOR = app->orb()->object_to_string( GeometryGUI::GetGeomGen() );
302 QString geomComp = getParentComponent( aStudy->FindObjectIOR( geomIOR.in() ) );
304 if ( parentComp != geomComp ) {
305 SUIT_MessageBox::warning ( app->desktop(),
306 QObject::tr("ERR_ERROR"),
307 QObject::tr("NON_GEOM_OBJECTS_SELECTED").arg( getGeometryGUI()->moduleName() ),
308 QObject::tr("BUT_OK") );
311 // VSR 17/11/04: check if all objects selected belong to GEOM component <-- finish
313 QStringList aNameList;
314 for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
315 Handle(SALOME_InteractiveObject) anIObject = It.Value();
316 QString aName = anIObject->getName();
317 if ( aName != "" && aName[ 0 ] != '*' ) {
318 aNameList.append( aName );
320 _PTR(SObject) obj ( aStudy->FindObjectID( anIObject->getEntry() ) );
321 _PTR(ChildIterator) it ( aStudy->NewChildIterator( obj ) );
322 for ( it->InitEx( true ); it->More(); it->Next() ) {
323 _PTR(SObject) child( it->Value() );
324 QString aName = child->GetName().c_str();
325 if ( aName != "" && aName[ 0 ] != '*' ) {
326 aNameList.append( aName );
332 GEOMToolsGUI_DeleteDlg dlg( app->desktop(), aNameList );
336 // QAD_Operation* op = new SALOMEGUI_ImportOperation(.....);
339 // prepare list of SALOME_Views
340 QList<SALOME_View*> views;
343 ViewManagerList vmans = app->viewManagers();
344 SUIT_ViewManager* vman;
345 QListIterator<SUIT_ViewManager*> it( vmans );
346 while ( it.hasNext() && (vman = it.next()) ) {
347 SUIT_ViewModel* vmod = vman->getViewModel();
348 view = dynamic_cast<SALOME_View*> ( vmod ); // must work for OCC and VTK views
350 views.append( view );
353 _PTR(StudyBuilder) aStudyBuilder (aStudy->NewBuilder());
354 _PTR(GenericAttribute) anAttr;
355 GEOM_Displayer* disp = new GEOM_Displayer( appStudy );
357 _PTR(SComponent) aGeom ( aStudy->FindComponent("GEOM") );
361 // MAIN LOOP OF SELECTED OBJECTS
362 for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
364 Handle(SALOME_InteractiveObject) io = It.Value();
365 if ( !io->hasEntry() )
368 _PTR(SObject) obj ( aStudy->FindObjectID( io->getEntry() ) );
370 // disable removal of "Geometry" component object
371 if ( !strcmp( obj->GetIOR().c_str(), geomIOR ) )
374 //If the object has been used to create another one,then it can't be deleted
375 _PTR(ChildIterator) it (aStudy->NewChildIterator(aGeom));
376 for ( it->InitEx( true ); it->More(); it->Next() ) {
377 _PTR(SObject) chobj (it->Value());
378 if(CheckSubObjectInUse(chobj, obj, aStudy)) return;
380 for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
381 _PTR(SObject) child (it->Value());
382 if(CheckSubObjectInUse( chobj, child, aStudy)) return;
386 RemoveObjectWithChildren(obj, aStudy, views, disp);
388 // Remove objects from Study
389 aStudyBuilder->RemoveObjectWithChildren( obj );
392 } // MAIN LOOP of selected
395 aSelMgr->setSelectedObjects( selected );
396 getGeometryGUI()->updateObjBrowser();
397 } // if ( selected not empty )
398 } // if ( selMgr && appStudy )
400 app->updateActions(); //SRN: To update a Save button in the toolbar
412 //==============================================================================
413 // function : OnEditCopy()
415 //==============================================================================
416 void GEOMToolsGUI::OnEditCopy()
419 SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection() );
420 GEOM::string_array_var listIOR = new GEOM::string_array;
422 const SALOME_ListIO& List = Sel->StoredIObjects();
424 myGeomBase->ConvertListOfIOInListOfIOR(List, listIOR);
426 Sel->ClearIObjects();
428 SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
429 int aStudyID = aStudy->StudyId();
431 for (unsigned int ind = 0; ind < listIOR->length();ind++) {
432 GEOM::GEOM_Object_var aShapeInit = myGeom->GetIORFromString(listIOR[ind]);
434 GEOM::GEOM_IInsertOperations_var IOp = myGeom->GetIInsertOperations(aStudyID);
435 GEOM::GEOM_Object_var result = IOp->MakeCopy(aShapeInit);
436 myGeomBase->Display(result);
438 catch (const SALOME::SALOME_Exception& S_ex) {
439 QtCatchCorbaException(S_ex);
443 QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_READY"));
448 //=====================================================================================
450 // purpose : BRep, Iges, Step
451 //=====================================================================================
452 bool GEOMToolsGUI::Import()
454 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( getGeometryGUI()->getApp() );
455 //SUIT_Application* app = getGeometryGUI()->getApp();
456 if (! app) return false;
458 SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
460 cout << "FAILED to cast active study to SalomeApp_Study" << endl;
463 _PTR(Study) aStudy = stud->studyDS();
465 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
467 SUIT_MessageBox::warning ( app->desktop(),
468 QObject::tr("WRN_WARNING"),
469 QObject::tr("WRN_STUDY_LOCKED"),
470 QObject::tr("BUT_OK") );
474 GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
475 if ( CORBA::is_nil( eng ) ) {
476 SUIT_MessageBox::critical( app->desktop(),
477 QObject::tr("WRN_WARNING"),
478 QObject::tr( "GEOM Engine is not started" ),
479 QObject::tr("BUT_OK") );
483 GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
484 if ( aInsOp->_is_nil() )
487 GEOM::GEOM_Object_var anObj;
489 // Obtain a list of available import formats
492 GEOM::string_array_var aFormats, aPatterns;
493 aInsOp->ImportTranslators( aFormats, aPatterns );
495 for ( int i = 0, n = aFormats->length(); i < n; i++ ) {
496 aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
497 filters.push_back( (char*)aPatterns[i] );
502 QString fileName = getFileName(app->desktop(), "", aMap, filters,
503 tr("GEOM_MEN_IMPORT"), true, fileType);
505 if (fileName.isEmpty())
508 if (fileType.isEmpty() )
510 // Trying to detect file type
511 QFileInfo aFileInfo( fileName );
512 QString aPossibleType = (aFileInfo.suffix()).toUpper() ;
514 if ( (aMap.values()).contains(aPossibleType) )
515 fileType = aPossibleType;
518 if (fileType.isEmpty())
521 GEOM_Operation* anOp = new GEOM_Operation (app, aInsOp.in());
523 SUIT_OverrideCursor wc;
525 app->putInfo(tr("GEOM_PRP_LOADING").arg(SUIT_Tools::file(fileName, /*withExten=*/true)));
529 CORBA::String_var fileN = CORBA::string_dup(fileName.toLatin1().data());
530 CORBA::String_var fileT = CORBA::string_dup(fileType.toLatin1().data());
531 anObj = aInsOp->Import(fileN, fileT);
533 if ( !anObj->_is_nil() && aInsOp->IsDone() ) {
534 QString aPublishObjName =
535 GEOMBase::GetDefaultName(SUIT_Tools::file(fileName, /*withExten=*/true));
537 SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy(aStudy);
538 GeometryGUI::GetGeomGen()->PublishInStudy(aDSStudy,
539 SALOMEDS::SObject::_nil(),
541 aPublishObjName.toStdString().c_str());
543 GEOM_Displayer( stud ).Display( anObj.in() );
545 // update data model and object browser
546 getGeometryGUI()->updateObjBrowser( true );
553 SUIT_MessageBox::critical( app->desktop(),
554 QObject::tr( "GEOM_ERROR" ),
555 QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
556 QObject::tr("BUT_OK") );
559 catch( const SALOME::SALOME_Exception& S_ex ) {
560 //QtCatchCorbaException(S_ex);
565 app->updateActions(); //SRN: To update a Save button in the toolbar
571 //=====================================================================================
573 // purpose : BRep, Iges, Step
574 //=====================================================================================
575 bool GEOMToolsGUI::Export()
577 SalomeApp_Application* app = getGeometryGUI()->getApp();
578 if (!app) return false;
580 SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
582 cout << "FAILED to cast active study to SalomeApp_Study" << endl;
585 _PTR(Study) aStudy = stud->studyDS();
587 GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
588 if ( CORBA::is_nil( eng ) ) {
589 SUIT_MessageBox::critical( app->desktop(),
590 QObject::tr("WRN_WARNING"),
591 QObject::tr( "GEOM Engine is not started" ),
592 QObject::tr("BUT_OK") );
596 GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
597 if ( aInsOp->_is_nil() )
600 // Obtain a list of available export formats
603 GEOM::string_array_var aFormats, aPatterns;
604 aInsOp->ExportTranslators( aFormats, aPatterns );
605 for ( int i = 0, n = aFormats->length(); i < n; i++ ) {
606 aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
607 filters.push_back( (char*)aPatterns[i] );
610 // Get selected objects
611 LightApp_SelectionMgr* sm = app->selectionMgr();
615 SALOME_ListIO selectedObjects;
616 sm->selectedObjects( selectedObjects );
618 SALOME_ListIteratorOfListIO It( selectedObjects );
619 for(;It.More();It.Next()) {
620 Handle(SALOME_InteractiveObject) IObject = It.Value();
621 Standard_Boolean found;
622 GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject(IObject, found);
624 if ( !found || anObj->_is_nil() )
628 QString file = getFileName(app->desktop(), QString( IObject->getName() ), aMap, filters,
629 tr("GEOM_MEN_EXPORT"), false, fileType);
631 // User has pressed "Cancel" --> stop the operation
632 if ( file.isEmpty() || fileType.isEmpty() )
635 GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
637 SUIT_OverrideCursor wc;
639 app->putInfo( tr("GEOM_PRP_EXPORT").arg(SUIT_Tools::file( file, /*withExten=*/true )) );
644 aInsOp->Export( anObj, file.toStdString().c_str(), fileType.toLatin1().constData() );
646 if ( aInsOp->IsDone() )
652 SUIT_MessageBox::critical( app->desktop(),
653 QObject::tr( "GEOM_ERROR" ),
654 QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
655 QObject::tr("BUT_OK") );
659 catch (const SALOME::SALOME_Exception& S_ex) {
660 //QtCatchCorbaException(S_ex);
670 QString GEOMToolsGUI::getParentComponent( _PTR( Study ) study, const SALOME_ListIO& iobjs )
674 for ( SALOME_ListIteratorOfListIO it( iobjs ); it.More(); it.Next() ) {
676 Handle(SALOME_InteractiveObject) io = it.Value();
677 if ( !io->hasEntry() )
680 QString compName = getParentComponent( study->FindObjectID( io->getEntry() ) );
682 if ( parentComp.isNull() )
683 parentComp = compName;
684 else if ( parentComp.compare( compName) != 0 ) { // objects belonging to different components are selected
685 parentComp = QString::null;
693 QString GEOMToolsGUI::getParentComponent( _PTR( SObject ) obj )
696 _PTR(SComponent) comp = obj->GetFatherComponent();
698 _PTR(GenericAttribute) anAttr;
699 if ( comp->FindAttribute( anAttr, "AttributeName") ) {
700 _PTR(AttributeName) aName( anAttr );
701 return QString( aName->Value().c_str() );
708 //=====================================================================================
709 // function : RemoveObjectWithChildren
710 // purpose : to be used by OnEditDelete() method
711 //=====================================================================================
712 void GEOMToolsGUI::RemoveObjectWithChildren(_PTR(SObject) obj,
714 QList<SALOME_View*> views,
715 GEOM_Displayer* disp)
717 // iterate through all children of obj
718 for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
719 _PTR(SObject) child (it->Value());
720 RemoveObjectWithChildren(child, aStudy, views, disp);
723 // erase object and remove it from engine
724 _PTR(GenericAttribute) anAttr;
725 if (obj->FindAttribute(anAttr, "AttributeIOR")) {
726 _PTR(AttributeIOR) anIOR (anAttr);
728 // Delete shape in Client
729 const TCollection_AsciiString ASCIor ((char*)anIOR->Value().c_str());
730 getGeometryGUI()->GetShapeReader().RemoveShapeFromBuffer(ASCIor);
732 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(obj);
733 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
734 if (!CORBA::is_nil(geomObj)) {
735 // Erase graphical object
736 QListIterator<SALOME_View*> it( views );
737 while ( it.hasNext() )
738 if ( SALOME_View* view = it.next() )
739 disp->Erase(geomObj, true, view);
741 // Remove object from Engine
742 GeometryGUI::GetGeomGen()->RemoveObject( geomObj );
747 //=====================================================================================
748 // function : CheckSubObjectInUse
749 // purpose : to be used by OnEditDelete() method
750 //=====================================================================================
751 bool GEOMToolsGUI::CheckSubObjectInUse(_PTR(SObject) checkobj,
752 _PTR(SObject) remobj,
755 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(checkobj);
756 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
757 if( CORBA::is_nil(geomObj) )
760 GEOM::ListOfGO_var list = geomObj->GetDependency();
761 if( list->length() > 1 )
762 for(int i = 0; i < list->length(); i++ ){
763 CORBA::Object_var corbaObj_rem = GeometryGUI::ClientSObjectToObject(remobj);
764 GEOM::GEOM_Object_var geomObj_rem = GEOM::GEOM_Object::_narrow( corbaObj_rem );
765 if( list[i]->_is_equivalent( geomObj_rem ) ){
766 SalomeApp_Application* app =
767 dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
769 SUIT_MessageBox::warning ( app->desktop(),
770 QObject::tr("WRN_WARNING"),
771 QObject::tr("DEP_OBJECT"),
772 QObject::tr("BUT_OK") );
780 //=================================================================================
781 // function : deactivate()
782 // purpose : Called when GEOM component is deactivated
783 //=================================================================================
784 void GEOMToolsGUI::deactivate()
786 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
788 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
789 GEOM_Displayer aDisp (appStudy);
790 aDisp.GlobalSelection();
791 getGeometryGUI()->setLocalSelectionMode(GEOM_ALLOBJECTS);
795 //=====================================================================================
797 //=====================================================================================
801 __declspec( dllexport )
803 GEOMGUI* GetLibGUI( GeometryGUI* parent )
805 return new GEOMToolsGUI( parent );