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 );
101 fd->setFilters( filters );
104 QString filename = fd->selectedFile();
105 format = filterMap[fd->selectedFilter()];
106 lastUsedFilter = fd->selectedFilter();
108 qApp->processEvents();
112 //=======================================================================
113 // function : GEOMToolsGUI()
114 // purpose : Constructor
115 //=======================================================================
116 GEOMToolsGUI::GEOMToolsGUI( GeometryGUI* parent )
122 //=======================================================================
123 // function : ~GEOMToolsGUI()
124 // purpose : Destructor
125 //=======================================================================
126 GEOMToolsGUI::~GEOMToolsGUI()
131 //=======================================================================
132 // function : OnGUIEvent()
134 //=======================================================================
135 bool GEOMToolsGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent)
137 getGeometryGUI()->EmitSignalDeactivateDialog();
139 switch (theCommandID)
151 case 111: // IMPORT BREP
152 case 112: // IMPORT IGES
153 case 113: // IMPORT STEP
158 case 121: // EXPORT BREP
159 case 122: // EXPORT IGES
160 case 123: // EXPORT STEP
165 case 2171: // POPUP VIEWER - SELECT ONLY - VERTEX
167 OnSelectOnly( GEOM_POINT );
170 case 2172: // POPUP VIEWER - SELECT ONLY - EDGE
172 OnSelectOnly( GEOM_EDGE );
175 case 2173: // POPUP VIEWER - SELECT ONLY - WIRE
177 OnSelectOnly( GEOM_WIRE );
180 case 2174: // POPUP VIEWER - SELECT ONLY - FACE
182 OnSelectOnly( GEOM_FACE );
185 case 2175: // POPUP VIEWER - SELECT ONLY - SHELL
187 OnSelectOnly( GEOM_SHELL );
190 case 2176: // POPUP VIEWER - SELECT ONLY - SOLID
192 OnSelectOnly( GEOM_SOLID );
195 case 2177: // POPUP VIEWER - SELECT ONLY - COMPOUND
197 OnSelectOnly( GEOM_COMPOUND );
200 case 2178: // POPUP VIEWER - SELECT ONLY - SELECT ALL
202 OnSelectOnly( GEOM_ALLOBJECTS );
205 case 411: // SETTINGS - ADD IN STUDY
207 // SAN -- TO BE REMOVED !!!
210 case 412: // SETTINGS - SHADING COLOR
215 case 804: // ADD IN STUDY - POPUP VIEWER
217 // SAN -- TO BE REMOVED !!!!
225 case 5103: // CHECK GEOMETRY
230 case 8032: // COLOR - POPUP VIEWER
235 case 8033: // TRANSPARENCY - POPUP VIEWER
240 case 8034: // ISOS - POPUP VIEWER
245 case 8035: // AUTO COLOR - POPUP VIEWER
250 case 8036: // DISABLE AUTO COLOR - POPUP VIEWER
252 OnDisableAutoColor();
255 case 9024 : // OPEN - OBJBROSER POPUP
262 SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
271 //===============================================================================
272 // function : OnEditDelete()
274 //===============================================================================
275 void GEOMToolsGUI::OnEditDelete()
277 SALOME_ListIO selected;
278 SalomeApp_Application* app =
279 dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
281 LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
282 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
283 if ( aSelMgr && appStudy ) {
284 aSelMgr->selectedObjects( selected, QString::null, false );
285 if ( !selected.IsEmpty() ) {
286 _PTR(Study) aStudy = appStudy->studyDS();
288 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
290 SUIT_MessageBox::warning ( app->desktop(),
291 QObject::tr("WRN_WARNING"),
292 QObject::tr("WRN_STUDY_LOCKED"),
293 QObject::tr("BUT_OK") );
297 // VSR 17/11/04: check if all objects selected belong to GEOM component --> start
298 // modifications of ASV 01.06.05
299 QString parentComp = getParentComponent( aStudy, selected );
300 CORBA::String_var geomIOR = app->orb()->object_to_string( GeometryGUI::GetGeomGen() );
301 QString geomComp = getParentComponent( aStudy->FindObjectIOR( geomIOR.in() ) );
303 if ( parentComp != geomComp ) {
304 SUIT_MessageBox::warning ( app->desktop(),
305 QObject::tr("ERR_ERROR"),
306 QObject::tr("NON_GEOM_OBJECTS_SELECTED").arg( getGeometryGUI()->moduleName() ),
307 QObject::tr("BUT_OK") );
310 // VSR 17/11/04: check if all objects selected belong to GEOM component <-- finish
312 QStringList aNameList;
313 for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
314 Handle(SALOME_InteractiveObject) anIObject = It.Value();
315 QString aName = anIObject->getName();
316 if ( aName != "" && aName[ 0 ] != '*' ) {
317 aNameList.append( aName );
319 _PTR(SObject) obj ( aStudy->FindObjectID( anIObject->getEntry() ) );
320 _PTR(ChildIterator) it ( aStudy->NewChildIterator( obj ) );
321 for ( it->InitEx( true ); it->More(); it->Next() ) {
322 _PTR(SObject) child( it->Value() );
323 QString aName = child->GetName().c_str();
324 if ( aName != "" && aName[ 0 ] != '*' ) {
325 aNameList.append( aName );
331 GEOMToolsGUI_DeleteDlg dlg( app->desktop(), aNameList );
335 // QAD_Operation* op = new SALOMEGUI_ImportOperation(.....);
338 // prepare list of SALOME_Views
339 QList<SALOME_View*> views;
342 ViewManagerList vmans = app->viewManagers();
343 SUIT_ViewManager* vman;
344 QListIterator<SUIT_ViewManager*> it( vmans );
345 while ( it.hasNext() && (vman = it.next()) ) {
346 SUIT_ViewModel* vmod = vman->getViewModel();
347 view = dynamic_cast<SALOME_View*> ( vmod ); // must work for OCC and VTK views
349 views.append( view );
352 _PTR(StudyBuilder) aStudyBuilder (aStudy->NewBuilder());
353 _PTR(GenericAttribute) anAttr;
354 GEOM_Displayer* disp = new GEOM_Displayer( appStudy );
356 _PTR(SComponent) aGeom ( aStudy->FindComponent("GEOM") );
360 // MAIN LOOP OF SELECTED OBJECTS
361 for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
363 Handle(SALOME_InteractiveObject) io = It.Value();
364 if ( !io->hasEntry() )
367 _PTR(SObject) obj ( aStudy->FindObjectID( io->getEntry() ) );
369 // disable removal of "Geometry" component object
370 if ( !strcmp( obj->GetIOR().c_str(), geomIOR ) )
373 //If the object has been used to create another one,then it can't be deleted
374 _PTR(ChildIterator) it (aStudy->NewChildIterator(aGeom));
375 for ( it->InitEx( true ); it->More(); it->Next() ) {
376 _PTR(SObject) chobj (it->Value());
377 if(CheckSubObjectInUse(chobj, obj, aStudy)) return;
379 for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
380 _PTR(SObject) child (it->Value());
381 if(CheckSubObjectInUse( chobj, child, aStudy)) return;
385 RemoveObjectWithChildren(obj, aStudy, views, disp);
387 // Remove objects from Study
388 aStudyBuilder->RemoveObjectWithChildren( obj );
391 } // MAIN LOOP of selected
394 aSelMgr->setSelectedObjects( selected );
395 getGeometryGUI()->updateObjBrowser();
396 } // if ( selected not empty )
397 } // if ( selMgr && appStudy )
399 app->updateActions(); //SRN: To update a Save button in the toolbar
411 //==============================================================================
412 // function : OnEditCopy()
414 //==============================================================================
415 void GEOMToolsGUI::OnEditCopy()
418 SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection() );
419 GEOM::string_array_var listIOR = new GEOM::string_array;
421 const SALOME_ListIO& List = Sel->StoredIObjects();
423 myGeomBase->ConvertListOfIOInListOfIOR(List, listIOR);
425 Sel->ClearIObjects();
427 SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
428 int aStudyID = aStudy->StudyId();
430 for (unsigned int ind = 0; ind < listIOR->length();ind++) {
431 GEOM::GEOM_Object_var aShapeInit = myGeom->GetIORFromString(listIOR[ind]);
433 GEOM::GEOM_IInsertOperations_var IOp = myGeom->GetIInsertOperations(aStudyID);
434 GEOM::GEOM_Object_var result = IOp->MakeCopy(aShapeInit);
435 myGeomBase->Display(result);
437 catch (const SALOME::SALOME_Exception& S_ex) {
438 QtCatchCorbaException(S_ex);
442 QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_READY"));
447 //=====================================================================================
449 // purpose : BRep, Iges, Step
450 //=====================================================================================
451 bool GEOMToolsGUI::Import()
453 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( getGeometryGUI()->getApp() );
454 //SUIT_Application* app = getGeometryGUI()->getApp();
455 if (! app) return false;
457 SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
459 cout << "FAILED to cast active study to SalomeApp_Study" << endl;
462 _PTR(Study) aStudy = stud->studyDS();
464 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
466 SUIT_MessageBox::warning ( app->desktop(),
467 QObject::tr("WRN_WARNING"),
468 QObject::tr("WRN_STUDY_LOCKED"),
469 QObject::tr("BUT_OK") );
473 GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
474 if ( CORBA::is_nil( eng ) ) {
475 SUIT_MessageBox::critical( app->desktop(),
476 QObject::tr("WRN_WARNING"),
477 QObject::tr( "GEOM Engine is not started" ),
478 QObject::tr("BUT_OK") );
482 GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
483 if ( aInsOp->_is_nil() )
486 GEOM::GEOM_Object_var anObj;
488 // Obtain a list of available import formats
491 GEOM::string_array_var aFormats, aPatterns;
492 aInsOp->ImportTranslators( aFormats, aPatterns );
494 for ( int i = 0, n = aFormats->length(); i < n; i++ ) {
495 aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
496 filters.push_back( (char*)aPatterns[i] );
501 QString fileName = getFileName(app->desktop(), "", aMap, filters,
502 tr("GEOM_MEN_IMPORT"), true, fileType);
504 if (fileType.isEmpty() )
506 // Trying to detect file type
507 QFileInfo aFileInfo( fileName );
508 QString aPossibleType = (aFileInfo.suffix()).toUpper() ;
510 if ( (aMap.values()).contains(aPossibleType) )
511 fileType = aPossibleType;
514 if (fileName.isEmpty() || fileType.isEmpty())
517 GEOM_Operation* anOp = new GEOM_Operation (app, aInsOp.in());
519 SUIT_OverrideCursor wc;
521 app->putInfo(tr("GEOM_PRP_LOADING").arg(SUIT_Tools::file(fileName, /*withExten=*/true)));
525 CORBA::String_var fileN = fileName.toLatin1().data();
526 CORBA::String_var fileT = fileType.toLatin1().data();
527 anObj = aInsOp->Import(fileN, fileT);
529 if ( !anObj->_is_nil() && aInsOp->IsDone() ) {
530 QString aPublishObjName =
531 GEOMBase::GetDefaultName(SUIT_Tools::file(fileName, /*withExten=*/true));
533 SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy(aStudy);
534 GeometryGUI::GetGeomGen()->PublishInStudy(aDSStudy,
535 SALOMEDS::SObject::_nil(),
537 aPublishObjName.toStdString().c_str());
539 GEOM_Displayer( stud ).Display( anObj.in() );
541 // update data model and object browser
542 getGeometryGUI()->updateObjBrowser( true );
549 SUIT_MessageBox::critical( app->desktop(),
550 QObject::tr( "GEOM_ERROR" ),
551 QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
552 QObject::tr("BUT_OK") );
555 catch( const SALOME::SALOME_Exception& S_ex ) {
556 //QtCatchCorbaException(S_ex);
561 app->updateActions(); //SRN: To update a Save button in the toolbar
567 //=====================================================================================
569 // purpose : BRep, Iges, Step
570 //=====================================================================================
571 bool GEOMToolsGUI::Export()
573 SalomeApp_Application* app = getGeometryGUI()->getApp();
574 if (!app) return false;
576 SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
578 cout << "FAILED to cast active study to SalomeApp_Study" << endl;
581 _PTR(Study) aStudy = stud->studyDS();
583 GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
584 if ( CORBA::is_nil( eng ) ) {
585 SUIT_MessageBox::critical( app->desktop(),
586 QObject::tr("WRN_WARNING"),
587 QObject::tr( "GEOM Engine is not started" ),
588 QObject::tr("BUT_OK") );
592 GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
593 if ( aInsOp->_is_nil() )
596 // Obtain a list of available export formats
599 GEOM::string_array_var aFormats, aPatterns;
600 aInsOp->ExportTranslators( aFormats, aPatterns );
601 for ( int i = 0, n = aFormats->length(); i < n; i++ ) {
602 aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
603 filters.push_back( (char*)aPatterns[i] );
606 // Get selected objects
607 LightApp_SelectionMgr* sm = app->selectionMgr();
611 SALOME_ListIO selectedObjects;
612 sm->selectedObjects( selectedObjects );
614 SALOME_ListIteratorOfListIO It( selectedObjects );
615 for(;It.More();It.Next()) {
616 Handle(SALOME_InteractiveObject) IObject = It.Value();
617 Standard_Boolean found;
618 GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject(IObject, found);
620 if ( !found || anObj->_is_nil() )
624 QString file = getFileName(app->desktop(), QString( IObject->getName() ), aMap, filters,
625 tr("GEOM_MEN_EXPORT"), false, fileType);
627 // User has pressed "Cancel" --> stop the operation
628 if ( file.isEmpty() || fileType.isEmpty() )
631 GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
633 SUIT_OverrideCursor wc;
635 app->putInfo( tr("GEOM_PRP_EXPORT").arg(SUIT_Tools::file( file, /*withExten=*/true )) );
640 aInsOp->Export( anObj, file.toStdString().c_str(), fileType.toLatin1().constData() );
642 if ( aInsOp->IsDone() )
648 SUIT_MessageBox::critical( app->desktop(),
649 QObject::tr( "GEOM_ERROR" ),
650 QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
651 QObject::tr("BUT_OK") );
655 catch (const SALOME::SALOME_Exception& S_ex) {
656 //QtCatchCorbaException(S_ex);
666 QString GEOMToolsGUI::getParentComponent( _PTR( Study ) study, const SALOME_ListIO& iobjs )
670 for ( SALOME_ListIteratorOfListIO it( iobjs ); it.More(); it.Next() ) {
672 Handle(SALOME_InteractiveObject) io = it.Value();
673 if ( !io->hasEntry() )
676 QString compName = getParentComponent( study->FindObjectID( io->getEntry() ) );
678 if ( parentComp.isNull() )
679 parentComp = compName;
680 else if ( parentComp.compare( compName) != 0 ) { // objects belonging to different components are selected
681 parentComp = QString::null;
689 QString GEOMToolsGUI::getParentComponent( _PTR( SObject ) obj )
692 _PTR(SComponent) comp = obj->GetFatherComponent();
694 _PTR(GenericAttribute) anAttr;
695 if ( comp->FindAttribute( anAttr, "AttributeName") ) {
696 _PTR(AttributeName) aName( anAttr );
697 return QString( aName->Value().c_str() );
704 //=====================================================================================
705 // function : RemoveObjectWithChildren
706 // purpose : to be used by OnEditDelete() method
707 //=====================================================================================
708 void GEOMToolsGUI::RemoveObjectWithChildren(_PTR(SObject) obj,
710 QList<SALOME_View*> views,
711 GEOM_Displayer* disp)
713 // iterate through all children of obj
714 for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
715 _PTR(SObject) child (it->Value());
716 RemoveObjectWithChildren(child, aStudy, views, disp);
719 // erase object and remove it from engine
720 _PTR(GenericAttribute) anAttr;
721 if (obj->FindAttribute(anAttr, "AttributeIOR")) {
722 _PTR(AttributeIOR) anIOR (anAttr);
724 // Delete shape in Client
725 const TCollection_AsciiString ASCIor ((char*)anIOR->Value().c_str());
726 getGeometryGUI()->GetShapeReader().RemoveShapeFromBuffer(ASCIor);
728 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(obj);
729 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
730 if (!CORBA::is_nil(geomObj)) {
731 // Erase graphical object
732 QListIterator<SALOME_View*> it( views );
733 while ( it.hasNext() )
734 if ( SALOME_View* view = it.next() )
735 disp->Erase(geomObj, true, view);
737 // Remove object from Engine
738 GeometryGUI::GetGeomGen()->RemoveObject( geomObj );
743 //=====================================================================================
744 // function : CheckSubObjectInUse
745 // purpose : to be used by OnEditDelete() method
746 //=====================================================================================
747 bool GEOMToolsGUI::CheckSubObjectInUse(_PTR(SObject) checkobj,
748 _PTR(SObject) remobj,
751 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(checkobj);
752 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
753 if( CORBA::is_nil(geomObj) )
756 GEOM::ListOfGO_var list = geomObj->GetDependency();
757 if( list->length() > 1 )
758 for(int i = 0; i < list->length(); i++ ){
759 CORBA::Object_var corbaObj_rem = GeometryGUI::ClientSObjectToObject(remobj);
760 GEOM::GEOM_Object_var geomObj_rem = GEOM::GEOM_Object::_narrow( corbaObj_rem );
761 if( list[i]->_is_equivalent( geomObj_rem ) ){
762 SalomeApp_Application* app =
763 dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
765 SUIT_MessageBox::warning ( app->desktop(),
766 QObject::tr("WRN_WARNING"),
767 QObject::tr("DEP_OBJECT"),
768 QObject::tr("BUT_OK") );
776 //=================================================================================
777 // function : deactivate()
778 // purpose : Called when GEOM component is deactivated
779 //=================================================================================
780 void GEOMToolsGUI::deactivate()
782 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
784 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
785 GEOM_Displayer aDisp (appStudy);
786 aDisp.GlobalSelection();
787 getGeometryGUI()->setLocalSelectionMode(GEOM_ALLOBJECTS);
791 //=====================================================================================
793 //=====================================================================================
797 __declspec( dllexport )
799 GEOMGUI* GetLibGUI( GeometryGUI* parent )
801 return new GEOMToolsGUI( parent );