Salome HOME
Issue #6 Extended processing of nested actions.
[modules/shaper.git] / src / XGUI / XGUI_Displayer.cpp
1 // File:        XGUI_Displayer.cpp
2 // Created:     20 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include "XGUI_Displayer.h"
6 #include "XGUI_Viewer.h"
7
8 #include <ModelAPI_Document.h>
9
10 #include <AIS_InteractiveContext.hxx>
11 #include <AIS_ListOfInteractive.hxx>
12 #include <AIS_ListIteratorOfListOfInteractive.hxx>
13
14 #include <AIS_Shape.hxx>
15
16 XGUI_Displayer::XGUI_Displayer(const Handle(AIS_InteractiveContext)& theAIS)
17 {
18   myAISContext = theAIS;
19 }
20
21 XGUI_Displayer::~XGUI_Displayer()
22 {
23 }
24
25 bool XGUI_Displayer::IsVisible(boost::shared_ptr<ModelAPI_Feature> theFeature)
26 {
27   return myFeature2AISObjectMap.find(theFeature) != myFeature2AISObjectMap.end();
28 }
29
30 void XGUI_Displayer::Display(boost::shared_ptr<ModelAPI_Feature> theFeature,
31                              const bool isUpdateViewer)
32 {
33 }
34
35 void XGUI_Displayer::Display(boost::shared_ptr<ModelAPI_Feature> theFeature,
36                              const TopoDS_Shape& theShape, const bool isUpdateViewer)
37 {
38   Handle(AIS_InteractiveContext) aContext = AISContext();
39
40   Handle(AIS_Shape) anAIS = new AIS_Shape(theShape);
41   std::vector<Handle(AIS_InteractiveObject)> aDispAIS;
42   if (myFeature2AISObjectMap.find(theFeature) != myFeature2AISObjectMap.end()) {
43     aDispAIS = myFeature2AISObjectMap[theFeature];
44   }
45   aDispAIS.push_back(anAIS);
46   myFeature2AISObjectMap[theFeature] = aDispAIS;
47
48   aContext->Display(anAIS, Standard_False);
49
50   if (isUpdateViewer)
51     aContext->UpdateCurrentViewer();
52 }
53
54 void XGUI_Displayer::Erase(boost::shared_ptr<ModelAPI_Feature> theFeature,
55                            const bool isUpdateViewer)
56 {
57   if (myFeature2AISObjectMap.find(theFeature) == myFeature2AISObjectMap.end())
58     return;
59
60   std::vector<Handle(AIS_InteractiveObject)> aDispAIS = myFeature2AISObjectMap[theFeature];
61   std::vector<Handle(AIS_InteractiveObject)>::const_iterator anIt = aDispAIS.begin(),
62                                                              aLast = aDispAIS.end();
63   Handle(AIS_InteractiveContext) aContext = AISContext();
64   for (; anIt != aLast; anIt++) {
65     Handle(AIS_InteractiveObject) anAIS = *anIt;
66     Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast(anAIS);
67     if (anAISShape.IsNull())
68       continue;
69       aContext->Erase(anAISShape);
70   }
71
72   if (isUpdateViewer)
73     aContext->UpdateCurrentViewer();
74 }
75
76 void XGUI_Displayer::LocalSelection(boost::shared_ptr<ModelAPI_Feature> theFeature,
77                                     const TopoDS_Shape& theShape,
78                                     const int theMode, const bool isUpdateViewer)
79 {
80   Handle(AIS_InteractiveContext) aContext = AISContext();
81   
82   if (IsVisible(theFeature)) {
83     Erase(theFeature, false);
84   }
85
86   Handle(AIS_Shape) anAIS = new AIS_Shape(theShape);
87   std::vector<Handle(AIS_InteractiveObject)> aDispAIS;
88   if (myFeature2AISObjectMap.find(theFeature) != myFeature2AISObjectMap.end()) {
89     aDispAIS = myFeature2AISObjectMap[theFeature];
90   }
91   aDispAIS.push_back(anAIS);
92   myFeature2AISObjectMap[theFeature] = aDispAIS;
93   aContext->Display(anAIS, Standard_False);
94
95   AIS_ListOfInteractive anAISList;
96   anAISList.Append(anAIS);
97   setLocalSelection(anAISList, theMode, true);
98 }
99
100 void XGUI_Displayer::GlobalSelection(const bool isUpdateViewer)
101 {
102   setGlobalSelection(true);
103 }
104
105 void XGUI_Displayer::setLocalSelection(const AIS_ListOfInteractive& theAISObjects, const int theMode,
106                                     const bool isUpdateViewer)
107 {
108   Handle(AIS_InteractiveContext) ic = AISContext();
109
110   // Open local context if there is no one
111   bool allObjects = false; // calculate by AIS shape
112   if (!ic->HasOpenedContext()) {
113     ic->ClearCurrents(false);
114     ic->OpenLocalContext(allObjects, true, true);
115   }
116
117   // Activate selection of objects from prs
118   AIS_ListIteratorOfListOfInteractive aIter(theAISObjects);
119   for (; aIter.More(); aIter.Next()) {
120     Handle(AIS_InteractiveObject) anAIS = aIter.Value();
121     if (!anAIS.IsNull()) {
122       if (anAIS->IsKind(STANDARD_TYPE(AIS_Shape))) {
123         ic->Load(anAIS, -1, false);
124         ic->Activate(anAIS, AIS_Shape::SelectionMode((TopAbs_ShapeEnum)theMode));
125       }
126       else if (anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron)) {
127         ic->Load(anAIS, -1, false);
128         ic->Activate(anAIS, theMode);
129       }
130     }
131   }
132   if (isUpdateViewer)
133     ic->UpdateCurrentViewer();
134 }
135
136 void XGUI_Displayer::setGlobalSelection(const bool isUpdateViewer)
137 {
138   Handle(AIS_InteractiveContext) ic = AISContext();
139   if (!ic.IsNull()) {
140     ic->CloseAllContexts(false);
141     if (isUpdateViewer)
142       ic->UpdateCurrentViewer();
143   }
144 }