1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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, or (at your option) any later version.
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
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File : GEOMBase_Tools.cxx
25 // Author : Damien COQUERET, Open CASCADE S.A.S.
27 #include "GEOMToolsGUI.h"
28 #include "GEOMToolsGUI_DeleteDlg.h"
30 #include <GeometryGUI.h>
31 #include "GeometryGUI_Operations.h"
33 #include <GEOM_Operation.h>
34 #include <GEOM_Displayer.h>
36 #include <SUIT_Session.h>
37 #include <SUIT_OverrideCursor.h>
38 #include <SUIT_MessageBox.h>
39 #include <SUIT_Tools.h>
40 #include <SUIT_FileDlg.h>
41 #include <SUIT_Desktop.h>
42 #include <SUIT_ViewModel.h>
43 #include <SUIT_ViewManager.h>
45 #include <SalomeApp_Application.h>
46 #include <SalomeApp_Study.h>
47 #include <LightApp_SelectionMgr.h>
48 #include <GEOMImpl_Types.hxx>
50 #include <SALOME_ListIO.hxx>
51 #include <SALOME_Prs.h>
54 #include <QApplication>
59 #include <TCollection_AsciiString.hxx>
61 typedef QMap<QString, QString> FilterMap;
62 static QString lastUsedFilter;
64 //=======================================================================
65 // function : getParentComponent
66 // purpose : Get object's parent component entry
67 //=======================================================================
68 static QString getParentComponent( _PTR( SObject ) obj )
71 _PTR(SComponent) comp = obj->GetFatherComponent();
73 return QString( comp->GetID().c_str() );
78 //=====================================================================================
80 // purpose : check if the object(s) passed as the the second arguments are used
81 // by the other objects in the study
82 //=====================================================================================
83 static bool inUse( _PTR(Study) study, const QString& component, const QMap<QString,QString>& objects )
85 _PTR(SObject) comp = study->FindObjectID( component.toLatin1().data() );
89 // collect all GEOM objects being deleted
90 QMap<QString, GEOM::GEOM_BaseObject_var> gobjects;
91 QMap<QString, QString>::ConstIterator oit;
92 std::list<_PTR(SObject)> aSelectedSO;
93 for ( oit = objects.begin(); oit != objects.end(); ++oit ) {
94 _PTR(SObject) so = study->FindObjectID( oit.key().toLatin1().data() );
97 aSelectedSO.push_back(so);
98 CORBA::Object_var corbaObj_rem = GeometryGUI::ClientSObjectToObject( so );
99 GEOM::GEOM_BaseObject_var geomObj_rem = GEOM::GEOM_BaseObject::_narrow( corbaObj_rem );
100 if( CORBA::is_nil( geomObj_rem ) )
102 gobjects.insert( oit.key(), geomObj_rem );
105 // Search References with other Modules
106 std::list< _PTR(SObject) >::iterator itSO = aSelectedSO.begin();
107 for ( ; itSO != aSelectedSO.end(); ++itSO ) {
108 std::vector<_PTR(SObject)> aReferences = study->FindDependances( *itSO );
109 int aRefLength = aReferences.size();
111 for (int i = 0; i < aRefLength; i++) {
112 _PTR(SObject) firstSO( aReferences[i] );
113 _PTR(SComponent) aComponent = firstSO->GetFatherComponent();
114 QString type = aComponent->ComponentDataType().c_str();
115 if ( type == "SMESH" )
121 // browse through all GEOM data tree
122 _PTR(ChildIterator) it ( study->NewChildIterator( comp ) );
123 for ( it->InitEx( true ); it->More(); it->Next() ) {
124 _PTR(SObject) child( it->Value() );
125 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject( child );
126 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
127 if( CORBA::is_nil( geomObj ) )
130 GEOM::ListOfGBO_var list = geomObj->GetDependency();
131 if( list->length() == 0 )
134 for( int i = 0; i < list->length(); i++ ) {
135 bool depends = false;
136 bool deleted = false;
137 QMap<QString, GEOM::GEOM_BaseObject_var>::Iterator git;
138 for ( git = gobjects.begin(); git != gobjects.end() && ( !depends || !deleted ); ++git ) {
139 depends = depends || list[i]->_is_equivalent( *git );
140 deleted = deleted || git.key() == child->GetID().c_str() ;//geomObj->_is_equivalent( *git );
142 if ( depends && !deleted )
149 //=======================================================================
150 // function : getGeomChildrenAndFolders
151 // purpose : Get direct (1-level) GEOM objects under each folder, sub-folder, etc. and these folders itself
152 //=======================================================================
153 static void getGeomChildrenAndFolders( _PTR(SObject) theSO,
154 QMap<QString,QString>& geomObjList,
155 QMap<QString,QString>& folderList ) {
156 if ( !theSO ) return;
157 _PTR(Study) aStudy = theSO->GetStudy();
158 if ( !aStudy ) return;
159 _PTR(UseCaseBuilder) aUseCaseBuilder = aStudy->GetUseCaseBuilder();
161 bool isFolder = false;
162 _PTR(GenericAttribute) anAttr;
163 if ( theSO->FindAttribute(anAttr, "AttributeLocalID") ) {
164 _PTR(AttributeLocalID) aLocalID( anAttr );
165 isFolder = aLocalID->Value() == 999;
167 QString anEntry = theSO->GetID().c_str();
168 QString aName = theSO->GetName().c_str();
170 folderList.insert( anEntry, aName );
171 _PTR(UseCaseIterator) ucit ( aUseCaseBuilder->GetUseCaseIterator( theSO ) );
172 for ( ucit->Init( false ); ucit->More(); ucit->Next() ) {
173 getGeomChildrenAndFolders( ucit->Value(), geomObjList, folderList );
176 geomObjList.insert( anEntry, aName );
180 //=======================================================================
181 // function : GEOMToolsGUI()
182 // purpose : Constructor
183 //=======================================================================
184 GEOMToolsGUI::GEOMToolsGUI( GeometryGUI* parent )
189 //=======================================================================
190 // function : ~GEOMToolsGUI()
191 // purpose : Destructor
192 //=======================================================================
193 GEOMToolsGUI::~GEOMToolsGUI()
197 //=======================================================================
198 // function : OnGUIEvent()
200 //=======================================================================
201 bool GEOMToolsGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent)
203 getGeometryGUI()->EmitSignalDeactivateDialog();
205 switch ( theCommandID ) {
206 case GEOMOp::OpDelete: // EDIT - DELETE
209 case GEOMOp::OpCheckGeom: // TOOLS - CHECK GEOMETRY
212 case GEOMOp::OpSelectVertex: // POPUP - SELECT ONLY - VERTEX
213 OnSelectOnly( GEOM_POINT );
215 case GEOMOp::OpSelectEdge: // POPUP - SELECT ONLY - EDGE
216 OnSelectOnly( GEOM_EDGE );
218 case GEOMOp::OpSelectWire: // POPUP - SELECT ONLY - WIRE
219 OnSelectOnly( GEOM_WIRE );
221 case GEOMOp::OpSelectFace: // POPUP - SELECT ONLY - FACE
222 OnSelectOnly( GEOM_FACE );
224 case GEOMOp::OpSelectShell: // POPUP - SELECT ONLY - SHELL
225 OnSelectOnly( GEOM_SHELL );
227 case GEOMOp::OpSelectSolid: // POPUP - SELECT ONLY - SOLID
228 OnSelectOnly( GEOM_SOLID );
230 case GEOMOp::OpSelectCompound: // POPUP - SELECT ONLY - COMPOUND
231 OnSelectOnly( GEOM_COMPOUND );
233 case GEOMOp::OpSelectAll: // POPUP - SELECT ONLY - SELECT ALL
234 OnSelectOnly( GEOM_ALLOBJECTS );
236 case GEOMOp::OpDeflection: // POPUP - DEFLECTION ANGLE
239 case GEOMOp::OpColor: // POPUP - COLOR
242 case GEOMOp::OpSetTexture: // POPUP - TEXTURE
245 case GEOMOp::OpTransparency: // POPUP - TRANSPARENCY
248 case GEOMOp::OpIncrTransparency: // SHORTCUT - INCREASE TRANSPARENCY
249 OnChangeTransparency( true );
251 case GEOMOp::OpDecrTransparency: // SHORTCUT - DECREASE TRANSPARENCY
252 OnChangeTransparency( false );
254 case GEOMOp::OpIsos: // POPUP - ISOS
257 case GEOMOp::OpIncrNbIsos: // SHORTCUT - INCREASE NB ISOLINES
260 case GEOMOp::OpDecrNbIsos: // SHORTCUT - DECREASE NB ISOLINES
263 case GEOMOp::OpMaterialProperties: // POPUP - MATERIAL PROPERTIES
264 OnMaterialProperties();
266 case GEOMOp::OpPredefMaterCustom: // POPUP - MATERIAL PROPERTIES - CUSTOM...
267 OnMaterialProperties();
269 case GEOMOp::OpMaterialsLibrary: // POPUP MENU - MATERIAL PROPERTIES
270 OnMaterialsLibrary();
272 case GEOMOp::OpAutoColor: // POPUP - AUTO COLOR
275 case GEOMOp::OpNoAutoColor: // POPUP - DISABLE AUTO COLOR
276 OnDisableAutoColor();
278 case GEOMOp::OpDiscloseChildren: // POPUP - SHOW CHILDREN
279 case GEOMOp::OpConcealChildren: // POPUP - HIDE CHILDREN
280 OnDiscloseConcealChildren( theCommandID == GEOMOp::OpDiscloseChildren );
282 case GEOMOp::OpPointMarker: // POPUP - POINT MARKER
285 case GEOMOp::OpUnpublishObject:// POPUP - UNPUBLISH
288 case GEOMOp::OpPublishObject:// GEOM ROOT OBJECT - POPUP - PUBLISH
291 case GEOMOp::OpEdgeWidth:
294 case GEOMOp::OpIsosWidth:
297 case GEOMOp::OpBringToFront:
300 case GEOMOp::OpClsBringToFront:
303 case GEOMOp::OpCreateFolder:
306 case GEOMOp::OpSortChildren:
309 case GEOMOp::OpShowDependencyTree:
310 OnShowDependencyTree();
312 case GEOMOp::OpReduceStudy:
316 SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
322 //=======================================================================
323 // function : OnGUIEvent()
325 //=======================================================================
326 bool GEOMToolsGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent, const QVariant& theParam )
328 getGeometryGUI()->EmitSignalDeactivateDialog();
330 switch ( theCommandID ) {
331 case GEOMOp::OpPredefMaterial: // POPUP MENU - MATERIAL PROPERTIES - <SOME MATERIAL>
332 OnSetMaterial( theParam );
335 SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
341 //===============================================================================
342 // function : OnEditDelete()
344 //===============================================================================
345 void GEOMToolsGUI::OnEditDelete()
347 SALOME_ListIO selected;
348 SalomeApp_Application* app =
349 dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
353 LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
354 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
355 if ( !aSelMgr || !appStudy )
359 aSelMgr->selectedObjects( selected, "ObjectBrowser", false );
360 if ( selected.IsEmpty() )
363 _PTR(Study) aStudy = appStudy->studyDS();
364 _PTR(UseCaseBuilder) aUseCaseBuilder = aStudy->GetUseCaseBuilder();
366 // check if study is locked
367 if ( _PTR(AttributeStudyProperties)( aStudy->GetProperties() )->IsLocked() ) {
368 SUIT_MessageBox::warning( app->desktop(),
370 tr("WRN_STUDY_LOCKED") );
371 return; // study is locked
374 // get GEOM component
375 CORBA::String_var geomIOR = app->orb()->object_to_string( GeometryGUI::GetGeomGen() );
376 QString geomComp = getParentComponent( aStudy->FindObjectIOR( geomIOR.in() ) );
378 // check each selected object: if belongs to GEOM, if not reference...
379 QMap<QString,QString> toBeDeleted;
380 QMap<QString,QString> allDeleted;
381 QMap<QString,QString> toBeDelFolders;
382 bool isComponentSelected = false;
384 for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
385 Handle(SALOME_InteractiveObject) anIObject = It.Value();
386 if ( !anIObject->hasEntry() )
387 continue; // invalid object
389 QString entry = anIObject->getEntry();
390 _PTR(SObject) obj = aStudy->FindObjectID( entry.toLatin1().data() );
391 // check parent component
392 QString parentComp = getParentComponent( obj );
393 if ( parentComp != geomComp ) {
394 SUIT_MessageBox::warning( app->desktop(),
395 QObject::tr("ERR_ERROR"),
396 QObject::tr("NON_GEOM_OBJECTS_SELECTED").arg( getGeometryGUI()->moduleName() ) );
397 return; // not GEOM object selected
400 ///////////////////////////////////////////////////////
401 // if GEOM component is selected, so skip other checks
402 if ( isComponentSelected ) continue;
403 ///////////////////////////////////////////////////////
405 // check if object is reference
406 _PTR(SObject) refobj;
407 if ( obj && obj->ReferencedObject( refobj ) ) {
408 // get the main object by reference IPAL 21354
410 entry = obj->GetID().c_str();
413 QString aName = obj->GetName().c_str();
414 if ( entry == geomComp ) {
415 // GEOM component is selected, skip other checks
416 isComponentSelected = true;
419 // all sub-objects of folder have to be deleted
420 getGeomChildrenAndFolders( obj, toBeDeleted, toBeDelFolders );
421 allDeleted.insert( entry, aName ); // skip GEOM component
422 // browse through all children recursively
423 _PTR(UseCaseIterator) it ( aUseCaseBuilder->GetUseCaseIterator( obj ) );
424 for ( it->Init( true ); it->More(); it->Next() ) {
425 _PTR(SObject) child( it->Value() );
426 if ( child && child->ReferencedObject( refobj ) )
427 continue; // skip references
428 aName = child->GetName().c_str();
429 if ( !aName.isEmpty() )
430 allDeleted.insert( child->GetID().c_str(), aName );
434 // is there is anything to delete?
435 if ( !isComponentSelected && allDeleted.count() <= 0 )
436 return; // nothing to delete
438 // show confirmation dialog box
439 GEOMToolsGUI_DeleteDlg dlg( app->desktop(), allDeleted, isComponentSelected );
441 return; // operation is cancelled by user
443 // get currently opened views
444 QList<SALOME_View*> views;
446 ViewManagerList vmans = app->viewManagers();
447 SUIT_ViewManager* vman;
448 foreach ( vman, vmans ) {
449 SUIT_ViewModel* vmod = vman->getViewModel();
450 view = dynamic_cast<SALOME_View*> ( vmod ); // must work for OCC and VTK views
452 views.append( view );
455 _PTR(StudyBuilder) aStudyBuilder (aStudy->NewBuilder());
456 GEOM_Displayer* disp = new GEOM_Displayer( appStudy );
458 if ( isComponentSelected ) {
459 // GEOM component is selected: delete all objects recursively
460 _PTR(SObject) comp = aStudy->FindObjectID( geomComp.toLatin1().data() );
463 _PTR(ChildIterator) it ( aStudy->NewChildIterator( comp ) );
464 // remove top-level objects only
465 for ( it->InitEx( false ); it->More(); it->Next() ) {
466 _PTR(SObject) child( it->Value() );
467 // remove object from GEOM engine
468 removeObjectWithChildren( child, aStudy, views, disp );
469 // remove object from study
470 aStudyBuilder->RemoveObjectWithChildren( child );
471 // remove object from use case tree
472 aUseCaseBuilder->Remove( child );
476 // GEOM component is not selected: check if selected objects are in use
477 if ( inUse( aStudy, geomComp, allDeleted ) &&
478 SUIT_MessageBox::question( app->desktop(),
479 QObject::tr("WRN_WARNING"),
480 QObject::tr("DEP_OBJECT"),
481 SUIT_MessageBox::Yes | SUIT_MessageBox::No,
482 SUIT_MessageBox::No ) != SUIT_MessageBox::Yes ) {
483 return; // object(s) in use
485 // ... and then delete all objects
486 QMap<QString, QString>::Iterator it;
487 for ( it = toBeDeleted.begin(); it != toBeDeleted.end(); ++it ) {
488 _PTR(SObject) obj ( aStudy->FindObjectID( it.key().toLatin1().data() ) );
489 // remove object from GEOM engine
490 removeObjectWithChildren( obj, aStudy, views, disp );
491 // remove objects from study
492 aStudyBuilder->RemoveObjectWithChildren( obj );
493 // remove object from use case tree
494 aUseCaseBuilder->Remove( obj );
496 // ... and then delete all folders
497 for ( it = toBeDelFolders.begin(); it != toBeDelFolders.end(); ++it ) {
498 _PTR(SObject) obj ( aStudy->FindObjectID( it.key().toLatin1().data() ) );
499 // remove object from GEOM engine
500 removeObjectWithChildren( obj, aStudy, views, disp );
501 // remove objects from study
502 aStudyBuilder->RemoveObjectWithChildren( obj );
503 // remove object from use case tree
504 aUseCaseBuilder->Remove( obj );
509 aSelMgr->setSelectedObjects( selected );
510 getGeometryGUI()->updateObjBrowser();
511 app->updateActions(); //SRN: To update a Save button in the toolbar
514 //=====================================================================================
515 // function : RemoveObjectWithChildren
516 // purpose : used by OnEditDelete() method
517 //=====================================================================================
518 void GEOMToolsGUI::removeObjectWithChildren(_PTR(SObject) obj,
520 QList<SALOME_View*> views,
521 GEOM_Displayer* disp)
523 // iterate through all children of obj
524 for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
525 // for (_PTR(UseCaseIterator) it (aStudy->GetUseCaseBuilder()->GetUseCaseIterator(obj)); it->More(); it->Next()) {
526 _PTR(SObject) child (it->Value());
527 removeObjectWithChildren(child, aStudy, views, disp);
530 // erase object and remove it from engine
531 _PTR(GenericAttribute) anAttr;
532 if (obj->FindAttribute(anAttr, "AttributeIOR")) {
533 _PTR(AttributeIOR) anIOR (anAttr);
535 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
537 // Delete shape in Client
538 const TCollection_AsciiString ASCIor ((char*)anIOR->Value().c_str());
539 getGeometryGUI()->GetShapeReader().RemoveShapeFromBuffer(ASCIor);
541 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(obj);
542 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
543 if (!CORBA::is_nil(geomObj)) {
545 //Remove visual properties of the object
546 appStudy->removeObjectProperties(obj->GetID().c_str());
548 // Erase graphical object
549 QListIterator<SALOME_View*> it( views );
550 while ( it.hasNext() )
551 if ( SALOME_View* view = it.next() )
552 disp->Erase(geomObj, true, true, view);
554 // Remove object from Engine
555 // We can't directly remove object from engine. All we can do is to unpublish the object
556 // from the study. Another client could be using the object.
557 // Unpublishing is done just after in aStudyBuilder->RemoveObjectWithChildren( child );
558 //GeometryGUI::GetGeomGen()->RemoveObject( geomObj );
563 //=================================================================================
564 // function : deactivate()
565 // purpose : Called when GEOM component is deactivated
566 //=================================================================================
567 void GEOMToolsGUI::deactivate()
569 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
571 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
572 GEOM_Displayer aDisp (appStudy);
573 aDisp.GlobalSelection();
574 getGeometryGUI()->setLocalSelectionMode(GEOM_ALLOBJECTS);
578 //=====================================================================================
580 //=====================================================================================
584 __declspec( dllexport )
586 GEOMGUI* GetLibGUI( GeometryGUI* parent )
588 return new GEOMToolsGUI( parent );