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);
723 anActor->SetVisibility(true);
724 if (theAction == eDisplayOnly) aRenderer->ResetCameraClippingRange();
727 //MESSAGE("--- erase " << anActor);
728 anActor->SetVisibility(false);
737 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(theWnd->getViewManager()->study());
738 _PTR(Study) aDocument = aStudy->studyDS();
739 // Pass non-visual objects (hypotheses, etc.), return true in this case
740 CORBA::Long anId = aDocument->StudyId();
741 TVisualObjPtr aVisualObj;
742 if ( (aVisualObj = GetVisualObj(anId,theEntry)) && aVisualObj->IsValid())
744 if ((anActor = CreateActor(aDocument,theEntry,true))) {
745 bool needFitAll = noSmeshActors(theWnd); // fit for the first object only
746 DisplayActor(theWnd,anActor);
747 // FitAll(); - PAL16770(Display of a group performs an automatic fit all)
748 if (needFitAll) FitAll();
764 bool UpdateView(EDisplaing theAction, const char* theEntry){
765 //MESSAGE("UpdateView");
766 SalomeApp_Study* aStudy = dynamic_cast< SalomeApp_Study* >( GetActiveStudy() );
767 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( aStudy->application() );
768 SUIT_ViewWindow *aWnd = app->activeViewManager()->getActiveView();
769 return UpdateView(aWnd,theAction,theEntry);
773 if(SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView()){
774 LightApp_SelectionMgr* mgr = SMESHGUI::selectionMgr();
775 SALOME_ListIO selected; mgr->selectedObjects( selected );
777 if( selected.Extent() == 0){
778 vtkRenderer* aRenderer = aWnd->getRenderer();
779 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
780 vtkActorCollection *aCollection = aCopy.GetActors();
781 aCollection->InitTraversal();
782 while(vtkActor *anAct = aCollection->GetNextActor()){
783 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
785 if (!Update(anActor->getIO(),anActor->GetVisibility()))
786 break; // avoid multiple warinings if visu failed
790 SALOME_ListIteratorOfListIO anIter( selected );
791 for( ; anIter.More(); anIter.Next()){
792 Handle(SALOME_InteractiveObject) anIO = anIter.Value();
793 if ( !Update(anIO,true) )
794 break; // avoid multiple warinings if visu failed
797 RepaintCurrentView();
802 bool Update(const Handle(SALOME_InteractiveObject)& theIO, bool theDisplay)
805 _PTR(Study) aStudy = GetActiveStudyDocument();
806 CORBA::Long anId = aStudy->StudyId();
807 if ( TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId,theIO->getEntry())) {
809 UpdateView(SMESH::eDisplay,theIO->getEntry());
815 bool UpdateNulData(const Handle(SALOME_InteractiveObject)& theIO, bool theDisplay)
817 MESSAGE("UpdateNulData");
818 _PTR(Study) aStudy = GetActiveStudyDocument();
819 CORBA::Long anId = aStudy->StudyId();
820 if ( TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId,theIO->getEntry(), true)) {
822 UpdateView(SMESH::eDisplay,theIO->getEntry());
828 void UpdateSelectionProp( SMESHGUI* theModule ) {
832 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( theModule->application() );
835 MESSAGE( "UpdateSelectionProp: Application is null" );
839 SUIT_ViewManager* vm = app->activeViewManager();
842 MESSAGE( "UpdateSelectionProp: View manager is null" );
846 QVector<SUIT_ViewWindow*> views = vm->getViews();
848 SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( theModule );
851 MESSAGE( "UpdateSelectionProp: Resource manager is null" );
855 QColor aHiColor = mgr->colorValue( "SMESH", "selection_object_color", Qt::white ),
856 aSelColor = mgr->colorValue( "SMESH", "selection_element_color", Qt::yellow ),
857 aPreColor = mgr->colorValue( "SMESH", "highlight_color", Qt::cyan );
859 int SW = mgr->integerValue( "SMESH", "selection_width", 5 ),
860 PW = mgr->integerValue( "SMESH", "highlight_width", 5 );
862 // adjust highlight_width to the width of mesh entities
863 int aElem0DSize = mgr->integerValue("SMESH", "elem0d_size", 5);
864 int aLineWidth = mgr->integerValue("SMESH", "element_width", 1);
865 int maxSize = aElem0DSize;
866 if (aElem0DSize > maxSize) maxSize = aElem0DSize;
867 if (aLineWidth > maxSize) maxSize = aLineWidth;
868 if (PW < maxSize + 2) PW = maxSize + 2;
870 double SP1 = mgr->doubleValue( "SMESH", "selection_precision_node", 0.025 ),
871 SP2 = mgr->doubleValue( "SMESH", "selection_precision_element", 0.001 ),
872 SP3 = mgr->doubleValue( "SMESH", "selection_precision_object", 0.025 );
874 for ( int i=0, n=views.count(); i<n; i++ ){
875 // update VTK viewer properties
876 if(SVTK_ViewWindow* aVtkView = GetVtkViewWindow( views[i] )){
877 // mesh element selection
878 aVtkView->SetSelectionProp(aSelColor.red()/255.,
879 aSelColor.green()/255.,
880 aSelColor.blue()/255.,
883 aVtkView->SetSelectionTolerance(SP1, SP2, SP3);
886 aVtkView->SetPreselectionProp(aPreColor.red()/255.,
887 aPreColor.green()/255.,
888 aPreColor.blue()/255.,
891 vtkRenderer* aRenderer = aVtkView->getRenderer();
892 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
893 vtkActorCollection *aCollection = aCopy.GetActors();
894 aCollection->InitTraversal();
895 while(vtkActor *anAct = aCollection->GetNextActor()){
896 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
897 anActor->SetHighlightColor(aHiColor.red()/255.,
898 aHiColor.green()/255.,
899 aHiColor.blue()/255.);
900 anActor->SetPreHighlightColor(aPreColor.red()/255.,
901 aPreColor.green()/255.,
902 aPreColor.blue()/255.);
910 //----------------------------------------------------------------------------
912 GetSelector(SUIT_ViewWindow *theWindow)
914 if(SVTK_ViewWindow* aWnd = GetVtkViewWindow(theWindow))
915 return aWnd->GetSelector();
920 void SetFilter(const Handle(VTKViewer_Filter)& theFilter,
921 SVTK_Selector* theSelector)
924 theSelector->SetFilter(theFilter);
927 Handle(VTKViewer_Filter) GetFilter(int theId, SVTK_Selector* theSelector)
929 return theSelector->GetFilter(theId);
932 bool IsFilterPresent(int theId, SVTK_Selector* theSelector)
934 return theSelector->IsFilterPresent(theId);
937 void RemoveFilter(int theId, SVTK_Selector* theSelector)
939 theSelector->RemoveFilter(theId);
942 void RemoveFilters(SVTK_Selector* theSelector)
944 for ( int id = SMESH::NodeFilter; theSelector && id < SMESH::LastFilter; id++ )
945 theSelector->RemoveFilter( id );
948 bool IsValid(SALOME_Actor* theActor, int theCellId,
949 SVTK_Selector* theSelector)
951 return theSelector->IsValid(theActor,theCellId);
955 //----------------------------------------------------------------------------
956 void SetPointRepresentation(bool theIsVisible){
957 if(SVTK_ViewWindow* aViewWindow = GetCurrentVtkView()){
958 vtkRenderer *aRenderer = aViewWindow->getRenderer();
959 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
960 vtkActorCollection *aCollection = aCopy.GetActors();
961 aCollection->InitTraversal();
962 while(vtkActor *anAct = aCollection->GetNextActor()){
963 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
964 if(anActor->GetVisibility()){
965 anActor->SetPointRepresentation(theIsVisible);
969 RepaintCurrentView();
974 void SetPickable(SMESH_Actor* theActor){
975 if(SVTK_ViewWindow* aWnd = GetCurrentVtkView()){
976 int anIsAllPickable = (theActor == NULL);
977 vtkRenderer *aRenderer = aWnd->getRenderer();
978 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
979 vtkActorCollection *aCollection = aCopy.GetActors();
980 aCollection->InitTraversal();
981 while(vtkActor *anAct = aCollection->GetNextActor()){
982 if(SALOME_Actor *anActor = dynamic_cast<SALOME_Actor*>(anAct)){
983 if(anActor->GetVisibility()){
984 anActor->SetPickable(anIsAllPickable);
989 theActor->SetPickable(!anIsAllPickable);
990 RepaintCurrentView();
995 //----------------------------------------------------------------------------
996 int GetNameOfSelectedNodes(SVTK_Selector* theSelector,
997 const Handle(SALOME_InteractiveObject)& theIO,
1001 TColStd_IndexedMapOfInteger aMapIndex;
1002 theSelector->GetIndex(theIO,aMapIndex);
1004 for(int i = 1; i <= aMapIndex.Extent(); i++)
1005 theName += QString(" %1").arg(aMapIndex(i));
1007 return aMapIndex.Extent();
1010 int GetNameOfSelectedElements(SVTK_Selector* theSelector,
1011 const Handle(SALOME_InteractiveObject)& theIO,
1015 TColStd_IndexedMapOfInteger aMapIndex;
1016 theSelector->GetIndex(theIO,aMapIndex);
1018 typedef std::set<int> TIdContainer;
1019 TIdContainer anIdContainer;
1020 for( int i = 1; i <= aMapIndex.Extent(); i++)
1021 anIdContainer.insert(aMapIndex(i));
1023 TIdContainer::const_iterator anIter = anIdContainer.begin();
1024 for( ; anIter != anIdContainer.end(); anIter++)
1025 theName += QString(" %1").arg(*anIter);
1027 return aMapIndex.Extent();
1031 int GetEdgeNodes(SVTK_Selector* theSelector,
1032 const TVisualObjPtr& theVisualObject,
1036 const SALOME_ListIO& selected = theSelector->StoredIObjects();
1038 if ( selected.Extent() != 1 )
1041 Handle(SALOME_InteractiveObject) anIO = selected.First();
1042 if ( anIO.IsNull() || !anIO->hasEntry() )
1045 TColStd_IndexedMapOfInteger aMapIndex;
1046 theSelector->GetIndex( anIO, aMapIndex );
1047 if ( aMapIndex.Extent() != 2 )
1050 int anObjId = -1, anEdgeNum = -1;
1051 for ( int i = 1; i <= aMapIndex.Extent(); i++ ) {
1052 int aVal = aMapIndex( i );
1056 anEdgeNum = abs( aVal ) - 1;
1059 if ( anObjId == -1 || anEdgeNum == -1 )
1062 return theVisualObject->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
1065 //----------------------------------------------------------------------------
1066 int GetNameOfSelectedNodes(LightApp_SelectionMgr *theMgr,
1067 const Handle(SALOME_InteractiveObject)& theIO,
1071 if(theIO->hasEntry()){
1072 if(FindActorByEntry(theIO->getEntry())){
1073 TColStd_IndexedMapOfInteger aMapIndex;
1074 theMgr->GetIndexes(theIO,aMapIndex);
1075 for(int i = 1; i <= aMapIndex.Extent(); i++){
1076 theName += QString(" %1").arg(aMapIndex(i));
1078 return aMapIndex.Extent();
1084 int GetNameOfSelectedNodes(LightApp_SelectionMgr *theMgr, QString& theName){
1086 SALOME_ListIO selected; theMgr->selectedObjects( selected );
1087 if(selected.Extent() == 1){
1088 Handle(SALOME_InteractiveObject) anIO = selected.First();
1089 return GetNameOfSelectedNodes(theMgr,anIO,theName);
1095 int GetNameOfSelectedElements(LightApp_SelectionMgr *theMgr,
1096 const Handle(SALOME_InteractiveObject)& theIO,
1100 if(theIO->hasEntry()){
1101 if(FindActorByEntry(theIO->getEntry())){
1102 TColStd_IndexedMapOfInteger aMapIndex;
1103 theMgr->GetIndexes(theIO,aMapIndex);
1104 typedef std::set<int> TIdContainer;
1105 TIdContainer anIdContainer;
1106 for( int i = 1; i <= aMapIndex.Extent(); i++)
1107 anIdContainer.insert(aMapIndex(i));
1108 TIdContainer::const_iterator anIter = anIdContainer.begin();
1109 for( ; anIter != anIdContainer.end(); anIter++){
1110 theName += QString(" %1").arg(*anIter);
1112 return aMapIndex.Extent();
1119 int GetNameOfSelectedElements(LightApp_SelectionMgr *theMgr, QString& theName)
1122 SALOME_ListIO selected; theMgr->selectedObjects( selected );
1124 if( selected.Extent() == 1){
1125 Handle(SALOME_InteractiveObject) anIO = selected.First();
1126 return GetNameOfSelectedElements(theMgr,anIO,theName);
1131 int GetSelected(LightApp_SelectionMgr* theMgr,
1132 TColStd_IndexedMapOfInteger& theMap,
1133 const bool theIsElement)
1136 SALOME_ListIO selected; theMgr->selectedObjects( selected );
1138 if ( selected.Extent() == 1 )
1140 Handle(SALOME_InteractiveObject) anIO = selected.First();
1141 if ( anIO->hasEntry() ) {
1142 theMgr->GetIndexes( anIO, theMap );
1145 return theMap.Extent();
1149 int GetEdgeNodes( LightApp_SelectionMgr* theMgr, int& theId1, int& theId2 )
1151 SALOME_ListIO selected; theMgr->selectedObjects( selected );
1153 if ( selected.Extent() != 1 )
1156 Handle(SALOME_InteractiveObject) anIO = selected.First();
1157 if ( anIO.IsNull() || !anIO->hasEntry() )
1160 SMESH_Actor *anActor = SMESH::FindActorByEntry( anIO->getEntry() );
1164 TColStd_IndexedMapOfInteger aMapIndex;
1165 theMgr->GetIndexes( anIO, aMapIndex );
1166 if ( aMapIndex.Extent() != 2 )
1169 int anObjId = -1, anEdgeNum = -1;
1170 for ( int i = 1; i <= aMapIndex.Extent(); i++ ) {
1171 int aVal = aMapIndex( i );
1175 anEdgeNum = abs( aVal );
1178 if ( anObjId == -1 || anEdgeNum == -1 )
1181 return anActor->GetObject()->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
1184 void SetControlsPrecision( const long theVal )
1186 if( SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView() )
1188 vtkRenderer *aRenderer = aWnd->getRenderer();
1189 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
1190 vtkActorCollection *aCollection = aCopy.GetActors();
1191 aCollection->InitTraversal();
1193 while ( vtkActor *anAct = aCollection->GetNextActor())
1195 if ( SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>( anAct ) )
1197 anActor->SetControlsPrecision( theVal );
1198 anActor->SetControlMode( anActor->GetControlMode() );
1205 //----------------------------------------------------------------------------
1206 // internal function
1207 void ComputeBoundsParam( vtkFloatingPointType theBounds[6],
1208 vtkFloatingPointType theDirection[3],
1209 vtkFloatingPointType theMinPnt[3],
1210 vtkFloatingPointType& theMaxBoundPrj,
1211 vtkFloatingPointType& theMinBoundPrj )
1213 //Enlarge bounds in order to avoid conflicts of precision
1214 for(int i = 0; i < 6; i += 2){
1215 static double EPS = 1.0E-3;
1216 vtkFloatingPointType aDelta = (theBounds[i+1] - theBounds[i])*EPS;
1217 theBounds[i] -= aDelta;
1218 theBounds[i+1] += aDelta;
1221 vtkFloatingPointType aBoundPoints[8][3] = { {theBounds[0],theBounds[2],theBounds[4]},
1222 {theBounds[1],theBounds[2],theBounds[4]},
1223 {theBounds[0],theBounds[3],theBounds[4]},
1224 {theBounds[1],theBounds[3],theBounds[4]},
1225 {theBounds[0],theBounds[2],theBounds[5]},
1226 {theBounds[1],theBounds[2],theBounds[5]},
1227 {theBounds[0],theBounds[3],theBounds[5]},
1228 {theBounds[1],theBounds[3],theBounds[5]}};
1230 int aMaxId = 0, aMinId = aMaxId;
1231 theMaxBoundPrj = vtkMath::Dot(theDirection,aBoundPoints[aMaxId]);
1232 theMinBoundPrj = theMaxBoundPrj;
1233 for(int i = 1; i < 8; i++){
1234 vtkFloatingPointType aTmp = vtkMath::Dot(theDirection,aBoundPoints[i]);
1235 if(theMaxBoundPrj < aTmp){
1236 theMaxBoundPrj = aTmp;
1239 if(theMinBoundPrj > aTmp){
1240 theMinBoundPrj = aTmp;
1244 vtkFloatingPointType *aMinPnt = aBoundPoints[aMaxId];
1245 theMinPnt[0] = aMinPnt[0];
1246 theMinPnt[1] = aMinPnt[1];
1247 theMinPnt[2] = aMinPnt[2];
1250 // internal function
1251 void DistanceToPosition( vtkFloatingPointType theBounds[6],
1252 vtkFloatingPointType theDirection[3],
1253 vtkFloatingPointType theDist,
1254 vtkFloatingPointType thePos[3] )
1256 vtkFloatingPointType aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
1257 ComputeBoundsParam(theBounds,theDirection,aMinPnt,aMaxBoundPrj,aMinBoundPrj);
1258 vtkFloatingPointType aLength = (aMaxBoundPrj-aMinBoundPrj)*theDist;
1259 thePos[0] = aMinPnt[0]-theDirection[0]*aLength;
1260 thePos[1] = aMinPnt[1]-theDirection[1]*aLength;
1261 thePos[2] = aMinPnt[2]-theDirection[2]*aLength;
1264 // internal function (currently unused, left just in case)
1265 void PositionToDistance( vtkFloatingPointType theBounds[6],
1266 vtkFloatingPointType theDirection[3],
1267 vtkFloatingPointType thePos[3],
1268 vtkFloatingPointType& theDist )
1270 vtkFloatingPointType aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
1271 ComputeBoundsParam(theBounds,theDirection,aMinPnt,aMaxBoundPrj,aMinBoundPrj);
1272 vtkFloatingPointType aPrj = vtkMath::Dot(theDirection,thePos);
1273 theDist = (aPrj-aMinBoundPrj)/(aMaxBoundPrj-aMinBoundPrj);
1276 bool ComputeClippingPlaneParameters( std::list<vtkActor*> theActorList,
1277 vtkFloatingPointType theNormal[3],
1278 vtkFloatingPointType theDist,
1279 vtkFloatingPointType theBounds[6],
1280 vtkFloatingPointType theOrigin[3] )
1282 bool anIsOk = false;
1283 theBounds[0] = theBounds[2] = theBounds[4] = VTK_DOUBLE_MAX;
1284 theBounds[1] = theBounds[3] = theBounds[5] = -VTK_DOUBLE_MAX;
1285 std::list<vtkActor*>::iterator anIter = theActorList.begin();
1286 for( ; anIter != theActorList.end(); anIter++ ) {
1287 if( vtkActor* aVTKActor = *anIter ) {
1288 if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) ) {
1289 vtkFloatingPointType aBounds[6];
1290 anActor->GetUnstructuredGrid()->GetBounds( aBounds );
1291 theBounds[0] = std::min( theBounds[0], aBounds[0] );
1292 theBounds[1] = std::max( theBounds[1], aBounds[1] );
1293 theBounds[2] = std::min( theBounds[2], aBounds[2] );
1294 theBounds[3] = std::max( theBounds[3], aBounds[3] );
1295 theBounds[4] = std::min( theBounds[4], aBounds[4] );
1296 theBounds[5] = std::max( theBounds[5], aBounds[5] );
1305 DistanceToPosition( theBounds, theNormal, theDist, theOrigin );
1309 #ifndef DISABLE_PLOT2DVIEWER
1310 //================================================================================
1312 * \brief Find all SMESH_Actor's in the View Window.
1313 * If actor constains Plot2d_Histogram object remove it from each Plot2d Viewer.
1315 //================================================================================
1317 void ClearPlot2Viewers( SUIT_ViewWindow* theWindow ) {
1318 if(SVTK_ViewWindow* aViewWindow = GetVtkViewWindow(theWindow)){
1319 vtkRenderer *aRenderer = aViewWindow->getRenderer();
1320 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
1321 vtkActorCollection *aCollection = aCopy.GetActors();
1322 aCollection->InitTraversal();
1323 while(vtkActor *anAct = aCollection->GetNextActor()){
1324 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
1325 if(anActor->hasIO() && anActor->GetPlot2Histogram() ){
1326 ProcessIn2DViewers(anActor,RemoveFrom2dViewer);
1335 } // end of namespace SMESH