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 9024 : // OPEN - OBJBROSER POPUP
267 SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
276 //===============================================================================
277 // function : OnEditDelete()
279 //===============================================================================
280 void GEOMToolsGUI::OnEditDelete()
282 SALOME_ListIO selected;
283 SalomeApp_Application* app =
284 dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
286 LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
287 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
288 if ( aSelMgr && appStudy ) {
289 aSelMgr->selectedObjects( selected, QString::null, false );
290 if ( !selected.IsEmpty() ) {
291 _PTR(Study) aStudy = appStudy->studyDS();
293 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
295 SUIT_MessageBox::warn1 ( app->desktop(),
296 QObject::tr("WRN_WARNING"),
297 QObject::tr("WRN_STUDY_LOCKED"),
298 QObject::tr("BUT_OK") );
302 // VSR 17/11/04: check if all objects selected belong to GEOM component --> start
303 // modifications of ASV 01.06.05
304 QString parentComp = getParentComponent( aStudy, selected );
305 const char* geomIOR = app->orb()->object_to_string( GeometryGUI::GetGeomGen() );
306 QString geomComp = getParentComponent( aStudy->FindObjectIOR( geomIOR ) );
308 if ( parentComp != geomComp ) {
309 SUIT_MessageBox::warn1 ( app->desktop(),
310 QObject::tr("ERR_ERROR"),
311 QObject::tr("NON_GEOM_OBJECTS_SELECTED").arg( getGeometryGUI()->moduleName() ),
312 QObject::tr("BUT_OK") );
315 // VSR 17/11/04: check if all objects selected belong to GEOM component <-- finish
317 if ( SUIT_MessageBox::warn2( app->desktop(),
318 QObject::tr( "GEOM_WRN_WARNING" ),
319 QObject::tr( "GEOM_REALLY_DELETE" ),
320 QObject::tr( "GEOM_BUT_YES" ),
321 QObject::tr( "GEOM_BUT_NO" ), 1, 0, 0 ) != 1 )
324 // QAD_Operation* op = new SALOMEGUI_ImportOperation(.....);
327 // prepare list of SALOME_Views
328 QPtrList<SALOME_View> views;
331 ViewManagerList vmans = app->viewManagers();
332 SUIT_ViewManager* vman;
333 for ( vman = vmans.first(); vman; vman = vmans.next() ) {
334 SUIT_ViewModel* vmod = vman->getViewModel();
335 view = dynamic_cast<SALOME_View*> ( vmod ); // must work for OCC and VTK views
337 views.append( view );
340 _PTR(StudyBuilder) aStudyBuilder (aStudy->NewBuilder());
341 _PTR(GenericAttribute) anAttr;
342 GEOM_Displayer* disp = new GEOM_Displayer( appStudy );
344 _PTR(SComponent) aGeom ( aStudy->FindComponent("GEOM") );
348 // MAIN LOOP OF SELECTED OBJECTS
349 for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
351 Handle(SALOME_InteractiveObject) io = It.Value();
352 if ( !io->hasEntry() )
355 _PTR(SObject) obj ( aStudy->FindObjectID( io->getEntry() ) );
357 // disable removal of "Geometry" component object
358 if ( !strcmp( obj->GetIOR().c_str(), geomIOR ) )
361 //If the object has been used to create another one,then it can't be deleted
362 _PTR(ChildIterator) it (aStudy->NewChildIterator(aGeom));
363 for ( it->InitEx( true ); it->More(); it->Next() ) {
364 _PTR(SObject) chobj (it->Value());
365 if(CheckSubObjectInUse(chobj, obj, aStudy)) return;
367 for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
368 _PTR(SObject) child (it->Value());
369 if(CheckSubObjectInUse( chobj, child, aStudy)) return;
373 RemoveObjectWithChildren(obj, aStudy, views, disp);
375 // Remove objects from Study
376 aStudyBuilder->RemoveObject( obj );
379 } // MAIN LOOP of selected
382 aSelMgr->setSelectedObjects( selected );
383 getGeometryGUI()->updateObjBrowser();
384 } // if ( selected not empty )
385 } // if ( selMgr && appStudy )
387 app->updateActions(); //SRN: To update a Save button in the toolbar
399 //==============================================================================
400 // function : OnEditCopy()
402 //==============================================================================
403 void GEOMToolsGUI::OnEditCopy()
406 SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection() );
407 GEOM::string_array_var listIOR = new GEOM::string_array;
409 const SALOME_ListIO& List = Sel->StoredIObjects();
411 myGeomBase->ConvertListOfIOInListOfIOR(List, listIOR);
413 Sel->ClearIObjects();
415 SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
416 int aStudyID = aStudy->StudyId();
418 for (unsigned int ind = 0; ind < listIOR->length();ind++) {
419 GEOM::GEOM_Object_var aShapeInit = myGeom->GetIORFromString(listIOR[ind]);
421 GEOM::GEOM_IInsertOperations_var IOp = myGeom->GetIInsertOperations(aStudyID);
422 GEOM::GEOM_Object_var result = IOp->MakeCopy(aShapeInit);
423 myGeomBase->Display(result);
425 catch (const SALOME::SALOME_Exception& S_ex) {
426 QtCatchCorbaException(S_ex);
430 QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_READY"));
435 //=====================================================================================
437 // purpose : BRep, Iges, Step
438 //=====================================================================================
439 bool GEOMToolsGUI::Import()
441 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( getGeometryGUI()->getApp() );
442 //SUIT_Application* app = getGeometryGUI()->getApp();
443 if (! app) return false;
445 SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
447 cout << "FAILED to cast active study to SalomeApp_Study" << endl;
450 _PTR(Study) aStudy = stud->studyDS();
452 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
454 SUIT_MessageBox::warn1 ( app->desktop(),
455 QObject::tr("WRN_WARNING"),
456 QObject::tr("WRN_STUDY_LOCKED"),
457 QObject::tr("BUT_OK") );
461 GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
462 if ( CORBA::is_nil( eng ) ) {
463 SUIT_MessageBox::error1( app->desktop(),
464 QObject::tr("WRN_WARNING"),
465 QObject::tr( "GEOM Engine is not started" ),
466 QObject::tr("BUT_OK") );
470 GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
471 if ( aInsOp->_is_nil() )
474 GEOM::GEOM_Object_var anObj;
476 // Obtain a list of available import formats
479 GEOM::string_array_var aFormats, aPatterns;
480 aInsOp->ImportTranslators( aFormats, aPatterns );
482 for ( int i = 0, n = aFormats->length(); i < n; i++ ) {
483 aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
484 filters.push_back( (char*)aPatterns[i] );
489 QString fileName = getFileName(app->desktop(), "", aMap, filters,
490 tr("GEOM_MEN_IMPORT"), true, fileType);
492 if (fileType.isEmpty() )
494 // Trying to detect file type
495 QFileInfo aFileInfo( fileName );
496 QString aPossibleType = (aFileInfo.extension(false)).upper() ;
498 if ( (aMap.values()).contains(aPossibleType) )
499 fileType = aPossibleType;
502 if (fileName.isEmpty() || fileType.isEmpty())
505 GEOM_Operation* anOp = new GEOM_Operation (app, aInsOp.in());
507 SUIT_OverrideCursor wc;
509 app->putInfo(tr("GEOM_PRP_LOADING").arg(SUIT_Tools::file(fileName, /*withExten=*/true)));
513 CORBA::String_var fileN = fileName.latin1();
514 CORBA::String_var fileT = fileType.latin1();
515 anObj = aInsOp->Import(fileN, fileT);
517 if ( !anObj->_is_nil() && aInsOp->IsDone() ) {
518 QString aPublishObjName =
519 GEOMBase::GetDefaultName(SUIT_Tools::file(fileName, /*withExten=*/true));
521 SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy(aStudy);
522 GeometryGUI::GetGeomGen()->PublishInStudy(aDSStudy,
523 SALOMEDS::SObject::_nil(),
527 GEOM_Displayer( stud ).Display( anObj.in() );
529 // update data model and object browser
530 getGeometryGUI()->updateObjBrowser( true );
537 SUIT_MessageBox::error1( app->desktop(),
538 QObject::tr( "GEOM_ERROR" ),
539 QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
540 QObject::tr("BUT_OK") );
543 catch( const SALOME::SALOME_Exception& S_ex ) {
544 //QtCatchCorbaException(S_ex);
549 app->updateActions(); //SRN: To update a Save button in the toolbar
555 //=====================================================================================
557 // purpose : BRep, Iges, Step
558 //=====================================================================================
559 bool GEOMToolsGUI::Export()
561 SalomeApp_Application* app = getGeometryGUI()->getApp();
562 if (!app) return false;
564 SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
566 cout << "FAILED to cast active study to SalomeApp_Study" << endl;
569 _PTR(Study) aStudy = stud->studyDS();
571 GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
572 if ( CORBA::is_nil( eng ) ) {
573 SUIT_MessageBox::error1( app->desktop(),
574 QObject::tr("WRN_WARNING"),
575 QObject::tr( "GEOM Engine is not started" ),
576 QObject::tr("BUT_OK") );
580 GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
581 if ( aInsOp->_is_nil() )
584 // Obtain a list of available export formats
587 GEOM::string_array_var aFormats, aPatterns;
588 aInsOp->ExportTranslators( aFormats, aPatterns );
589 for ( int i = 0, n = aFormats->length(); i < n; i++ ) {
590 aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
591 filters.push_back( (char*)aPatterns[i] );
594 // Get selected objects
595 LightApp_SelectionMgr* sm = app->selectionMgr();
599 SALOME_ListIO selectedObjects;
600 sm->selectedObjects( selectedObjects );
602 SALOME_ListIteratorOfListIO It( selectedObjects );
603 for(;It.More();It.Next()) {
604 Handle(SALOME_InteractiveObject) IObject = It.Value();
605 Standard_Boolean found;
606 GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject(IObject, found);
608 if ( !found || anObj->_is_nil() )
612 QString file = getFileName(app->desktop(), QString( IObject->getName() ), aMap, filters,
613 tr("GEOM_MEN_EXPORT"), false, fileType);
615 // User has pressed "Cancel" --> stop the operation
616 if ( file.isEmpty() || fileType.isEmpty() )
619 GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
621 SUIT_OverrideCursor wc;
623 app->putInfo( tr("GEOM_PRP_EXPORT").arg(SUIT_Tools::file( file, /*withExten=*/true )) );
628 aInsOp->Export( anObj, file, fileType.latin1() );
630 if ( aInsOp->IsDone() )
636 SUIT_MessageBox::error1( app->desktop(),
637 QObject::tr( "GEOM_ERROR" ),
638 QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
639 QObject::tr("BUT_OK") );
643 catch (const SALOME::SALOME_Exception& S_ex) {
644 //QtCatchCorbaException(S_ex);
654 QString GEOMToolsGUI::getParentComponent( _PTR( Study ) study, const SALOME_ListIO& iobjs )
658 for ( SALOME_ListIteratorOfListIO it( iobjs ); it.More(); it.Next() ) {
660 Handle(SALOME_InteractiveObject) io = it.Value();
661 if ( !io->hasEntry() )
664 QString compName = getParentComponent( study->FindObjectID( io->getEntry() ) );
666 if ( parentComp.isNull() )
667 parentComp = compName;
668 else if ( parentComp.compare( compName) != 0 ) { // objects belonging to different components are selected
669 parentComp = QString::null;
677 QString GEOMToolsGUI::getParentComponent( _PTR( SObject ) obj )
680 _PTR(SComponent) comp = obj->GetFatherComponent();
682 _PTR(GenericAttribute) anAttr;
683 if ( comp->FindAttribute( anAttr, "AttributeName") ) {
684 _PTR(AttributeName) aName( anAttr );
685 return QString( aName->Value().c_str() );
692 //=====================================================================================
693 // function : RemoveObjectWithChildren
694 // purpose : to be used by OnEditDelete() method
695 //=====================================================================================
696 void GEOMToolsGUI::RemoveObjectWithChildren(_PTR(SObject) obj,
698 QPtrList<SALOME_View> views,
699 GEOM_Displayer* disp)
701 // iterate through all children of obj
702 for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
703 _PTR(SObject) child (it->Value());
704 RemoveObjectWithChildren(child, aStudy, views, disp);
707 // erase object and remove it from engine
708 _PTR(GenericAttribute) anAttr;
709 if (obj->FindAttribute(anAttr, "AttributeIOR")) {
710 _PTR(AttributeIOR) anIOR (anAttr);
712 // Delete shape in Client
713 const TCollection_AsciiString ASCIor ((char*)anIOR->Value().c_str());
714 getGeometryGUI()->GetShapeReader().RemoveShapeFromBuffer(ASCIor);
716 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(obj);
717 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
718 if (!CORBA::is_nil(geomObj)) {
719 // Erase graphical object
720 SALOME_View* view = views.first();
721 for (; view; view = views.next()) {
722 disp->Erase(geomObj, true, view);
725 // Remove object from Engine
726 GeometryGUI::GetGeomGen()->RemoveObject( geomObj );
731 //=====================================================================================
732 // function : CheckSubObjectInUse
733 // purpose : to be used by OnEditDelete() method
734 //=====================================================================================
735 bool GEOMToolsGUI::CheckSubObjectInUse(_PTR(SObject) checkobj,
736 _PTR(SObject) remobj,
739 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(checkobj);
740 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
741 if( CORBA::is_nil(geomObj) )
744 GEOM::ListOfGO_var list = geomObj->GetDependency();
745 if( list->length() > 1 )
746 for(int i = 0; i < list->length(); i++ ){
747 CORBA::Object_var corbaObj_rem = GeometryGUI::ClientSObjectToObject(remobj);
748 GEOM::GEOM_Object_var geomObj_rem = GEOM::GEOM_Object::_narrow( corbaObj_rem );
749 if( list[i]->_is_equivalent( geomObj_rem ) ){
750 SalomeApp_Application* app =
751 dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
753 SUIT_MessageBox::warn1 ( app->desktop(),
754 QObject::tr("WRN_WARNING"),
755 QObject::tr("DEP_OBJECT"),
756 QObject::tr("BUT_OK") );
764 //=================================================================================
765 // function : deactivate()
766 // purpose : Called when GEOM component is deactivated
767 //=================================================================================
768 void GEOMToolsGUI::deactivate()
770 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
772 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
773 GEOM_Displayer aDisp (appStudy);
774 aDisp.GlobalSelection();
775 getGeometryGUI()->setLocalSelectionMode(GEOM_ALLOBJECTS);
779 //=====================================================================================
781 //=====================================================================================
785 __declspec( dllexport )
787 GEOMGUI* GetLibGUI( GeometryGUI* parent )
789 return new GEOMToolsGUI( parent );