]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Displayer.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom
[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 #include "XGUI_Workshop.h"
8 #include "XGUI_ViewerProxy.h"
9
10 #include <ModelAPI_Document.h>
11
12 #include <AIS_InteractiveContext.hxx>
13 #include <AIS_ListOfInteractive.hxx>
14 #include <AIS_ListIteratorOfListOfInteractive.hxx>
15
16 #include <AIS_Shape.hxx>
17
18 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
19 {
20   myWorkshop = theWorkshop;
21 }
22
23 XGUI_Displayer::~XGUI_Displayer()
24 {
25 }
26
27 bool XGUI_Displayer::IsVisible(boost::shared_ptr<ModelAPI_Feature> theFeature)
28 {
29   return myFeature2AISObjectMap.find(theFeature) != myFeature2AISObjectMap.end();
30 }
31
32 void XGUI_Displayer::Display(boost::shared_ptr<ModelAPI_Feature> theFeature,
33                              const bool isUpdateViewer)
34 {
35 }
36
37 void XGUI_Displayer::Display(boost::shared_ptr<ModelAPI_Feature> theFeature,
38                              const TopoDS_Shape& theShape, const bool isUpdateViewer)
39 {
40   Handle(AIS_InteractiveContext) aContext = AISContext();
41
42   Handle(AIS_Shape) anAIS = new AIS_Shape(theShape);
43   myFeature2AISObjectMap[theFeature] = anAIS;
44
45   aContext->Display(anAIS, Standard_False);
46   if (isUpdateViewer)
47     aContext->UpdateCurrentViewer();
48 }
49
50 boost::shared_ptr<ModelAPI_Feature> XGUI_Displayer::GetFeature(const TopoDS_Shape& theShape)
51 {
52   boost::shared_ptr<ModelAPI_Feature> aFeature;
53
54   FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
55                                   aFLast = myFeature2AISObjectMap.end();
56   for (; aFIt != aFLast && !aFeature; aFIt++)
57   {
58     Handle(AIS_InteractiveObject) anAIS = (*aFIt).second;
59     Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast(anAIS);
60     if (!anAISShape.IsNull() && anAISShape->Shape() == theShape) {
61       aFeature = (*aFIt).first;
62     }
63   }
64   return aFeature;
65 }
66
67 void XGUI_Displayer::Erase(boost::shared_ptr<ModelAPI_Feature> theFeature,
68                            const bool isUpdateViewer)
69 {
70   if (myFeature2AISObjectMap.find(theFeature) == myFeature2AISObjectMap.end())
71     return;
72
73   Handle(AIS_InteractiveContext) aContext = AISContext();
74   Handle(AIS_InteractiveObject) anAIS = myFeature2AISObjectMap[theFeature];
75   Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast(anAIS);
76   if (!anAISShape.IsNull())
77   {
78     aContext->Erase(anAISShape);
79   }
80   myFeature2AISObjectMap.erase(theFeature);
81
82   if (isUpdateViewer)
83     aContext->UpdateCurrentViewer();
84 }
85
86 void XGUI_Displayer::RedisplayInLocalContext(boost::shared_ptr<ModelAPI_Feature> theFeature,
87                                              const TopoDS_Shape& theShape,
88                                              const std::list<int>& theModes, const bool isUpdateViewer)
89 {
90   Handle(AIS_InteractiveContext) aContext = AISContext();
91   // Open local context if there is no one
92   if (!aContext->HasOpenedContext()) {
93     aContext->ClearCurrents(false);
94     aContext->OpenLocalContext(false/*use displayed objects*/, /*true*/false/*use displayed objects*/,
95                          true/*allow shape decomposition*/);
96   }
97   // display or redisplay presentation
98   Handle(AIS_Shape) anAIS;
99   if (IsVisible(theFeature)) {
100     anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[theFeature]);
101     if (!anAIS.IsNull()) {
102       anAIS->Set(theShape);
103       anAIS->Redisplay();
104     }
105   }
106   else {
107     anAIS = new AIS_Shape(theShape);
108     myFeature2AISObjectMap[theFeature] = anAIS;
109     aContext->Display(anAIS, false);
110   }
111   // Activate selection of objects from prs
112   if (!anAIS.IsNull()) {
113     aContext->Load(anAIS, -1, true/*allow decomposition*/);
114     std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
115     for (; anIt != aLast; anIt++)
116     {
117       aContext->Activate(anAIS, AIS_Shape::SelectionMode((TopAbs_ShapeEnum)*anIt));
118     }
119   }
120
121   if (isUpdateViewer)
122     aContext->UpdateCurrentViewer();
123 }
124
125 void XGUI_Displayer::CloseLocalContexts(const bool isUpdateViewer)
126 {
127   closeAllContexts(true);
128 }
129
130 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
131 {
132   Handle(AIS_InteractiveContext) ic = AISContext();
133   if (!ic.IsNull()) {
134     ic->CloseAllContexts(false);
135     if (isUpdateViewer)
136       ic->UpdateCurrentViewer();
137   }
138 }
139
140 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const 
141
142   return myWorkshop->viewer()->AISContext(); 
143 }