1 // Copyright (C) 2007-2008 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.
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 // SMESH SMESHGUI : GUI for SMESH component
23 // File : SMESHGUI_VTKUtils.cxx
24 // Author : Open CASCADE S.A.S.
27 #include "SMESHGUI_VTKUtils.h"
30 #include "SMESHGUI_Utils.h"
31 #include "SMESHGUI_Filter.h"
33 #include <SMESH_Actor.h>
34 #include <SMESH_ActorUtils.h>
35 #include <SMESH_ObjectDef.h>
36 #include <SMDS_Mesh.hxx>
38 // SALOME GUI includes
39 #include <SUIT_Desktop.h>
40 #include <SUIT_Session.h>
41 #include <SUIT_MessageBox.h>
42 #include <SUIT_ViewManager.h>
43 #include <SUIT_ResourceMgr.h>
45 #include <SALOME_ListIO.hxx>
46 #include <SALOME_ListIteratorOfListIO.hxx>
48 #include <SVTK_Selector.h>
49 #include <SVTK_ViewModel.h>
50 #include <SVTK_ViewWindow.h>
52 #include <VTKViewer_Algorithm.h>
54 #include <LightApp_SelectionMgr.h>
55 #include <SalomeApp_Application.h>
56 #include <SalomeApp_Study.h>
58 // SALOME KERNEL includes
59 #include <utilities.h>
62 #include <SALOMEconfig.h>
63 #include CORBA_CLIENT_HEADER(SMESH_Mesh)
64 #include CORBA_CLIENT_HEADER(SMESH_Group)
67 #include <vtkRenderer.h>
68 #include <vtkActorCollection.h>
69 #include <vtkUnstructuredGrid.h>
72 #include <TColStd_IndexedMapOfInteger.hxx>
73 #include <Standard_ErrorHandler.hxx>
77 typedef std::map<TKeyOfVisualObj,TVisualObjPtr> TVisualObjCont;
78 static TVisualObjCont VISUAL_OBJ_CONT;
80 //=============================================================================
82 * \brief Allocate some memory at construction and release it at destruction.
83 * Is used to be able to continue working after mesh generation or visualization
84 * break due to lack of memory
86 //=============================================================================
91 MemoryReserve(): myBuf( new char[1024*1024*1] ){} // 1M
92 void Free() { if (myBuf) { delete [] myBuf; myBuf = 0; }}
93 ~MemoryReserve() { Free(); }
95 static MemoryReserve* theVISU_MemoryReserve = new MemoryReserve;
97 //================================================================================
99 * \brief Remove VisualObj and its actor from all views
101 //================================================================================
103 void RemoveVisualObjectWithActors( const char* theEntry )
105 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
106 ( SUIT_Session::session()->activeApplication() );
107 SUIT_ViewManager* aViewManager =
108 app ? app->getViewManager(SVTK_Viewer::Type(), true) : 0;
109 if ( aViewManager ) {
110 QVector<SUIT_ViewWindow*> views = aViewManager->getViews();
111 for ( int iV = 0; iV < views.count(); ++iV ) {
112 if ( SMESH_Actor* actor = FindActorByEntry( views[iV], theEntry)) {
113 if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(views[iV]))
114 vtkWnd->RemoveActor(actor);
118 int aStudyId = aViewManager->study()->id();
119 TVisualObjCont::key_type aKey(aStudyId,theEntry);
120 TVisualObjCont::iterator anIter = VISUAL_OBJ_CONT.find(aKey);
121 if(anIter != VISUAL_OBJ_CONT.end()) {
122 // for unknown reason, object destructor is not called, so clear object manually
123 anIter->second->GetUnstructuredGrid()->SetCells(0,0,0);
124 anIter->second->GetUnstructuredGrid()->SetPoints(0);
126 VISUAL_OBJ_CONT.erase(aKey);
129 //================================================================================
131 * \brief Remove all VisualObjs and their actors from all views
133 //================================================================================
135 void RemoveAllObjectsWithActors()
137 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
138 ( SUIT_Session::session()->activeApplication() );
140 ViewManagerList viewMgrs = app->viewManagers();
141 for ( int iM = 0; iM < viewMgrs.count(); ++iM ) {
142 SUIT_ViewManager* aViewManager = viewMgrs.at( iM );
143 if ( aViewManager && aViewManager->getType() == SVTK_Viewer::Type()) {
144 QVector<SUIT_ViewWindow*> views = aViewManager->getViews();
145 for ( int iV = 0; iV < views.count(); ++iV ) {
146 if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(views[iV])) {
147 vtkRenderer *aRenderer = vtkWnd->getRenderer();
148 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
149 vtkActorCollection *actors = aCopy.GetActors();
150 for (int i = 0; i < actors->GetNumberOfItems(); ++i ) {
151 // size of actors changes inside the loop
152 if (SMESH_Actor *actor = dynamic_cast<SMESH_Actor*>(actors->GetItemAsObject(i)))
154 vtkWnd->RemoveActor(actor);
162 TVisualObjCont::iterator anIter = VISUAL_OBJ_CONT.begin();
163 for ( ; anIter != VISUAL_OBJ_CONT.end(); ++anIter ) {
164 // for unknown reason, object destructor is not called, so clear object manually
165 anIter->second->GetUnstructuredGrid()->SetCells(0,0,0);
166 anIter->second->GetUnstructuredGrid()->SetPoints(0);
168 VISUAL_OBJ_CONT.clear();
171 //================================================================================
173 * \brief Remove all VisualObjs of a study
175 //================================================================================
177 void RemoveVisuData(int studyID)
179 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
180 ( SUIT_Session::session()->activeApplication() );
182 ViewManagerList viewMgrs = app->viewManagers();
183 for ( int iM = 0; iM < viewMgrs.count(); ++iM ) {
184 SUIT_ViewManager* aViewManager = viewMgrs.at( iM );
185 if ( aViewManager && aViewManager->getType() == SVTK_Viewer::Type() &&
186 aViewManager->study()->id() == studyID ) {
187 QVector<SUIT_ViewWindow*> views = aViewManager->getViews();
188 for ( int iV = 0; iV < views.count(); ++iV ) {
189 if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(views[iV])) {
190 vtkRenderer *aRenderer = vtkWnd->getRenderer();
191 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
192 vtkActorCollection *actors = aCopy.GetActors();
193 for (int i = 0; i < actors->GetNumberOfItems(); ++i ) {
194 // size of actors changes inside the loop
195 if(SMESH_Actor *actor = dynamic_cast<SMESH_Actor*>(actors->GetItemAsObject(i)))
197 vtkWnd->RemoveActor(actor);
205 TVisualObjCont::iterator anIter = VISUAL_OBJ_CONT.begin();
206 for ( ; anIter != VISUAL_OBJ_CONT.end(); ++anIter ) {
207 int curId = anIter->first.first;
208 if ( curId == studyID ) {
209 // for unknown reason, object destructor is not called, so clear object manually
210 anIter->second->GetUnstructuredGrid()->SetCells(0,0,0);
211 anIter->second->GetUnstructuredGrid()->SetPoints(0);
212 VISUAL_OBJ_CONT.erase( anIter-- ); // dercement occures before erase()
217 //================================================================================
219 * \brief Notify the user on problems during visualization
221 //================================================================================
223 void OnVisuException()
226 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
229 // PAL16774 (Crash after display of many groups). Salome sometimes crashes just
230 // after or at showing this message, so we do an additional check of available memory
231 // char* buf = new char[100*1024];
233 SUIT_MessageBox::warning(SMESHGUI::desktop(), QObject::tr("SMESH_WRN_WARNING"),
234 QObject::tr("SMESH_VISU_PROBLEM"));
236 // no more memory at all: last resort
237 MESSAGE_BEGIN ( "SMESHGUI_VTKUtils::OnVisuException(), exception even at showing a message!!!" <<
238 std::endl << "Try to remove all visual data..." );
239 if (theVISU_MemoryReserve) {
240 delete theVISU_MemoryReserve;
241 theVISU_MemoryReserve = 0;
243 RemoveAllObjectsWithActors();
244 SUIT_MessageBox::warning(SMESHGUI::desktop(), QObject::tr("SMESH_WRN_WARNING"),
245 QObject::tr("SMESH_VISU_PROBLEM_CLEAR"));
246 MESSAGE_END ( "...done" );
249 //================================================================================
251 * \brief Returns an updated visual object
253 //================================================================================
255 TVisualObjPtr GetVisualObj(int theStudyId, const char* theEntry){
256 TVisualObjPtr aVisualObj;
257 TVisualObjCont::key_type aKey(theStudyId,theEntry);
259 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
262 TVisualObjCont::iterator anIter = VISUAL_OBJ_CONT.find(aKey);
263 if(anIter != VISUAL_OBJ_CONT.end()){
264 aVisualObj = anIter->second;
266 SalomeApp_Application* app =
267 dynamic_cast<SalomeApp_Application*>( SMESHGUI::activeStudy()->application() );
268 _PTR(Study) aStudy = SMESHGUI::activeStudy()->studyDS();
269 _PTR(SObject) aSObj = aStudy->FindObjectID(theEntry);
271 _PTR(GenericAttribute) anAttr;
272 if(aSObj->FindAttribute(anAttr,"AttributeIOR")){
273 _PTR(AttributeIOR) anIOR = anAttr;
274 CORBA::String_var aVal = anIOR->Value().c_str();
275 CORBA::Object_var anObj = app->orb()->string_to_object( aVal.in() );
276 if(!CORBA::is_nil(anObj)){
277 //Try narrow to SMESH_Mesh interface
278 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(anObj);
279 if(!aMesh->_is_nil()){
280 aVisualObj.reset(new SMESH_MeshObj(aMesh));
281 TVisualObjCont::value_type aValue(aKey,aVisualObj);
282 VISUAL_OBJ_CONT.insert(aValue);
284 //Try narrow to SMESH_Group interface
285 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(anObj);
286 if(!aGroup->_is_nil()){
287 _PTR(SObject) aFatherSObj = aSObj->GetFather();
288 if(!aFatherSObj) return aVisualObj;
289 aFatherSObj = aFatherSObj->GetFather();
290 if(!aFatherSObj) return aVisualObj;
291 CORBA::String_var anEntry = aFatherSObj->GetID().c_str();
292 TVisualObjPtr aVisObj = GetVisualObj(theStudyId,anEntry.in());
293 if(SMESH_MeshObj* aMeshObj = dynamic_cast<SMESH_MeshObj*>(aVisObj.get())){
294 aVisualObj.reset(new SMESH_GroupObj(aGroup,aMeshObj));
295 TVisualObjCont::value_type aValue(aKey,aVisualObj);
296 VISUAL_OBJ_CONT.insert(aValue);
299 //Try narrow to SMESH_subMesh interface
300 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(anObj);
301 if(!aSubMesh->_is_nil()){
302 _PTR(SObject) aFatherSObj = aSObj->GetFather();
303 if(!aFatherSObj) return aVisualObj;
304 aFatherSObj = aFatherSObj->GetFather();
305 if(!aFatherSObj) return aVisualObj;
306 CORBA::String_var anEntry = aFatherSObj->GetID().c_str();
307 TVisualObjPtr aVisObj = GetVisualObj(theStudyId,anEntry.in());
308 if(SMESH_MeshObj* aMeshObj = dynamic_cast<SMESH_MeshObj*>(aVisObj.get())){
309 aVisualObj.reset(new SMESH_subMeshObj(aSubMesh,aMeshObj));
310 TVisualObjCont::value_type aValue(aKey,aVisualObj);
311 VISUAL_OBJ_CONT.insert(aValue);
319 INFOS("GetMeshObj - There is no SMESH_Mesh object for the SALOMEDS::Strudy and Entry!!!");
320 return TVisualObjPtr();
323 bool objModified = false;
326 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
329 objModified = aVisualObj->Update();
333 MESSAGE ( "Exception in SMESHGUI_VTKUtils::GetVisualObj()" );
335 RemoveVisualObjectWithActors( theEntry ); // remove this object
342 // PAL16631. Mesurements showed that to show aVisualObj in SHADING(default) mode,
343 // ~10 times more memory is used than it occupies.
344 // Warn the user if there is less free memory than 30 sizes of a grid
345 // TODO: estimate memory usage in other modes and take current mode into account
346 int freeMB = SMDS_Mesh::CheckMemory(true);
347 int usedMB = aVisualObj->GetUnstructuredGrid()->GetActualMemorySize() / 1024;
348 if ( freeMB > 0 && usedMB * 30 > freeMB ) {
350 MESSAGE ( "SMESHGUI_VTKUtils::GetVisualObj(), freeMB=" << freeMB
351 << ", usedMB=" << usedMB );
353 bool continu = false;
354 if ( usedMB * 10 > freeMB )
355 // even dont try to show
356 SUIT_MessageBox::warning(SMESHGUI::desktop(), QObject::tr("SMESH_WRN_WARNING"),
357 QObject::tr("SMESH_NO_MESH_VISUALIZATION"));
359 // there is a chance to succeed
360 continu = SUIT_MessageBox::warning
361 (SMESHGUI::desktop(),
362 QObject::tr("SMESH_WRN_WARNING"),
363 QObject::tr("SMESH_CONTINUE_MESH_VISUALIZATION"),
364 SUIT_MessageBox::Yes | SUIT_MessageBox::No,
365 SUIT_MessageBox::Yes ) == SUIT_MessageBox::Yes;
367 // remove the corresponding actors from all views
368 RemoveVisualObjectWithActors( theEntry );
378 /*! Return active view window, if it instantiates SVTK_ViewWindow class,
379 * overwise find or create corresponding view window, make it active and return it.
380 * \note Active VVTK_ViewWindow can be returned, because it inherits SVTK_ViewWindow.
382 SVTK_ViewWindow* GetViewWindow (const SalomeApp_Module* theModule,
383 bool createIfNotFound)
385 SalomeApp_Application* anApp;
387 anApp = theModule->getApp();
389 anApp = dynamic_cast<SalomeApp_Application*>
390 (SUIT_Session::session()->activeApplication());
393 if (SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(anApp->desktop()->activeWindow()))
396 SUIT_ViewManager* aViewManager =
397 anApp->getViewManager(SVTK_Viewer::Type(), createIfNotFound);
399 if (SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView()) {
400 if (SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)) {
401 aViewWindow->raise();
402 aViewWindow->setFocus();
411 SVTK_ViewWindow* FindVtkViewWindow (SUIT_ViewManager* theMgr,
412 SUIT_ViewWindow * theWindow)
417 QVector<SUIT_ViewWindow*> views = theMgr->getViews();
418 if( views.contains( theWindow ) )
419 return GetVtkViewWindow( theWindow );
424 SVTK_ViewWindow* GetVtkViewWindow(SUIT_ViewWindow* theWindow){
425 return dynamic_cast<SVTK_ViewWindow*>(theWindow);
428 /* SUIT_ViewWindow* GetActiveWindow()
430 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
433 SUIT_ViewManager* mgr = app->activeViewManager();
435 return mgr->getActiveView();
440 SVTK_ViewWindow* GetCurrentVtkView(){
441 return GetVtkViewWindow( GetActiveWindow() );
445 void RepaintCurrentView()
447 if (SVTK_ViewWindow* wnd = GetCurrentVtkView())
450 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
453 wnd->getRenderer()->Render();
458 MESSAGE ( "Exception in SMESHGUI_VTKUtils::RepaintCurrentView()" );
465 void RepaintViewWindow(SVTK_ViewWindow* theWindow)
468 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
471 theWindow->getRenderer()->Render();
472 theWindow->Repaint();
476 MESSAGE ( "Exception in SMESHGUI_VTKUtils::RepaintViewWindow(SVTK_ViewWindow*)" );
482 void RenderViewWindow(SVTK_ViewWindow* theWindow)
485 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
488 theWindow->getRenderer()->Render();
489 theWindow->Repaint();
493 MESSAGE ( "Exception in SMESHGUI_VTKUtils::RenderViewWindow(SVTK_ViewWindow*)" );
500 if(SVTK_ViewWindow* wnd = GetCurrentVtkView() ){
502 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
510 MESSAGE ( "Exception in SMESHGUI_VTKUtils::FitAll()" );
518 SMESH_Actor* FindActorByEntry(SUIT_ViewWindow *theWindow,
519 const char* theEntry)
521 if(SVTK_ViewWindow* aViewWindow = GetVtkViewWindow(theWindow)){
522 vtkRenderer *aRenderer = aViewWindow->getRenderer();
523 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
524 vtkActorCollection *aCollection = aCopy.GetActors();
525 aCollection->InitTraversal();
526 while(vtkActor *anAct = aCollection->GetNextActor()){
527 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
528 if(anActor->hasIO()){
529 Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
530 if(anIO->hasEntry() && strcmp(anIO->getEntry(),theEntry) == 0){
541 SMESH_Actor* FindActorByEntry(const char* theEntry){
542 return FindActorByEntry(GetActiveWindow(),theEntry);
546 SMESH_Actor* FindActorByObject(CORBA::Object_ptr theObject){
547 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
551 if(!CORBA::is_nil(theObject)){
552 _PTR(Study) aStudy = GetActiveStudyDocument();
553 CORBA::String_var anIOR = app->orb()->object_to_string( theObject );
554 _PTR(SObject) aSObject = aStudy->FindObjectIOR(anIOR.in());
556 CORBA::String_var anEntry = aSObject->GetID().c_str();
557 return FindActorByEntry(anEntry.in());
564 SMESH_Actor* CreateActor(_PTR(Study) theStudy,
565 const char* theEntry,
568 SMESH_Actor *anActor = NULL;
569 CORBA::Long anId = theStudy->StudyId();
570 if(TVisualObjPtr aVisualObj = GetVisualObj(anId,theEntry)){
571 _PTR(SObject) aSObj = theStudy->FindObjectID(theEntry);
573 _PTR(GenericAttribute) anAttr;
574 if(aSObj->FindAttribute(anAttr,"AttributeName")){
575 _PTR(AttributeName) aName = anAttr;
576 std::string aNameVal = aName->Value();
577 anActor = SMESH_Actor::New(aVisualObj,theEntry,aNameVal.c_str(),theIsClear);
580 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow( SMESH::SObjectToObject( aSObj ));
581 if(!CORBA::is_nil(aGroup))
583 SALOMEDS::Color aColor = aGroup->GetColor();
584 if( !( aColor.R > 0 || aColor.G > 0 || aColor.B > 0 ) )
586 int r = 0, g = 0, b = 0;
587 SMESH::GetColor( "SMESH", "fill_color", r, g, b, QColor( 0, 170, 255 ) );
588 aColor.R = (float)r / 255.0;
589 aColor.G = (float)g / 255.0;
590 aColor.B = (float)b / 255.0;
591 aGroup->SetColor( aColor );
593 if( aGroup->GetType() == SMESH::NODE )
594 anActor->SetNodeColor( aColor.R, aColor.G, aColor.B );
595 else if( aGroup->GetType() == SMESH::EDGE )
596 anActor->SetEdgeColor( aColor.R, aColor.G, aColor.B );
597 else if( aGroup->GetType() == SMESH::ELEM0D )
598 anActor->Set0DColor( aColor.R, aColor.G, aColor.B );
600 anActor->SetSufaceColor( aColor.R, aColor.G, aColor.B );
608 void DisplayActor( SUIT_ViewWindow *theWnd, SMESH_Actor* theActor){
609 if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(theWnd)){
611 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
614 vtkWnd->AddActor(theActor);
619 MESSAGE ( "Exception in SMESHGUI_VTKUtils::DisplayActor()" );
627 void RemoveActor( SUIT_ViewWindow *theWnd, SMESH_Actor* theActor){
628 if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(theWnd)){
629 vtkWnd->RemoveActor(theActor);
630 if(theActor->hasIO()){
631 Handle(SALOME_InteractiveObject) anIO = theActor->getIO();
632 if(anIO->hasEntry()){
633 std::string anEntry = anIO->getEntry();
634 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( vtkWnd->getViewManager()->study() );
635 int aStudyId = aStudy->id();
636 TVisualObjCont::key_type aKey(aStudyId,anEntry);
637 VISUAL_OBJ_CONT.erase(aKey);
645 //================================================================================
647 * \brief Return true if there are no SMESH actors in a view
649 //================================================================================
651 bool noSmeshActors(SUIT_ViewWindow *theWnd)
653 if(SVTK_ViewWindow* aViewWindow = GetVtkViewWindow(theWnd)) {
654 vtkRenderer *aRenderer = aViewWindow->getRenderer();
655 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
656 vtkActorCollection *aCollection = aCopy.GetActors();
657 aCollection->InitTraversal();
658 while(vtkActor *anAct = aCollection->GetNextActor())
659 if(dynamic_cast<SMESH_Actor*>(anAct))
665 bool UpdateView(SUIT_ViewWindow *theWnd, EDisplaing theAction, const char* theEntry)
668 SVTK_ViewWindow* aViewWnd = GetVtkViewWindow(theWnd);
674 vtkRenderer *aRenderer = aViewWnd->getRenderer();
675 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
676 vtkActorCollection *aCollection = aCopy.GetActors();
677 aCollection->InitTraversal();
681 while (vtkActor *anAct = aCollection->GetNextActor()) {
682 if (SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)) {
683 anActor->SetVisibility(true);
690 while (vtkActor *anAct = aCollection->GetNextActor()) {
691 if (SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)) {
692 anActor->SetVisibility(false);
697 if (SMESH_Actor *anActor = FindActorByEntry(theWnd,theEntry)) {
701 anActor->SetVisibility(true);
702 if (theAction == eDisplayOnly) aRenderer->ResetCameraClippingRange();
705 anActor->SetVisibility(false);
713 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(theWnd->getViewManager()->study());
714 _PTR(Study) aDocument = aStudy->studyDS();
715 // Pass non-visual objects (hypotheses, etc.), return true in this case
716 CORBA::Long anId = aDocument->StudyId();
717 TVisualObjPtr aVisualObj;
718 if ( (aVisualObj = GetVisualObj(anId,theEntry)) && aVisualObj->IsValid())
720 if ((anActor = CreateActor(aDocument,theEntry,true))) {
721 bool needFitAll = noSmeshActors(theWnd); // fit for the first object only
722 DisplayActor(theWnd,anActor);
723 // FitAll(); - PAL16770(Display of a group performs an automatic fit all)
724 if (needFitAll) FitAll();
740 bool UpdateView(EDisplaing theAction, const char* theEntry){
741 SalomeApp_Study* aStudy = dynamic_cast< SalomeApp_Study* >( GetActiveStudy() );
742 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( aStudy->application() );
743 SUIT_ViewWindow *aWnd = app->activeViewManager()->getActiveView();
744 return UpdateView(aWnd,theAction,theEntry);
748 if(SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView()){
749 LightApp_SelectionMgr* mgr = SMESHGUI::selectionMgr();
750 SALOME_ListIO selected; mgr->selectedObjects( selected );
752 if( selected.Extent() == 0){
753 vtkRenderer* aRenderer = aWnd->getRenderer();
754 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
755 vtkActorCollection *aCollection = aCopy.GetActors();
756 aCollection->InitTraversal();
757 while(vtkActor *anAct = aCollection->GetNextActor()){
758 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
760 if (!Update(anActor->getIO(),anActor->GetVisibility()))
761 break; // avoid multiple warinings if visu failed
765 SALOME_ListIteratorOfListIO anIter( selected );
766 for( ; anIter.More(); anIter.Next()){
767 Handle(SALOME_InteractiveObject) anIO = anIter.Value();
768 if ( !Update(anIO,true) )
769 break; // avoid multiple warinings if visu failed
772 RepaintCurrentView();
777 bool Update(const Handle(SALOME_InteractiveObject)& theIO, bool theDisplay)
779 _PTR(Study) aStudy = GetActiveStudyDocument();
780 CORBA::Long anId = aStudy->StudyId();
781 if ( TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId,theIO->getEntry())) {
783 UpdateView(SMESH::eDisplay,theIO->getEntry());
790 void UpdateSelectionProp( SMESHGUI* theModule ) {
794 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( theModule->application() );
797 MESSAGE( "UpdateSelectionProp: Application is null" );
801 SUIT_ViewManager* vm = app->activeViewManager();
804 MESSAGE( "UpdateSelectionProp: View manager is null" );
808 QVector<SUIT_ViewWindow*> views = vm->getViews();
810 SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( theModule );
813 MESSAGE( "UpdateSelectionProp: Resource manager is null" );
817 QColor aHiColor = mgr->colorValue( "SMESH", "selection_object_color", Qt::white ),
818 aSelColor = mgr->colorValue( "SMESH", "selection_element_color", Qt::yellow ),
819 aPreColor = mgr->colorValue( "SMESH", "highlight_color", Qt::cyan );
821 int SW = mgr->integerValue( "SMESH", "selection_width", 5 ),
822 PW = mgr->integerValue( "SMESH", "highlight_width", 5 );
824 // adjust highlight_width to the width of mesh entities
825 int aElem0DSize = mgr->integerValue("SMESH", "elem0d_size", 5);
826 int aLineWidth = mgr->integerValue("SMESH", "element_width", 1);
827 int maxSize = aElem0DSize;
828 if (aElem0DSize > maxSize) maxSize = aElem0DSize;
829 if (aLineWidth > maxSize) maxSize = aLineWidth;
830 if (PW < maxSize + 2) PW = maxSize + 2;
832 double SP1 = mgr->doubleValue( "SMESH", "selection_precision_node", 0.025 ),
833 SP2 = mgr->doubleValue( "SMESH", "selection_precision_element", 0.001 ),
834 SP3 = mgr->doubleValue( "SMESH", "selection_precision_object", 0.025 );
836 for ( int i=0, n=views.count(); i<n; i++ ){
837 // update VTK viewer properties
838 if(SVTK_ViewWindow* aVtkView = GetVtkViewWindow( views[i] )){
839 // mesh element selection
840 aVtkView->SetSelectionProp(aSelColor.red()/255.,
841 aSelColor.green()/255.,
842 aSelColor.blue()/255.,
845 aVtkView->SetSelectionTolerance(SP1, SP2, SP3);
848 aVtkView->SetPreselectionProp(aPreColor.red()/255.,
849 aPreColor.green()/255.,
850 aPreColor.blue()/255.,
853 vtkRenderer* aRenderer = aVtkView->getRenderer();
854 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
855 vtkActorCollection *aCollection = aCopy.GetActors();
856 aCollection->InitTraversal();
857 while(vtkActor *anAct = aCollection->GetNextActor()){
858 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
859 anActor->SetHighlightColor(aHiColor.red()/255.,
860 aHiColor.green()/255.,
861 aHiColor.blue()/255.);
862 anActor->SetPreHighlightColor(aPreColor.red()/255.,
863 aPreColor.green()/255.,
864 aPreColor.blue()/255.);
872 //----------------------------------------------------------------------------
874 GetSelector(SUIT_ViewWindow *theWindow)
876 if(SVTK_ViewWindow* aWnd = GetVtkViewWindow(theWindow))
877 return aWnd->GetSelector();
882 void SetFilter(const Handle(VTKViewer_Filter)& theFilter,
883 SVTK_Selector* theSelector)
886 theSelector->SetFilter(theFilter);
889 Handle(VTKViewer_Filter) GetFilter(int theId, SVTK_Selector* theSelector)
891 return theSelector->GetFilter(theId);
894 bool IsFilterPresent(int theId, SVTK_Selector* theSelector)
896 return theSelector->IsFilterPresent(theId);
899 void RemoveFilter(int theId, SVTK_Selector* theSelector)
901 theSelector->RemoveFilter(theId);
904 void RemoveFilters(SVTK_Selector* theSelector)
906 for ( int id = SMESH::NodeFilter; theSelector && id < SMESH::LastFilter; id++ )
907 theSelector->RemoveFilter( id );
910 bool IsValid(SALOME_Actor* theActor, int theCellId,
911 SVTK_Selector* theSelector)
913 return theSelector->IsValid(theActor,theCellId);
917 //----------------------------------------------------------------------------
918 void SetPointRepresentation(bool theIsVisible){
919 if(SVTK_ViewWindow* aViewWindow = GetCurrentVtkView()){
920 vtkRenderer *aRenderer = aViewWindow->getRenderer();
921 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
922 vtkActorCollection *aCollection = aCopy.GetActors();
923 aCollection->InitTraversal();
924 while(vtkActor *anAct = aCollection->GetNextActor()){
925 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
926 if(anActor->GetVisibility()){
927 anActor->SetPointRepresentation(theIsVisible);
931 RepaintCurrentView();
936 void SetPickable(SMESH_Actor* theActor){
937 if(SVTK_ViewWindow* aWnd = GetCurrentVtkView()){
938 int anIsAllPickable = (theActor == NULL);
939 vtkRenderer *aRenderer = aWnd->getRenderer();
940 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
941 vtkActorCollection *aCollection = aCopy.GetActors();
942 aCollection->InitTraversal();
943 while(vtkActor *anAct = aCollection->GetNextActor()){
944 if(SALOME_Actor *anActor = dynamic_cast<SALOME_Actor*>(anAct)){
945 if(anActor->GetVisibility()){
946 anActor->SetPickable(anIsAllPickable);
951 theActor->SetPickable(!anIsAllPickable);
952 RepaintCurrentView();
957 //----------------------------------------------------------------------------
958 int GetNameOfSelectedNodes(SVTK_Selector* theSelector,
959 const Handle(SALOME_InteractiveObject)& theIO,
963 TColStd_IndexedMapOfInteger aMapIndex;
964 theSelector->GetIndex(theIO,aMapIndex);
966 for(int i = 1; i <= aMapIndex.Extent(); i++)
967 theName += QString(" %1").arg(aMapIndex(i));
969 return aMapIndex.Extent();
972 int GetNameOfSelectedElements(SVTK_Selector* theSelector,
973 const Handle(SALOME_InteractiveObject)& theIO,
977 TColStd_IndexedMapOfInteger aMapIndex;
978 theSelector->GetIndex(theIO,aMapIndex);
980 typedef std::set<int> TIdContainer;
981 TIdContainer anIdContainer;
982 for( int i = 1; i <= aMapIndex.Extent(); i++)
983 anIdContainer.insert(aMapIndex(i));
985 TIdContainer::const_iterator anIter = anIdContainer.begin();
986 for( ; anIter != anIdContainer.end(); anIter++)
987 theName += QString(" %1").arg(*anIter);
989 return aMapIndex.Extent();
993 int GetEdgeNodes(SVTK_Selector* theSelector,
994 const TVisualObjPtr& theVisualObject,
998 const SALOME_ListIO& selected = theSelector->StoredIObjects();
1000 if ( selected.Extent() != 1 )
1003 Handle(SALOME_InteractiveObject) anIO = selected.First();
1004 if ( anIO.IsNull() || !anIO->hasEntry() )
1007 TColStd_IndexedMapOfInteger aMapIndex;
1008 theSelector->GetIndex( anIO, aMapIndex );
1009 if ( aMapIndex.Extent() != 2 )
1012 int anObjId = -1, anEdgeNum = -1;
1013 for ( int i = 1; i <= aMapIndex.Extent(); i++ ) {
1014 int aVal = aMapIndex( i );
1018 anEdgeNum = abs( aVal ) - 1;
1021 if ( anObjId == -1 || anEdgeNum == -1 )
1024 return theVisualObject->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
1027 //----------------------------------------------------------------------------
1028 int GetNameOfSelectedNodes(LightApp_SelectionMgr *theMgr,
1029 const Handle(SALOME_InteractiveObject)& theIO,
1033 if(theIO->hasEntry()){
1034 if(FindActorByEntry(theIO->getEntry())){
1035 TColStd_IndexedMapOfInteger aMapIndex;
1036 theMgr->GetIndexes(theIO,aMapIndex);
1037 for(int i = 1; i <= aMapIndex.Extent(); i++){
1038 theName += QString(" %1").arg(aMapIndex(i));
1040 return aMapIndex.Extent();
1046 int GetNameOfSelectedNodes(LightApp_SelectionMgr *theMgr, QString& theName){
1048 SALOME_ListIO selected; theMgr->selectedObjects( selected );
1049 if(selected.Extent() == 1){
1050 Handle(SALOME_InteractiveObject) anIO = selected.First();
1051 return GetNameOfSelectedNodes(theMgr,anIO,theName);
1057 int GetNameOfSelectedElements(LightApp_SelectionMgr *theMgr,
1058 const Handle(SALOME_InteractiveObject)& theIO,
1062 if(theIO->hasEntry()){
1063 if(FindActorByEntry(theIO->getEntry())){
1064 TColStd_IndexedMapOfInteger aMapIndex;
1065 theMgr->GetIndexes(theIO,aMapIndex);
1066 typedef std::set<int> TIdContainer;
1067 TIdContainer anIdContainer;
1068 for( int i = 1; i <= aMapIndex.Extent(); i++)
1069 anIdContainer.insert(aMapIndex(i));
1070 TIdContainer::const_iterator anIter = anIdContainer.begin();
1071 for( ; anIter != anIdContainer.end(); anIter++){
1072 theName += QString(" %1").arg(*anIter);
1074 return aMapIndex.Extent();
1081 int GetNameOfSelectedElements(LightApp_SelectionMgr *theMgr, QString& theName)
1084 SALOME_ListIO selected; theMgr->selectedObjects( selected );
1086 if( selected.Extent() == 1){
1087 Handle(SALOME_InteractiveObject) anIO = selected.First();
1088 return GetNameOfSelectedElements(theMgr,anIO,theName);
1093 int GetSelected(LightApp_SelectionMgr* theMgr,
1094 TColStd_IndexedMapOfInteger& theMap,
1095 const bool theIsElement)
1098 SALOME_ListIO selected; theMgr->selectedObjects( selected );
1100 if ( selected.Extent() == 1 )
1102 Handle(SALOME_InteractiveObject) anIO = selected.First();
1103 if ( anIO->hasEntry() ) {
1104 theMgr->GetIndexes( anIO, theMap );
1107 return theMap.Extent();
1111 int GetEdgeNodes( LightApp_SelectionMgr* theMgr, int& theId1, int& theId2 )
1113 SALOME_ListIO selected; theMgr->selectedObjects( selected );
1115 if ( selected.Extent() != 1 )
1118 Handle(SALOME_InteractiveObject) anIO = selected.First();
1119 if ( anIO.IsNull() || !anIO->hasEntry() )
1122 SMESH_Actor *anActor = SMESH::FindActorByEntry( anIO->getEntry() );
1126 TColStd_IndexedMapOfInteger aMapIndex;
1127 theMgr->GetIndexes( anIO, aMapIndex );
1128 if ( aMapIndex.Extent() != 2 )
1131 int anObjId = -1, anEdgeNum = -1;
1132 for ( int i = 1; i <= aMapIndex.Extent(); i++ ) {
1133 int aVal = aMapIndex( i );
1137 anEdgeNum = abs( aVal );
1140 if ( anObjId == -1 || anEdgeNum == -1 )
1143 return anActor->GetObject()->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
1146 void SetControlsPrecision( const long theVal )
1148 if( SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView() )
1150 vtkRenderer *aRenderer = aWnd->getRenderer();
1151 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
1152 vtkActorCollection *aCollection = aCopy.GetActors();
1153 aCollection->InitTraversal();
1155 while ( vtkActor *anAct = aCollection->GetNextActor())
1157 if ( SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>( anAct ) )
1159 anActor->SetControlsPrecision( theVal );
1160 anActor->SetControlMode( anActor->GetControlMode() );
1166 } // end of namespace SMESH