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);
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);
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);
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){
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 objModified = aVisualObj->Update();
338 MESSAGE ( "Exception in SMESHGUI_VTKUtils::GetVisualObj()" );
340 RemoveVisualObjectWithActors( theEntry ); // remove this object
347 // PAL16631. Mesurements showed that to show aVisualObj in SHADING(default) mode,
348 // ~10 times more memory is used than it occupies.
349 // Warn the user if there is less free memory than 30 sizes of a grid
350 // TODO: estimate memory usage in other modes and take current mode into account
351 int freeMB = SMDS_Mesh::CheckMemory(true);
352 int usedMB = aVisualObj->GetUnstructuredGrid()->GetActualMemorySize() / 1024;
353 if ( freeMB > 0 && usedMB * 30 > freeMB ) {
355 MESSAGE ( "SMESHGUI_VTKUtils::GetVisualObj(), freeMB=" << freeMB
356 << ", usedMB=" << usedMB );
358 bool continu = false;
359 if ( usedMB * 10 > freeMB )
360 // even dont try to show
361 SUIT_MessageBox::warning(SMESHGUI::desktop(), QObject::tr("SMESH_WRN_WARNING"),
362 QObject::tr("SMESH_NO_MESH_VISUALIZATION"));
364 // there is a chance to succeed
365 continu = SUIT_MessageBox::warning
366 (SMESHGUI::desktop(),
367 QObject::tr("SMESH_WRN_WARNING"),
368 QObject::tr("SMESH_CONTINUE_MESH_VISUALIZATION"),
369 SUIT_MessageBox::Yes | SUIT_MessageBox::No,
370 SUIT_MessageBox::Yes ) == SUIT_MessageBox::Yes;
372 // remove the corresponding actors from all views
373 RemoveVisualObjectWithActors( theEntry );
383 /*! Return active view window, if it instantiates SVTK_ViewWindow class,
384 * overwise find or create corresponding view window, make it active and return it.
385 * \note Active VVTK_ViewWindow can be returned, because it inherits SVTK_ViewWindow.
387 SVTK_ViewWindow* GetViewWindow (const SalomeApp_Module* theModule,
388 bool createIfNotFound)
390 SalomeApp_Application* anApp;
392 anApp = theModule->getApp();
394 anApp = dynamic_cast<SalomeApp_Application*>
395 (SUIT_Session::session()->activeApplication());
398 if (SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(anApp->desktop()->activeWindow()))
401 SUIT_ViewManager* aViewManager =
402 anApp->getViewManager(SVTK_Viewer::Type(), createIfNotFound);
404 if (SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView()) {
405 if (SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)) {
406 aViewWindow->raise();
407 aViewWindow->setFocus();
416 SVTK_ViewWindow* FindVtkViewWindow (SUIT_ViewManager* theMgr,
417 SUIT_ViewWindow * theWindow)
422 QVector<SUIT_ViewWindow*> views = theMgr->getViews();
423 if( views.contains( theWindow ) )
424 return GetVtkViewWindow( theWindow );
429 SVTK_ViewWindow* GetVtkViewWindow(SUIT_ViewWindow* theWindow){
430 return dynamic_cast<SVTK_ViewWindow*>(theWindow);
433 /* SUIT_ViewWindow* GetActiveWindow()
435 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
438 SUIT_ViewManager* mgr = app->activeViewManager();
440 return mgr->getActiveView();
445 SVTK_ViewWindow* GetCurrentVtkView(){
446 return GetVtkViewWindow( GetActiveWindow() );
450 void RepaintCurrentView()
452 if (SVTK_ViewWindow* wnd = GetCurrentVtkView())
455 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
458 wnd->getRenderer()->Render();
463 MESSAGE ( "Exception in SMESHGUI_VTKUtils::RepaintCurrentView()" );
470 void RepaintViewWindow(SVTK_ViewWindow* theWindow)
473 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
476 theWindow->getRenderer()->Render();
477 theWindow->Repaint();
481 MESSAGE ( "Exception in SMESHGUI_VTKUtils::RepaintViewWindow(SVTK_ViewWindow*)" );
487 void RenderViewWindow(SVTK_ViewWindow* theWindow)
490 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
493 theWindow->getRenderer()->Render();
494 theWindow->Repaint();
498 MESSAGE ( "Exception in SMESHGUI_VTKUtils::RenderViewWindow(SVTK_ViewWindow*)" );
505 if(SVTK_ViewWindow* wnd = GetCurrentVtkView() ){
507 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
515 MESSAGE ( "Exception in SMESHGUI_VTKUtils::FitAll()" );
523 SMESH_Actor* FindActorByEntry(SUIT_ViewWindow *theWindow,
524 const char* theEntry)
526 if(SVTK_ViewWindow* aViewWindow = GetVtkViewWindow(theWindow)){
527 vtkRenderer *aRenderer = aViewWindow->getRenderer();
528 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
529 vtkActorCollection *aCollection = aCopy.GetActors();
530 aCollection->InitTraversal();
531 while(vtkActor *anAct = aCollection->GetNextActor()){
532 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
533 if(anActor->hasIO()){
534 Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
535 if(anIO->hasEntry() && strcmp(anIO->getEntry(),theEntry) == 0){
546 SMESH_Actor* FindActorByEntry(const char* theEntry){
547 return FindActorByEntry(GetActiveWindow(),theEntry);
551 SMESH_Actor* FindActorByObject(CORBA::Object_ptr theObject){
552 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
556 if(!CORBA::is_nil(theObject)){
557 _PTR(Study) aStudy = GetActiveStudyDocument();
558 CORBA::String_var anIOR = app->orb()->object_to_string( theObject );
559 _PTR(SObject) aSObject = aStudy->FindObjectIOR(anIOR.in());
561 CORBA::String_var anEntry = aSObject->GetID().c_str();
562 return FindActorByEntry(anEntry.in());
569 SMESH_Actor* CreateActor(_PTR(Study) theStudy,
570 const char* theEntry,
573 SMESH_Actor *anActor = NULL;
574 CORBA::Long anId = theStudy->StudyId();
575 if(TVisualObjPtr aVisualObj = GetVisualObj(anId,theEntry)){
576 _PTR(SObject) aSObj = theStudy->FindObjectID(theEntry);
578 _PTR(GenericAttribute) anAttr;
579 if(aSObj->FindAttribute(anAttr,"AttributeName")){
580 _PTR(AttributeName) aName = anAttr;
581 std::string aNameVal = aName->Value();
582 anActor = SMESH_Actor::New(aVisualObj,theEntry,aNameVal.c_str(),theIsClear);
585 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow( SMESH::SObjectToObject( aSObj ));
586 if(!CORBA::is_nil(aGroup))
588 SALOMEDS::Color aColor = aGroup->GetColor();
589 if( !( aColor.R > 0 || aColor.G > 0 || aColor.B > 0 ) )
591 int r = 0, g = 0, b = 0;
592 SMESH::GetColor( "SMESH", "fill_color", r, g, b, QColor( 0, 170, 255 ) );
593 aColor.R = (float)r / 255.0;
594 aColor.G = (float)g / 255.0;
595 aColor.B = (float)b / 255.0;
596 aGroup->SetColor( aColor );
598 if( aGroup->GetType() == SMESH::NODE )
599 anActor->SetNodeColor( aColor.R, aColor.G, aColor.B );
600 else if( aGroup->GetType() == SMESH::EDGE )
601 anActor->SetEdgeColor( aColor.R, aColor.G, aColor.B );
602 else if( aGroup->GetType() == SMESH::ELEM0D )
603 anActor->Set0DColor( aColor.R, aColor.G, aColor.B );
605 anActor->SetSufaceColor( aColor.R, aColor.G, aColor.B );
610 if( SMESHGUI* aSMESHGUI = SMESHGUI::GetSMESHGUI() )
611 aSMESHGUI->addActorAsObserver( anActor );
616 void DisplayActor( SUIT_ViewWindow *theWnd, SMESH_Actor* theActor){
617 if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(theWnd)){
619 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
622 vtkWnd->AddActor(theActor);
627 MESSAGE ( "Exception in SMESHGUI_VTKUtils::DisplayActor()" );
635 void RemoveActor( SUIT_ViewWindow *theWnd, SMESH_Actor* theActor){
636 if(SVTK_ViewWindow* vtkWnd = GetVtkViewWindow(theWnd)){
637 vtkWnd->RemoveActor(theActor);
638 if(theActor->hasIO()){
639 Handle(SALOME_InteractiveObject) anIO = theActor->getIO();
640 if(anIO->hasEntry()){
641 std::string anEntry = anIO->getEntry();
642 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( vtkWnd->getViewManager()->study() );
643 int aStudyId = aStudy->id();
644 TVisualObjCont::key_type aKey(aStudyId,anEntry);
645 VISUAL_OBJ_CONT.erase(aKey);
653 //================================================================================
655 * \brief Return true if there are no SMESH actors in a view
657 //================================================================================
659 bool noSmeshActors(SUIT_ViewWindow *theWnd)
661 if(SVTK_ViewWindow* aViewWindow = GetVtkViewWindow(theWnd)) {
662 vtkRenderer *aRenderer = aViewWindow->getRenderer();
663 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
664 vtkActorCollection *aCollection = aCopy.GetActors();
665 aCollection->InitTraversal();
666 while(vtkActor *anAct = aCollection->GetNextActor())
667 if(dynamic_cast<SMESH_Actor*>(anAct))
673 bool UpdateView(SUIT_ViewWindow *theWnd, EDisplaing theAction, const char* theEntry)
676 SVTK_ViewWindow* aViewWnd = GetVtkViewWindow(theWnd);
682 vtkRenderer *aRenderer = aViewWnd->getRenderer();
683 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
684 vtkActorCollection *aCollection = aCopy.GetActors();
685 aCollection->InitTraversal();
689 while (vtkActor *anAct = aCollection->GetNextActor()) {
690 if (SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)) {
691 anActor->SetVisibility(true);
698 while (vtkActor *anAct = aCollection->GetNextActor()) {
699 if (SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)) {
700 anActor->SetVisibility(false);
705 if (SMESH_Actor *anActor = FindActorByEntry(theWnd,theEntry)) {
709 anActor->SetVisibility(true);
710 if (theAction == eDisplayOnly) aRenderer->ResetCameraClippingRange();
713 anActor->SetVisibility(false);
721 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>(theWnd->getViewManager()->study());
722 _PTR(Study) aDocument = aStudy->studyDS();
723 // Pass non-visual objects (hypotheses, etc.), return true in this case
724 CORBA::Long anId = aDocument->StudyId();
725 TVisualObjPtr aVisualObj;
726 if ( (aVisualObj = GetVisualObj(anId,theEntry)) && aVisualObj->IsValid())
728 if ((anActor = CreateActor(aDocument,theEntry,true))) {
729 bool needFitAll = noSmeshActors(theWnd); // fit for the first object only
730 DisplayActor(theWnd,anActor);
731 // FitAll(); - PAL16770(Display of a group performs an automatic fit all)
732 if (needFitAll) FitAll();
748 bool UpdateView(EDisplaing theAction, const char* theEntry){
749 SalomeApp_Study* aStudy = dynamic_cast< SalomeApp_Study* >( GetActiveStudy() );
750 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( aStudy->application() );
751 SUIT_ViewWindow *aWnd = app->activeViewManager()->getActiveView();
752 return UpdateView(aWnd,theAction,theEntry);
756 if(SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView()){
757 LightApp_SelectionMgr* mgr = SMESHGUI::selectionMgr();
758 SALOME_ListIO selected; mgr->selectedObjects( selected );
760 if( selected.Extent() == 0){
761 vtkRenderer* aRenderer = aWnd->getRenderer();
762 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
763 vtkActorCollection *aCollection = aCopy.GetActors();
764 aCollection->InitTraversal();
765 while(vtkActor *anAct = aCollection->GetNextActor()){
766 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
768 if (!Update(anActor->getIO(),anActor->GetVisibility()))
769 break; // avoid multiple warinings if visu failed
773 SALOME_ListIteratorOfListIO anIter( selected );
774 for( ; anIter.More(); anIter.Next()){
775 Handle(SALOME_InteractiveObject) anIO = anIter.Value();
776 if ( !Update(anIO,true) )
777 break; // avoid multiple warinings if visu failed
780 RepaintCurrentView();
785 bool Update(const Handle(SALOME_InteractiveObject)& theIO, bool theDisplay)
787 _PTR(Study) aStudy = GetActiveStudyDocument();
788 CORBA::Long anId = aStudy->StudyId();
789 if ( TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId,theIO->getEntry())) {
791 UpdateView(SMESH::eDisplay,theIO->getEntry());
798 void UpdateSelectionProp( SMESHGUI* theModule ) {
802 SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( theModule->application() );
805 MESSAGE( "UpdateSelectionProp: Application is null" );
809 SUIT_ViewManager* vm = app->activeViewManager();
812 MESSAGE( "UpdateSelectionProp: View manager is null" );
816 QVector<SUIT_ViewWindow*> views = vm->getViews();
818 SUIT_ResourceMgr* mgr = SMESH::GetResourceMgr( theModule );
821 MESSAGE( "UpdateSelectionProp: Resource manager is null" );
825 QColor aHiColor = mgr->colorValue( "SMESH", "selection_object_color", Qt::white ),
826 aSelColor = mgr->colorValue( "SMESH", "selection_element_color", Qt::yellow ),
827 aPreColor = mgr->colorValue( "SMESH", "highlight_color", Qt::cyan );
829 int SW = mgr->integerValue( "SMESH", "selection_width", 5 ),
830 PW = mgr->integerValue( "SMESH", "highlight_width", 5 );
832 // adjust highlight_width to the width of mesh entities
833 int aElem0DSize = mgr->integerValue("SMESH", "elem0d_size", 5);
834 int aLineWidth = mgr->integerValue("SMESH", "element_width", 1);
835 int maxSize = aElem0DSize;
836 if (aElem0DSize > maxSize) maxSize = aElem0DSize;
837 if (aLineWidth > maxSize) maxSize = aLineWidth;
838 if (PW < maxSize + 2) PW = maxSize + 2;
840 double SP1 = mgr->doubleValue( "SMESH", "selection_precision_node", 0.025 ),
841 SP2 = mgr->doubleValue( "SMESH", "selection_precision_element", 0.001 ),
842 SP3 = mgr->doubleValue( "SMESH", "selection_precision_object", 0.025 );
844 for ( int i=0, n=views.count(); i<n; i++ ){
845 // update VTK viewer properties
846 if(SVTK_ViewWindow* aVtkView = GetVtkViewWindow( views[i] )){
847 // mesh element selection
848 aVtkView->SetSelectionProp(aSelColor.red()/255.,
849 aSelColor.green()/255.,
850 aSelColor.blue()/255.,
853 aVtkView->SetSelectionTolerance(SP1, SP2, SP3);
856 aVtkView->SetPreselectionProp(aPreColor.red()/255.,
857 aPreColor.green()/255.,
858 aPreColor.blue()/255.,
861 vtkRenderer* aRenderer = aVtkView->getRenderer();
862 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
863 vtkActorCollection *aCollection = aCopy.GetActors();
864 aCollection->InitTraversal();
865 while(vtkActor *anAct = aCollection->GetNextActor()){
866 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
867 anActor->SetHighlightColor(aHiColor.red()/255.,
868 aHiColor.green()/255.,
869 aHiColor.blue()/255.);
870 anActor->SetPreHighlightColor(aPreColor.red()/255.,
871 aPreColor.green()/255.,
872 aPreColor.blue()/255.);
880 //----------------------------------------------------------------------------
882 GetSelector(SUIT_ViewWindow *theWindow)
884 if(SVTK_ViewWindow* aWnd = GetVtkViewWindow(theWindow))
885 return aWnd->GetSelector();
890 void SetFilter(const Handle(VTKViewer_Filter)& theFilter,
891 SVTK_Selector* theSelector)
894 theSelector->SetFilter(theFilter);
897 Handle(VTKViewer_Filter) GetFilter(int theId, SVTK_Selector* theSelector)
899 return theSelector->GetFilter(theId);
902 bool IsFilterPresent(int theId, SVTK_Selector* theSelector)
904 return theSelector->IsFilterPresent(theId);
907 void RemoveFilter(int theId, SVTK_Selector* theSelector)
909 theSelector->RemoveFilter(theId);
912 void RemoveFilters(SVTK_Selector* theSelector)
914 for ( int id = SMESH::NodeFilter; theSelector && id < SMESH::LastFilter; id++ )
915 theSelector->RemoveFilter( id );
918 bool IsValid(SALOME_Actor* theActor, int theCellId,
919 SVTK_Selector* theSelector)
921 return theSelector->IsValid(theActor,theCellId);
925 //----------------------------------------------------------------------------
926 void SetPointRepresentation(bool theIsVisible){
927 if(SVTK_ViewWindow* aViewWindow = GetCurrentVtkView()){
928 vtkRenderer *aRenderer = aViewWindow->getRenderer();
929 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
930 vtkActorCollection *aCollection = aCopy.GetActors();
931 aCollection->InitTraversal();
932 while(vtkActor *anAct = aCollection->GetNextActor()){
933 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
934 if(anActor->GetVisibility()){
935 anActor->SetPointRepresentation(theIsVisible);
939 RepaintCurrentView();
944 void SetPickable(SMESH_Actor* theActor){
945 if(SVTK_ViewWindow* aWnd = GetCurrentVtkView()){
946 int anIsAllPickable = (theActor == NULL);
947 vtkRenderer *aRenderer = aWnd->getRenderer();
948 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
949 vtkActorCollection *aCollection = aCopy.GetActors();
950 aCollection->InitTraversal();
951 while(vtkActor *anAct = aCollection->GetNextActor()){
952 if(SALOME_Actor *anActor = dynamic_cast<SALOME_Actor*>(anAct)){
953 if(anActor->GetVisibility()){
954 anActor->SetPickable(anIsAllPickable);
959 theActor->SetPickable(!anIsAllPickable);
960 RepaintCurrentView();
965 //----------------------------------------------------------------------------
966 int GetNameOfSelectedNodes(SVTK_Selector* theSelector,
967 const Handle(SALOME_InteractiveObject)& theIO,
971 TColStd_IndexedMapOfInteger aMapIndex;
972 theSelector->GetIndex(theIO,aMapIndex);
974 for(int i = 1; i <= aMapIndex.Extent(); i++)
975 theName += QString(" %1").arg(aMapIndex(i));
977 return aMapIndex.Extent();
980 int GetNameOfSelectedElements(SVTK_Selector* theSelector,
981 const Handle(SALOME_InteractiveObject)& theIO,
985 TColStd_IndexedMapOfInteger aMapIndex;
986 theSelector->GetIndex(theIO,aMapIndex);
988 typedef std::set<int> TIdContainer;
989 TIdContainer anIdContainer;
990 for( int i = 1; i <= aMapIndex.Extent(); i++)
991 anIdContainer.insert(aMapIndex(i));
993 TIdContainer::const_iterator anIter = anIdContainer.begin();
994 for( ; anIter != anIdContainer.end(); anIter++)
995 theName += QString(" %1").arg(*anIter);
997 return aMapIndex.Extent();
1001 int GetEdgeNodes(SVTK_Selector* theSelector,
1002 const TVisualObjPtr& theVisualObject,
1006 const SALOME_ListIO& selected = theSelector->StoredIObjects();
1008 if ( selected.Extent() != 1 )
1011 Handle(SALOME_InteractiveObject) anIO = selected.First();
1012 if ( anIO.IsNull() || !anIO->hasEntry() )
1015 TColStd_IndexedMapOfInteger aMapIndex;
1016 theSelector->GetIndex( anIO, aMapIndex );
1017 if ( aMapIndex.Extent() != 2 )
1020 int anObjId = -1, anEdgeNum = -1;
1021 for ( int i = 1; i <= aMapIndex.Extent(); i++ ) {
1022 int aVal = aMapIndex( i );
1026 anEdgeNum = abs( aVal ) - 1;
1029 if ( anObjId == -1 || anEdgeNum == -1 )
1032 return theVisualObject->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
1035 //----------------------------------------------------------------------------
1036 int GetNameOfSelectedNodes(LightApp_SelectionMgr *theMgr,
1037 const Handle(SALOME_InteractiveObject)& theIO,
1041 if(theIO->hasEntry()){
1042 if(FindActorByEntry(theIO->getEntry())){
1043 TColStd_IndexedMapOfInteger aMapIndex;
1044 theMgr->GetIndexes(theIO,aMapIndex);
1045 for(int i = 1; i <= aMapIndex.Extent(); i++){
1046 theName += QString(" %1").arg(aMapIndex(i));
1048 return aMapIndex.Extent();
1054 int GetNameOfSelectedNodes(LightApp_SelectionMgr *theMgr, QString& theName){
1056 SALOME_ListIO selected; theMgr->selectedObjects( selected );
1057 if(selected.Extent() == 1){
1058 Handle(SALOME_InteractiveObject) anIO = selected.First();
1059 return GetNameOfSelectedNodes(theMgr,anIO,theName);
1065 int GetNameOfSelectedElements(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 typedef std::set<int> TIdContainer;
1075 TIdContainer anIdContainer;
1076 for( int i = 1; i <= aMapIndex.Extent(); i++)
1077 anIdContainer.insert(aMapIndex(i));
1078 TIdContainer::const_iterator anIter = anIdContainer.begin();
1079 for( ; anIter != anIdContainer.end(); anIter++){
1080 theName += QString(" %1").arg(*anIter);
1082 return aMapIndex.Extent();
1089 int GetNameOfSelectedElements(LightApp_SelectionMgr *theMgr, QString& theName)
1092 SALOME_ListIO selected; theMgr->selectedObjects( selected );
1094 if( selected.Extent() == 1){
1095 Handle(SALOME_InteractiveObject) anIO = selected.First();
1096 return GetNameOfSelectedElements(theMgr,anIO,theName);
1101 int GetSelected(LightApp_SelectionMgr* theMgr,
1102 TColStd_IndexedMapOfInteger& theMap,
1103 const bool theIsElement)
1106 SALOME_ListIO selected; theMgr->selectedObjects( selected );
1108 if ( selected.Extent() == 1 )
1110 Handle(SALOME_InteractiveObject) anIO = selected.First();
1111 if ( anIO->hasEntry() ) {
1112 theMgr->GetIndexes( anIO, theMap );
1115 return theMap.Extent();
1119 int GetEdgeNodes( LightApp_SelectionMgr* theMgr, int& theId1, int& theId2 )
1121 SALOME_ListIO selected; theMgr->selectedObjects( selected );
1123 if ( selected.Extent() != 1 )
1126 Handle(SALOME_InteractiveObject) anIO = selected.First();
1127 if ( anIO.IsNull() || !anIO->hasEntry() )
1130 SMESH_Actor *anActor = SMESH::FindActorByEntry( anIO->getEntry() );
1134 TColStd_IndexedMapOfInteger aMapIndex;
1135 theMgr->GetIndexes( anIO, aMapIndex );
1136 if ( aMapIndex.Extent() != 2 )
1139 int anObjId = -1, anEdgeNum = -1;
1140 for ( int i = 1; i <= aMapIndex.Extent(); i++ ) {
1141 int aVal = aMapIndex( i );
1145 anEdgeNum = abs( aVal );
1148 if ( anObjId == -1 || anEdgeNum == -1 )
1151 return anActor->GetObject()->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
1154 void SetControlsPrecision( const long theVal )
1156 if( SVTK_ViewWindow* aWnd = SMESH::GetCurrentVtkView() )
1158 vtkRenderer *aRenderer = aWnd->getRenderer();
1159 VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
1160 vtkActorCollection *aCollection = aCopy.GetActors();
1161 aCollection->InitTraversal();
1163 while ( vtkActor *anAct = aCollection->GetNextActor())
1165 if ( SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>( anAct ) )
1167 anActor->SetControlsPrecision( theVal );
1168 anActor->SetControlMode( anActor->GetControlMode() );
1175 //----------------------------------------------------------------------------
1176 // internal function
1177 void ComputeBoundsParam( vtkFloatingPointType theBounds[6],
1178 vtkFloatingPointType theDirection[3],
1179 vtkFloatingPointType theMinPnt[3],
1180 vtkFloatingPointType& theMaxBoundPrj,
1181 vtkFloatingPointType& theMinBoundPrj )
1183 //Enlarge bounds in order to avoid conflicts of precision
1184 for(int i = 0; i < 6; i += 2){
1185 static double EPS = 1.0E-3;
1186 vtkFloatingPointType aDelta = (theBounds[i+1] - theBounds[i])*EPS;
1187 theBounds[i] -= aDelta;
1188 theBounds[i+1] += aDelta;
1191 vtkFloatingPointType aBoundPoints[8][3] = { {theBounds[0],theBounds[2],theBounds[4]},
1192 {theBounds[1],theBounds[2],theBounds[4]},
1193 {theBounds[0],theBounds[3],theBounds[4]},
1194 {theBounds[1],theBounds[3],theBounds[4]},
1195 {theBounds[0],theBounds[2],theBounds[5]},
1196 {theBounds[1],theBounds[2],theBounds[5]},
1197 {theBounds[0],theBounds[3],theBounds[5]},
1198 {theBounds[1],theBounds[3],theBounds[5]}};
1200 int aMaxId = 0, aMinId = aMaxId;
1201 theMaxBoundPrj = vtkMath::Dot(theDirection,aBoundPoints[aMaxId]);
1202 theMinBoundPrj = theMaxBoundPrj;
1203 for(int i = 1; i < 8; i++){
1204 vtkFloatingPointType aTmp = vtkMath::Dot(theDirection,aBoundPoints[i]);
1205 if(theMaxBoundPrj < aTmp){
1206 theMaxBoundPrj = aTmp;
1209 if(theMinBoundPrj > aTmp){
1210 theMinBoundPrj = aTmp;
1214 vtkFloatingPointType *aMinPnt = aBoundPoints[aMaxId];
1215 theMinPnt[0] = aMinPnt[0];
1216 theMinPnt[1] = aMinPnt[1];
1217 theMinPnt[2] = aMinPnt[2];
1220 // internal function
1221 void DistanceToPosition( vtkFloatingPointType theBounds[6],
1222 vtkFloatingPointType theDirection[3],
1223 vtkFloatingPointType theDist,
1224 vtkFloatingPointType thePos[3] )
1226 vtkFloatingPointType aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
1227 ComputeBoundsParam(theBounds,theDirection,aMinPnt,aMaxBoundPrj,aMinBoundPrj);
1228 vtkFloatingPointType aLength = (aMaxBoundPrj-aMinBoundPrj)*theDist;
1229 thePos[0] = aMinPnt[0]-theDirection[0]*aLength;
1230 thePos[1] = aMinPnt[1]-theDirection[1]*aLength;
1231 thePos[2] = aMinPnt[2]-theDirection[2]*aLength;
1234 // internal function (currently unused, left just in case)
1235 void PositionToDistance( vtkFloatingPointType theBounds[6],
1236 vtkFloatingPointType theDirection[3],
1237 vtkFloatingPointType thePos[3],
1238 vtkFloatingPointType& theDist )
1240 vtkFloatingPointType aMaxBoundPrj, aMinBoundPrj, aMinPnt[3];
1241 ComputeBoundsParam(theBounds,theDirection,aMinPnt,aMaxBoundPrj,aMinBoundPrj);
1242 vtkFloatingPointType aPrj = vtkMath::Dot(theDirection,thePos);
1243 theDist = (aPrj-aMinBoundPrj)/(aMaxBoundPrj-aMinBoundPrj);
1246 bool ComputeClippingPlaneParameters( std::list<vtkActor*> theActorList,
1247 vtkFloatingPointType theNormal[3],
1248 vtkFloatingPointType theDist,
1249 vtkFloatingPointType theBounds[6],
1250 vtkFloatingPointType theOrigin[3] )
1252 bool anIsOk = false;
1253 theBounds[0] = theBounds[2] = theBounds[4] = VTK_DOUBLE_MAX;
1254 theBounds[1] = theBounds[3] = theBounds[5] = -VTK_DOUBLE_MAX;
1255 std::list<vtkActor*>::iterator anIter = theActorList.begin();
1256 for( ; anIter != theActorList.end(); anIter++ ) {
1257 if( vtkActor* aVTKActor = *anIter ) {
1258 if( SMESH_Actor* anActor = SMESH_Actor::SafeDownCast( aVTKActor ) ) {
1259 vtkFloatingPointType aBounds[6];
1260 anActor->GetUnstructuredGrid()->GetBounds( aBounds );
1261 theBounds[0] = std::min( theBounds[0], aBounds[0] );
1262 theBounds[1] = std::max( theBounds[1], aBounds[1] );
1263 theBounds[2] = std::min( theBounds[2], aBounds[2] );
1264 theBounds[3] = std::max( theBounds[3], aBounds[3] );
1265 theBounds[4] = std::min( theBounds[4], aBounds[4] );
1266 theBounds[5] = std::max( theBounds[5], aBounds[5] );
1275 DistanceToPosition( theBounds, theNormal, theDist, theOrigin );
1278 } // end of namespace SMESH