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 QString& caption,
79 static QString lastUsedFilter;
82 for ( FilterMap::const_iterator it = filterMap.begin(); it != filterMap.end(); ++it ) {
83 filters.push_back( it.key() );
85 if (it.key().contains("BREP", false)) {
86 aBrepFilter = it.key();
90 SUIT_FileDlg* fd = new SUIT_FileDlg( parent, open, true, true );
91 if ( !caption.isEmpty() )
92 fd->setCaption( caption );
94 if ( !initial.isEmpty() )
95 fd->setSelection( initial );
97 fd->setFilters( filters );
99 if ( !lastUsedFilter.isEmpty() && filterMap.contains( lastUsedFilter ) )
100 fd->setSelectedFilter( lastUsedFilter );
102 if (!aBrepFilter.isEmpty()) {
103 fd->setSelectedFilter(aBrepFilter);
108 QString filename = fd->selectedFile();
109 format = filterMap[fd->selectedFilter()];
110 lastUsedFilter = fd->selectedFilter();
112 qApp->processEvents();
116 //=======================================================================
117 // function : GEOMToolsGUI()
118 // purpose : Constructor
119 //=======================================================================
120 GEOMToolsGUI::GEOMToolsGUI( GeometryGUI* parent )
126 //=======================================================================
127 // function : ~GEOMToolsGUI()
128 // purpose : Destructor
129 //=======================================================================
130 GEOMToolsGUI::~GEOMToolsGUI()
135 //=======================================================================
136 // function : OnGUIEvent()
138 //=======================================================================
139 bool GEOMToolsGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent)
141 getGeometryGUI()->EmitSignalDeactivateDialog();
143 switch (theCommandID)
155 case 111: // IMPORT BREP
156 case 112: // IMPORT IGES
157 case 113: // IMPORT STEP
162 case 121: // EXPORT BREP
163 case 122: // EXPORT IGES
164 case 123: // EXPORT STEP
169 case 2171: // POPUP VIEWER - SELECT ONLY - VERTEX
171 OnSelectOnly( GEOM_POINT );
174 case 2172: // POPUP VIEWER - SELECT ONLY - EDGE
176 OnSelectOnly( GEOM_EDGE );
179 case 2173: // POPUP VIEWER - SELECT ONLY - WIRE
181 OnSelectOnly( GEOM_WIRE );
184 case 2174: // POPUP VIEWER - SELECT ONLY - FACE
186 OnSelectOnly( GEOM_FACE );
189 case 2175: // POPUP VIEWER - SELECT ONLY - SHELL
191 OnSelectOnly( GEOM_SHELL );
194 case 2176: // POPUP VIEWER - SELECT ONLY - SOLID
196 OnSelectOnly( GEOM_SOLID );
199 case 2177: // POPUP VIEWER - SELECT ONLY - COMPOUND
201 OnSelectOnly( GEOM_COMPOUND );
204 case 2178: // POPUP VIEWER - SELECT ONLY - SELECT ALL
206 OnSelectOnly( GEOM_ALLOBJECTS );
209 case 411: // SETTINGS - ADD IN STUDY
211 // SAN -- TO BE REMOVED !!!
214 case 412: // SETTINGS - SHADING COLOR
219 case 413: // SETTINGS - ISOS
224 case 414: // SETTINGS : STEP VALUE FOR SPIN BOXES
229 case 804: // ADD IN STUDY - POPUP VIEWER
231 // SAN -- TO BE REMOVED !!!!
239 case 5103: // CHECK GEOMETRY
244 case 8032: // COLOR - POPUP VIEWER
249 case 8033: // TRANSPARENCY - POPUP VIEWER
254 case 8034: // ISOS - POPUP VIEWER
259 case 9024 : // OPEN - OBJBROSER POPUP
266 SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
275 //===============================================================================
276 // function : OnEditDelete()
278 //===============================================================================
279 void GEOMToolsGUI::OnEditDelete()
281 SALOME_ListIO selected;
282 SalomeApp_Application* app =
283 dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
285 LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
286 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
287 if ( aSelMgr && appStudy ) {
288 aSelMgr->selectedObjects( selected, QString::null, false );
289 if ( !selected.IsEmpty() ) {
290 _PTR(Study) aStudy = appStudy->studyDS();
292 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
294 SUIT_MessageBox::warn1 ( app->desktop(),
295 QObject::tr("WRN_WARNING"),
296 QObject::tr("WRN_STUDY_LOCKED"),
297 QObject::tr("BUT_OK") );
301 // VSR 17/11/04: check if all objects selected belong to GEOM component --> start
302 // modifications of ASV 01.06.05
303 QString parentComp = getParentComponent( aStudy, selected );
304 CORBA::String_var geomIOR = app->orb()->object_to_string( GeometryGUI::GetGeomGen() );
305 QString geomComp = getParentComponent( aStudy->FindObjectIOR( geomIOR.in() ) );
307 if ( parentComp != geomComp ) {
308 SUIT_MessageBox::warn1 ( app->desktop(),
309 QObject::tr("ERR_ERROR"),
310 QObject::tr("NON_GEOM_OBJECTS_SELECTED").arg( getGeometryGUI()->moduleName() ),
311 QObject::tr("BUT_OK") );
314 // VSR 17/11/04: check if all objects selected belong to GEOM component <-- finish
316 if ( SUIT_MessageBox::warn2( app->desktop(),
317 QObject::tr( "GEOM_WRN_WARNING" ),
318 QObject::tr( "GEOM_REALLY_DELETE" ),
319 QObject::tr( "GEOM_BUT_YES" ),
320 QObject::tr( "GEOM_BUT_NO" ), 1, 0, 0 ) != 1 )
323 // QAD_Operation* op = new SALOMEGUI_ImportOperation(.....);
326 // prepare list of SALOME_Views
327 QPtrList<SALOME_View> views;
330 ViewManagerList vmans = app->viewManagers();
331 SUIT_ViewManager* vman;
332 for ( vman = vmans.first(); vman; vman = vmans.next() ) {
333 SUIT_ViewModel* vmod = vman->getViewModel();
334 view = dynamic_cast<SALOME_View*> ( vmod ); // must work for OCC and VTK views
336 views.append( view );
339 _PTR(StudyBuilder) aStudyBuilder (aStudy->NewBuilder());
340 _PTR(GenericAttribute) anAttr;
341 GEOM_Displayer* disp = new GEOM_Displayer( appStudy );
343 _PTR(SComponent) aGeom ( aStudy->FindComponent("GEOM") );
347 // MAIN LOOP OF SELECTED OBJECTS
348 for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
350 Handle(SALOME_InteractiveObject) io = It.Value();
351 if ( !io->hasEntry() )
354 _PTR(SObject) obj ( aStudy->FindObjectID( io->getEntry() ) );
356 // disable removal of "Geometry" component object
357 if ( !strcmp( obj->GetIOR().c_str(), geomIOR ) )
360 //If the object has been used to create another one,then it can't be deleted
361 _PTR(ChildIterator) it (aStudy->NewChildIterator(aGeom));
362 for ( it->InitEx( true ); it->More(); it->Next() ) {
363 _PTR(SObject) chobj (it->Value());
364 if(CheckSubObjectInUse(chobj, obj, aStudy)) return;
366 for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
367 _PTR(SObject) child (it->Value());
368 if(CheckSubObjectInUse( chobj, child, aStudy)) return;
372 RemoveObjectWithChildren(obj, aStudy, views, disp);
374 // Remove objects from Study
375 aStudyBuilder->RemoveObject( obj );
378 } // MAIN LOOP of selected
381 aSelMgr->setSelectedObjects( selected );
382 getGeometryGUI()->updateObjBrowser();
383 } // if ( selected not empty )
384 } // if ( selMgr && appStudy )
386 app->updateActions(); //SRN: To update a Save button in the toolbar
398 //==============================================================================
399 // function : OnEditCopy()
401 //==============================================================================
402 void GEOMToolsGUI::OnEditCopy()
405 SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection() );
406 GEOM::string_array_var listIOR = new GEOM::string_array;
408 const SALOME_ListIO& List = Sel->StoredIObjects();
410 myGeomBase->ConvertListOfIOInListOfIOR(List, listIOR);
412 Sel->ClearIObjects();
414 SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
415 int aStudyID = aStudy->StudyId();
417 for (unsigned int ind = 0; ind < listIOR->length();ind++) {
418 GEOM::GEOM_Object_var aShapeInit = myGeom->GetIORFromString(listIOR[ind]);
420 GEOM::GEOM_IInsertOperations_var IOp = myGeom->GetIInsertOperations(aStudyID);
421 GEOM::GEOM_Object_var result = IOp->MakeCopy(aShapeInit);
422 myGeomBase->Display(result);
424 catch (const SALOME::SALOME_Exception& S_ex) {
425 QtCatchCorbaException(S_ex);
429 QAD_Application::getDesktop()->putInfo(tr("GEOM_PRP_READY"));
434 //=====================================================================================
436 // purpose : BRep, Iges, Step
437 //=====================================================================================
438 bool GEOMToolsGUI::Import()
440 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( getGeometryGUI()->getApp() );
441 //SUIT_Application* app = getGeometryGUI()->getApp();
442 if (! app) return false;
444 SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
446 cout << "FAILED to cast active study to SalomeApp_Study" << endl;
449 _PTR(Study) aStudy = stud->studyDS();
451 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->GetProperties()))->IsLocked();
453 SUIT_MessageBox::warn1 ( app->desktop(),
454 QObject::tr("WRN_WARNING"),
455 QObject::tr("WRN_STUDY_LOCKED"),
456 QObject::tr("BUT_OK") );
460 GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
461 if ( CORBA::is_nil( eng ) ) {
462 SUIT_MessageBox::error1( app->desktop(),
463 QObject::tr("WRN_WARNING"),
464 QObject::tr( "GEOM Engine is not started" ),
465 QObject::tr("BUT_OK") );
469 GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
470 if ( aInsOp->_is_nil() )
473 GEOM::GEOM_Object_var anObj;
475 // Obtain a list of available import formats
477 GEOM::string_array_var aFormats, aPatterns;
478 aInsOp->ImportTranslators( aFormats, aPatterns );
480 for ( int i = 0, n = aFormats->length(); i < n; i++ )
481 aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
485 QString fileName = getFileName(app->desktop(), "", aMap,
486 tr("GEOM_MEN_IMPORT"), true, fileType);
488 if (fileType.isEmpty() )
490 // Trying to detect file type
491 QFileInfo aFileInfo( fileName );
492 QString aPossibleType = (aFileInfo.extension(false)).upper() ;
494 if ( (aMap.values()).contains(aPossibleType) )
495 fileType = aPossibleType;
498 if (fileName.isEmpty() || fileType.isEmpty())
501 GEOM_Operation* anOp = new GEOM_Operation (app, aInsOp.in());
503 SUIT_OverrideCursor wc;
505 app->putInfo(tr("GEOM_PRP_LOADING").arg(SUIT_Tools::file(fileName, /*withExten=*/true)));
509 CORBA::String_var fileN = fileName.latin1();
510 CORBA::String_var fileT = fileType.latin1();
511 anObj = aInsOp->Import(fileN, fileT);
513 if ( !anObj->_is_nil() && aInsOp->IsDone() ) {
514 QString aPublishObjName =
515 GEOMBase::GetDefaultName(SUIT_Tools::file(fileName, /*withExten=*/true));
517 SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy(aStudy);
518 GeometryGUI::GetGeomGen()->PublishInStudy(aDSStudy,
519 SALOMEDS::SObject::_nil(),
523 GEOM_Displayer( stud ).Display( anObj.in() );
525 // update data model and object browser
526 getGeometryGUI()->updateObjBrowser( true );
533 SUIT_MessageBox::error1( app->desktop(),
534 QObject::tr( "GEOM_ERROR" ),
535 QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
536 QObject::tr("BUT_OK") );
539 catch( const SALOME::SALOME_Exception& S_ex ) {
540 //QtCatchCorbaException(S_ex);
545 app->updateActions(); //SRN: To update a Save button in the toolbar
551 //=====================================================================================
553 // purpose : BRep, Iges, Step
554 //=====================================================================================
555 bool GEOMToolsGUI::Export()
557 SalomeApp_Application* app = getGeometryGUI()->getApp();
558 if (!app) return false;
560 SalomeApp_Study* stud = dynamic_cast<SalomeApp_Study*> ( app->activeStudy() );
562 cout << "FAILED to cast active study to SalomeApp_Study" << endl;
565 _PTR(Study) aStudy = stud->studyDS();
567 GEOM::GEOM_Gen_var eng = GeometryGUI::GetGeomGen();
568 if ( CORBA::is_nil( eng ) ) {
569 SUIT_MessageBox::error1( app->desktop(),
570 QObject::tr("WRN_WARNING"),
571 QObject::tr( "GEOM Engine is not started" ),
572 QObject::tr("BUT_OK") );
576 GEOM::GEOM_IInsertOperations_var aInsOp = eng->GetIInsertOperations( aStudy->StudyId() );
577 if ( aInsOp->_is_nil() )
580 // Obtain a list of available export formats
582 GEOM::string_array_var aFormats, aPatterns;
583 aInsOp->ExportTranslators( aFormats, aPatterns );
584 for ( int i = 0, n = aFormats->length(); i < n; i++ )
585 aMap.insert( (char*)aPatterns[i], (char*)aFormats[i] );
587 // Get selected objects
588 LightApp_SelectionMgr* sm = app->selectionMgr();
592 SALOME_ListIO selectedObjects;
593 sm->selectedObjects( selectedObjects );
595 SALOME_ListIteratorOfListIO It( selectedObjects );
596 for(;It.More();It.Next()) {
597 Handle(SALOME_InteractiveObject) IObject = It.Value();
598 Standard_Boolean found;
599 GEOM::GEOM_Object_var anObj = GEOMBase::ConvertIOinGEOMObject(IObject, found);
601 if ( !found || anObj->_is_nil() )
605 QString file = getFileName(app->desktop(), QString( IObject->getName() ), aMap,
606 tr("GEOM_MEN_EXPORT"), false, fileType);
608 // User has pressed "Cancel" --> stop the operation
609 if ( file.isEmpty() || fileType.isEmpty() )
612 GEOM_Operation* anOp = new GEOM_Operation( app, aInsOp.in() );
614 SUIT_OverrideCursor wc;
616 app->putInfo( tr("GEOM_PRP_EXPORT").arg(SUIT_Tools::file( file, /*withExten=*/true )) );
621 aInsOp->Export( anObj, file, fileType.latin1() );
623 if ( aInsOp->IsDone() )
629 SUIT_MessageBox::error1( app->desktop(),
630 QObject::tr( "GEOM_ERROR" ),
631 QObject::tr("GEOM_PRP_ABORT") + "\n" + QString( aInsOp->GetErrorCode() ),
632 QObject::tr("BUT_OK") );
636 catch (const SALOME::SALOME_Exception& S_ex) {
637 //QtCatchCorbaException(S_ex);
647 QString GEOMToolsGUI::getParentComponent( _PTR( Study ) study, const SALOME_ListIO& iobjs )
651 for ( SALOME_ListIteratorOfListIO it( iobjs ); it.More(); it.Next() ) {
653 Handle(SALOME_InteractiveObject) io = it.Value();
654 if ( !io->hasEntry() )
657 QString compName = getParentComponent( study->FindObjectID( io->getEntry() ) );
659 if ( parentComp.isNull() )
660 parentComp = compName;
661 else if ( parentComp.compare( compName) != 0 ) { // objects belonging to different components are selected
662 parentComp = QString::null;
670 QString GEOMToolsGUI::getParentComponent( _PTR( SObject ) obj )
673 _PTR(SComponent) comp = obj->GetFatherComponent();
675 _PTR(GenericAttribute) anAttr;
676 if ( comp->FindAttribute( anAttr, "AttributeName") ) {
677 _PTR(AttributeName) aName( anAttr );
678 return QString( aName->Value().c_str() );
685 //=====================================================================================
686 // function : RemoveObjectWithChildren
687 // purpose : to be used by OnEditDelete() method
688 //=====================================================================================
689 void GEOMToolsGUI::RemoveObjectWithChildren(_PTR(SObject) obj,
691 QPtrList<SALOME_View> views,
692 GEOM_Displayer* disp)
694 // iterate through all children of obj
695 for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
696 _PTR(SObject) child (it->Value());
697 RemoveObjectWithChildren(child, aStudy, views, disp);
700 // erase object and remove it from engine
701 _PTR(GenericAttribute) anAttr;
702 if (obj->FindAttribute(anAttr, "AttributeIOR")) {
703 _PTR(AttributeIOR) anIOR (anAttr);
705 // Delete shape in Client
706 const TCollection_AsciiString ASCIor ((char*)anIOR->Value().c_str());
707 getGeometryGUI()->GetShapeReader().RemoveShapeFromBuffer(ASCIor);
709 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(obj);
710 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
711 if (!CORBA::is_nil(geomObj)) {
712 // Erase graphical object
713 SALOME_View* view = views.first();
714 for (; view; view = views.next()) {
715 disp->Erase(geomObj, true, view);
718 // Remove object from Engine
719 GeometryGUI::GetGeomGen()->RemoveObject( geomObj );
724 //=====================================================================================
725 // function : CheckSubObjectInUse
726 // purpose : to be used by OnEditDelete() method
727 //=====================================================================================
728 bool GEOMToolsGUI::CheckSubObjectInUse(_PTR(SObject) checkobj,
729 _PTR(SObject) remobj,
732 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(checkobj);
733 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
734 if( CORBA::is_nil(geomObj) )
737 GEOM::ListOfGO_var list = geomObj->GetDependency();
738 if( list->length() > 1 )
739 for(int i = 0; i < list->length(); i++ ){
740 CORBA::Object_var corbaObj_rem = GeometryGUI::ClientSObjectToObject(remobj);
741 GEOM::GEOM_Object_var geomObj_rem = GEOM::GEOM_Object::_narrow( corbaObj_rem );
742 if( list[i]->_is_equivalent( geomObj_rem ) ){
743 SalomeApp_Application* app =
744 dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
746 SUIT_MessageBox::warn1 ( app->desktop(),
747 QObject::tr("WRN_WARNING"),
748 QObject::tr("DEP_OBJECT"),
749 QObject::tr("BUT_OK") );
757 //=================================================================================
758 // function : deactivate()
759 // purpose : Called when GEOM component is deactivated
760 //=================================================================================
761 void GEOMToolsGUI::deactivate()
763 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
765 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
766 GEOM_Displayer aDisp (appStudy);
767 aDisp.GlobalSelection();
768 getGeometryGUI()->setLocalSelectionMode(GEOM_ALLOBJECTS);
772 //=====================================================================================
774 //=====================================================================================
778 GEOMGUI* GetLibGUI( GeometryGUI* parent )
780 return new GEOMToolsGUI( parent );