1 // Copyright (C) 2007-2016 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 #ifndef DISABLE_PYCONSOLE
210 case GEOMOp::OpCheckGeom: // TOOLS - CHECK GEOMETRY
214 case GEOMOp::OpSelectVertex: // POPUP - SELECT ONLY - VERTEX
215 OnSelectOnly( GEOM_POINT );
217 case GEOMOp::OpSelectEdge: // POPUP - SELECT ONLY - EDGE
218 OnSelectOnly( GEOM_EDGE );
220 case GEOMOp::OpSelectWire: // POPUP - SELECT ONLY - WIRE
221 OnSelectOnly( GEOM_WIRE );
223 case GEOMOp::OpSelectFace: // POPUP - SELECT ONLY - FACE
224 OnSelectOnly( GEOM_FACE );
226 case GEOMOp::OpSelectShell: // POPUP - SELECT ONLY - SHELL
227 OnSelectOnly( GEOM_SHELL );
229 case GEOMOp::OpSelectSolid: // POPUP - SELECT ONLY - SOLID
230 OnSelectOnly( GEOM_SOLID );
232 case GEOMOp::OpSelectCompound: // POPUP - SELECT ONLY - COMPOUND
233 OnSelectOnly( GEOM_COMPOUND );
235 case GEOMOp::OpSelectAll: // POPUP - SELECT ONLY - SELECT ALL
236 OnSelectOnly( GEOM_ALLOBJECTS );
238 case GEOMOp::OpDeflection: // POPUP - DEFLECTION ANGLE
241 case GEOMOp::OpColor: // POPUP - COLOR
244 case GEOMOp::OpSetTexture: // POPUP - TEXTURE
247 case GEOMOp::OpTransparency: // POPUP - TRANSPARENCY
250 case GEOMOp::OpIncrTransparency: // SHORTCUT - INCREASE TRANSPARENCY
251 OnChangeTransparency( true );
253 case GEOMOp::OpDecrTransparency: // SHORTCUT - DECREASE TRANSPARENCY
254 OnChangeTransparency( false );
256 case GEOMOp::OpIsos: // POPUP - ISOS
259 case GEOMOp::OpIncrNbIsos: // SHORTCUT - INCREASE NB ISOLINES
262 case GEOMOp::OpDecrNbIsos: // SHORTCUT - DECREASE NB ISOLINES
265 case GEOMOp::OpMaterialProperties: // POPUP - MATERIAL PROPERTIES
266 OnMaterialProperties();
268 case GEOMOp::OpPredefMaterCustom: // POPUP - MATERIAL PROPERTIES - CUSTOM...
269 OnMaterialProperties();
271 case GEOMOp::OpMaterialsLibrary: // POPUP MENU - MATERIAL PROPERTIES
272 OnMaterialsLibrary();
274 case GEOMOp::OpAutoColor: // POPUP - AUTO COLOR
277 case GEOMOp::OpNoAutoColor: // POPUP - DISABLE AUTO COLOR
278 OnDisableAutoColor();
280 case GEOMOp::OpDiscloseChildren: // POPUP - SHOW CHILDREN
281 case GEOMOp::OpConcealChildren: // POPUP - HIDE CHILDREN
282 OnDiscloseConcealChildren( theCommandID == GEOMOp::OpDiscloseChildren );
284 case GEOMOp::OpPointMarker: // POPUP - POINT MARKER
287 case GEOMOp::OpUnpublishObject:// POPUP - UNPUBLISH
290 case GEOMOp::OpPublishObject:// GEOM ROOT OBJECT - POPUP - PUBLISH
293 case GEOMOp::OpEdgeWidth:
296 case GEOMOp::OpIsosWidth:
299 case GEOMOp::OpBringToFront:
302 case GEOMOp::OpClsBringToFront:
305 case GEOMOp::OpCreateFolder:
308 case GEOMOp::OpSortChildren:
311 #ifndef DISABLE_GRAPHICSVIEW
312 case GEOMOp::OpShowDependencyTree:
313 OnShowDependencyTree();
316 case GEOMOp::OpReduceStudy:
320 SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
326 //=======================================================================
327 // function : OnGUIEvent()
329 //=======================================================================
330 bool GEOMToolsGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent, const QVariant& theParam )
332 getGeometryGUI()->EmitSignalDeactivateDialog();
334 switch ( theCommandID ) {
335 case GEOMOp::OpPredefMaterial: // POPUP MENU - MATERIAL PROPERTIES - <SOME MATERIAL>
336 OnSetMaterial( theParam );
339 SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
345 //===============================================================================
346 // function : OnEditDelete()
348 //===============================================================================
349 void GEOMToolsGUI::OnEditDelete()
351 SALOME_ListIO selected;
352 SalomeApp_Application* app =
353 dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
357 LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
358 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
359 if ( !aSelMgr || !appStudy )
363 aSelMgr->selectedObjects( selected, "ObjectBrowser", false );
364 if ( selected.IsEmpty() )
367 _PTR(Study) aStudy = appStudy->studyDS();
368 _PTR(UseCaseBuilder) aUseCaseBuilder = aStudy->GetUseCaseBuilder();
370 // check if study is locked
371 if ( _PTR(AttributeStudyProperties)( aStudy->GetProperties() )->IsLocked() ) {
372 SUIT_MessageBox::warning( app->desktop(),
374 tr("WRN_STUDY_LOCKED") );
375 return; // study is locked
378 // get GEOM component
379 CORBA::String_var geomIOR = app->orb()->object_to_string( GeometryGUI::GetGeomGen() );
380 QString geomComp = getParentComponent( aStudy->FindObjectIOR( geomIOR.in() ) );
382 // check each selected object: if belongs to GEOM, if not reference...
383 QMap<QString,QString> toBeDeleted;
384 QMap<QString,QString> allDeleted;
385 QMap<QString,QString> toBeDelFolders;
386 bool isComponentSelected = false;
388 for ( SALOME_ListIteratorOfListIO It( selected ); It.More(); It.Next() ) {
389 Handle(SALOME_InteractiveObject) anIObject = It.Value();
390 if ( !anIObject->hasEntry() )
391 continue; // invalid object
393 QString entry = anIObject->getEntry();
394 _PTR(SObject) obj = aStudy->FindObjectID( entry.toLatin1().data() );
395 // check parent component
396 QString parentComp = getParentComponent( obj );
397 if ( parentComp != geomComp ) {
398 SUIT_MessageBox::warning( app->desktop(),
399 QObject::tr("ERR_ERROR"),
400 QObject::tr("NON_GEOM_OBJECTS_SELECTED").arg( getGeometryGUI()->moduleName() ) );
401 return; // not GEOM object selected
404 ///////////////////////////////////////////////////////
405 // if GEOM component is selected, so skip other checks
406 if ( isComponentSelected ) continue;
407 ///////////////////////////////////////////////////////
409 // check if object is reference
410 _PTR(SObject) refobj;
411 if ( obj && obj->ReferencedObject( refobj ) ) {
412 // get the main object by reference IPAL 21354
414 entry = obj->GetID().c_str();
417 QString aName = obj->GetName().c_str();
418 if ( entry == geomComp ) {
419 // GEOM component is selected, skip other checks
420 isComponentSelected = true;
423 // all sub-objects of folder have to be deleted
424 getGeomChildrenAndFolders( obj, toBeDeleted, toBeDelFolders );
425 allDeleted.insert( entry, aName ); // skip GEOM component
426 // browse through all children recursively
427 _PTR(UseCaseIterator) it ( aUseCaseBuilder->GetUseCaseIterator( obj ) );
428 for ( it->Init( true ); it->More(); it->Next() ) {
429 _PTR(SObject) child( it->Value() );
430 if ( child && child->ReferencedObject( refobj ) )
431 continue; // skip references
432 aName = child->GetName().c_str();
433 if ( !aName.isEmpty() )
434 allDeleted.insert( child->GetID().c_str(), aName );
438 // is there is anything to delete?
439 if ( !isComponentSelected && allDeleted.count() <= 0 )
440 return; // nothing to delete
442 // show confirmation dialog box
443 GEOMToolsGUI_DeleteDlg dlg( app->desktop(), allDeleted, isComponentSelected );
445 return; // operation is cancelled by user
447 // get currently opened views
448 QList<SALOME_View*> views;
450 ViewManagerList vmans = app->viewManagers();
451 SUIT_ViewManager* vman;
452 foreach ( vman, vmans ) {
453 SUIT_ViewModel* vmod = vman->getViewModel();
454 view = dynamic_cast<SALOME_View*> ( vmod ); // must work for OCC and VTK views
456 views.append( view );
459 _PTR(StudyBuilder) aStudyBuilder (aStudy->NewBuilder());
460 GEOM_Displayer* disp = new GEOM_Displayer( appStudy );
462 if ( isComponentSelected ) {
463 // GEOM component is selected: delete all objects recursively
464 _PTR(SObject) comp = aStudy->FindObjectID( geomComp.toLatin1().data() );
467 _PTR(ChildIterator) it ( aStudy->NewChildIterator( comp ) );
468 // remove top-level objects only
469 for ( it->InitEx( false ); it->More(); it->Next() ) {
470 _PTR(SObject) child( it->Value() );
471 // remove object from GEOM engine
472 removeObjectWithChildren( child, aStudy, views, disp );
473 // remove object from study
474 aStudyBuilder->RemoveObjectWithChildren( child );
475 // remove object from use case tree
476 aUseCaseBuilder->Remove( child );
480 // GEOM component is not selected: check if selected objects are in use
481 if ( inUse( aStudy, geomComp, allDeleted ) &&
482 SUIT_MessageBox::question( app->desktop(),
483 QObject::tr("WRN_WARNING"),
484 QObject::tr("DEP_OBJECT"),
485 SUIT_MessageBox::Yes | SUIT_MessageBox::No,
486 SUIT_MessageBox::No ) != SUIT_MessageBox::Yes ) {
487 return; // object(s) in use
489 // ... and then delete all objects
490 QMap<QString, QString>::Iterator it;
491 for ( it = toBeDeleted.begin(); it != toBeDeleted.end(); ++it ) {
492 _PTR(SObject) obj ( aStudy->FindObjectID( it.key().toLatin1().data() ) );
493 // remove object from GEOM engine
494 removeObjectWithChildren( obj, aStudy, views, disp );
495 // remove objects from study
496 aStudyBuilder->RemoveObjectWithChildren( obj );
497 // remove object from use case tree
498 aUseCaseBuilder->Remove( obj );
500 // ... and then delete all folders
501 for ( it = toBeDelFolders.begin(); it != toBeDelFolders.end(); ++it ) {
502 _PTR(SObject) obj ( aStudy->FindObjectID( it.key().toLatin1().data() ) );
503 // remove object from GEOM engine
504 removeObjectWithChildren( obj, aStudy, views, disp );
505 // remove objects from study
506 aStudyBuilder->RemoveObjectWithChildren( obj );
507 // remove object from use case tree
508 aUseCaseBuilder->Remove( obj );
513 aSelMgr->setSelectedObjects( selected );
514 getGeometryGUI()->updateObjBrowser();
515 app->updateActions(); //SRN: To update a Save button in the toolbar
518 //=====================================================================================
519 // function : RemoveObjectWithChildren
520 // purpose : used by OnEditDelete() method
521 //=====================================================================================
522 void GEOMToolsGUI::removeObjectWithChildren(_PTR(SObject) obj,
524 QList<SALOME_View*> views,
525 GEOM_Displayer* disp)
527 // iterate through all children of obj
528 for (_PTR(ChildIterator) it (aStudy->NewChildIterator(obj)); it->More(); it->Next()) {
529 // for (_PTR(UseCaseIterator) it (aStudy->GetUseCaseBuilder()->GetUseCaseIterator(obj)); it->More(); it->Next()) {
530 _PTR(SObject) child (it->Value());
531 removeObjectWithChildren(child, aStudy, views, disp);
534 // erase object and remove it from engine
535 _PTR(GenericAttribute) anAttr;
536 if (obj->FindAttribute(anAttr, "AttributeIOR")) {
537 _PTR(AttributeIOR) anIOR (anAttr);
539 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
541 // Delete shape in Client
542 const TCollection_AsciiString ASCIor ((char*)anIOR->Value().c_str());
543 getGeometryGUI()->GetShapeReader().RemoveShapeFromBuffer(ASCIor);
545 CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject(obj);
546 GEOM::GEOM_Object_var geomObj = GEOM::GEOM_Object::_narrow( corbaObj );
547 if (!CORBA::is_nil(geomObj)) {
549 //Remove visual properties of the object
550 appStudy->removeObjectProperties(obj->GetID().c_str());
552 // Erase graphical object
553 QListIterator<SALOME_View*> it( views );
554 while ( it.hasNext() )
555 if ( SALOME_View* view = it.next() )
556 disp->Erase(geomObj, true, true, view);
558 // Remove object from Engine
559 // We can't directly remove object from engine. All we can do is to unpublish the object
560 // from the study. Another client could be using the object.
561 // Unpublishing is done just after in aStudyBuilder->RemoveObjectWithChildren( child );
562 //GeometryGUI::GetGeomGen()->RemoveObject( geomObj );
567 //=================================================================================
568 // function : deactivate()
569 // purpose : Called when GEOM component is deactivated
570 //=================================================================================
571 void GEOMToolsGUI::deactivate()
573 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
575 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
576 GEOM_Displayer aDisp (appStudy);
577 aDisp.GlobalSelection();
578 getGeometryGUI()->setLocalSelectionMode(GEOM_ALLOBJECTS);
582 //=====================================================================================
584 //=====================================================================================
588 __declspec( dllexport )
590 GEOMGUI* GetLibGUI( GeometryGUI* parent )
592 return new GEOMToolsGUI( parent );