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"
34 #include <SMESH_Actor.h>
35 #include <SMESH_ActorUtils.h>
36 #include <SMESH_ObjectDef.h>
37 #include <SMDS_Mesh.hxx>
39 // SALOME GUI includes
40 #include <SUIT_Desktop.h>
41 #include <SUIT_Session.h>
42 #include <SUIT_MessageBox.h>
43 #include <SUIT_ViewManager.h>
44 #include <SUIT_ResourceMgr.h>
46 #include <SALOME_ListIO.hxx>
47 #include <SALOME_ListIteratorOfListIO.hxx>
49 #include <SVTK_Selector.h>
50 #include <SVTK_ViewModel.h>
51 #include <SVTK_ViewWindow.h>
53 #include <VTKViewer_Algorithm.h>
55 #include <LightApp_SelectionMgr.h>
56 #include <SalomeApp_Application.h>
57 #include <SalomeApp_Study.h>
59 // SALOME KERNEL includes
60 #include <utilities.h>
63 #include <SALOMEconfig.h>
64 #include CORBA_CLIENT_HEADER(SMESH_Mesh)
65 #include CORBA_CLIENT_HEADER(SMESH_Group)
69 #include <vtkRenderer.h>
70 #include <vtkActorCollection.h>
71 #include <vtkUnstructuredGrid.h>
74 #include <TColStd_IndexedMapOfInteger.hxx>
75 #include <Standard_ErrorHandler.hxx>
79 typedef std::map<TKeyOfVisualObj,TVisualObjPtr> TVisualObjCont;
80 static TVisualObjCont VISUAL_OBJ_CONT;
82 //=============================================================================
84 * \brief Allocate some memory at construction and release it at destruction.
85 * Is used to be able to continue working after mesh generation or visualization
86 * break due to lack of memory
88 //=============================================================================
93 MemoryReserve(): myBuf( new char[1024*1024*1] ){} // 1M
94 void Free() { if (myBuf) { delete [] myBuf; myBuf = 0; }}
95 ~MemoryReserve() { Free(); }
97 static MemoryReserve* theVISU_MemoryReserve = new MemoryReserve;
99 //================================================================================
101 * \brief Remove VisualObj and its actor from all views
103 //================================================================================
105 void RemoveVisualObjectWithActors( const char* theEntry )
107 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
108 ( SUIT_Session::session()->activeApplication() );
109 SUIT_ViewManager* aViewManager =
110 app ? app->getViewManager(SVTK_Viewer::Type(), true) : 0;
111 if ( aViewManager ) {
112 QVector<SUIT_ViewWindow*> views = aViewManager->getViews();
113 for ( int iV = 0; iV < views.count(); ++iV ) {
114 if ( SMESH_Actor* actor = FindActorByEntry( views[iV], theEntry)) {
115 if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(views[iV]))
116 vtkWnd->RemoveActor(actor);
120 int aStudyId = aViewManager->study()->id();
121 TVisualObjCont::key_type aKey(aStudyId,theEntry);
122 TVisualObjCont::iterator anIter = VISUAL_OBJ_CONT.find(aKey);
123 if(anIter != VISUAL_OBJ_CONT.end()) {
124 // for unknown reason, object destructor is not called, so clear object manually
125 anIter->second->GetUnstructuredGrid()->SetCells(0,0,0,0,0);
126 anIter->second->GetUnstructuredGrid()->SetPoints(0);
128 VISUAL_OBJ_CONT.erase(aKey);
131 //================================================================================
133 * \brief Remove all VisualObjs and their actors from all views
135 //================================================================================
137 void RemoveAllObjectsWithActors()
139 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
140 ( SUIT_Session::session()->activeApplication() );
142 ViewManagerList viewMgrs = app->viewManagers();
143 for ( int iM = 0; iM < viewMgrs.count(); ++iM ) {
144 SUIT_ViewManager* aViewManager = viewMgrs.at( iM );
145 if ( aViewManager && aViewManager->getType() == SVTK_Viewer::Type()) {
146 QVector<SUIT_ViewWindow*> views = aViewManager->getViews();
147 for ( int iV = 0; iV < views.count(); ++iV ) {
148 if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(views[iV])) {
149 vtkRenderer *aRenderer = vtkWnd->getRenderer();
150 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
151 vtkActorCollection *actors = aCopy.GetActors();
152 for (int i = 0; i < actors->GetNumberOfItems(); ++i ) {
153 // size of actors changes inside the loop
154 if (SMESH_Actor *actor = dynamic_cast<SMESH_Actor*>(actors->GetItemAsObject(i)))
156 vtkWnd->RemoveActor(actor);
164 TVisualObjCont::iterator anIter = VISUAL_OBJ_CONT.begin();
165 for ( ; anIter != VISUAL_OBJ_CONT.end(); ++anIter ) {
166 // for unknown reason, object destructor is not called, so clear object manually
167 anIter->second->GetUnstructuredGrid()->SetCells(0,0,0,0,0);
168 anIter->second->GetUnstructuredGrid()->SetPoints(0);
170 VISUAL_OBJ_CONT.clear();
173 //================================================================================
175 * \brief Remove all VisualObjs of a study
177 //================================================================================
179 void RemoveVisuData(int studyID)
181 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>
182 ( SUIT_Session::session()->activeApplication() );
184 ViewManagerList viewMgrs = app->viewManagers();
185 for ( int iM = 0; iM < viewMgrs.count(); ++iM ) {
186 SUIT_ViewManager* aViewManager = viewMgrs.at( iM );
187 if ( aViewManager && aViewManager->getType() == SVTK_Viewer::Type() &&
188 aViewManager->study()->id() == studyID ) {
189 QVector<SUIT_ViewWindow*> views = aViewManager->getViews();
190 for ( int iV = 0; iV < views.count(); ++iV ) {
191 if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(views[iV])) {
192 vtkRenderer *aRenderer = vtkWnd->getRenderer();
193 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
194 vtkActorCollection *actors = aCopy.GetActors();
195 for (int i = 0; i < actors->GetNumberOfItems(); ++i ) {
196 // size of actors changes inside the loop
197 if(SMESH_Actor *actor = dynamic_cast<SMESH_Actor*>(actors->GetItemAsObject(i)))
199 vtkWnd->RemoveActor(actor);
207 TVisualObjCont::iterator anIter = VISUAL_OBJ_CONT.begin();
208 for ( ; anIter != VISUAL_OBJ_CONT.end(); ) {
209 int curId = anIter->first.first;
210 if ( curId == studyID ) {
211 // for unknown reason, object destructor is not called, so clear object manually
212 anIter->second->GetUnstructuredGrid()->SetCells(0,0,0,0,0);
213 anIter->second->GetUnstructuredGrid()->SetPoints(0);
214 VISUAL_OBJ_CONT.erase( anIter++ ); // anIter++ returns a copy of self before incrementing
222 //================================================================================
224 * \brief Notify the user on problems during visualization
226 //================================================================================
228 void OnVisuException()
231 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
234 // PAL16774 (Crash after display of many groups). Salome sometimes crashes just
235 // after or at showing this message, so we do an additional check of available memory
236 // char* buf = new char[100*1024];
238 SUIT_MessageBox::warning(SMESHGUI::desktop(), QObject::tr("SMESH_WRN_WARNING"),
239 QObject::tr("SMESH_VISU_PROBLEM"));
241 // no more memory at all: last resort
242 MESSAGE_BEGIN ( "SMESHGUI_VTKUtils::OnVisuException(), exception even at showing a message!!!" <<
243 std::endl << "Try to remove all visual data..." );
244 if (theVISU_MemoryReserve) {
245 delete theVISU_MemoryReserve;
246 theVISU_MemoryReserve = 0;
248 RemoveAllObjectsWithActors();
249 SUIT_MessageBox::warning(SMESHGUI::desktop(), QObject::tr("SMESH_WRN_WARNING"),
250 QObject::tr("SMESH_VISU_PROBLEM_CLEAR"));
251 MESSAGE_END ( "...done" );
254 //================================================================================
256 * \brief Returns an updated visual object
258 //================================================================================
260 TVisualObjPtr GetVisualObj(int theStudyId, const char* theEntry, bool nulData){
261 TVisualObjPtr aVisualObj;
262 TVisualObjCont::key_type aKey(theStudyId,theEntry);
264 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
267 TVisualObjCont::iterator anIter = VISUAL_OBJ_CONT.find(aKey);
268 if(anIter != VISUAL_OBJ_CONT.end()){
269 aVisualObj = anIter->second;
271 SalomeApp_Application* app =
272 dynamic_cast<SalomeApp_Application*>( SMESHGUI::activeStudy()->application() );
273 _PTR(Study) aStudy = SMESHGUI::activeStudy()->studyDS();
274 _PTR(SObject) aSObj = aStudy->FindObjectID(theEntry);
276 _PTR(GenericAttribute) anAttr;
277 if(aSObj->FindAttribute(anAttr,"AttributeIOR")){
278 _PTR(AttributeIOR) anIOR = anAttr;
279 CORBA::String_var aVal = anIOR->Value().c_str();
280 CORBA::Object_var anObj = app->orb()->string_to_object( aVal.in() );
281 if(!CORBA::is_nil(anObj)){
282 //Try narrow to SMESH_Mesh interface
283 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(anObj);
284 if(!aMesh->_is_nil()){
285 aVisualObj.reset(new SMESH_MeshObj(aMesh));
286 TVisualObjCont::value_type aValue(aKey,aVisualObj);
287 VISUAL_OBJ_CONT.insert(aValue);
289 //Try narrow to SMESH_Group interface
290 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(anObj);
291 if(!aGroup->_is_nil()){
292 _PTR(SObject) aFatherSObj = aSObj->GetFather();
293 if(!aFatherSObj) return aVisualObj;
294 aFatherSObj = aFatherSObj->GetFather();
295 if(!aFatherSObj) return aVisualObj;
296 CORBA::String_var anEntry = aFatherSObj->GetID().c_str();
297 TVisualObjPtr aVisObj = GetVisualObj(theStudyId,anEntry.in());
298 if(SMESH_MeshObj* aMeshObj = dynamic_cast<SMESH_MeshObj*>(aVisObj.get())){
299 aVisualObj.reset(new SMESH_GroupObj(aGroup,aMeshObj));
300 TVisualObjCont::value_type aValue(aKey,aVisualObj);
301 VISUAL_OBJ_CONT.insert(aValue);
304 //Try narrow to SMESH_subMesh interface
305 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(anObj);
306 if(!aSubMesh->_is_nil()){
307 _PTR(SObject) aFatherSObj = aSObj->GetFather();
308 if(!aFatherSObj) return aVisualObj;
309 aFatherSObj = aFatherSObj->GetFather();
310 if(!aFatherSObj) return aVisualObj;
311 CORBA::String_var anEntry = aFatherSObj->GetID().c_str();
312 TVisualObjPtr aVisObj = GetVisualObj(theStudyId,anEntry.in());
313 if(SMESH_MeshObj* aMeshObj = dynamic_cast<SMESH_MeshObj*>(aVisObj.get())){
314 aVisualObj.reset(new SMESH_subMeshObj(aSubMesh,aMeshObj));
315 TVisualObjCont::value_type aValue(aKey,aVisualObj);
316 VISUAL_OBJ_CONT.insert(aValue);
324 INFOS("GetMeshObj - There is no SMESH_Mesh object for the SALOMEDS::Strudy and Entry!!!");
325 return TVisualObjPtr();
328 bool objModified = false;
331 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
334 //MESSAGE("GetVisualObj");
336 objModified = aVisualObj->NulData();
338 objModified = aVisualObj->Update();
342 MESSAGE ( "Exception in SMESHGUI_VTKUtils::GetVisualObj()" );
344 RemoveVisualObjectWithActors( theEntry ); // remove this object
351 // PAL16631. Mesurements showed that to show aVisualObj in SHADING(default) mode,
352 // ~10 times more memory is used than it occupies.
353 // Warn the user if there is less free memory than 30 sizes of a grid
354 // TODO: estimate memory usage in other modes and take current mode into account
355 int freeMB = SMDS_Mesh::CheckMemory(true);
356 int usedMB = aVisualObj->GetUnstructuredGrid()->GetActualMemorySize() / 1024;
357 if ( freeMB > 0 && usedMB * 30 > freeMB ) {
359 MESSAGE ( "SMESHGUI_VTKUtils::GetVisualObj(), freeMB=" << freeMB
360 << ", usedMB=" << usedMB );
362 // bool continu = false;
363 // if ( usedMB * 10 > freeMB )
364 // // even dont try to show
365 // SUIT_MessageBox::warning(SMESHGUI::desktop(), QObject::tr("SMESH_WRN_WARNING"),
366 // QObject::tr("SMESH_NO_MESH_VISUALIZATION"));
368 // // there is a chance to succeed
369 // continu = SUIT_MessageBox::warning
370 // (SMESHGUI::desktop(),
371 // QObject::tr("SMESH_WRN_WARNING"),
372 // QObject::tr("SMESH_CONTINUE_MESH_VISUALIZATION"),
373 // SUIT_MessageBox::Yes | SUIT_MessageBox::No,
374 // SUIT_MessageBox::Yes ) == SUIT_MessageBox::Yes;
376 // // remove the corresponding actors from all views
377 // RemoveVisualObjectWithActors( theEntry );
378 // aVisualObj.reset();
387 /*! Return active view window, if it instantiates SVTK_ViewWindow class,
388 * overwise find or create corresponding view window, make it active and return it.
389 * \note Active VVTK_ViewWindow can be returned, because it inherits SVTK_ViewWindow.
391 SVTK_ViewWindow* GetViewWindow (const SalomeApp_Module* theModule,
392 bool createIfNotFound)
394 SalomeApp_Application* anApp;
396 anApp = theModule->getApp();
398 anApp = dynamic_cast<SalomeApp_Application*>
399 (SUIT_Session::session()->activeApplication());
402 if (SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(anApp->desktop()->activeWindow()))
405 SUIT_ViewManager* aViewManager =
406 anApp->getViewManager(SVTK_Viewer::Type(), createIfNotFound);
408 if (SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView()) {
409 if (SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)) {
410 aViewWindow->raise();
411 aViewWindow->setFocus();
420 SVTK_ViewWindow* FindVtkViewWindow (SUIT_ViewManager* theMgr,
421 SUIT_ViewWindow * theWindow)
426 QVector<SUIT_ViewWindow*> views = theMgr->getViews();
427 if( views.contains( theWindow ) )
428 return GetVtkViewWindow( theWindow );
433 SVTK_ViewWindow* GetVtkViewWindow(SUIT_ViewWindow* theWindow){
434 return dynamic_cast<SVTK_ViewWindow*>(theWindow);
437 /* SUIT_ViewWindow* GetActiveWindow()
439 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
442 SUIT_ViewManager* mgr = app->activeViewManager();
444 return mgr->getActiveView();
449 SVTK_ViewWindow* GetCurrentVtkView(){
450 return GetVtkViewWindow( GetActiveWindow() );
454 void RepaintCurrentView()
456 if (SVTK_ViewWindow* wnd = GetCurrentVtkView())
459 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
462 wnd->getRenderer()->Render();
467 MESSAGE ( "Exception in SMESHGUI_VTKUtils::RepaintCurrentView()" );
474 void RepaintViewWindow(SVTK_ViewWindow* theWindow)
477 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
480 theWindow->getRenderer()->Render();
481 theWindow->Repaint();
485 MESSAGE ( "Exception in SMESHGUI_VTKUtils::RepaintViewWindow(SVTK_ViewWindow*)" );
491 void RenderViewWindow(SVTK_ViewWindow* theWindow)
494 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
497 theWindow->getRenderer()->Render();
498 theWindow->Repaint();
502 MESSAGE ( "Exception in SMESHGUI_VTKUtils::RenderViewWindow(SVTK_ViewWindow*)" );
509 if(SVTK_ViewWindow* wnd = GetCurrentVtkView() ){
511 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
519 MESSAGE ( "Exception in SMESHGUI_VTKUtils::FitAll()" );
527 SMESH_Actor* FindActorByEntry(SUIT_ViewWindow *theWindow,
528 const char* theEntry)
530 if(SVTK_ViewWindow* aViewWindow = GetVtkViewWindow(theWindow)){
531 vtkRenderer *aRenderer = aViewWindow->getRenderer();
532 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
533 vtkActorCollection *aCollection = aCopy.GetActors();
534 aCollection->InitTraversal();
535 while(vtkActor *anAct = aCollection->GetNextActor()){
536 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
537 if(anActor->hasIO()){
538 Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
539 if(anIO->hasEntry() && strcmp(anIO->getEntry(),theEntry) == 0){
550 SMESH_Actor* FindActorByEntry(const char* theEntry){
551 return FindActorByEntry(GetActiveWindow(),theEntry);
555 SMESH_Actor* FindActorByObject(CORBA::Object_ptr theObject){
556 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
560 if(!CORBA::is_nil(theObject)){
561 _PTR(Study) aStudy = GetActiveStudyDocument();
562 CORBA::String_var anIOR = app->orb()->object_to_string( theObject );
563 _PTR(SObject) aSObject = aStudy->FindObjectIOR(anIOR.in());
565 CORBA::String_var anEntry = aSObject->GetID().c_str();
566 return FindActorByEntry(anEntry.in());
573 SMESH_Actor* CreateActor(_PTR(Study) theStudy,
574 const char* theEntry,
577 SMESH_Actor *anActor = NULL;
578 CORBA::Long anId = theStudy->StudyId();
579 if(TVisualObjPtr aVisualObj = GetVisualObj(anId,theEntry)){
580 _PTR(SObject) aSObj = theStudy->FindObjectID(theEntry);
582 _PTR(GenericAttribute) anAttr;
583 if(aSObj->FindAttribute(anAttr,"AttributeName")){
584 _PTR(AttributeName) aName = anAttr;
585 std::string aNameVal = aName->Value();
586 anActor = SMESH_Actor::New(aVisualObj,theEntry,aNameVal.c_str(),theIsClear);
589 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow( SMESH::SObjectToObject( aSObj ));
590 if(!CORBA::is_nil(aGroup))
592 SALOMEDS::Color aColor = aGroup->GetColor();
593 if( !( aColor.R > 0 || aColor.G > 0 || aColor.B > 0 ) )
595 int r = 0, g = 0, b = 0;
596 SMESH::GetColor( "SMESH", "fill_color", r, g, b, QColor( 0, 170, 255 ) );
597 aColor.R = (float)r / 255.0;
598 aColor.G = (float)g / 255.0;
599 aColor.B = (float)b / 255.0;
600 aGroup->SetColor( aColor );
602 if( aGroup->GetType() == SMESH::NODE )
603 anActor->SetNodeColor( aColor.R, aColor.G, aColor.B );
604 else if( aGroup->GetType() == SMESH::EDGE )
605 anActor->SetEdgeColor( aColor.R, aColor.G, aColor.B );
606 else if( aGroup->GetType() == SMESH::ELEM0D )
607 anActor->Set0DColor( aColor.R, aColor.G, aColor.B );
609 anActor->SetSufaceColor( aColor.R, aColor.G, aColor.B );
613 MESSAGE("CreateActor " << anActor);
615 if( SMESHGUI* aSMESHGUI = SMESHGUI::GetSMESHGUI() )
616 aSMESHGUI->addActorAsObserver( anActor );
621 void DisplayActor( SUIT_ViewWindow *theWnd, SMESH_Actor* theActor){
622 if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(theWnd)){
624 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
627 MESSAGE("DisplayActor " << theActor);
628 vtkWnd->AddActor(theActor);
633 MESSAGE ( "Exception in SMESHGUI_VTKUtils::DisplayActor()" );
641 void RemoveActor( SUIT_ViewWindow *theWnd, SMESH_Actor* theActor){
642 if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(theWnd)){
643 MESSAGE("RemoveActor " << theActor);
644 vtkWnd->RemoveActor(theActor);
645 if(theActor->hasIO()){
646 Handle(SALOME_InteractiveObject) anIO = theActor->getIO();
647 if(anIO->hasEntry()){
648 std::string anEntry = anIO->getEntry();
649 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( vtkWnd->getViewManager()->study() );
650 int aStudyId = aStudy->id();
651 TVisualObjCont::key_type aKey(aStudyId,anEntry);
652 VISUAL_OBJ_CONT.erase(aKey);
660 //================================================================================
662 * \brief Return true if there are no SMESH actors in a view
664 //================================================================================
666 bool noSmeshActors(SUIT_ViewWindow *theWnd)
668 if(SVTK_ViewWindow* aViewWindow = GetVtkViewWindow(theWnd)) {
669 vtkRenderer *aRenderer = aViewWindow->getRenderer();
670 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
671 vtkActorCollection *aCollection = aCopy.GetActors();
672 aCollection->InitTraversal();
673 while(vtkActor *anAct = aCollection->GetNextActor())
674 if(dynamic_cast<SMESH_Actor*>(anAct))
680 bool UpdateView(SUIT_ViewWindow *theWnd, EDisplaing theAction, const char* theEntry)
682 //MESSAGE("UpdateView");
684 SVTK_ViewWindow* aViewWnd = GetVtkViewWindow(theWnd);
690 vtkRenderer *aRenderer = aViewWnd->getRenderer();
691 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
692 vtkActorCollection *aCollection = aCopy.GetActors();
693 aCollection->InitTraversal();
697 while (vtkActor *anAct = aCollection->GetNextActor()) {
698 if (SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)) {
699 MESSAGE("--- display " << anActor);
700 anActor->SetVisibility(true);
707 //MESSAGE("---case eDisplayOnly");
708 while (vtkActor *anAct = aCollection->GetNextActor()) {
709 if (SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)) {
710 //MESSAGE("--- erase " << anActor);
711 anActor->SetVisibility(false);
716 if (SMESH_Actor *anActor = FindActorByEntry(theWnd,theEntry)) {
720 //MESSAGE("--- display " << anActor);
721 anActor->SetVisibility(true);
722 if (theAction == eDisplayOnly) aRenderer->ResetCameraClippingRange();
725 //MESSAGE("--- erase " << anActor);
726 anActor->SetVisibility(false);
735 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(theWnd->getViewManager()->study());
736 _PTR(Study) aDocument = aStudy->studyDS();
737 // Pass non-visual objects (hypotheses, etc.), return true in this case
738 CORBA::Long anId = aDocument->StudyId();
739 TVisualObjPtr aVisualObj;
740 if ( (aVisualObj = GetVisualObj(anId,theEntry)) && aVisualObj->IsValid())
742 if ((anActor = CreateActor(aDocument,theEntry,true))) {
743 bool needFitAll = noSmeshActors(theWnd); // fit for the first object only
744 DisplayActor(theWnd,anActor);
745 // FitAll(); - PAL16770(Display of a group performs an automatic fit all)
746 if (needFitAll) FitAll();
762 bool UpdateView(EDisplaing theAction, const char* theEntry){
763 //MESSAGE("UpdateView");
764 SalomeApp_Study* aStudy = dynamic_cast< SalomeApp_Study* >( GetActiveStudy() );
765 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( aStudy->application() );
766 SUIT_ViewWindow *aWnd = app->activeViewManager()->getActiveView();
767 return UpdateView(aWnd,theAction,theEntry);
771 if(SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView()){
772 LightApp_SelectionMgr* mgr = SMESHGUI::selectionMgr();
773 SALOME_ListIO selected; mgr->selectedObjects( selected );
775 if( selected.Extent() == 0){
776 vtkRenderer* aRenderer = aWnd->getRenderer();
777 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
778 vtkActorCollection *aCollection = aCopy.GetActors();
779 aCollection->InitTraversal();
780 while(vtkActor *anAct = aCollection->GetNextActor()){
781 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
783 if (!Update(anActor->getIO(),anActor->GetVisibility()))
784 break; // avoid multiple warinings if visu failed
788 SALOME_ListIteratorOfListIO anIter( selected );
789 for( ; anIter.More(); anIter.Next()){
790 Handle(SALOME_InteractiveObject) anIO = anIter.Value();
791 if ( !Update(anIO,true) )
792 break; // avoid multiple warinings if visu failed
795 RepaintCurrentView();
800 bool Update(const Handle(SALOME_InteractiveObject)& theIO, bool theDisplay)
803 _PTR(Study) aStudy = GetActiveStudyDocument();
804 CORBA::Long anId = aStudy->StudyId();
805 if ( TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId,theIO->getEntry())) {
807 UpdateView(SMESH::eDisplay,theIO->getEntry());
813 bool UpdateNulData(const Handle(SALOME_InteractiveObject)& theIO, bool theDisplay)
815 MESSAGE("UpdateNulData");
816 _PTR(Study) aStudy = GetActiveStudyDocument();
817 CORBA::Long anId = aStudy->StudyId();
818 if ( TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId,theIO->getEntry(), true)) {
820 UpdateView(SMESH::eDisplay,theIO->getEntry());
826 void UpdateSelectionProp( SMESHGUI* theModule ) {
830 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( theModule->application() );
833 MESSAGE( "UpdateSelectionProp: Application is null" );
837 SUIT_ViewManager* vm = app->activeViewManager();
840 MESSAGE( "UpdateSelectionProp: View manager is null" );
844 QVector<SUIT_ViewWindow*> views = vm->getViews();
846 SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( theModule );
849 MESSAGE( "UpdateSelectionProp: Resource manager is null" );
853 QColor aHiColor = mgr->colorValue( "SMESH", "selection_object_color", Qt::white ),
854 aSelColor = mgr->colorValue( "SMESH", "selection_element_color", Qt::yellow ),
855 aPreColor = mgr->colorValue( "SMESH", "highlight_color", Qt::cyan );
857 int SW = mgr->integerValue( "SMESH", "selection_width", 5 ),
858 PW = mgr->integerValue( "SMESH", "highlight_width", 5 );
860 // adjust highlight_width to the width of mesh entities
861 int aElem0DSize = mgr->integerValue("SMESH", "elem0d_size", 5);
862 int aLineWidth = mgr->integerValue("SMESH", "element_width", 1);
863 int maxSize = aElem0DSize;
864 if (aElem0DSize > maxSize) maxSize = aElem0DSize;
865 if (aLineWidth > maxSize) maxSize = aLineWidth;
866 if (PW < maxSize + 2) PW = maxSize + 2;
868 double SP1 = mgr->doubleValue( "SMESH", "selection_precision_node", 0.025 ),
869 SP2 = mgr->doubleValue( "SMESH", "selection_precision_element", 0.001 ),
870 SP3 = mgr->doubleValue( "SMESH", "selection_precision_object", 0.025 );
872 for ( int i=0, n=views.count(); i<n; i++ ){
873 // update VTK viewer properties
874 if(SVTK_ViewWindow* aVtkView = GetVtkViewWindow( views[i] )){
875 // mesh element selection
876 aVtkView->SetSelectionProp(aSelColor.red()/255.,
877 aSelColor.green()/255.,
878 aSelColor.blue()/255.,
881 aVtkView->SetSelectionTolerance(SP1, SP2, SP3);
884 aVtkView->SetPreselectionProp(aPreColor.red()/255.,
885 aPreColor.green()/255.,
886 aPreColor.blue()/255.,
889 vtkRenderer* aRenderer = aVtkView->getRenderer();
890 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
891 vtkActorCollection *aCollection = aCopy.GetActors();
892 aCollection->InitTraversal();
893 while(vtkActor *anAct = aCollection->GetNextActor()){
894 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
895 anActor->SetHighlightColor(aHiColor.red()/255.,
896 aHiColor.green()/255.,
897 aHiColor.blue()/255.);
898 anActor->SetPreHighlightColor(aPreColor.red()/255.,
899 aPreColor.green()/255.,
900 aPreColor.blue()/255.);
908 //----------------------------------------------------------------------------
910 GetSelector(SUIT_ViewWindow *theWindow)
912 if(SVTK_ViewWindow* aWnd = GetVtkViewWindow(theWindow))
913 return aWnd->GetSelector();
918 void SetFilter(const Handle(VTKViewer_Filter)& theFilter,
919 SVTK_Selector* theSelector)
922 theSelector->SetFilter(theFilter);
925 Handle(VTKViewer_Filter) GetFilter(int theId, SVTK_Selector* theSelector)
927 return theSelector->GetFilter(theId);
930 bool IsFilterPresent(int theId, SVTK_Selector* theSelector)
932 return theSelector->IsFilterPresent(theId);
935 void RemoveFilter(int theId, SVTK_Selector* theSelector)
937 theSelector->RemoveFilter(theId);
940 void RemoveFilters(SVTK_Selector* theSelector)
942 for ( int id = SMESH::NodeFilter; theSelector && id < SMESH::LastFilter; id++ )
943 theSelector->RemoveFilter( id );
946 bool IsValid(SALOME_Actor* theActor, int theCellId,
947 SVTK_Selector* theSelector)
949 return theSelector->IsValid(theActor,theCellId);
953 //----------------------------------------------------------------------------
954 void SetPointRepresentation(bool theIsVisible){
955 if(SVTK_ViewWindow* aViewWindow = GetCurrentVtkView()){
956 vtkRenderer *aRenderer = aViewWindow->getRenderer();
957 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
958 vtkActorCollection *aCollection = aCopy.GetActors();
959 aCollection->InitTraversal();
960 while(vtkActor *anAct = aCollection->GetNextActor()){
961 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
962 if(anActor->GetVisibility()){
963 anActor->SetPointRepresentation(theIsVisible);
967 RepaintCurrentView();
972 void SetPickable(SMESH_Actor* theActor){
973 if(SVTK_ViewWindow* aWnd = GetCurrentVtkView()){
974 int anIsAllPickable = (theActor == NULL);
975 vtkRenderer *aRenderer = aWnd->getRenderer();
976 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
977 vtkActorCollection *aCollection = aCopy.GetActors();
978 aCollection->InitTraversal();
979 while(vtkActor *anAct = aCollection->GetNextActor()){
980 if(SALOME_Actor *anActor = dynamic_cast<SALOME_Actor*>(anAct)){
981 if(anActor->GetVisibility()){
982 anActor->SetPickable(anIsAllPickable);
987 theActor->SetPickable(!anIsAllPickable);
988 RepaintCurrentView();
993 //----------------------------------------------------------------------------
994 int GetNameOfSelectedNodes(SVTK_Selector* theSelector,
995 const Handle(SALOME_InteractiveObject)& theIO,
999 TColStd_IndexedMapOfInteger aMapIndex;
1000 theSelector->GetIndex(theIO,aMapIndex);
1002 for(int i = 1; i <= aMapIndex.Extent(); i++)
1003 theName += QString(" %1").arg(aMapIndex(i));
1005 return aMapIndex.Extent();
1008 int GetNameOfSelectedElements(SVTK_Selector* theSelector,
1009 const Handle(SALOME_InteractiveObject)& theIO,
1013 TColStd_IndexedMapOfInteger aMapIndex;
1014 theSelector->GetIndex(theIO,aMapIndex);
1016 typedef std::set<int> TIdContainer;
1017 TIdContainer anIdContainer;
1018 for( int i = 1; i <= aMapIndex.Extent(); i++)
1019 anIdContainer.insert(aMapIndex(i));
1021 TIdContainer::const_iterator anIter = anIdContainer.begin();
1022 for( ; anIter != anIdContainer.end(); anIter++)
1023 theName += QString(" %1").arg(*anIter);
1025 return aMapIndex.Extent();
1029 int GetEdgeNodes(SVTK_Selector* theSelector,
1030 const TVisualObjPtr& theVisualObject,
1034 const SALOME_ListIO& selected = theSelector->StoredIObjects();
1036 if ( selected.Extent() != 1 )
1039 Handle(SALOME_InteractiveObject) anIO = selected.First();
1040 if ( anIO.IsNull() || !anIO->hasEntry() )
1043 TColStd_IndexedMapOfInteger aMapIndex;
1044 theSelector->GetIndex( anIO, aMapIndex );
1045 if ( aMapIndex.Extent() != 2 )
1048 int anObjId = -1, anEdgeNum = -1;
1049 for ( int i = 1; i <= aMapIndex.Extent(); i++ ) {
1050 int aVal = aMapIndex( i );
1054 anEdgeNum = abs( aVal ) - 1;
1057 if ( anObjId == -1 || anEdgeNum == -1 )
1060 return theVisualObject->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
1063 //----------------------------------------------------------------------------
1064 int GetNameOfSelectedNodes(LightApp_SelectionMgr *theMgr,
1065 const Handle(SALOME_InteractiveObject)& theIO,
1069 if(theIO->hasEntry()){
1070 if(FindActorByEntry(theIO->getEntry())){
1071 TColStd_IndexedMapOfInteger aMapIndex;
1072 theMgr->GetIndexes(theIO,aMapIndex);
1073 for(int i = 1; i <= aMapIndex.Extent(); i++){
1074 theName += QString(" %1").arg(aMapIndex(i));
1076 return aMapIndex.Extent();
1082 int GetNameOfSelectedNodes(LightApp_SelectionMgr *theMgr, QString& theName){
1084 SALOME_ListIO selected; theMgr->selectedObjects( selected );
1085 if(selected.Extent() == 1){
1086 Handle(SALOME_InteractiveObject) anIO = selected.First();
1087 return GetNameOfSelectedNodes(theMgr,anIO,theName);
1093 int GetNameOfSelectedElements(LightApp_SelectionMgr *theMgr,
1094 const Handle(SALOME_InteractiveObject)& theIO,
1098 if(theIO->hasEntry()){
1099 if(FindActorByEntry(theIO->getEntry())){
1100 TColStd_IndexedMapOfInteger aMapIndex;
1101 theMgr->GetIndexes(theIO,aMapIndex);
1102 typedef std::set<int> TIdContainer;
1103 TIdContainer anIdContainer;
1104 for( int i = 1; i <= aMapIndex.Extent(); i++)
1105 anIdContainer.insert(aMapIndex(i));
1106 TIdContainer::const_iterator anIter = anIdContainer.begin();
1107 for( ; anIter != anIdContainer.end(); anIter++){
1108 theName += QString(" %1").arg(*anIter);
1110 return aMapIndex.Extent();
1117 int GetNameOfSelectedElements(LightApp_SelectionMgr *theMgr, QString& theName)
1120 SALOME_ListIO selected; theMgr->selectedObjects( selected );
1122 if( selected.Extent() == 1){
1123 Handle(SALOME_InteractiveObject) anIO = selected.First();
1124 return GetNameOfSelectedElements(theMgr,anIO,theName);
1129 int GetSelected(LightApp_SelectionMgr* theMgr,
1130 TColStd_IndexedMapOfInteger& theMap,
1131 const bool theIsElement)
1134 SALOME_ListIO selected; theMgr->selectedObjects( selected );
1136 if ( selected.Extent() == 1 )
1138 Handle(SALOME_InteractiveObject) anIO = selected.First();
1139 if ( anIO->hasEntry() ) {
1140 theMgr->GetIndexes( anIO, theMap );
1143 return theMap.Extent();
1147 int GetEdgeNodes( LightApp_SelectionMgr* theMgr, int& theId1, int& theId2 )
1149 SALOME_ListIO selected; theMgr->selectedObjects( selected );
1151 if ( selected.Extent() != 1 )
1154 Handle(SALOME_InteractiveObject) anIO = selected.First();
1155 if ( anIO.IsNull() || !anIO->hasEntry() )
1158 SMESH_Actor *anActor = SMESH::FindActorByEntry( anIO->getEntry() );
1162 TColStd_IndexedMapOfInteger aMapIndex;
1163 theMgr->GetIndexes( anIO, aMapIndex );
1164 if ( aMapIndex.Extent() != 2 )
1167 int anObjId = -1, anEdgeNum = -1;
1168 for ( int i = 1; i <= aMapIndex.Extent(); i++ ) {
1169 int aVal = aMapIndex( i );
1173 anEdgeNum = abs( aVal );
1176 if ( anObjId == -1 || anEdgeNum == -1 )
1179 return anActor->GetObject()->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
1182 void SetControlsPrecision( const long theVal )
1184 if( SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView() )
1186 vtkRenderer *aRenderer = aWnd->getRenderer();
1187 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
1188 vtkActorCollection *aCollection = aCopy.GetActors();
1189 aCollection->InitTraversal();
1191 while ( vtkActor *anAct = aCollection->GetNextActor())
1193 if ( SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>( anAct ) )
1195 anActor->SetControlsPrecision( theVal );
1196 anActor->SetControlMode( anActor->GetControlMode() );
1203 //----------------------------------------------------------------------------
1204 // internal function
1205 void ComputeBoundsParam( vtkFloatingPointType theBounds[6],
1206 vtkFloatingPointType theDirection[3],
1207 vtkFloatingPointType theMinPnt[3],
1208 vtkFloatingPointType& theMaxBoundPrj,
1209 vtkFloatingPointType& theMinBoundPrj )
1211 //Enlarge bounds in order to avoid conflicts of precision
1212 for(int i = 0; i < 6; i += 2){
1213 static double EPS = 1.0E-3;
1214 vtkFloatingPointType aDelta = (theBounds[i+1] - theBounds[i])*EPS;
1215 theBounds[i] -= aDelta;
1216 theBounds[i+1] += aDelta;
1219 vtkFloatingPointType aBoundPoints[8][3] = { {theBounds[0],theBounds[2],theBounds[4]},
1220 {theBounds[1],theBounds[2],theBounds[4]},
1221 {theBounds[0],theBounds[3],theBounds[4]},
1222 {theBounds[1],theBounds[3],theBounds[4]},
1223 {theBounds[0],theBounds[2],theBounds[5]},
1224 {theBounds[1],theBounds[2],theBounds[5]},
1225 {theBounds[0],theBounds[3],theBounds[5]},
1226 {theBounds[1],theBounds[3],theBounds[5]}};
1228 int aMaxId = 0, aMinId = aMaxId;
1229 theMaxBoundPrj = vtkMath::Dot(theDirection,aBoundPoints[aMaxId]);
1230 theMinBoundPrj = theMaxBoundPrj;
1231 for(int i = 1; i < 8; i++){
1232 vtkFloatingPointType aTmp = vtkMath::Dot(theDirection,aBoundPoints[i]);
1233 if(theMaxBoundPrj < aTmp){
1234 theMaxBoundPrj = aTmp;
1237 if(theMinBoundPrj > aTmp){
1238 theMinBoundPrj = aTmp;
1242 vtkFloatingPointType *aMinPnt = aBoundPoints[aMaxId];
1243 theMinPnt[0] = aMinPnt[0];
1244 theMinPnt[1] = aMinPnt[1];
1245 theMinPnt[2] = aMinPnt[2];
1248 // internal function
1249 void DistanceToPosition( vtkFloatingPointType theBounds[6],
1250 vtkFloatingPointType theDirection[3],
1251 vtkFloatingPointType theDist,
1252 vtkFloatingPointType thePos[3] )
1254 vtkFloatingPointType aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
1255 ComputeBoundsParam(theBounds,theDirection,aMinPnt,aMaxBoundPrj,aMinBoundPrj);
1256 vtkFloatingPointType aLength = (aMaxBoundPrj-aMinBoundPrj)*theDist;
1257 thePos[0] = aMinPnt[0]-theDirection[0]*aLength;
1258 thePos[1] = aMinPnt[1]-theDirection[1]*aLength;
1259 thePos[2] = aMinPnt[2]-theDirection[2]*aLength;
1262 // internal function (currently unused, left just in case)
1263 void PositionToDistance( vtkFloatingPointType theBounds[6],
1264 vtkFloatingPointType theDirection[3],
1265 vtkFloatingPointType thePos[3],
1266 vtkFloatingPointType& theDist )
1268 vtkFloatingPointType aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
1269 ComputeBoundsParam(theBounds,theDirection,aMinPnt,aMaxBoundPrj,aMinBoundPrj);
1270 vtkFloatingPointType aPrj = vtkMath::Dot(theDirection,thePos);
1271 theDist = (aPrj-aMinBoundPrj)/(aMaxBoundPrj-aMinBoundPrj);
1274 bool ComputeClippingPlaneParameters( std::list<vtkActor*> theActorList,
1275 vtkFloatingPointType theNormal[3],
1276 vtkFloatingPointType theDist,
1277 vtkFloatingPointType theBounds[6],
1278 vtkFloatingPointType theOrigin[3] )
1280 bool anIsOk = false;
1281 theBounds[0] = theBounds[2] = theBounds[4] = VTK_DOUBLE_MAX;
1282 theBounds[1] = theBounds[3] = theBounds[5] = -VTK_DOUBLE_MAX;
1283 std::list<vtkActor*>::iterator anIter = theActorList.begin();
1284 for( ; anIter != theActorList.end(); anIter++ ) {
1285 if( vtkActor* aVTKActor = *anIter ) {
1286 if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) ) {
1287 vtkFloatingPointType aBounds[6];
1288 anActor->GetUnstructuredGrid()->GetBounds( aBounds );
1289 theBounds[0] = std::min( theBounds[0], aBounds[0] );
1290 theBounds[1] = std::max( theBounds[1], aBounds[1] );
1291 theBounds[2] = std::min( theBounds[2], aBounds[2] );
1292 theBounds[3] = std::max( theBounds[3], aBounds[3] );
1293 theBounds[4] = std::min( theBounds[4], aBounds[4] );
1294 theBounds[5] = std::max( theBounds[5], aBounds[5] );
1303 DistanceToPosition( theBounds, theNormal, theDist, theOrigin );
1306 } // end of namespace SMESH