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"
58 typedef map<TKeyOfVisualObj,TVisualObjPtr> TVisualObjCont;
59 static TVisualObjCont VISUAL_OBJ_CONT;
61 TVisualObjPtr GetVisualObj(int theStudyId, const char* theEntry){
62 TVisualObjPtr aVisualObj;
64 TVisualObjCont::key_type aKey(theStudyId,theEntry);
65 TVisualObjCont::iterator anIter = VISUAL_OBJ_CONT.find(aKey);
66 if(anIter != VISUAL_OBJ_CONT.end()){
67 aVisualObj = anIter->second;
69 SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
70 SALOMEDS::SObject_var aSObj = aStudy->FindObjectID(theEntry);
71 if(!CORBA::is_nil(aSObj)){
72 SALOMEDS::GenericAttribute_var anAttr;
73 if(aSObj->FindAttribute(anAttr,"AttributeIOR")){
74 SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
75 CORBA::String_var aVal = anIOR->Value();
76 CORBA::Object_var anObj = aStudy->ConvertIORToObject(aVal.in());
77 if(!CORBA::is_nil(anObj)){
78 //Try narrow to SMESH_Mesh interafce
79 SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow(anObj);
80 if(!aMesh->_is_nil()){
81 aVisualObj.reset(new SMESH_MeshObj(aMesh));
83 TVisualObjCont::value_type aValue(aKey,aVisualObj);
84 VISUAL_OBJ_CONT.insert(aValue);
87 //Try narrow to SMESH_Group interafce
88 SMESH::SMESH_GroupBase_var aGroup = SMESH::SMESH_GroupBase::_narrow(anObj);
89 if(!aGroup->_is_nil()){
90 SALOMEDS::SObject_var aFatherSObj = aSObj->GetFather();
91 if(aFatherSObj->_is_nil()) return aVisualObj;
92 aFatherSObj = aFatherSObj->GetFather();
93 if(aFatherSObj->_is_nil()) return aVisualObj;
94 CORBA::String_var anEntry = aFatherSObj->GetID();
95 TVisualObjPtr aVisObj = GetVisualObj(theStudyId,anEntry.in());
96 if(SMESH_MeshObj* aMeshObj = dynamic_cast<SMESH_MeshObj*>(aVisObj.get())){
97 aVisualObj.reset(new SMESH_GroupObj(aGroup,aMeshObj));
99 TVisualObjCont::value_type aValue(aKey,aVisualObj);
100 VISUAL_OBJ_CONT.insert(aValue);
104 //Try narrow to SMESH_subMesh interafce
105 SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(anObj);
106 if(!aSubMesh->_is_nil()){
107 SALOMEDS::SObject_var aFatherSObj = aSObj->GetFather();
108 if(aFatherSObj->_is_nil()) return aVisualObj;
109 aFatherSObj = aFatherSObj->GetFather();
110 if(aFatherSObj->_is_nil()) return aVisualObj;
111 CORBA::String_var anEntry = aFatherSObj->GetID();
112 TVisualObjPtr aVisObj = GetVisualObj(theStudyId,anEntry.in());
113 if(SMESH_MeshObj* aMeshObj = dynamic_cast<SMESH_MeshObj*>(aVisObj.get())){
114 aVisualObj.reset(new SMESH_subMeshObj(aSubMesh,aMeshObj));
115 aVisualObj->Update();
116 TVisualObjCont::value_type aValue(aKey,aVisualObj);
117 VISUAL_OBJ_CONT.insert(aValue);
126 INFOS("GetMeshObj - There is no SMESH_Mesh object for the SALOMEDS::Strudy and Entry!!!");
132 VTKViewer_ViewFrame* FindVtkViewFrame(QAD_Study* theStudy,
133 QAD_StudyFrame* theStudyFrame)
135 QList<QAD_Study>& aStudies =
136 QAD_Application::getDesktop()->getActiveApp()->getStudies();
137 if(aStudies.containsRef(theStudy)){
138 const QList<QAD_StudyFrame>& aStudyFrames = theStudy->getStudyFrames();
139 if(aStudyFrames.containsRef(theStudyFrame)){
140 return GetVtkViewFrame(theStudyFrame);
147 VTKViewer_ViewFrame* GetVtkViewFrame(QAD_StudyFrame* theStudyFrame){
148 QAD_ViewFrame* aViewFrame = theStudyFrame->getRightFrame()->getViewFrame();
149 return dynamic_cast<VTKViewer_ViewFrame*>(aViewFrame);
153 VTKViewer_ViewFrame* GetCurrentVtkView(){
154 return GetVtkViewFrame(GetActiveStudy()->getActiveStudyFrame());
158 SMESH_Actor* FindActorByEntry(QAD_StudyFrame *theStudyFrame,
159 const char* theEntry)
161 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
162 vtkRenderer *aRenderer = aViewFrame->getRenderer();
163 vtkActorCollection *aCollection = aRenderer->GetActors();
164 aCollection->InitTraversal();
165 while(vtkActor *anAct = aCollection->GetNextActor()){
166 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
167 if(anActor->hasIO()){
168 Handle(SALOME_InteractiveObject) anIO = anActor->getIO();
169 if(anIO->hasEntry() && strcmp(anIO->getEntry(),theEntry) == 0){
180 SMESH_Actor* FindActorByEntry(const char* theEntry){
181 return FindActorByEntry(GetActiveStudy()->getActiveStudyFrame(),theEntry);
185 SMESH_Actor* FindActorByObject(CORBA::Object_ptr theObject){
186 if(!CORBA::is_nil(theObject)){
187 SALOMEDS::Study_var aStudy = GetActiveStudyDocument();
188 CORBA::String_var anIOR = aStudy->ConvertObjectToIOR(theObject);
189 SALOMEDS::SObject_var aSObject = aStudy->FindObjectIOR(anIOR.in());
190 if(!aSObject->_is_nil()){
191 CORBA::String_var anEntry = aSObject->GetID();
192 return FindActorByEntry(anEntry.in());
199 SMESH_Actor* CreateActor(SALOMEDS::Study_ptr theStudy,
200 const char* theEntry,
203 SMESH_Actor *anActor = NULL;
204 CORBA::Long anId = theStudy->StudyId();
205 if(TVisualObjPtr aVisualObj = GetVisualObj(anId,theEntry)){
206 SALOMEDS::SObject_var aSObj = theStudy->FindObjectID(theEntry);
207 if(!aSObj->_is_nil()){
208 SALOMEDS::GenericAttribute_var anAttr;
209 if(aSObj->FindAttribute(anAttr,"AttributeName")){
210 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
211 CORBA::String_var aVal = aName->Value();
212 string aNameVal = aVal.in();
213 anActor = SMESH_Actor::New(aVisualObj,theEntry,aNameVal.c_str(),theIsClear);
221 void DisplayActor(QAD_StudyFrame *theStudyFrame, SMESH_Actor* theActor){
222 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
223 aViewFrame->AddActor(theActor);
224 aViewFrame->Repaint();
229 void RemoveActor(QAD_StudyFrame *theStudyFrame, SMESH_Actor* theActor){
230 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
231 aViewFrame->RemoveActor(theActor);
232 aViewFrame->Repaint();
238 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(GetActiveStudy()->getActiveStudyFrame())){
239 aViewFrame->onViewFitAll();
240 aViewFrame->Repaint();
244 vtkRenderer* GetCurrentRenderer(){
245 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(GetActiveStudy()->getActiveStudyFrame()))
246 return aViewFrame->getRenderer();
250 void RepaintCurrentView(){
251 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(GetActiveStudy()->getActiveStudyFrame())){
252 aViewFrame->getRW()->getRenderWindow()->Render();
253 //aViewFrame->Repaint();
258 void UpdateView(QAD_StudyFrame *theStudyFrame, EDisplaing theAction, const char* theEntry)
260 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
261 vtkRenderer *aRenderer = aViewFrame->getRenderer();
262 vtkActorCollection *aCollection = aRenderer->GetActors();
263 aCollection->InitTraversal();
266 while(vtkActor *anAct = aCollection->GetNextActor()){
267 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
268 anActor->SetVisibility(true);
275 while(vtkActor *anAct = aCollection->GetNextActor()){
276 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
277 anActor->SetVisibility(false);
282 if(SMESH_Actor *anActor = FindActorByEntry(theStudyFrame,theEntry)){
286 anActor->SetVisibility(true);
289 anActor->SetVisibility(false);
296 QAD_Study* aStudy = theStudyFrame->getStudy();
297 SALOMEDS::Study_var aDocument = aStudy->getStudyDocument();
298 if((anActor = CreateActor(aDocument,theEntry,true))) {
299 DisplayActor(theStudyFrame,anActor);
312 void UpdateView(EDisplaing theAction, const char* theEntry){
313 QAD_Study* aStudy = GetActiveStudy();
314 QAD_StudyFrame *aStudyFrame = aStudy->getActiveStudyFrame();
315 UpdateView(aStudyFrame,theAction,theEntry);
319 if(VTKViewer_ViewFrame* aViewFrame = SMESH::GetCurrentVtkView()){
320 SALOME_Selection *aSel = SALOME_Selection::Selection(GetActiveStudy()->getSelection());
321 if(aSel->IObjectCount() == 0){
322 vtkRenderer* aRenderer = aViewFrame->getRenderer();
323 vtkActorCollection *aCollection = aRenderer->GetActors();
324 aCollection->InitTraversal();
325 while(vtkActor *anAct = aCollection->GetNextActor()){
326 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
328 Update(anActor->getIO(),anActor->GetVisibility());
332 SALOME_ListIteratorOfListIO anIter(aSel->StoredIObjects());
333 for(; anIter.More(); anIter.Next()){
334 Handle(SALOME_InteractiveObject) anIO = anIter.Value();
338 RepaintCurrentView();
343 void Update(const Handle(SALOME_InteractiveObject)& theIO,
346 SALOMEDS::Study_var aStudy = GetActiveStudyDocument();
347 CORBA::Long anId = aStudy->StudyId();
348 TVisualObjPtr aVisualObj = SMESH::GetVisualObj(anId,theIO->getEntry());
349 aVisualObj->Update();
351 UpdateView(SMESH::eDisplay,theIO->getEntry());
355 void UpdateSelectionProp() {
356 QAD_Study* aStudy = GetActiveStudy();
357 QList<QAD_StudyFrame> aFrameList = aStudy->getStudyFrames();
359 QString SCr, SCg, SCb;
360 SCr = QAD_CONFIG->getSetting("SMESH:SettingsSelectColorRed");
361 SCg = QAD_CONFIG->getSetting("SMESH:SettingsSelectColorGreen");
362 SCb = QAD_CONFIG->getSetting("SMESH:SettingsSelectColorBlue");
363 QColor aHiColor = Qt::white;
364 if (!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())
365 aHiColor = QColor(SCr.toInt(), SCg.toInt(), SCb.toInt());
367 SCr = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectColorRed");
368 SCg = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectColorGreen");
369 SCb = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectColorBlue");
370 QColor aSelColor = Qt::yellow;
371 if (!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())
372 aSelColor = QColor(SCr.toInt(), SCg.toInt(), SCb.toInt());
373 QString SW = QAD_CONFIG->getSetting("SMESH:SettingsItemSelectWidth");
374 if (SW.isEmpty()) SW = "5";
376 SCr = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectColorRed");
377 SCg = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectColorGreen");
378 SCb = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectColorBlue");
379 QColor aPreColor = Qt::cyan;
380 if (!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())
381 aPreColor = QColor(SCr.toInt(), SCg.toInt(), SCb.toInt());
382 QString PW = QAD_CONFIG->getSetting("SMESH:SettingsPreSelectWidth");
383 if (PW.isEmpty()) PW = "5";
385 QString SP1 = QAD_CONFIG->getSetting("SMESH:SettingsNodeSelectTol");
386 if (SP1.isEmpty()) SP1 = "0.025";
387 QString SP2 = QAD_CONFIG->getSetting("SMESH:SettingsElementsSelectTol");
388 if (SP2.isEmpty()) SP2 = "0.001";
390 for (QAD_StudyFrame* aStudyFrame = aFrameList.first(); aStudyFrame; aStudyFrame = aFrameList.next()) {
391 if (aStudyFrame->getTypeView() == VIEW_VTK) {
392 VTKViewer_ViewFrame* aVtkViewFrame = GetVtkViewFrame(aStudyFrame);
393 if (!aVtkViewFrame) continue;
394 // update VTK viewer properties
395 VTKViewer_RenderWindowInteractor* anInteractor = aVtkViewFrame->getRWInteractor();
397 // mesh element selection
398 anInteractor->SetSelectionProp(aSelColor.red()/255., aSelColor.green()/255.,
399 aSelColor.blue()/255., SW.toInt());
402 anInteractor->SetSelectionTolerance(SP1.toDouble(), SP2.toDouble());
405 VTKViewer_InteractorStyleSALOME* aStyle = anInteractor->GetInteractorStyleSALOME();
407 aStyle->setPreselectionProp(aPreColor.red()/255., aPreColor.green()/255.,
408 aPreColor.blue()/255., PW.toInt());
412 vtkRenderer* aRenderer = aVtkViewFrame->getRenderer();
413 vtkActorCollection *aCollection = aRenderer->GetActors();
414 aCollection->InitTraversal();
415 while(vtkActor *anAct = aCollection->GetNextActor()){
416 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
417 anActor->SetHighlightColor(aHiColor.red()/255., aHiColor.green()/255.,
418 aHiColor.blue()/255.);
419 anActor->SetPreHighlightColor(aPreColor.red()/255., aPreColor.green()/255.,
420 aPreColor.blue()/255.);
428 //----------------------------------------------------------------------------
429 VTKViewer_InteractorStyleSALOME* GetInteractorStyle(QAD_StudyFrame *theStudyFrame){
430 if(VTKViewer_ViewFrame* aViewFrame = GetVtkViewFrame(theStudyFrame)){
431 if(VTKViewer_RenderWindowInteractor* anInteractor = aViewFrame->getRWInteractor()){
432 return anInteractor->GetInteractorStyleSALOME();
438 void SetFilter(const Handle(VTKViewer_Filter)& theFilter,
439 VTKViewer_InteractorStyleSALOME* theStyle)
441 theStyle->SetFilter(theFilter);
444 Handle(VTKViewer_Filter) GetFilter(int theId, VTKViewer_InteractorStyleSALOME* theStyle)
446 return theStyle->GetFilter(theId);
449 bool IsFilterPresent(int theId, VTKViewer_InteractorStyleSALOME* theStyle)
451 return theStyle->IsFilterPresent(theId);
454 void RemoveFilter(int theId, VTKViewer_InteractorStyleSALOME* theStyle){
455 theStyle->RemoveFilter(theId);
458 bool IsValid(SALOME_Actor* theActor, int theCellId,
459 VTKViewer_InteractorStyleSALOME* theStyle)
461 return theStyle->IsValid(theActor,theCellId);
465 //----------------------------------------------------------------------------
466 void SetPointRepresentation(bool theIsVisible){
467 if(VTKViewer_ViewFrame* aViewFrame = GetCurrentVtkView()){
468 vtkRenderer *aRenderer = aViewFrame->getRenderer();
469 vtkActorCollection *aCollection = aRenderer->GetActors();
470 aCollection->InitTraversal();
471 while(vtkActor *anAct = aCollection->GetNextActor()){
472 if(SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>(anAct)){
473 if(anActor->GetVisibility()){
474 anActor->SetPointRepresentation(theIsVisible);
478 RepaintCurrentView();
483 void SetPickable(SMESH_Actor* theActor){
484 if(VTKViewer_ViewFrame* aViewFrame = GetCurrentVtkView()){
485 int anIsAllPickable = (theActor == NULL);
486 vtkRenderer *aRenderer = aViewFrame->getRenderer();
487 vtkActorCollection *aCollection = aRenderer->GetActors();
488 aCollection->InitTraversal();
489 while(vtkActor *anAct = aCollection->GetNextActor()){
490 if(SALOME_Actor *anActor = dynamic_cast<SALOME_Actor*>(anAct)){
491 if(anActor->GetVisibility()){
492 anActor->SetPickable(anIsAllPickable);
497 theActor->SetPickable(!anIsAllPickable);
498 RepaintCurrentView();
503 int GetNameOfSelectedNodes(SALOME_Selection *theSel,
504 const Handle(SALOME_InteractiveObject)& theIO,
508 if(theIO->hasEntry()){
509 if(SMESH_Actor *anActor = FindActorByEntry(theIO->getEntry())){
510 TColStd_IndexedMapOfInteger aMapIndex;
511 theSel->GetIndex(theIO,aMapIndex);
512 for(int i = 1; i <= aMapIndex.Extent(); i++){
513 theName += QString(" %1").arg(aMapIndex(i));
515 return aMapIndex.Extent();
522 int GetNameOfSelectedNodes(SALOME_Selection *theSel, QString& theName){
524 if(theSel->IObjectCount() == 1){
525 Handle(SALOME_InteractiveObject) anIO = theSel->firstIObject();
526 return GetNameOfSelectedNodes(theSel,anIO,theName);
532 int GetNameOfSelectedElements(SALOME_Selection *theSel,
533 const Handle(SALOME_InteractiveObject)& theIO,
537 if(theIO->hasEntry()){
538 if(SMESH_Actor *anActor = FindActorByEntry(theIO->getEntry())){
539 TColStd_IndexedMapOfInteger aMapIndex;
540 theSel->GetIndex(theIO,aMapIndex);
541 typedef std::set<int> TIdContainer;
542 TIdContainer anIdContainer;
543 for( int i = 1; i <= aMapIndex.Extent(); i++)
544 anIdContainer.insert(aMapIndex(i));
545 TIdContainer::const_iterator anIter = anIdContainer.begin();
546 for(; anIter != anIdContainer.end(); anIter++){
547 theName += QString(" %1").arg(*anIter);
549 return aMapIndex.Extent();
556 int GetNameOfSelectedElements(SALOME_Selection *theSel, QString& theName)
559 if(theSel->IObjectCount() == 1){
560 Handle(SALOME_InteractiveObject) anIO = theSel->firstIObject();
561 return GetNameOfSelectedElements(theSel,anIO,theName);
567 int GetSelected(SALOME_Selection* theSel,
568 TColStd_IndexedMapOfInteger& theMap,
569 const bool theIsElement)
573 if ( theSel->IObjectCount() == 1 )
575 Handle(SALOME_InteractiveObject) anIO = theSel->firstIObject();
576 if ( anIO->hasEntry() ) {
577 theSel->GetIndex( anIO, theMap );
580 return theMap.Extent();
584 int GetEdgeNodes( SALOME_Selection* theSel, int& theId1, int& theId2 )
586 if ( theSel->IObjectCount() != 1 )
589 Handle(SALOME_InteractiveObject) anIO = theSel->firstIObject();
590 if ( anIO.IsNull() || !anIO->hasEntry() )
593 SMESH_Actor *anActor = SMESH::FindActorByEntry( anIO->getEntry() );
597 TColStd_IndexedMapOfInteger aMapIndex;
598 theSel->GetIndex( anIO, aMapIndex );
599 if ( aMapIndex.Extent() != 2 )
602 int anObjId = -1, anEdgeNum = -1;
603 for ( int i = 1; i <= aMapIndex.Extent(); i++ ) {
604 int aVal = aMapIndex( i );
608 anEdgeNum = abs( aVal );
611 if ( anObjId == -1 || anEdgeNum == -1 )
614 return anActor->GetObject()->GetEdgeNodes( anObjId, anEdgeNum, theId1, theId2 ) ? 1 : -1;
617 void SetControlsPrecision( const long theVal )
619 if( VTKViewer_ViewFrame* aViewFrame = SMESH::GetCurrentVtkView() )
621 vtkRenderer *aRenderer = aViewFrame->getRenderer();
622 vtkActorCollection *aCollection = aRenderer->GetActors();
623 aCollection->InitTraversal();
625 while ( vtkActor *anAct = aCollection->GetNextActor())
627 if ( SMESH_Actor *anActor = dynamic_cast<SMESH_Actor*>( anAct ) )
629 anActor->SetControlsPrecision( theVal );
630 anActor->SetControlMode( anActor->GetControlMode() );