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 804: // ADD IN STUDY - POPUP VIEWER
205 // SAN -- TO BE REMOVED !!!!
213 case 5103: // CHECK GEOMETRY
218 case 8032: // COLOR - POPUP VIEWER
223 case 8033: // TRANSPARENCY - POPUP VIEWER
228 case 8034: // ISOS - POPUP VIEWER
233 case 9024 : // OPEN - OBJBROSER POPUP
240 SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
249 //===============================================================================
250 // function : OnEditDelete()
252 //===============================================================================
253 void GEOMToolsGUI::OnEditDelete()
255 SALOME_ListIO selected;
256 SalomeApp_Application* app =
257 dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
259 LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
260 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
261 if ( aSelMgr && appStudy ) {
262 aSelMgr->selectedObjects( selected, QString::null, false );
263 if ( !selected.IsEmpty() ) {
264 _PTR(Study) aStudy = appStudy->studyDS();
266 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
268 SUIT_MessageBox::warning ( app->desktop(),
269 QObject::tr("WRN_WARNING"),
270 QObject::tr("WRN_STUDY_LOCKED"),
271 QObject::tr("BUT_OK") );
275 // VSR 17/11/04: check if all objects selected belong to GEOM component --> start
276 // modifications of ASV 01.06.05
277 QString parentComp = getParentComponent( aStudy, selected );
278 CORBA::String_var geomIOR = app->orb()->object_to_string( GeometryGUI::GetGeomGen() );
279 QString geomComp = getParentComponent( aStudy->FindObjectIOR( geomIOR.in() ) );
281 if ( parentComp != geomComp ) {
282 SUIT_MessageBox::warning ( app->desktop(),
283 QObject::tr("ERR_ERROR"),
284 QObject::tr("NON_GEOM_OBJECTS_SELECTED").arg( getGeometryGUI()->moduleName() ),
285 QObject::tr("BUT_OK") );
288 // VSR 17/11/04: check if all objects selected belong to GEOM component <-- finish
290 if ( SUIT_MessageBox::warning( app->desktop(),
291 QObject::tr( "GEOM_WRN_WARNING" ),
292 QObject::tr( "GEOM_REALLY_DELETE" ),
293 QObject::tr( "GEOM_BUT_YES" ),
294 QObject::tr( "GEOM_BUT_NO" ), 1 ) != 0 )
297 // QAD_Operation* op = new SALOMEGUI_ImportOperation(.....);
300 // prepare list of SALOME_Views
301 QList<SALOME_View*> views;
304 ViewManagerList vmans = app->viewManagers();
305 SUIT_ViewManager* vman;
306 QListIterator<SUIT_ViewManager*> it( vmans );
307 while ( it.hasNext() && (vman = it.next()) ) {
308 SUIT_ViewModel* vmod = vman->getViewModel();
309 view = dynamic_cast<SALOME_View*> ( vmod ); // must work for OCC and VTK views
311 views.append( view );
314 _PTR(StudyBuilder) aStudyBuilder (aStudy->NewBuilder());
315 _PTR(GenericAttribute) anAttr;
316 GEOM_Displayer* disp = new GEOM_Displayer( appStudy );
318 _PTR(SComponent) aGeom ( aStudy->FindComponent("GEOM") );
322 // MAIN LOOP OF SELECTED OBJECTS
323 for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
325 Handle(SALOME_InteractiveObject) io = It.Value();
326 if ( !io->hasEntry() )
329 _PTR(SObject) obj ( aStudy->FindObjectID( io->getEntry() ) );
331 // disable removal of "Geometry" component object
332 if ( !strcmp( obj->GetIOR().c_str(), geomIOR ) )
335 //If the object has been used to create another one,then it can't be deleted
336 _PTR(ChildIterator) it (aStudy->NewChildIterator(aGeom));
337 for ( it->InitEx( true ); it->More(); it->Next() ) {
338 _PTR(SObject) chobj (it->Value());
339 if(CheckSubObjectInUse(chobj, obj, aStudy)) return;
341 for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
342 _PTR(SObject) child (it->Value());
343 if(CheckSubObjectInUse( chobj, child, aStudy)) return;
347 RemoveObjectWithChildren(obj, aStudy, views, disp);
349 // Remove objects from Study
350 aStudyBuilder->RemoveObject( obj );
353 } // MAIN LOOP of selected
356 aSelMgr->setSelectedObjects( selected );
357 getGeometryGUI()->updateObjBrowser();
358 } // if ( selected not empty )
359 } // if ( selMgr && appStudy )
361 app->updateActions(); //SRN: To update a Save button in the toolbar
373 //==============================================================================
374 // function : OnEditCopy()
376 //==============================================================================
377 void GEOMToolsGUI::OnEditCopy()
380 SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection() );
381 GEOM::string_array_var listIOR = new GEOM::string_array;
383 const SALOME_ListIO& List = Sel->StoredIObjects();
385 myGeomBase->ConvertListOfIOInListOfIOR(List, listIOR);
387 Sel->ClearIObjects();
389 SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
390 int aStudyID = aStudy->StudyId();
392 for (unsigned int ind = 0; ind < listIOR->length();ind++) {
393 GEOM::GEOM_Object_var aShapeInit = myGeom->GetIORFromString(listIOR[ind]);
395 GEOM::GEOM_IInsertOperations_var IOp = myGeom->GetIInsertOperations(aStudyID);
396 GEOM::GEOM_Object_var result = IOp->MakeCopy(aShapeInit);
397 myGeomBase->Display(result);
399 catch (const SALOME::SALOME_Exception& S_ex) {
400 QtCatchCorbaException(S_ex);
404 QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_READY"));
409 //=====================================================================================
411 // purpose : BRep, Iges, Step
412 //=====================================================================================
413 bool GEOMToolsGUI::Import()
415 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( getGeometryGUI()->getApp() );
416 //SUIT_Application* app = getGeometryGUI()->getApp();
417 if (! app) return false;
419 SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
421 cout << "FAILED to cast active study to SalomeApp_Study" << endl;
424 _PTR(Study) aStudy = stud->studyDS();
426 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
428 SUIT_MessageBox::warning ( app->desktop(),
429 QObject::tr("WRN_WARNING"),
430 QObject::tr("WRN_STUDY_LOCKED"),
431 QObject::tr("BUT_OK") );
435 GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
436 if ( CORBA::is_nil( eng ) ) {
437 SUIT_MessageBox::critical( app->desktop(),
438 QObject::tr("WRN_WARNING"),
439 QObject::tr( "GEOM Engine is not started" ),
440 QObject::tr("BUT_OK") );
444 GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
445 if ( aInsOp->_is_nil() )
448 GEOM::GEOM_Object_var anObj;
450 // Obtain a list of available import formats
452 GEOM::string_array_var aFormats, aPatterns;
453 aInsOp->ImportTranslators( aFormats, aPatterns );
455 for ( int i = 0, n = aFormats->length(); i < n; i++ )
456 aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
460 QString fileName = getFileName(app->desktop(), "", aMap,
461 tr("GEOM_MEN_IMPORT"), true, fileType);
463 if (fileType.isEmpty() )
465 // Trying to detect file type
466 QFileInfo aFileInfo( fileName );
467 QString aPossibleType = (aFileInfo.suffix()).toUpper() ;
469 if ( (aMap.values()).contains(aPossibleType) )
470 fileType = aPossibleType;
473 if (fileName.isEmpty() || fileType.isEmpty())
476 GEOM_Operation* anOp = new GEOM_Operation (app, aInsOp.in());
478 SUIT_OverrideCursor wc;
480 app->putInfo(tr("GEOM_PRP_LOADING").arg(SUIT_Tools::file(fileName, /*withExten=*/true)));
484 CORBA::String_var fileN = fileName.toLatin1().data();
485 CORBA::String_var fileT = fileType.toLatin1().data();
486 anObj = aInsOp->Import(fileN, fileT);
488 if ( !anObj->_is_nil() && aInsOp->IsDone() ) {
489 QString aPublishObjName =
490 GEOMBase::GetDefaultName(SUIT_Tools::file(fileName, /*withExten=*/true));
492 SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy(aStudy);
493 GeometryGUI::GetGeomGen()->PublishInStudy(aDSStudy,
494 SALOMEDS::SObject::_nil(),
496 aPublishObjName.toStdString().c_str());
498 GEOM_Displayer( stud ).Display( anObj.in() );
500 // update data model and object browser
501 getGeometryGUI()->updateObjBrowser( true );
508 SUIT_MessageBox::critical( app->desktop(),
509 QObject::tr( "GEOM_ERROR" ),
510 QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
511 QObject::tr("BUT_OK") );
514 catch( const SALOME::SALOME_Exception& S_ex ) {
515 //QtCatchCorbaException(S_ex);
520 app->updateActions(); //SRN: To update a Save button in the toolbar
526 //=====================================================================================
528 // purpose : BRep, Iges, Step
529 //=====================================================================================
530 bool GEOMToolsGUI::Export()
532 SalomeApp_Application* app = getGeometryGUI()->getApp();
533 if (!app) return false;
535 SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
537 cout << "FAILED to cast active study to SalomeApp_Study" << endl;
540 _PTR(Study) aStudy = stud->studyDS();
542 GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
543 if ( CORBA::is_nil( eng ) ) {
544 SUIT_MessageBox::critical( app->desktop(),
545 QObject::tr("WRN_WARNING"),
546 QObject::tr( "GEOM Engine is not started" ),
547 QObject::tr("BUT_OK") );
551 GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
552 if ( aInsOp->_is_nil() )
555 // Obtain a list of available export formats
557 GEOM::string_array_var aFormats, aPatterns;
558 aInsOp->ExportTranslators( aFormats, aPatterns );
559 for ( int i = 0, n = aFormats->length(); i < n; i++ )
560 aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
562 // Get selected objects
563 LightApp_SelectionMgr* sm = app->selectionMgr();
567 SALOME_ListIO selectedObjects;
568 sm->selectedObjects( selectedObjects );
570 SALOME_ListIteratorOfListIO It( selectedObjects );
571 for(;It.More();It.Next()) {
572 Handle(SALOME_InteractiveObject) IObject = It.Value();
573 Standard_Boolean found;
574 GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject(IObject, found);
576 if ( !found || anObj->_is_nil() )
580 QString file = getFileName(app->desktop(), QString( IObject->getName() ), aMap,
581 tr("GEOM_MEN_EXPORT"), false, fileType);
583 // User has pressed "Cancel" --> stop the operation
584 if ( file.isEmpty() || fileType.isEmpty() )
587 GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
589 SUIT_OverrideCursor wc;
591 app->putInfo( tr("GEOM_PRP_EXPORT").arg(SUIT_Tools::file( file, /*withExten=*/true )) );
596 aInsOp->Export( anObj, file.toStdString().c_str(), fileType.toLatin1().constData() );
598 if ( aInsOp->IsDone() )
604 SUIT_MessageBox::critical( app->desktop(),
605 QObject::tr( "GEOM_ERROR" ),
606 QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
607 QObject::tr("BUT_OK") );
611 catch (const SALOME::SALOME_Exception& S_ex) {
612 //QtCatchCorbaException(S_ex);
622 QString GEOMToolsGUI::getParentComponent( _PTR( Study ) study, const SALOME_ListIO& iobjs )
626 for ( SALOME_ListIteratorOfListIO it( iobjs ); it.More(); it.Next() ) {
628 Handle(SALOME_InteractiveObject) io = it.Value();
629 if ( !io->hasEntry() )
632 QString compName = getParentComponent( study->FindObjectID( io->getEntry() ) );
634 if ( parentComp.isNull() )
635 parentComp = compName;
636 else if ( parentComp.compare( compName) != 0 ) { // objects belonging to different components are selected
637 parentComp = QString::null;
645 QString GEOMToolsGUI::getParentComponent( _PTR( SObject ) obj )
648 _PTR(SComponent) comp = obj->GetFatherComponent();
650 _PTR(GenericAttribute) anAttr;
651 if ( comp->FindAttribute( anAttr, "AttributeName") ) {
652 _PTR(AttributeName) aName( anAttr );
653 return QString( aName->Value().c_str() );
660 //=====================================================================================
661 // function : RemoveObjectWithChildren
662 // purpose : to be used by OnEditDelete() method
663 //=====================================================================================
664 void GEOMToolsGUI::RemoveObjectWithChildren(_PTR(SObject) obj,
666 QList<SALOME_View*> views,
667 GEOM_Displayer* disp)
669 // iterate through all children of obj
670 for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
671 _PTR(SObject) child (it->Value());
672 RemoveObjectWithChildren(child, aStudy, views, disp);
675 // erase object and remove it from engine
676 _PTR(GenericAttribute) anAttr;
677 if (obj->FindAttribute(anAttr, "AttributeIOR")) {
678 _PTR(AttributeIOR) anIOR (anAttr);
680 // Delete shape in Client
681 const TCollection_AsciiString ASCIor ((char*)anIOR->Value().c_str());
682 getGeometryGUI()->GetShapeReader().RemoveShapeFromBuffer(ASCIor);
684 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(obj);
685 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
686 if (!CORBA::is_nil(geomObj)) {
687 // Erase graphical object
688 QListIterator<SALOME_View*> it( views );
689 while ( it.hasNext() )
690 if ( SALOME_View* view = it.next() )
691 disp->Erase(geomObj, true, view);
693 // Remove object from Engine
694 GeometryGUI::GetGeomGen()->RemoveObject( geomObj );
699 //=====================================================================================
700 // function : CheckSubObjectInUse
701 // purpose : to be used by OnEditDelete() method
702 //=====================================================================================
703 bool GEOMToolsGUI::CheckSubObjectInUse(_PTR(SObject) checkobj,
704 _PTR(SObject) remobj,
707 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(checkobj);
708 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
709 if( CORBA::is_nil(geomObj) )
712 GEOM::ListOfGO_var list = geomObj->GetDependency();
713 if( list->length() > 1 )
714 for(int i = 0; i < list->length(); i++ ){
715 CORBA::Object_var corbaObj_rem = GeometryGUI::ClientSObjectToObject(remobj);
716 GEOM::GEOM_Object_var geomObj_rem = GEOM::GEOM_Object::_narrow( corbaObj_rem );
717 if( list[i]->_is_equivalent( geomObj_rem ) ){
718 SalomeApp_Application* app =
719 dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
721 SUIT_MessageBox::warning ( app->desktop(),
722 QObject::tr("WRN_WARNING"),
723 QObject::tr("DEP_OBJECT"),
724 QObject::tr("BUT_OK") );
732 //=================================================================================
733 // function : deactivate()
734 // purpose : Called when GEOM component is deactivated
735 //=================================================================================
736 void GEOMToolsGUI::deactivate()
738 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
740 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
741 GEOM_Displayer aDisp (appStudy);
742 aDisp.GlobalSelection();
743 getGeometryGUI()->setLocalSelectionMode(GEOM_ALLOBJECTS);
747 //=====================================================================================
749 //=====================================================================================
753 __declspec( dllexport )
755 GEOMGUI* GetLibGUI( GeometryGUI* parent )
757 return new GEOMToolsGUI( parent );