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 SMESH_Actor* FindActorByEntry(QAD_StudyFrame *theStudyFrame,
160 const char* theEntry)
162 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
163 vtkRenderer *aRenderer = aViewFrame->getRenderer();
164 vtkActorCollection *aCollection = aRenderer->GetActors();
165 aCollection->InitTraversal();
166 while(vtkActor *anAct = aCollection->GetNextActor()){
167 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
168 if(anActor->hasIO()){
169 Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
170 if(anIO->hasEntry() && strcmp(anIO->getEntry(),theEntry) == 0){
181 SMESH_Actor* FindActorByEntry(const char* theEntry){
182 return FindActorByEntry(GetActiveStudy()->getActiveStudyFrame(),theEntry);
186 SMESH_Actor* FindActorByObject(CORBA::Object_ptr theObject){
187 if(!CORBA::is_nil(theObject)){
188 SALOMEDS::Study_var aStudy = GetActiveStudyDocument();
189 CORBA::String_var anIOR = aStudy->ConvertObjectToIOR(theObject);
190 SALOMEDS::SObject_var aSObject = aStudy->FindObjectIOR(anIOR.in());
191 if(!aSObject->_is_nil()){
192 CORBA::String_var anEntry = aSObject->GetID();
193 return FindActorByEntry(anEntry.in());
200 SMESH_Actor* CreateActor(SALOMEDS::Study_ptr theStudy,
201 const char* theEntry,
204 SMESH_Actor *anActor = NULL;
205 CORBA::Long anId = theStudy->StudyId();
206 if(TVisualObjPtr aVisualObj = GetVisualObj(anId,theEntry)){
207 SALOMEDS::SObject_var aSObj = theStudy->FindObjectID(theEntry);
208 if(!aSObj->_is_nil()){
209 SALOMEDS::GenericAttribute_var anAttr;
210 if(aSObj->FindAttribute(anAttr,"AttributeName")){
211 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
212 CORBA::String_var aVal = aName->Value();
213 string aNameVal = aVal.in();
214 anActor = SMESH_Actor::New(aVisualObj,theEntry,aNameVal.c_str(),theIsClear);
222 void DisplayActor(QAD_StudyFrame *theStudyFrame, SMESH_Actor* theActor){
223 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
224 aViewFrame->AddActor(theActor);
225 aViewFrame->Repaint();
230 void RemoveActor(QAD_StudyFrame *theStudyFrame, SMESH_Actor* theActor){
231 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
232 aViewFrame->RemoveActor(theActor);
233 aViewFrame->Repaint();
239 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(GetActiveStudy()->getActiveStudyFrame())){
240 aViewFrame->onViewFitAll();
241 aViewFrame->Repaint();
245 vtkRenderer* GetCurrentRenderer(){
246 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(GetActiveStudy()->getActiveStudyFrame()))
247 return aViewFrame->getRenderer();
251 void RepaintCurrentView(){
252 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(GetActiveStudy()->getActiveStudyFrame())){
253 aViewFrame->getRW()->getRenderWindow()->Render();
254 //aViewFrame->Repaint();
259 void UpdateView(QAD_StudyFrame *theStudyFrame, EDisplaing theAction, const char* theEntry)
261 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
262 vtkRenderer *aRenderer = aViewFrame->getRenderer();
263 vtkActorCollection *aCollection = aRenderer->GetActors();
264 aCollection->InitTraversal();
267 while(vtkActor *anAct = aCollection->GetNextActor()){
268 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
269 anActor->SetVisibility(true);
276 while(vtkActor *anAct = aCollection->GetNextActor()){
277 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
278 anActor->SetVisibility(false);
283 if(SMESH_Actor *anActor = FindActorByEntry(theStudyFrame,theEntry)){
287 anActor->SetVisibility(true);
290 anActor->SetVisibility(false);
297 QAD_Study* aStudy = theStudyFrame->getStudy();
298 SALOMEDS::Study_var aDocument = aStudy->getStudyDocument();
299 if((anActor = CreateActor(aDocument,theEntry,true))) {
300 DisplayActor(theStudyFrame,anActor);
313 void UpdateView(EDisplaing theAction, const char* theEntry){
314 QAD_Study* aStudy = GetActiveStudy();
315 QAD_StudyFrame *aStudyFrame = aStudy->getActiveStudyFrame();
316 UpdateView(aStudyFrame,theAction,theEntry);
320 if(VTKViewer_ViewFrame* aViewFrame = SMESH::GetCurrentVtkView()){
321 SALOME_Selection *aSel = SALOME_Selection::Selection(GetActiveStudy()->getSelection());
322 if(aSel->IObjectCount() == 0){
323 vtkRenderer* aRenderer = aViewFrame->getRenderer();
324 vtkActorCollection *aCollection = aRenderer->GetActors();
325 aCollection->InitTraversal();
326 while(vtkActor *anAct = aCollection->GetNextActor()){
327 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
329 Update(anActor->getIO(),anActor->GetVisibility());
333 SALOME_ListIteratorOfListIO anIter(aSel->StoredIObjects());
334 for(; anIter.More(); anIter.Next()){
335 Handle(SALOME_InteractiveObject) anIO = anIter.Value();
339 RepaintCurrentView();
344 void Update(const Handle(SALOME_InteractiveObject)& theIO,
347 SALOMEDS::Study_var aStudy = GetActiveStudyDocument();
348 CORBA::Long anId = aStudy->StudyId();
349 TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId,theIO->getEntry());
350 aVisualObj->Update();
352 UpdateView(SMESH::eDisplay,theIO->getEntry());
356 void UpdateSelectionProp() {
357 QAD_Study* aStudy = GetActiveStudy();
358 QList<QAD_StudyFrame> aFrameList = aStudy->getStudyFrames();
360 QString SCr, SCg, SCb;
361 SCr = QAD_CONFIG->getSetting("SMESH:SettingsSelectColorRed");
362 SCg = QAD_CONFIG->getSetting("SMESH:SettingsSelectColorGreen");
363 SCb = QAD_CONFIG->getSetting("SMESH:SettingsSelectColorBlue");
364 QColor aHiColor = Qt::white;
365 if (!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())
366 aHiColor = QColor(SCr.toInt(), SCg.toInt(), SCb.toInt());
368 SCr = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectColorRed");
369 SCg = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectColorGreen");
370 SCb = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectColorBlue");
371 QColor aSelColor = Qt::yellow;
372 if (!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())
373 aSelColor = QColor(SCr.toInt(), SCg.toInt(), SCb.toInt());
374 QString SW = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectWidth");
375 if (SW.isEmpty()) SW = "5";
377 SCr = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectColorRed");
378 SCg = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectColorGreen");
379 SCb = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectColorBlue");
380 QColor aPreColor = Qt::cyan;
381 if (!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())
382 aPreColor = QColor(SCr.toInt(), SCg.toInt(), SCb.toInt());
383 QString PW = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectWidth");
384 if (PW.isEmpty()) PW = "5";
386 QString SP1 = QAD_CONFIG->getSetting("SMESH:SettingsNodeSelectTol");
387 if (SP1.isEmpty()) SP1 = "0.025";
388 QString SP2 = QAD_CONFIG->getSetting("SMESH:SettingsElementsSelectTol");
389 if (SP2.isEmpty()) SP2 = "0.001";
391 for (QAD_StudyFrame* aStudyFrame = aFrameList.first(); aStudyFrame; aStudyFrame = aFrameList.next()) {
392 if (aStudyFrame->getTypeView() == VIEW_VTK) {
393 VTKViewer_ViewFrame* aVtkViewFrame = GetVtkViewFrame(aStudyFrame);
394 if (!aVtkViewFrame) continue;
395 // update VTK viewer properties
396 VTKViewer_RenderWindowInteractor* anInteractor = aVtkViewFrame->getRWInteractor();
398 // mesh element selection
399 anInteractor->SetSelectionProp(aSelColor.red()/255., aSelColor.green()/255.,
400 aSelColor.blue()/255., SW.toInt());
403 anInteractor->SetSelectionTolerance(SP1.toDouble(), SP2.toDouble());
406 VTKViewer_InteractorStyleSALOME* aStyle = anInteractor->GetInteractorStyleSALOME();
408 aStyle->setPreselectionProp(aPreColor.red()/255., aPreColor.green()/255.,
409 aPreColor.blue()/255., PW.toInt());
413 vtkRenderer* aRenderer = aVtkViewFrame->getRenderer();
414 vtkActorCollection *aCollection = aRenderer->GetActors();
415 aCollection->InitTraversal();
416 while(vtkActor *anAct = aCollection->GetNextActor()){
417 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
418 anActor->SetHighlightColor(aHiColor.red()/255., aHiColor.green()/255.,
419 aHiColor.blue()/255.);
420 anActor->SetPreHighlightColor(aPreColor.red()/255., aPreColor.green()/255.,
421 aPreColor.blue()/255.);
429 //----------------------------------------------------------------------------
430 VTKViewer_InteractorStyleSALOME* GetInteractorStyle(QAD_StudyFrame *theStudyFrame){
431 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
432 if(VTKViewer_RenderWindowInteractor* anInteractor = aViewFrame->getRWInteractor()){
433 return anInteractor->GetInteractorStyleSALOME();
439 void SetFilter(const Handle(VTKViewer_Filter)& theFilter,
440 VTKViewer_InteractorStyleSALOME* theStyle)
442 theStyle->SetFilter(theFilter);
445 Handle(VTKViewer_Filter) GetFilter(int theId, VTKViewer_InteractorStyleSALOME* theStyle)
447 return theStyle->GetFilter(theId);
450 bool IsFilterPresent(int theId, VTKViewer_InteractorStyleSALOME* theStyle)
452 return theStyle->IsFilterPresent(theId);
455 void RemoveFilter(int theId, VTKViewer_InteractorStyleSALOME* theStyle){
456 theStyle->RemoveFilter(theId);
459 bool IsValid(SALOME_Actor* theActor, int theCellId,
460 VTKViewer_InteractorStyleSALOME* theStyle)
462 return theStyle->IsValid(theActor,theCellId);
466 //----------------------------------------------------------------------------
467 void SetPointRepresentation(bool theIsVisible){
468 if(VTKViewer_ViewFrame* aViewFrame = GetCurrentVtkView()){
469 vtkRenderer *aRenderer = aViewFrame->getRenderer();
470 vtkActorCollection *aCollection = aRenderer->GetActors();
471 aCollection->InitTraversal();
472 while(vtkActor *anAct = aCollection->GetNextActor()){
473 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
474 if(anActor->GetVisibility()){
475 anActor->SetPointRepresentation(theIsVisible);
479 RepaintCurrentView();
484 void SetPickable(SMESH_Actor* theActor){
485 if(VTKViewer_ViewFrame* aViewFrame = GetCurrentVtkView()){
486 int anIsAllPickable = (theActor == NULL);
487 vtkRenderer *aRenderer = aViewFrame->getRenderer();
488 vtkActorCollection *aCollection = aRenderer->GetActors();
489 aCollection->InitTraversal();
490 while(vtkActor *anAct = aCollection->GetNextActor()){
491 if(SALOME_Actor *anActor = dynamic_cast<SALOME_Actor*>(anAct)){
492 if(anActor->GetVisibility()){
493 anActor->SetPickable(anIsAllPickable);
498 theActor->SetPickable(!anIsAllPickable);
499 RepaintCurrentView();
504 int GetNameOfSelectedNodes(SALOME_Selection *theSel,
505 const Handle(SALOME_InteractiveObject)& theIO,
509 if(theIO->hasEntry()){
510 if(SMESH_Actor *anActor = FindActorByEntry(theIO->getEntry())){
511 TColStd_IndexedMapOfInteger aMapIndex;
512 theSel->GetIndex(theIO,aMapIndex);
513 for(int i = 1; i <= aMapIndex.Extent(); i++){
514 theName += QString(" %1").arg(aMapIndex(i));
516 return aMapIndex.Extent();
523 int GetNameOfSelectedNodes(SALOME_Selection *theSel, QString& theName){
525 if(theSel->IObjectCount() == 1){
526 Handle(SALOME_InteractiveObject) anIO = theSel->firstIObject();
527 return GetNameOfSelectedNodes(theSel,anIO,theName);
533 int GetNameOfSelectedElements(SALOME_Selection *theSel,
534 const Handle(SALOME_InteractiveObject)& theIO,
538 if(theIO->hasEntry()){
539 if(SMESH_Actor *anActor = FindActorByEntry(theIO->getEntry())){
540 TColStd_IndexedMapOfInteger aMapIndex;
541 theSel->GetIndex(theIO,aMapIndex);
542 typedef std::set<int> TIdContainer;
543 TIdContainer anIdContainer;
544 for( int i = 1; i <= aMapIndex.Extent(); i++)
545 anIdContainer.insert(aMapIndex(i));
546 TIdContainer::const_iterator anIter = anIdContainer.begin();
547 for(; anIter != anIdContainer.end(); anIter++){
548 theName += QString(" %1").arg(*anIter);
550 return aMapIndex.Extent();
557 int GetNameOfSelectedElements(SALOME_Selection *theSel, QString& theName)
560 if(theSel->IObjectCount() == 1){
561 Handle(SALOME_InteractiveObject) anIO = theSel->firstIObject();
562 return GetNameOfSelectedElements(theSel,anIO,theName);
568 int GetSelected(SALOME_Selection* theSel,
569 TColStd_IndexedMapOfInteger& theMap,
570 const bool theIsElement)
574 if ( theSel->IObjectCount() == 1 )
576 Handle(SALOME_InteractiveObject) anIO = theSel->firstIObject();
577 if ( anIO->hasEntry() ) {
578 theSel->GetIndex( anIO, theMap );
581 return theMap.Extent();
585 int GetEdgeNodes( SALOME_Selection* theSel, int& theId1, int& theId2 )
587 if ( theSel->IObjectCount() != 1 )
590 Handle(SALOME_InteractiveObject) anIO = theSel->firstIObject();
591 if ( anIO.IsNull() || !anIO->hasEntry() )
594 SMESH_Actor *anActor = SMESH::FindActorByEntry( anIO->getEntry() );
598 TColStd_IndexedMapOfInteger aMapIndex;
599 theSel->GetIndex( anIO, aMapIndex );
600 if ( aMapIndex.Extent() != 2 )
603 int anObjId = -1, anEdgeNum = -1;
604 for ( int i = 1; i <= aMapIndex.Extent(); i++ ) {
605 int aVal = aMapIndex( i );
609 anEdgeNum = abs( aVal );
612 if ( anObjId == -1 || anEdgeNum == -1 )
615 return anActor->GetObject()->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
618 void SetControlsPrecision( const long theVal )
620 if( VTKViewer_ViewFrame* aViewFrame = SMESH::GetCurrentVtkView() )
622 vtkRenderer *aRenderer = aViewFrame->getRenderer();
623 vtkActorCollection *aCollection = aRenderer->GetActors();
624 aCollection->InitTraversal();
626 while ( vtkActor *anAct = aCollection->GetNextActor())
628 if ( SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>( anAct ) )
630 anActor->SetControlsPrecision( theVal );
631 anActor->SetControlMode( anActor->GetControlMode() );