]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Displayer.cpp
Salome HOME
8daeaa9ed808efd4c3aeae18401926ebb719fa55
[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 #include <ModelAPI_Data.h>
12
13 #include <AIS_InteractiveContext.hxx>
14 #include <AIS_ListOfInteractive.hxx>
15 #include <AIS_ListIteratorOfListOfInteractive.hxx>
16
17 #include <AIS_Shape.hxx>
18
19 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
20 {
21   myWorkshop = theWorkshop;
22 }
23
24 XGUI_Displayer::~XGUI_Displayer()
25 {
26 }
27
28 bool XGUI_Displayer::IsVisible(boost::shared_ptr<ModelAPI_Feature> theFeature)
29 {
30   return myFeature2AISObjectMap.find(theFeature) != myFeature2AISObjectMap.end();
31 }
32
33 void XGUI_Displayer::Display(boost::shared_ptr<ModelAPI_Feature> theFeature,
34                              const bool isUpdateViewer)
35 {
36 }
37
38 void XGUI_Displayer::Display(boost::shared_ptr<ModelAPI_Feature> theFeature,
39                              const TopoDS_Shape& theShape, const bool isUpdateViewer)
40 {
41   Handle(AIS_InteractiveContext) aContext = AISContext();
42
43   Handle(AIS_Shape) anAIS = new AIS_Shape(theShape);
44   myFeature2AISObjectMap[theFeature] = anAIS;
45
46   aContext->Display(anAIS, Standard_False);
47   if (isUpdateViewer)
48     aContext->UpdateCurrentViewer();
49 }
50
51 boost::shared_ptr<ModelAPI_Feature> XGUI_Displayer::GetFeature(const TopoDS_Shape& theShape)
52 {
53   boost::shared_ptr<ModelAPI_Feature> aFeature;
54
55   FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
56                                   aFLast = myFeature2AISObjectMap.end();
57   for (; aFIt != aFLast && !aFeature; aFIt++)
58   {
59     Handle(AIS_InteractiveObject) anAIS = (*aFIt).second;
60     Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast(anAIS);
61     if (!anAISShape.IsNull() && anAISShape->Shape() == theShape) {
62       aFeature = (*aFIt).first;
63     }
64   }
65   return aFeature;
66 }
67
68 std::list<XGUI_ViewerPrs> XGUI_Displayer::GetViewerPrs
69                                                 (const NCollection_List<TopoDS_Shape>& theShapes)
70 {
71   std::list<XGUI_ViewerPrs> aPresentations;
72   if (theShapes.IsEmpty())
73     return aPresentations;
74
75   NCollection_List<TopoDS_Shape>::Iterator anIt(theShapes);
76   for (; anIt.More(); anIt.Next()) {
77     const TopoDS_Shape& aShape = anIt.Value();
78     if (aShape.IsNull())
79       continue;
80     boost::shared_ptr<ModelAPI_Feature> aFeature = GetFeature(aShape);
81     aPresentations.push_back(XGUI_ViewerPrs(aFeature, aShape));
82   }
83
84   return aPresentations;
85 }
86
87 void XGUI_Displayer::Erase(boost::shared_ptr<ModelAPI_Feature> theFeature,
88                            const bool isUpdateViewer)
89 {
90   if (myFeature2AISObjectMap.find(theFeature) == myFeature2AISObjectMap.end())
91     return;
92
93   Handle(AIS_InteractiveContext) aContext = AISContext();
94   Handle(AIS_InteractiveObject) anAIS = myFeature2AISObjectMap[theFeature];
95   Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast(anAIS);
96   if (!anAISShape.IsNull())
97   {
98     aContext->Erase(anAISShape);
99   }
100   myFeature2AISObjectMap.erase(theFeature);
101
102   if (isUpdateViewer)
103     aContext->UpdateCurrentViewer();
104 }
105
106 void XGUI_Displayer::RedisplayInLocalContext(boost::shared_ptr<ModelAPI_Feature> theFeature,
107                                              const TopoDS_Shape& theShape,
108                                              const std::list<int>& theModes, const bool isUpdateViewer)
109 {
110   Handle(AIS_InteractiveContext) aContext = AISContext();
111   // Open local context if there is no one
112   if (!aContext->HasOpenedContext()) {
113     aContext->ClearCurrents(false);
114     aContext->OpenLocalContext(false/*use displayed objects*/, /*true*/false/*use displayed objects*/,
115                          true/*allow shape decomposition*/);
116   }
117   // display or redisplay presentation
118   Handle(AIS_Shape) anAIS;
119   if (IsVisible(theFeature)) {
120     anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[theFeature]);
121     if (!anAIS.IsNull()) {
122       anAIS->Set(theShape);
123       anAIS->Redisplay();
124     }
125   }
126   else {
127     anAIS = new AIS_Shape(theShape);
128     myFeature2AISObjectMap[theFeature] = anAIS;
129     aContext->Display(anAIS, false);
130   }
131   // Activate selection of objects from prs
132   if (!anAIS.IsNull()) {
133     aContext->Load(anAIS, -1, true/*allow decomposition*/);
134     std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
135     for (; anIt != aLast; anIt++)
136     {
137       aContext->Activate(anAIS, AIS_Shape::SelectionMode((TopAbs_ShapeEnum)*anIt));
138     }
139   }
140
141   if (isUpdateViewer)
142     aContext->UpdateCurrentViewer();
143 }
144
145 void XGUI_Displayer::EraseAll(const bool isUpdateViewer)
146 {
147   Handle(AIS_InteractiveContext) ic = AISContext();
148
149   AIS_ListOfInteractive aList;
150   ic->DisplayedObjects(aList);
151   AIS_ListIteratorOfListOfInteractive anIter(aList);
152   for (; anIter.More(); anIter.Next()) {
153     if ((anIter.Value()->DynamicType() == STANDARD_TYPE(AIS_Trihedron)))
154       continue;
155
156     // erase an object
157     Handle(AIS_InteractiveObject) anIO = anIter.Value();
158     ic->Erase(anIO, false);
159   }
160   myFeature2AISObjectMap.clear();
161   if (isUpdateViewer)
162     ic->UpdateCurrentViewer();
163 }
164
165 void XGUI_Displayer::EraseDeletedFeatures(const bool isUpdateViewer)
166 {
167   Handle(AIS_InteractiveContext) aContext = AISContext();
168
169   FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
170                                   aFLast = myFeature2AISObjectMap.end();
171   std::list<boost::shared_ptr<ModelAPI_Feature>> aRemoved;
172   for (; aFIt != aFLast; aFIt++)
173   {
174     boost::shared_ptr<ModelAPI_Feature> aFeature = (*aFIt).first;
175     if (!aFeature || !aFeature->data()->isValid()) {
176       Handle(AIS_InteractiveObject) anAIS = (*aFIt).second;
177       if (!anAIS.IsNull()) {
178         aContext->Erase(anAIS, false);
179         aRemoved.push_back(aFeature);
180       }
181     }
182   }
183   std::list<boost::shared_ptr<ModelAPI_Feature>>::const_iterator anIt = aRemoved.begin(),
184                                                                  aLast = aRemoved.end();
185   for (; anIt != aLast; anIt++) {
186     myFeature2AISObjectMap.erase(myFeature2AISObjectMap.find(*anIt));
187   }
188
189   if (isUpdateViewer)
190     aContext->UpdateCurrentViewer();
191 }
192
193 void XGUI_Displayer::CloseLocalContexts(const bool isUpdateViewer)
194 {
195   closeAllContexts(true);
196 }
197
198 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
199 {
200   Handle(AIS_InteractiveContext) ic = AISContext();
201   if (!ic.IsNull()) {
202     ic->CloseAllContexts(false);
203     if (isUpdateViewer)
204       ic->UpdateCurrentViewer();
205   }
206 }
207
208 void XGUI_Displayer::UpdateViewer()
209 {
210   Handle(AIS_InteractiveContext) ic = AISContext();
211   if (!ic.IsNull())
212     ic->UpdateCurrentViewer();
213 }
214
215 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const 
216
217   return myWorkshop->viewer()->AISContext(); 
218 }