1 // Copyright (C) 2007-2010 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
23 // SMESH SMESHGUI : GUI for SMESH component
24 // File : SMESHGUI_VTKUtils.cxx
25 // Author : Open CASCADE S.A.S.
28 #include "SMESHGUI_VTKUtils.h"
31 #include "SMESHGUI_Utils.h"
32 #include "SMESHGUI_Filter.h"
33 #include "SMESH_ControlsDef.hxx"
35 #include <SMESH_Actor.h>
36 #include <SMESH_ActorUtils.h>
37 #include <SMESH_ObjectDef.h>
38 #include <SMDS_Mesh.hxx>
40 // SALOME GUI includes
41 #include <SUIT_Desktop.h>
42 #include <SUIT_Session.h>
43 #include <SUIT_MessageBox.h>
44 #include <SUIT_ViewManager.h>
45 #include <SUIT_ResourceMgr.h>
47 #include <SALOME_ListIO.hxx>
48 #include <SALOME_ListIteratorOfListIO.hxx>
50 #include <SVTK_Selector.h>
51 #include <SVTK_ViewModel.h>
52 #include <SVTK_ViewWindow.h>
54 #include <VTKViewer_Algorithm.h>
56 #include <LightApp_SelectionMgr.h>
57 #include <SalomeApp_Application.h>
58 #include <SalomeApp_Study.h>
60 // SALOME KERNEL includes
61 #include <utilities.h>
64 #include <SALOMEconfig.h>
65 #include CORBA_CLIENT_HEADER(SMESH_Mesh)
66 #include CORBA_CLIENT_HEADER(SMESH_Group)
70 #include <vtkRenderer.h>
71 #include <vtkActorCollection.h>
72 #include <vtkUnstructuredGrid.h>
75 #include <TColStd_IndexedMapOfInteger.hxx>
76 #include <Standard_ErrorHandler.hxx>
80 typedef std::map<TKeyOfVisualObj,TVisualObjPtr> TVisualObjCont;
81 static TVisualObjCont VISUAL_OBJ_CONT;
83 //=============================================================================
85 * \brief Allocate some memory at construction and release it at destruction.
86 * Is used to be able to continue working after mesh generation or visualization
87 * break due to lack of memory
89 //=============================================================================
94 MemoryReserve(): myBuf( new char[1024*1024*1] ){} // 1M
95 void Free() { if (myBuf) { delete [] myBuf; myBuf = 0; }}
96 ~MemoryReserve() { Free(); }
98 static MemoryReserve* theVISU_MemoryReserve = new MemoryReserve;
100 //================================================================================
102 * \brief Remove VisualObj and its actor from all views
104 //================================================================================
106 void RemoveVisualObjectWithActors( const char* theEntry )
108 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
109 ( SUIT_Session::session()->activeApplication() );
110 SUIT_ViewManager* aViewManager =
111 app ? app->getViewManager(SVTK_Viewer::Type(), true) : 0;
112 if ( aViewManager ) {
113 QVector<SUIT_ViewWindow*> views = aViewManager->getViews();
114 for ( int iV = 0; iV < views.count(); ++iV ) {
115 if ( SMESH_Actor* actor = FindActorByEntry( views[iV], theEntry)) {
116 if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(views[iV]))
117 vtkWnd->RemoveActor(actor);
121 int aStudyId = aViewManager->study()->id();
122 TVisualObjCont::key_type aKey(aStudyId,theEntry);
123 TVisualObjCont::iterator anIter = VISUAL_OBJ_CONT.find(aKey);
124 if(anIter != VISUAL_OBJ_CONT.end()) {
125 // for unknown reason, object destructor is not called, so clear object manually
126 anIter->second->GetUnstructuredGrid()->SetCells(0,0,0,0,0);
127 anIter->second->GetUnstructuredGrid()->SetPoints(0);
129 VISUAL_OBJ_CONT.erase(aKey);
132 //================================================================================
134 * \brief Remove all VisualObjs and their actors from all views
136 //================================================================================
138 void RemoveAllObjectsWithActors()
140 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
141 ( SUIT_Session::session()->activeApplication() );
143 ViewManagerList viewMgrs = app->viewManagers();
144 for ( int iM = 0; iM < viewMgrs.count(); ++iM ) {
145 SUIT_ViewManager* aViewManager = viewMgrs.at( iM );
146 if ( aViewManager && aViewManager->getType() == SVTK_Viewer::Type()) {
147 QVector<SUIT_ViewWindow*> views = aViewManager->getViews();
148 for ( int iV = 0; iV < views.count(); ++iV ) {
149 if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(views[iV])) {
150 vtkRenderer *aRenderer = vtkWnd->getRenderer();
151 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
152 vtkActorCollection *actors = aCopy.GetActors();
153 for (int i = 0; i < actors->GetNumberOfItems(); ++i ) {
154 // size of actors changes inside the loop
155 if (SMESH_Actor *actor = dynamic_cast<SMESH_Actor*>(actors->GetItemAsObject(i)))
157 vtkWnd->RemoveActor(actor);
165 TVisualObjCont::iterator anIter = VISUAL_OBJ_CONT.begin();
166 for ( ; anIter != VISUAL_OBJ_CONT.end(); ++anIter ) {
167 // for unknown reason, object destructor is not called, so clear object manually
168 anIter->second->GetUnstructuredGrid()->SetCells(0,0,0,0,0);
169 anIter->second->GetUnstructuredGrid()->SetPoints(0);
171 VISUAL_OBJ_CONT.clear();
174 //================================================================================
176 * \brief Remove all VisualObjs of a study
178 //================================================================================
180 void RemoveVisuData(int studyID)
182 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
183 ( SUIT_Session::session()->activeApplication() );
185 ViewManagerList viewMgrs = app->viewManagers();
186 for ( int iM = 0; iM < viewMgrs.count(); ++iM ) {
187 SUIT_ViewManager* aViewManager = viewMgrs.at( iM );
188 if ( aViewManager && aViewManager->getType() == SVTK_Viewer::Type() &&
189 aViewManager->study()->id() == studyID ) {
190 QVector<SUIT_ViewWindow*> views = aViewManager->getViews();
191 for ( int iV = 0; iV < views.count(); ++iV ) {
192 if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(views[iV])) {
193 vtkRenderer *aRenderer = vtkWnd->getRenderer();
194 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
195 vtkActorCollection *actors = aCopy.GetActors();
196 for (int i = 0; i < actors->GetNumberOfItems(); ++i ) {
197 // size of actors changes inside the loop
198 if(SMESH_Actor *actor = dynamic_cast<SMESH_Actor*>(actors->GetItemAsObject(i)))
200 vtkWnd->RemoveActor(actor);
208 TVisualObjCont::iterator anIter = VISUAL_OBJ_CONT.begin();
209 for ( ; anIter != VISUAL_OBJ_CONT.end(); ) {
210 int curId = anIter->first.first;
211 if ( curId == studyID ) {
212 // for unknown reason, object destructor is not called, so clear object manually
213 anIter->second->GetUnstructuredGrid()->SetCells(0,0,0,0,0);
214 anIter->second->GetUnstructuredGrid()->SetPoints(0);
215 VISUAL_OBJ_CONT.erase( anIter++ ); // anIter++ returns a copy of self before incrementing
223 //================================================================================
225 * \brief Notify the user on problems during visualization
227 //================================================================================
229 void OnVisuException()
232 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
235 // PAL16774 (Crash after display of many groups). Salome sometimes crashes just
236 // after or at showing this message, so we do an additional check of available memory
237 // char* buf = new char[100*1024];
239 SUIT_MessageBox::warning(SMESHGUI::desktop(), QObject::tr("SMESH_WRN_WARNING"),
240 QObject::tr("SMESH_VISU_PROBLEM"));
242 // no more memory at all: last resort
243 MESSAGE_BEGIN ( "SMESHGUI_VTKUtils::OnVisuException(), exception even at showing a message!!!" <<
244 std::endl << "Try to remove all visual data..." );
245 if (theVISU_MemoryReserve) {
246 delete theVISU_MemoryReserve;
247 theVISU_MemoryReserve = 0;
249 RemoveAllObjectsWithActors();
250 SUIT_MessageBox::warning(SMESHGUI::desktop(), QObject::tr("SMESH_WRN_WARNING"),
251 QObject::tr("SMESH_VISU_PROBLEM_CLEAR"));
252 MESSAGE_END ( "...done" );
255 //================================================================================
257 * \brief Returns an updated visual object
259 //================================================================================
261 TVisualObjPtr GetVisualObj(int theStudyId, const char* theEntry, bool nulData){
262 TVisualObjPtr aVisualObj;
263 TVisualObjCont::key_type aKey(theStudyId,theEntry);
265 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
268 TVisualObjCont::iterator anIter = VISUAL_OBJ_CONT.find(aKey);
269 if(anIter != VISUAL_OBJ_CONT.end()){
270 aVisualObj = anIter->second;
272 SalomeApp_Application* app =
273 dynamic_cast<SalomeApp_Application*>( SMESHGUI::activeStudy()->application() );
274 _PTR(Study) aStudy = SMESHGUI::activeStudy()->studyDS();
275 _PTR(SObject) aSObj = aStudy->FindObjectID(theEntry);
277 _PTR(GenericAttribute) anAttr;
278 if(aSObj->FindAttribute(anAttr,"AttributeIOR")){
279 _PTR(AttributeIOR) anIOR = anAttr;
280 CORBA::String_var aVal = anIOR->Value().c_str();
281 CORBA::Object_var anObj = app->orb()->string_to_object( aVal.in() );
282 if(!CORBA::is_nil(anObj)){
283 //Try narrow to SMESH_Mesh interface
284 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(anObj);
285 if(!aMesh->_is_nil()){
286 aVisualObj.reset(new SMESH_MeshObj(aMesh));
287 TVisualObjCont::value_type aValue(aKey,aVisualObj);
288 VISUAL_OBJ_CONT.insert(aValue);
290 //Try narrow to SMESH_Group interface
291 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(anObj);
292 if(!aGroup->_is_nil()){
293 _PTR(SObject) aFatherSObj = aSObj->GetFather();
294 if(!aFatherSObj) return aVisualObj;
295 aFatherSObj = aFatherSObj->GetFather();
296 if(!aFatherSObj) return aVisualObj;
297 CORBA::String_var anEntry = aFatherSObj->GetID().c_str();
298 TVisualObjPtr aVisObj = GetVisualObj(theStudyId,anEntry.in());
299 if(SMESH_MeshObj* aMeshObj = dynamic_cast<SMESH_MeshObj*>(aVisObj.get())){
300 aVisualObj.reset(new SMESH_GroupObj(aGroup,aMeshObj));
301 TVisualObjCont::value_type aValue(aKey,aVisualObj);
302 VISUAL_OBJ_CONT.insert(aValue);
305 //Try narrow to SMESH_subMesh interface
306 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(anObj);
307 if(!aSubMesh->_is_nil()){
308 _PTR(SObject) aFatherSObj = aSObj->GetFather();
309 if(!aFatherSObj) return aVisualObj;
310 aFatherSObj = aFatherSObj->GetFather();
311 if(!aFatherSObj) return aVisualObj;
312 CORBA::String_var anEntry = aFatherSObj->GetID().c_str();
313 TVisualObjPtr aVisObj = GetVisualObj(theStudyId,anEntry.in());
314 if(SMESH_MeshObj* aMeshObj = dynamic_cast<SMESH_MeshObj*>(aVisObj.get())){
315 aVisualObj.reset(new SMESH_subMeshObj(aSubMesh,aMeshObj));
316 TVisualObjCont::value_type aValue(aKey,aVisualObj);
317 VISUAL_OBJ_CONT.insert(aValue);
325 INFOS("GetMeshObj - There is no SMESH_Mesh object for the SALOMEDS::Strudy and Entry!!!");
326 return TVisualObjPtr();
329 bool objModified = false;
332 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
335 //MESSAGE("GetVisualObj");
337 objModified = aVisualObj->NulData();
339 objModified = aVisualObj->Update();
343 MESSAGE ( "Exception in SMESHGUI_VTKUtils::GetVisualObj()" );
345 RemoveVisualObjectWithActors( theEntry ); // remove this object
352 // PAL16631. Mesurements showed that to show aVisualObj in SHADING(default) mode,
353 // ~10 times more memory is used than it occupies.
354 // Warn the user if there is less free memory than 30 sizes of a grid
355 // TODO: estimate memory usage in other modes and take current mode into account
356 int freeMB = SMDS_Mesh::CheckMemory(true);
357 int usedMB = aVisualObj->GetUnstructuredGrid()->GetActualMemorySize() / 1024;
358 if ( freeMB > 0 && usedMB * 30 > freeMB ) {
360 MESSAGE ( "SMESHGUI_VTKUtils::GetVisualObj(), freeMB=" << freeMB
361 << ", usedMB=" << usedMB );
363 // bool continu = false;
364 // if ( usedMB * 10 > freeMB )
365 // // even dont try to show
366 // SUIT_MessageBox::warning(SMESHGUI::desktop(), QObject::tr("SMESH_WRN_WARNING"),
367 // QObject::tr("SMESH_NO_MESH_VISUALIZATION"));
369 // // there is a chance to succeed
370 // continu = SUIT_MessageBox::warning
371 // (SMESHGUI::desktop(),
372 // QObject::tr("SMESH_WRN_WARNING"),
373 // QObject::tr("SMESH_CONTINUE_MESH_VISUALIZATION"),
374 // SUIT_MessageBox::Yes | SUIT_MessageBox::No,
375 // SUIT_MessageBox::Yes ) == SUIT_MessageBox::Yes;
377 // // remove the corresponding actors from all views
378 // RemoveVisualObjectWithActors( theEntry );
379 // aVisualObj.reset();
388 /*! Return active view window, if it instantiates SVTK_ViewWindow class,
389 * overwise find or create corresponding view window, make it active and return it.
390 * \note Active VVTK_ViewWindow can be returned, because it inherits SVTK_ViewWindow.
392 SVTK_ViewWindow* GetViewWindow (const SalomeApp_Module* theModule,
393 bool createIfNotFound)
395 SalomeApp_Application* anApp;
397 anApp = theModule->getApp();
399 anApp = dynamic_cast<SalomeApp_Application*>
400 (SUIT_Session::session()->activeApplication());
403 if (SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(anApp->desktop()->activeWindow()))
406 SUIT_ViewManager* aViewManager =
407 anApp->getViewManager(SVTK_Viewer::Type(), createIfNotFound);
409 if (SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView()) {
410 if (SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)) {
411 aViewWindow->raise();
412 aViewWindow->setFocus();
421 SVTK_ViewWindow* FindVtkViewWindow (SUIT_ViewManager* theMgr,
422 SUIT_ViewWindow * theWindow)
427 QVector<SUIT_ViewWindow*> views = theMgr->getViews();
428 if( views.contains( theWindow ) )
429 return GetVtkViewWindow( theWindow );
434 SVTK_ViewWindow* GetVtkViewWindow(SUIT_ViewWindow* theWindow){
435 return dynamic_cast<SVTK_ViewWindow*>(theWindow);
438 /* SUIT_ViewWindow* GetActiveWindow()
440 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
443 SUIT_ViewManager* mgr = app->activeViewManager();
445 return mgr->getActiveView();
450 SVTK_ViewWindow* GetCurrentVtkView(){
451 return GetVtkViewWindow( GetActiveWindow() );
455 void RepaintCurrentView()
457 if (SVTK_ViewWindow* wnd = GetCurrentVtkView())
460 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
463 wnd->getRenderer()->Render();
468 MESSAGE ( "Exception in SMESHGUI_VTKUtils::RepaintCurrentView()" );
475 void RepaintViewWindow(SVTK_ViewWindow* theWindow)
478 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
481 theWindow->getRenderer()->Render();
482 theWindow->Repaint();
486 MESSAGE ( "Exception in SMESHGUI_VTKUtils::RepaintViewWindow(SVTK_ViewWindow*)" );
492 void RenderViewWindow(SVTK_ViewWindow* theWindow)
495 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
498 theWindow->getRenderer()->Render();
499 theWindow->Repaint();
503 MESSAGE ( "Exception in SMESHGUI_VTKUtils::RenderViewWindow(SVTK_ViewWindow*)" );
510 if(SVTK_ViewWindow* wnd = GetCurrentVtkView() ){
512 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
520 MESSAGE ( "Exception in SMESHGUI_VTKUtils::FitAll()" );
528 SMESH_Actor* FindActorByEntry(SUIT_ViewWindow *theWindow,
529 const char* theEntry)
531 if(SVTK_ViewWindow* aViewWindow = GetVtkViewWindow(theWindow)){
532 vtkRenderer *aRenderer = aViewWindow->getRenderer();
533 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
534 vtkActorCollection *aCollection = aCopy.GetActors();
535 aCollection->InitTraversal();
536 while(vtkActor *anAct = aCollection->GetNextActor()){
537 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
538 if(anActor->hasIO()){
539 Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
540 if(anIO->hasEntry() && strcmp(anIO->getEntry(),theEntry) == 0){
551 SMESH_Actor* FindActorByEntry(const char* theEntry){
552 return FindActorByEntry(GetActiveWindow(),theEntry);
556 SMESH_Actor* FindActorByObject(CORBA::Object_ptr theObject){
557 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
561 if(!CORBA::is_nil(theObject)){
562 _PTR(Study) aStudy = GetActiveStudyDocument();
563 CORBA::String_var anIOR = app->orb()->object_to_string( theObject );
564 _PTR(SObject) aSObject = aStudy->FindObjectIOR(anIOR.in());
566 CORBA::String_var anEntry = aSObject->GetID().c_str();
567 return FindActorByEntry(anEntry.in());
574 SMESH_Actor* CreateActor(_PTR(Study) theStudy,
575 const char* theEntry,
578 SMESH_Actor *anActor = NULL;
579 CORBA::Long anId = theStudy->StudyId();
580 if(TVisualObjPtr aVisualObj = GetVisualObj(anId,theEntry)){
581 _PTR(SObject) aSObj = theStudy->FindObjectID(theEntry);
583 _PTR(GenericAttribute) anAttr;
584 if(aSObj->FindAttribute(anAttr,"AttributeName")){
585 _PTR(AttributeName) aName = anAttr;
586 std::string aNameVal = aName->Value();
587 anActor = SMESH_Actor::New(aVisualObj,theEntry,aNameVal.c_str(),theIsClear);
590 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow( SMESH::SObjectToObject( aSObj ));
591 if(!CORBA::is_nil(aGroup))
593 SALOMEDS::Color aColor = aGroup->GetColor();
594 if( !( aColor.R > 0 || aColor.G > 0 || aColor.B > 0 ) )
596 int r = 0, g = 0, b = 0;
597 SMESH::GetColor( "SMESH", "fill_color", r, g, b, QColor( 0, 170, 255 ) );
598 aColor.R = (float)r / 255.0;
599 aColor.G = (float)g / 255.0;
600 aColor.B = (float)b / 255.0;
601 aGroup->SetColor( aColor );
603 if( aGroup->GetType() == SMESH::NODE )
604 anActor->SetNodeColor( aColor.R, aColor.G, aColor.B );
605 else if( aGroup->GetType() == SMESH::EDGE )
606 anActor->SetEdgeColor( aColor.R, aColor.G, aColor.B );
607 else if( aGroup->GetType() == SMESH::ELEM0D )
608 anActor->Set0DColor( aColor.R, aColor.G, aColor.B );
610 anActor->SetSufaceColor( aColor.R, aColor.G, aColor.B );
614 MESSAGE("CreateActor " << anActor);
616 if( SMESHGUI* aSMESHGUI = SMESHGUI::GetSMESHGUI() )
617 aSMESHGUI->addActorAsObserver( anActor );
622 void DisplayActor( SUIT_ViewWindow *theWnd, SMESH_Actor* theActor){
623 if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(theWnd)){
625 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
628 MESSAGE("DisplayActor " << theActor);
629 vtkWnd->AddActor(theActor);
634 MESSAGE ( "Exception in SMESHGUI_VTKUtils::DisplayActor()" );
642 void RemoveActor( SUIT_ViewWindow *theWnd, SMESH_Actor* theActor){
643 if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(theWnd)){
644 MESSAGE("RemoveActor " << theActor);
645 vtkWnd->RemoveActor(theActor);
646 if(theActor->hasIO()){
647 Handle(SALOME_InteractiveObject) anIO = theActor->getIO();
648 if(anIO->hasEntry()){
649 std::string anEntry = anIO->getEntry();
650 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( vtkWnd->getViewManager()->study() );
651 int aStudyId = aStudy->id();
652 TVisualObjCont::key_type aKey(aStudyId,anEntry);
653 VISUAL_OBJ_CONT.erase(aKey);
661 //================================================================================
663 * \brief Return true if there are no SMESH actors in a view
665 //================================================================================
667 bool noSmeshActors(SUIT_ViewWindow *theWnd)
669 if(SVTK_ViewWindow* aViewWindow = GetVtkViewWindow(theWnd)) {
670 vtkRenderer *aRenderer = aViewWindow->getRenderer();
671 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
672 vtkActorCollection *aCollection = aCopy.GetActors();
673 aCollection->InitTraversal();
674 while(vtkActor *anAct = aCollection->GetNextActor())
675 if(dynamic_cast<SMESH_Actor*>(anAct))
681 bool UpdateView(SUIT_ViewWindow *theWnd, EDisplaing theAction, const char* theEntry)
683 //MESSAGE("UpdateView");
685 SVTK_ViewWindow* aViewWnd = GetVtkViewWindow(theWnd);
691 vtkRenderer *aRenderer = aViewWnd->getRenderer();
692 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
693 vtkActorCollection *aCollection = aCopy.GetActors();
694 aCollection->InitTraversal();
698 while (vtkActor *anAct = aCollection->GetNextActor()) {
699 if (SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)) {
700 MESSAGE("--- display " << anActor);
701 anActor->SetVisibility(true);
708 //MESSAGE("---case eDisplayOnly");
709 while (vtkActor *anAct = aCollection->GetNextActor()) {
710 if (SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)) {
711 //MESSAGE("--- erase " << anActor);
712 anActor->SetVisibility(false);
717 if (SMESH_Actor *anActor = FindActorByEntry(theWnd,theEntry)) {
721 //MESSAGE("--- display " << anActor);
722 anActor->SetVisibility(true);
723 if (theAction == eDisplayOnly) aRenderer->ResetCameraClippingRange();
726 //MESSAGE("--- erase " << anActor);
727 anActor->SetVisibility(false);
736 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(theWnd->getViewManager()->study());
737 _PTR(Study) aDocument = aStudy->studyDS();
738 // Pass non-visual objects (hypotheses, etc.), return true in this case
739 CORBA::Long anId = aDocument->StudyId();
740 TVisualObjPtr aVisualObj;
741 if ( (aVisualObj = GetVisualObj(anId,theEntry)) && aVisualObj->IsValid())
743 if ((anActor = CreateActor(aDocument,theEntry,true))) {
744 bool needFitAll = noSmeshActors(theWnd); // fit for the first object only
745 DisplayActor(theWnd,anActor);
746 // FitAll(); - PAL16770(Display of a group performs an automatic fit all)
747 if (needFitAll) FitAll();
763 bool UpdateView(EDisplaing theAction, const char* theEntry){
764 //MESSAGE("UpdateView");
765 SalomeApp_Study* aStudy = dynamic_cast< SalomeApp_Study* >( GetActiveStudy() );
766 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( aStudy->application() );
767 SUIT_ViewWindow *aWnd = app->activeViewManager()->getActiveView();
768 return UpdateView(aWnd,theAction,theEntry);
772 if(SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView()){
773 LightApp_SelectionMgr* mgr = SMESHGUI::selectionMgr();
774 SALOME_ListIO selected; mgr->selectedObjects( selected );
776 if( selected.Extent() == 0){
777 vtkRenderer* aRenderer = aWnd->getRenderer();
778 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
779 vtkActorCollection *aCollection = aCopy.GetActors();
780 aCollection->InitTraversal();
781 while(vtkActor *anAct = aCollection->GetNextActor()){
782 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
784 if (!Update(anActor->getIO(),anActor->GetVisibility()))
785 break; // avoid multiple warinings if visu failed
789 SALOME_ListIteratorOfListIO anIter( selected );
790 for( ; anIter.More(); anIter.Next()){
791 Handle(SALOME_InteractiveObject) anIO = anIter.Value();
792 if ( !Update(anIO,true) )
793 break; // avoid multiple warinings if visu failed
796 RepaintCurrentView();
801 bool Update(const Handle(SALOME_InteractiveObject)& theIO, bool theDisplay)
804 _PTR(Study) aStudy = GetActiveStudyDocument();
805 CORBA::Long anId = aStudy->StudyId();
806 if ( TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId,theIO->getEntry())) {
808 UpdateView(SMESH::eDisplay,theIO->getEntry());
814 bool UpdateNulData(const Handle(SALOME_InteractiveObject)& theIO, bool theDisplay)
816 MESSAGE("UpdateNulData");
817 _PTR(Study) aStudy = GetActiveStudyDocument();
818 CORBA::Long anId = aStudy->StudyId();
819 if ( TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId,theIO->getEntry(), true)) {
821 UpdateView(SMESH::eDisplay,theIO->getEntry());
827 void UpdateSelectionProp( SMESHGUI* theModule ) {
831 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( theModule->application() );
834 MESSAGE( "UpdateSelectionProp: Application is null" );
838 SUIT_ViewManager* vm = app->activeViewManager();
841 MESSAGE( "UpdateSelectionProp: View manager is null" );
845 QVector<SUIT_ViewWindow*> views = vm->getViews();
847 SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( theModule );
850 MESSAGE( "UpdateSelectionProp: Resource manager is null" );
854 QColor aHiColor = mgr->colorValue( "SMESH", "selection_object_color", Qt::white ),
855 aSelColor = mgr->colorValue( "SMESH", "selection_element_color", Qt::yellow ),
856 aPreColor = mgr->colorValue( "SMESH", "highlight_color", Qt::cyan );
858 int SW = mgr->integerValue( "SMESH", "selection_width", 5 ),
859 PW = mgr->integerValue( "SMESH", "highlight_width", 5 );
861 // adjust highlight_width to the width of mesh entities
862 int aElem0DSize = mgr->integerValue("SMESH", "elem0d_size", 5);
863 int aLineWidth = mgr->integerValue("SMESH", "element_width", 1);
864 int maxSize = aElem0DSize;
865 if (aElem0DSize > maxSize) maxSize = aElem0DSize;
866 if (aLineWidth > maxSize) maxSize = aLineWidth;
867 if (PW < maxSize + 2) PW = maxSize + 2;
869 double SP1 = mgr->doubleValue( "SMESH", "selection_precision_node", 0.025 ),
870 SP2 = mgr->doubleValue( "SMESH", "selection_precision_element", 0.001 ),
871 SP3 = mgr->doubleValue( "SMESH", "selection_precision_object", 0.025 );
873 for ( int i=0, n=views.count(); i<n; i++ ){
874 // update VTK viewer properties
875 if(SVTK_ViewWindow* aVtkView = GetVtkViewWindow( views[i] )){
876 // mesh element selection
877 aVtkView->SetSelectionProp(aSelColor.red()/255.,
878 aSelColor.green()/255.,
879 aSelColor.blue()/255.,
882 aVtkView->SetSelectionTolerance(SP1, SP2, SP3);
885 aVtkView->SetPreselectionProp(aPreColor.red()/255.,
886 aPreColor.green()/255.,
887 aPreColor.blue()/255.,
890 vtkRenderer* aRenderer = aVtkView->getRenderer();
891 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
892 vtkActorCollection *aCollection = aCopy.GetActors();
893 aCollection->InitTraversal();
894 while(vtkActor *anAct = aCollection->GetNextActor()){
895 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
896 anActor->SetHighlightColor(aHiColor.red()/255.,
897 aHiColor.green()/255.,
898 aHiColor.blue()/255.);
899 anActor->SetPreHighlightColor(aPreColor.red()/255.,
900 aPreColor.green()/255.,
901 aPreColor.blue()/255.);
909 //----------------------------------------------------------------------------
911 GetSelector(SUIT_ViewWindow *theWindow)
913 if(SVTK_ViewWindow* aWnd = GetVtkViewWindow(theWindow))
914 return aWnd->GetSelector();
919 void SetFilter(const Handle(VTKViewer_Filter)& theFilter,
920 SVTK_Selector* theSelector)
923 theSelector->SetFilter(theFilter);
926 Handle(VTKViewer_Filter) GetFilter(int theId, SVTK_Selector* theSelector)
928 return theSelector->GetFilter(theId);
931 bool IsFilterPresent(int theId, SVTK_Selector* theSelector)
933 return theSelector->IsFilterPresent(theId);
936 void RemoveFilter(int theId, SVTK_Selector* theSelector)
938 theSelector->RemoveFilter(theId);
941 void RemoveFilters(SVTK_Selector* theSelector)
943 for ( int id = SMESH::NodeFilter; theSelector && id < SMESH::LastFilter; id++ )
944 theSelector->RemoveFilter( id );
947 bool IsValid(SALOME_Actor* theActor, int theCellId,
948 SVTK_Selector* theSelector)
950 return theSelector->IsValid(theActor,theCellId);
954 //----------------------------------------------------------------------------
955 void SetPointRepresentation(bool theIsVisible){
956 if(SVTK_ViewWindow* aViewWindow = GetCurrentVtkView()){
957 vtkRenderer *aRenderer = aViewWindow->getRenderer();
958 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
959 vtkActorCollection *aCollection = aCopy.GetActors();
960 aCollection->InitTraversal();
961 while(vtkActor *anAct = aCollection->GetNextActor()){
962 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
963 if(anActor->GetVisibility()){
964 anActor->SetPointRepresentation(theIsVisible);
968 RepaintCurrentView();
973 void SetPickable(SMESH_Actor* theActor){
974 if(SVTK_ViewWindow* aWnd = GetCurrentVtkView()){
975 int anIsAllPickable = (theActor == NULL);
976 vtkRenderer *aRenderer = aWnd->getRenderer();
977 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
978 vtkActorCollection *aCollection = aCopy.GetActors();
979 aCollection->InitTraversal();
980 while(vtkActor *anAct = aCollection->GetNextActor()){
981 if(SALOME_Actor *anActor = dynamic_cast<SALOME_Actor*>(anAct)){
982 if(anActor->GetVisibility()){
983 anActor->SetPickable(anIsAllPickable);
988 theActor->SetPickable(!anIsAllPickable);
989 RepaintCurrentView();
994 //----------------------------------------------------------------------------
995 int GetNameOfSelectedNodes(SVTK_Selector* theSelector,
996 const Handle(SALOME_InteractiveObject)& theIO,
1000 TColStd_IndexedMapOfInteger aMapIndex;
1001 theSelector->GetIndex(theIO,aMapIndex);
1003 for(int i = 1; i <= aMapIndex.Extent(); i++)
1004 theName += QString(" %1").arg(aMapIndex(i));
1006 return aMapIndex.Extent();
1009 int GetNameOfSelectedElements(SVTK_Selector* theSelector,
1010 const Handle(SALOME_InteractiveObject)& theIO,
1014 TColStd_IndexedMapOfInteger aMapIndex;
1015 theSelector->GetIndex(theIO,aMapIndex);
1017 typedef std::set<int> TIdContainer;
1018 TIdContainer anIdContainer;
1019 for( int i = 1; i <= aMapIndex.Extent(); i++)
1020 anIdContainer.insert(aMapIndex(i));
1022 TIdContainer::const_iterator anIter = anIdContainer.begin();
1023 for( ; anIter != anIdContainer.end(); anIter++)
1024 theName += QString(" %1").arg(*anIter);
1026 return aMapIndex.Extent();
1030 int GetEdgeNodes(SVTK_Selector* theSelector,
1031 const TVisualObjPtr& theVisualObject,
1035 const SALOME_ListIO& selected = theSelector->StoredIObjects();
1037 if ( selected.Extent() != 1 )
1040 Handle(SALOME_InteractiveObject) anIO = selected.First();
1041 if ( anIO.IsNull() || !anIO->hasEntry() )
1044 TColStd_IndexedMapOfInteger aMapIndex;
1045 theSelector->GetIndex( anIO, aMapIndex );
1046 if ( aMapIndex.Extent() != 2 )
1049 int anObjId = -1, anEdgeNum = -1;
1050 for ( int i = 1; i <= aMapIndex.Extent(); i++ ) {
1051 int aVal = aMapIndex( i );
1055 anEdgeNum = abs( aVal ) - 1;
1058 if ( anObjId == -1 || anEdgeNum == -1 )
1061 return theVisualObject->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
1064 //----------------------------------------------------------------------------
1065 int GetNameOfSelectedNodes(LightApp_SelectionMgr *theMgr,
1066 const Handle(SALOME_InteractiveObject)& theIO,
1070 if(theIO->hasEntry()){
1071 if(FindActorByEntry(theIO->getEntry())){
1072 TColStd_IndexedMapOfInteger aMapIndex;
1073 theMgr->GetIndexes(theIO,aMapIndex);
1074 for(int i = 1; i <= aMapIndex.Extent(); i++){
1075 theName += QString(" %1").arg(aMapIndex(i));
1077 return aMapIndex.Extent();
1083 int GetNameOfSelectedNodes(LightApp_SelectionMgr *theMgr, QString& theName){
1085 SALOME_ListIO selected; theMgr->selectedObjects( selected );
1086 if(selected.Extent() == 1){
1087 Handle(SALOME_InteractiveObject) anIO = selected.First();
1088 return GetNameOfSelectedNodes(theMgr,anIO,theName);
1094 int GetNameOfSelectedElements(LightApp_SelectionMgr *theMgr,
1095 const Handle(SALOME_InteractiveObject)& theIO,
1099 if(theIO->hasEntry()){
1100 if(FindActorByEntry(theIO->getEntry())){
1101 TColStd_IndexedMapOfInteger aMapIndex;
1102 theMgr->GetIndexes(theIO,aMapIndex);
1103 typedef std::set<int> TIdContainer;
1104 TIdContainer anIdContainer;
1105 for( int i = 1; i <= aMapIndex.Extent(); i++)
1106 anIdContainer.insert(aMapIndex(i));
1107 TIdContainer::const_iterator anIter = anIdContainer.begin();
1108 for( ; anIter != anIdContainer.end(); anIter++){
1109 theName += QString(" %1").arg(*anIter);
1111 return aMapIndex.Extent();
1118 int GetNameOfSelectedElements(LightApp_SelectionMgr *theMgr, QString& theName)
1121 SALOME_ListIO selected; theMgr->selectedObjects( selected );
1123 if( selected.Extent() == 1){
1124 Handle(SALOME_InteractiveObject) anIO = selected.First();
1125 return GetNameOfSelectedElements(theMgr,anIO,theName);
1130 int GetSelected(LightApp_SelectionMgr* theMgr,
1131 TColStd_IndexedMapOfInteger& theMap,
1132 const bool theIsElement)
1135 SALOME_ListIO selected; theMgr->selectedObjects( selected );
1137 if ( selected.Extent() == 1 )
1139 Handle(SALOME_InteractiveObject) anIO = selected.First();
1140 if ( anIO->hasEntry() ) {
1141 theMgr->GetIndexes( anIO, theMap );
1144 return theMap.Extent();
1148 int GetEdgeNodes( LightApp_SelectionMgr* theMgr, int& theId1, int& theId2 )
1150 SALOME_ListIO selected; theMgr->selectedObjects( selected );
1152 if ( selected.Extent() != 1 )
1155 Handle(SALOME_InteractiveObject) anIO = selected.First();
1156 if ( anIO.IsNull() || !anIO->hasEntry() )
1159 SMESH_Actor *anActor = SMESH::FindActorByEntry( anIO->getEntry() );
1163 TColStd_IndexedMapOfInteger aMapIndex;
1164 theMgr->GetIndexes( anIO, aMapIndex );
1165 if ( aMapIndex.Extent() != 2 )
1168 int anObjId = -1, anEdgeNum = -1;
1169 for ( int i = 1; i <= aMapIndex.Extent(); i++ ) {
1170 int aVal = aMapIndex( i );
1174 anEdgeNum = abs( aVal );
1177 if ( anObjId == -1 || anEdgeNum == -1 )
1180 return anActor->GetObject()->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
1183 void SetControlsPrecision( const long theVal )
1185 if( SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView() )
1187 vtkRenderer *aRenderer = aWnd->getRenderer();
1188 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
1189 vtkActorCollection *aCollection = aCopy.GetActors();
1190 aCollection->InitTraversal();
1192 while ( vtkActor *anAct = aCollection->GetNextActor())
1194 if ( SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>( anAct ) )
1196 anActor->SetControlsPrecision( theVal );
1197 anActor->SetControlMode( anActor->GetControlMode() );
1204 //----------------------------------------------------------------------------
1205 // internal function
1206 void ComputeBoundsParam( vtkFloatingPointType theBounds[6],
1207 vtkFloatingPointType theDirection[3],
1208 vtkFloatingPointType theMinPnt[3],
1209 vtkFloatingPointType& theMaxBoundPrj,
1210 vtkFloatingPointType& theMinBoundPrj )
1212 //Enlarge bounds in order to avoid conflicts of precision
1213 for(int i = 0; i < 6; i += 2){
1214 static double EPS = 1.0E-3;
1215 vtkFloatingPointType aDelta = (theBounds[i+1] - theBounds[i])*EPS;
1216 theBounds[i] -= aDelta;
1217 theBounds[i+1] += aDelta;
1220 vtkFloatingPointType aBoundPoints[8][3] = { {theBounds[0],theBounds[2],theBounds[4]},
1221 {theBounds[1],theBounds[2],theBounds[4]},
1222 {theBounds[0],theBounds[3],theBounds[4]},
1223 {theBounds[1],theBounds[3],theBounds[4]},
1224 {theBounds[0],theBounds[2],theBounds[5]},
1225 {theBounds[1],theBounds[2],theBounds[5]},
1226 {theBounds[0],theBounds[3],theBounds[5]},
1227 {theBounds[1],theBounds[3],theBounds[5]}};
1229 int aMaxId = 0, aMinId = aMaxId;
1230 theMaxBoundPrj = vtkMath::Dot(theDirection,aBoundPoints[aMaxId]);
1231 theMinBoundPrj = theMaxBoundPrj;
1232 for(int i = 1; i < 8; i++){
1233 vtkFloatingPointType aTmp = vtkMath::Dot(theDirection,aBoundPoints[i]);
1234 if(theMaxBoundPrj < aTmp){
1235 theMaxBoundPrj = aTmp;
1238 if(theMinBoundPrj > aTmp){
1239 theMinBoundPrj = aTmp;
1243 vtkFloatingPointType *aMinPnt = aBoundPoints[aMaxId];
1244 theMinPnt[0] = aMinPnt[0];
1245 theMinPnt[1] = aMinPnt[1];
1246 theMinPnt[2] = aMinPnt[2];
1249 // internal function
1250 void DistanceToPosition( vtkFloatingPointType theBounds[6],
1251 vtkFloatingPointType theDirection[3],
1252 vtkFloatingPointType theDist,
1253 vtkFloatingPointType thePos[3] )
1255 vtkFloatingPointType aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
1256 ComputeBoundsParam(theBounds,theDirection,aMinPnt,aMaxBoundPrj,aMinBoundPrj);
1257 vtkFloatingPointType aLength = (aMaxBoundPrj-aMinBoundPrj)*theDist;
1258 thePos[0] = aMinPnt[0]-theDirection[0]*aLength;
1259 thePos[1] = aMinPnt[1]-theDirection[1]*aLength;
1260 thePos[2] = aMinPnt[2]-theDirection[2]*aLength;
1263 // internal function (currently unused, left just in case)
1264 void PositionToDistance( vtkFloatingPointType theBounds[6],
1265 vtkFloatingPointType theDirection[3],
1266 vtkFloatingPointType thePos[3],
1267 vtkFloatingPointType& theDist )
1269 vtkFloatingPointType aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
1270 ComputeBoundsParam(theBounds,theDirection,aMinPnt,aMaxBoundPrj,aMinBoundPrj);
1271 vtkFloatingPointType aPrj = vtkMath::Dot(theDirection,thePos);
1272 theDist = (aPrj-aMinBoundPrj)/(aMaxBoundPrj-aMinBoundPrj);
1275 bool ComputeClippingPlaneParameters( std::list<vtkActor*> theActorList,
1276 vtkFloatingPointType theNormal[3],
1277 vtkFloatingPointType theDist,
1278 vtkFloatingPointType theBounds[6],
1279 vtkFloatingPointType theOrigin[3] )
1281 bool anIsOk = false;
1282 theBounds[0] = theBounds[2] = theBounds[4] = VTK_DOUBLE_MAX;
1283 theBounds[1] = theBounds[3] = theBounds[5] = -VTK_DOUBLE_MAX;
1284 std::list<vtkActor*>::iterator anIter = theActorList.begin();
1285 for( ; anIter != theActorList.end(); anIter++ ) {
1286 if( vtkActor* aVTKActor = *anIter ) {
1287 if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) ) {
1288 vtkFloatingPointType aBounds[6];
1289 anActor->GetUnstructuredGrid()->GetBounds( aBounds );
1290 theBounds[0] = std::min( theBounds[0], aBounds[0] );
1291 theBounds[1] = std::max( theBounds[1], aBounds[1] );
1292 theBounds[2] = std::min( theBounds[2], aBounds[2] );
1293 theBounds[3] = std::max( theBounds[3], aBounds[3] );
1294 theBounds[4] = std::min( theBounds[4], aBounds[4] );
1295 theBounds[5] = std::max( theBounds[5], aBounds[5] );
1304 DistanceToPosition( theBounds, theNormal, theDist, theOrigin );
1308 #ifndef DISABLE_PLOT2DVIEWER
1309 //================================================================================
1311 * \brief Find all SMESH_Actor's in the View Window.
1312 * If actor constains Plot2d_Histogram object remove it from each Plot2d Viewer.
1314 //================================================================================
1316 void ClearPlot2Viewers( SUIT_ViewWindow* theWindow ) {
1317 if(SVTK_ViewWindow* aViewWindow = GetVtkViewWindow(theWindow)){
1318 vtkRenderer *aRenderer = aViewWindow->getRenderer();
1319 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
1320 vtkActorCollection *aCollection = aCopy.GetActors();
1321 aCollection->InitTraversal();
1322 while(vtkActor *anAct = aCollection->GetNextActor()){
1323 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
1324 if(anActor->hasIO() && anActor->GetPlot2Histogram() ){
1325 ProcessIn2DViewers(anActor,RemoveFrom2dViewer);
1334 } // end of namespace SMESH