1 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 #include "SMESHGUI_VTKUtils.h"
22 #include "SMESHGUI_Utils.h"
24 #include <vtkRenderer.h>
25 #include <vtkActorCollection.h>
27 #include <TColStd_IndexedMapOfInteger.hxx>
29 #include "QAD_Config.h"
30 #include "QAD_Desktop.h"
31 #include "QAD_Study.h"
32 #include "QAD_Settings.h"
33 #include "QAD_RightFrame.h"
35 #include "SALOME_Selection.h"
36 #include "SALOME_ListIteratorOfListIO.hxx"
38 #include "VTKViewer_ViewFrame.h"
39 #include "VTKViewer_RenderWindow.h"
40 #include "VTKViewer_InteractorStyleSALOME.h"
41 #include "VTKViewer_RenderWindowInteractor.h"
43 #include "utilities.h"
45 #include "SALOMEconfig.h"
46 #include CORBA_CLIENT_HEADER(SMESH_Gen)
47 #include CORBA_CLIENT_HEADER(SMESH_Mesh)
48 #include CORBA_CLIENT_HEADER(SMESH_Group)
49 #include CORBA_CLIENT_HEADER(SMESH_Hypothesis)
50 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
52 #include "SMESH_Actor.h"
53 #include "SMESH_ObjectDef.h"
59 typedef map<TKeyOfVisualObj,TVisualObjPtr> TVisualObjCont;
60 static TVisualObjCont VISUAL_OBJ_CONT;
62 TVisualObjPtr GetVisualObj(int theStudyId, const char* theEntry){
63 TVisualObjPtr aVisualObj;
65 TVisualObjCont::key_type aKey(theStudyId,theEntry);
66 TVisualObjCont::iterator anIter = VISUAL_OBJ_CONT.find(aKey);
67 if(anIter != VISUAL_OBJ_CONT.end()){
68 aVisualObj = anIter->second;
70 SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
71 SALOMEDS::SObject_var aSObj = aStudy->FindObjectID(theEntry);
72 if(!CORBA::is_nil(aSObj)){
73 SALOMEDS::GenericAttribute_var anAttr;
74 if(aSObj->FindAttribute(anAttr,"AttributeIOR")){
75 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
76 CORBA::String_var aVal = anIOR->Value();
77 CORBA::Object_var anObj = aStudy->ConvertIORToObject(aVal.in());
78 if(!CORBA::is_nil(anObj)){
79 //Try narrow to SMESH_Mesh interafce
80 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(anObj);
81 if(!aMesh->_is_nil()){
82 aVisualObj.reset(new SMESH_MeshObj(aMesh));
84 TVisualObjCont::value_type aValue(aKey,aVisualObj);
85 VISUAL_OBJ_CONT.insert(aValue);
88 //Try narrow to SMESH_Group interafce
89 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(anObj);
90 if(!aGroup->_is_nil()){
91 SALOMEDS::SObject_var aFatherSObj = aSObj->GetFather();
92 if(aFatherSObj->_is_nil()) return aVisualObj;
93 aFatherSObj = aFatherSObj->GetFather();
94 if(aFatherSObj->_is_nil()) return aVisualObj;
95 CORBA::String_var anEntry = aFatherSObj->GetID();
96 TVisualObjPtr aVisObj = GetVisualObj(theStudyId,anEntry.in());
97 if(SMESH_MeshObj* aMeshObj = dynamic_cast<SMESH_MeshObj*>(aVisObj.get())){
98 aVisualObj.reset(new SMESH_GroupObj(aGroup,aMeshObj));
100 TVisualObjCont::value_type aValue(aKey,aVisualObj);
101 VISUAL_OBJ_CONT.insert(aValue);
105 //Try narrow to SMESH_subMesh interafce
106 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(anObj);
107 if(!aSubMesh->_is_nil()){
108 SALOMEDS::SObject_var aFatherSObj = aSObj->GetFather();
109 if(aFatherSObj->_is_nil()) return aVisualObj;
110 aFatherSObj = aFatherSObj->GetFather();
111 if(aFatherSObj->_is_nil()) return aVisualObj;
112 CORBA::String_var anEntry = aFatherSObj->GetID();
113 TVisualObjPtr aVisObj = GetVisualObj(theStudyId,anEntry.in());
114 if(SMESH_MeshObj* aMeshObj = dynamic_cast<SMESH_MeshObj*>(aVisObj.get())){
115 aVisualObj.reset(new SMESH_subMeshObj(aSubMesh,aMeshObj));
116 aVisualObj->Update();
117 TVisualObjCont::value_type aValue(aKey,aVisualObj);
118 VISUAL_OBJ_CONT.insert(aValue);
127 INFOS("GetMeshObj - There is no SMESH_Mesh object for the SALOMEDS::Strudy and Entry!!!");
133 VTKViewer_ViewFrame* FindVtkViewFrame(QAD_Study* theStudy,
134 QAD_StudyFrame* theStudyFrame)
136 QList<QAD_Study>& aStudies =
137 QAD_Application::getDesktop()->getActiveApp()->getStudies();
138 if(aStudies.containsRef(theStudy)){
139 const QList<QAD_StudyFrame>& aStudyFrames = theStudy->getStudyFrames();
140 if(aStudyFrames.containsRef(theStudyFrame)){
141 return GetVtkViewFrame(theStudyFrame);
148 VTKViewer_ViewFrame* GetVtkViewFrame(QAD_StudyFrame* theStudyFrame){
149 QAD_ViewFrame* aViewFrame = theStudyFrame->getRightFrame()->getViewFrame();
150 return dynamic_cast<VTKViewer_ViewFrame*>(aViewFrame);
154 VTKViewer_ViewFrame* GetCurrentVtkView(){
155 return GetVtkViewFrame(GetActiveStudy()->getActiveStudyFrame());
159 void RepaintViewFrame(VTKViewer_ViewFrame* theFrame)
165 void RenderViewFrame(VTKViewer_ViewFrame* theFrame)
167 theFrame->getRW()->getRenderWindow()->Render();
172 SMESH_Actor* FindActorByEntry(QAD_StudyFrame *theStudyFrame,
173 const char* theEntry)
175 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
176 vtkRenderer *aRenderer = aViewFrame->getRenderer();
177 vtkActorCollection *aCollection = aRenderer->GetActors();
178 aCollection->InitTraversal();
179 while(vtkActor *anAct = aCollection->GetNextActor()){
180 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
181 if(anActor->hasIO()){
182 Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
183 if(anIO->hasEntry() && strcmp(anIO->getEntry(),theEntry) == 0){
194 SMESH_Actor* FindActorByEntry(const char* theEntry){
195 return FindActorByEntry(GetActiveStudy()->getActiveStudyFrame(),theEntry);
199 SMESH_Actor* FindActorByObject(CORBA::Object_ptr theObject){
200 if(!CORBA::is_nil(theObject)){
201 SALOMEDS::Study_var aStudy = GetActiveStudyDocument();
202 CORBA::String_var anIOR = aStudy->ConvertObjectToIOR(theObject);
203 SALOMEDS::SObject_var aSObject = aStudy->FindObjectIOR(anIOR.in());
204 if(!aSObject->_is_nil()){
205 CORBA::String_var anEntry = aSObject->GetID();
206 return FindActorByEntry(anEntry.in());
213 SMESH_Actor* CreateActor(SALOMEDS::Study_ptr theStudy,
214 const char* theEntry,
217 SMESH_Actor *anActor = NULL;
218 CORBA::Long anId = theStudy->StudyId();
219 if(TVisualObjPtr aVisualObj = GetVisualObj(anId,theEntry)){
220 SALOMEDS::SObject_var aSObj = theStudy->FindObjectID(theEntry);
221 if(!aSObj->_is_nil()){
222 SALOMEDS::GenericAttribute_var anAttr;
223 if(aSObj->FindAttribute(anAttr,"AttributeName")){
224 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
225 CORBA::String_var aVal = aName->Value();
226 string aNameVal = aVal.in();
227 anActor = SMESH_Actor::New(aVisualObj,theEntry,aNameVal.c_str(),theIsClear);
235 void DisplayActor(QAD_StudyFrame *theStudyFrame, SMESH_Actor* theActor){
236 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
237 aViewFrame->AddActor(theActor);
238 aViewFrame->Repaint();
243 void RemoveActor(QAD_StudyFrame *theStudyFrame, SMESH_Actor* theActor){
244 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
245 aViewFrame->RemoveActor(theActor);
246 aViewFrame->Repaint();
252 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(GetActiveStudy()->getActiveStudyFrame())){
253 aViewFrame->onViewFitAll();
254 aViewFrame->Repaint();
258 vtkRenderer* GetCurrentRenderer(){
259 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(GetActiveStudy()->getActiveStudyFrame()))
260 return aViewFrame->getRenderer();
264 void RepaintCurrentView(){
265 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(GetActiveStudy()->getActiveStudyFrame())){
266 aViewFrame->getRW()->getRenderWindow()->Render();
267 //aViewFrame->Repaint();
272 void UpdateView(QAD_StudyFrame *theStudyFrame, EDisplaing theAction, const char* theEntry)
274 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
275 vtkRenderer *aRenderer = aViewFrame->getRenderer();
276 vtkActorCollection *aCollection = aRenderer->GetActors();
277 aCollection->InitTraversal();
280 while(vtkActor *anAct = aCollection->GetNextActor()){
281 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
282 anActor->SetVisibility(true);
289 while(vtkActor *anAct = aCollection->GetNextActor()){
290 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
291 anActor->SetVisibility(false);
296 if(SMESH_Actor *anActor = FindActorByEntry(theStudyFrame,theEntry)){
300 anActor->SetVisibility(true);
303 anActor->SetVisibility(false);
310 QAD_Study* aStudy = theStudyFrame->getStudy();
311 SALOMEDS::Study_var aDocument = aStudy->getStudyDocument();
312 if((anActor = CreateActor(aDocument,theEntry,true))) {
313 DisplayActor(theStudyFrame,anActor);
326 void UpdateView(EDisplaing theAction, const char* theEntry){
327 QAD_Study* aStudy = GetActiveStudy();
328 QAD_StudyFrame *aStudyFrame = aStudy->getActiveStudyFrame();
329 UpdateView(aStudyFrame,theAction,theEntry);
333 if(VTKViewer_ViewFrame* aViewFrame = SMESH::GetCurrentVtkView()){
334 SALOME_Selection *aSel = SALOME_Selection::Selection(GetActiveStudy()->getSelection());
335 if(aSel->IObjectCount() == 0){
336 vtkRenderer* aRenderer = aViewFrame->getRenderer();
337 vtkActorCollection *aCollection = aRenderer->GetActors();
338 aCollection->InitTraversal();
339 while(vtkActor *anAct = aCollection->GetNextActor()){
340 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
342 Update(anActor->getIO(),anActor->GetVisibility());
346 SALOME_ListIteratorOfListIO anIter(aSel->StoredIObjects());
347 for(; anIter.More(); anIter.Next()){
348 Handle(SALOME_InteractiveObject) anIO = anIter.Value();
352 RepaintCurrentView();
357 void Update(const Handle(SALOME_InteractiveObject)& theIO,
360 SALOMEDS::Study_var aStudy = GetActiveStudyDocument();
361 CORBA::Long anId = aStudy->StudyId();
362 TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId,theIO->getEntry());
363 aVisualObj->Update();
365 UpdateView(SMESH::eDisplay,theIO->getEntry());
369 void UpdateSelectionProp() {
370 QAD_Study* aStudy = GetActiveStudy();
371 QList<QAD_StudyFrame> aFrameList = aStudy->getStudyFrames();
373 QString SCr, SCg, SCb;
374 SCr = QAD_CONFIG->getSetting("SMESH:SettingsSelectColorRed");
375 SCg = QAD_CONFIG->getSetting("SMESH:SettingsSelectColorGreen");
376 SCb = QAD_CONFIG->getSetting("SMESH:SettingsSelectColorBlue");
377 QColor aHiColor = Qt::white;
378 if (!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())
379 aHiColor = QColor(SCr.toInt(), SCg.toInt(), SCb.toInt());
381 SCr = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectColorRed");
382 SCg = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectColorGreen");
383 SCb = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectColorBlue");
384 QColor aSelColor = Qt::yellow;
385 if (!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())
386 aSelColor = QColor(SCr.toInt(), SCg.toInt(), SCb.toInt());
387 QString SW = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectWidth");
388 if (SW.isEmpty()) SW = "5";
390 SCr = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectColorRed");
391 SCg = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectColorGreen");
392 SCb = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectColorBlue");
393 QColor aPreColor = Qt::cyan;
394 if (!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())
395 aPreColor = QColor(SCr.toInt(), SCg.toInt(), SCb.toInt());
396 QString PW = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectWidth");
397 if (PW.isEmpty()) PW = "5";
399 QString SP1 = QAD_CONFIG->getSetting("SMESH:SettingsNodeSelectTol");
400 if (SP1.isEmpty()) SP1 = "0.025";
401 QString SP2 = QAD_CONFIG->getSetting("SMESH:SettingsElementsSelectTol");
402 if (SP2.isEmpty()) SP2 = "0.001";
404 for (QAD_StudyFrame* aStudyFrame = aFrameList.first(); aStudyFrame; aStudyFrame = aFrameList.next()) {
405 if (aStudyFrame->getTypeView() == VIEW_VTK) {
406 VTKViewer_ViewFrame* aVtkViewFrame = GetVtkViewFrame(aStudyFrame);
407 if (!aVtkViewFrame) continue;
408 // update VTK viewer properties
409 VTKViewer_RenderWindowInteractor* anInteractor = aVtkViewFrame->getRWInteractor();
411 // mesh element selection
412 anInteractor->SetSelectionProp(aSelColor.red()/255., aSelColor.green()/255.,
413 aSelColor.blue()/255., SW.toInt());
416 anInteractor->SetSelectionTolerance(SP1.toDouble(), SP2.toDouble());
419 VTKViewer_InteractorStyleSALOME* aStyle = anInteractor->GetInteractorStyleSALOME();
421 aStyle->setPreselectionProp(aPreColor.red()/255., aPreColor.green()/255.,
422 aPreColor.blue()/255., PW.toInt());
426 vtkRenderer* aRenderer = aVtkViewFrame->getRenderer();
427 vtkActorCollection *aCollection = aRenderer->GetActors();
428 aCollection->InitTraversal();
429 while(vtkActor *anAct = aCollection->GetNextActor()){
430 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
431 anActor->SetHighlightColor(aHiColor.red()/255., aHiColor.green()/255.,
432 aHiColor.blue()/255.);
433 anActor->SetPreHighlightColor(aPreColor.red()/255., aPreColor.green()/255.,
434 aPreColor.blue()/255.);
442 //----------------------------------------------------------------------------
443 VTKViewer_InteractorStyleSALOME* GetInteractorStyle(QAD_StudyFrame *theStudyFrame){
444 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
445 if(VTKViewer_RenderWindowInteractor* anInteractor = aViewFrame->getRWInteractor()){
446 return anInteractor->GetInteractorStyleSALOME();
452 void SetFilter(const Handle(VTKViewer_Filter)& theFilter,
453 VTKViewer_InteractorStyleSALOME* theStyle)
455 theStyle->SetFilter(theFilter);
458 Handle(VTKViewer_Filter) GetFilter(int theId, VTKViewer_InteractorStyleSALOME* theStyle)
460 return theStyle->GetFilter(theId);
463 bool IsFilterPresent(int theId, VTKViewer_InteractorStyleSALOME* theStyle)
465 return theStyle->IsFilterPresent(theId);
468 void RemoveFilter(int theId, VTKViewer_InteractorStyleSALOME* theStyle){
469 theStyle->RemoveFilter(theId);
472 bool IsValid(SALOME_Actor* theActor, int theCellId,
473 VTKViewer_InteractorStyleSALOME* theStyle)
475 return theStyle->IsValid(theActor,theCellId);
479 //----------------------------------------------------------------------------
480 void SetPointRepresentation(bool theIsVisible){
481 if(VTKViewer_ViewFrame* aViewFrame = GetCurrentVtkView()){
482 vtkRenderer *aRenderer = aViewFrame->getRenderer();
483 vtkActorCollection *aCollection = aRenderer->GetActors();
484 aCollection->InitTraversal();
485 while(vtkActor *anAct = aCollection->GetNextActor()){
486 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
487 if(anActor->GetVisibility()){
488 anActor->SetPointRepresentation(theIsVisible);
492 RepaintCurrentView();
497 void SetPickable(SMESH_Actor* theActor){
498 if(VTKViewer_ViewFrame* aViewFrame = GetCurrentVtkView()){
499 int anIsAllPickable = (theActor == NULL);
500 vtkRenderer *aRenderer = aViewFrame->getRenderer();
501 vtkActorCollection *aCollection = aRenderer->GetActors();
502 aCollection->InitTraversal();
503 while(vtkActor *anAct = aCollection->GetNextActor()){
504 if(SALOME_Actor *anActor = dynamic_cast<SALOME_Actor*>(anAct)){
505 if(anActor->GetVisibility()){
506 anActor->SetPickable(anIsAllPickable);
511 theActor->SetPickable(!anIsAllPickable);
512 RepaintCurrentView();
517 int GetNameOfSelectedNodes(SALOME_Selection *theSel,
518 const Handle(SALOME_InteractiveObject)& theIO,
522 if(theIO->hasEntry()){
523 if(FindActorByEntry(theIO->getEntry())){
524 TColStd_IndexedMapOfInteger aMapIndex;
525 theSel->GetIndex(theIO,aMapIndex);
526 for(int i = 1; i <= aMapIndex.Extent(); i++){
527 theName += QString(" %1").arg(aMapIndex(i));
529 return aMapIndex.Extent();
536 int GetNameOfSelectedNodes(SALOME_Selection *theSel, QString& theName){
538 if(theSel->IObjectCount() == 1){
539 Handle(SALOME_InteractiveObject) anIO = theSel->firstIObject();
540 return GetNameOfSelectedNodes(theSel,anIO,theName);
546 int GetNameOfSelectedElements(SALOME_Selection *theSel,
547 const Handle(SALOME_InteractiveObject)& theIO,
551 if(theIO->hasEntry()){
552 if(FindActorByEntry(theIO->getEntry())){
553 TColStd_IndexedMapOfInteger aMapIndex;
554 theSel->GetIndex(theIO,aMapIndex);
555 typedef std::set<int> TIdContainer;
556 TIdContainer anIdContainer;
557 for( int i = 1; i <= aMapIndex.Extent(); i++)
558 anIdContainer.insert(aMapIndex(i));
559 TIdContainer::const_iterator anIter = anIdContainer.begin();
560 for(; anIter != anIdContainer.end(); anIter++){
561 theName += QString(" %1").arg(*anIter);
563 return aMapIndex.Extent();
570 int GetNameOfSelectedElements(SALOME_Selection *theSel, QString& theName)
573 if(theSel->IObjectCount() == 1){
574 Handle(SALOME_InteractiveObject) anIO = theSel->firstIObject();
575 return GetNameOfSelectedElements(theSel,anIO,theName);
581 int GetSelected(SALOME_Selection* theSel,
582 TColStd_IndexedMapOfInteger& theMap,
583 const bool theIsElement)
587 if ( theSel->IObjectCount() == 1 )
589 Handle(SALOME_InteractiveObject) anIO = theSel->firstIObject();
590 if ( anIO->hasEntry() ) {
591 theSel->GetIndex( anIO, theMap );
594 return theMap.Extent();
598 int GetEdgeNodes( SALOME_Selection* theSel, int& theId1, int& theId2 )
600 if ( theSel->IObjectCount() != 1 )
603 Handle(SALOME_InteractiveObject) anIO = theSel->firstIObject();
604 if ( anIO.IsNull() || !anIO->hasEntry() )
607 SMESH_Actor *anActor = SMESH::FindActorByEntry( anIO->getEntry() );
611 TColStd_IndexedMapOfInteger aMapIndex;
612 theSel->GetIndex( anIO, aMapIndex );
613 if ( aMapIndex.Extent() != 2 )
616 int anObjId = -1, anEdgeNum = -1;
617 for ( int i = 1; i <= aMapIndex.Extent(); i++ ) {
618 int aVal = aMapIndex( i );
622 anEdgeNum = abs( aVal );
625 if ( anObjId == -1 || anEdgeNum == -1 )
628 return anActor->GetObject()->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
631 void SetControlsPrecision( const long theVal )
633 if( VTKViewer_ViewFrame* aViewFrame = SMESH::GetCurrentVtkView() )
635 vtkRenderer *aRenderer = aViewFrame->getRenderer();
636 vtkActorCollection *aCollection = aRenderer->GetActors();
637 aCollection->InitTraversal();
639 while ( vtkActor *anAct = aCollection->GetNextActor())
641 if ( SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>( anAct ) )
643 anActor->SetControlsPrecision( theVal );
644 anActor->SetControlMode( anActor->GetControlMode() );