1 // File: XGUI_Displayer.cpp
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include "XGUI_Displayer.h"
6 #include "XGUI_Viewer.h"
7 #include "XGUI_Workshop.h"
8 #include "XGUI_ViewerProxy.h"
9 #include "XGUI_Tools.h"
11 #include <ModelAPI_Document.h>
12 #include <ModelAPI_Data.h>
14 #include <AIS_InteractiveContext.hxx>
15 #include <AIS_LocalContext.hxx>
16 #include <AIS_ListOfInteractive.hxx>
17 #include <AIS_ListIteratorOfListOfInteractive.hxx>
19 #include <AIS_Shape.hxx>
23 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
25 myWorkshop = theWorkshop;
28 XGUI_Displayer::~XGUI_Displayer()
32 bool XGUI_Displayer::IsVisible(boost::shared_ptr<ModelAPI_Feature> theFeature)
34 return myFeature2AISObjectMap.find(theFeature) != myFeature2AISObjectMap.end();
37 void XGUI_Displayer::Display(boost::shared_ptr<ModelAPI_Feature> theFeature,
38 const bool isUpdateViewer)
42 /*void XGUI_Displayer::Display(boost::shared_ptr<ModelAPI_Feature> theFeature,
43 const TopoDS_Shape& theShape, const bool isUpdateViewer)
45 Handle(AIS_InteractiveContext) aContext = AISContext();
47 Handle(AIS_Shape) anAIS = new AIS_Shape(theShape);
48 myFeature2AISObjectMap[theFeature] = anAIS;
50 aContext->Display(anAIS, Standard_False);
52 aContext->UpdateCurrentViewer();
56 std::list<XGUI_ViewerPrs> XGUI_Displayer::GetSelected()
58 std::set<boost::shared_ptr<ModelAPI_Feature> > aPrsFeatures;
59 std::list<XGUI_ViewerPrs> aPresentations;
61 Handle(AIS_InteractiveContext) aContext = AISContext();
62 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
63 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
64 TopoDS_Shape aShape = aContext->SelectedShape();
66 boost::shared_ptr<ModelAPI_Feature> aFeature = GetFeature(anIO);
67 if (aPrsFeatures.find(aFeature) != aPrsFeatures.end())
69 aPresentations.push_back(XGUI_ViewerPrs(aFeature, aShape));
70 aPrsFeatures.insert(aFeature);
72 return aPresentations;
75 std::list<XGUI_ViewerPrs> XGUI_Displayer::GetHighlighted()
77 std::set<boost::shared_ptr<ModelAPI_Feature> > aPrsFeatures;
78 std::list<XGUI_ViewerPrs> aPresentations;
80 Handle(AIS_InteractiveContext) aContext = AISContext();
81 for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
82 Handle(AIS_InteractiveObject) anIO = aContext->DetectedInteractive();
83 TopoDS_Shape aShape = aContext->DetectedShape();
85 boost::shared_ptr<ModelAPI_Feature> aFeature = GetFeature(anIO);
86 if (aPrsFeatures.find(aFeature) != aPrsFeatures.end())
88 aPresentations.push_back(XGUI_ViewerPrs(aFeature, aShape));
89 aPrsFeatures.insert(aFeature);
92 return aPresentations;
95 void XGUI_Displayer::Erase(boost::shared_ptr<ModelAPI_Feature> theFeature,
96 const bool isUpdateViewer)
98 if (myFeature2AISObjectMap.find(theFeature) == myFeature2AISObjectMap.end())
101 Handle(AIS_InteractiveContext) aContext = AISContext();
102 Handle(AIS_InteractiveObject) anAIS = myFeature2AISObjectMap[theFeature];
103 Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast(anAIS);
104 if (!anAISShape.IsNull())
106 aContext->Erase(anAISShape);
108 myFeature2AISObjectMap.erase(theFeature);
111 aContext->UpdateCurrentViewer();
114 void XGUI_Displayer::Redisplay(boost::shared_ptr<ModelAPI_Feature> theFeature,
115 const TopoDS_Shape& theShape, const bool isUpdateViewer)
117 Handle(AIS_InteractiveContext) aContext = AISContext();
118 // Open local context if there is no one
119 if (!aContext->HasOpenedContext()) {
120 aContext->ClearCurrents(false);
121 aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
123 // display or redisplay presentation
124 Handle(AIS_Shape) anAIS;
125 if (IsVisible(theFeature)) {
126 anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[theFeature]);
127 if (!anAIS.IsNull()) {
128 // if the AIS object is displayed in the opened local context in some mode, additional
129 // AIS sub objects are created there. They should be rebuild for correct selecting.
130 // It is possible to correct it by closing local context before the shape set and opening
131 // after. Another workaround to thrown down the selection and reselecting the AIS.
132 // If there was a problem here, try the first solution with close/open local context.
133 anAIS->Set(theShape);
136 /*if (aContext->IsSelected(anAIS)) {
137 aContext->AddOrRemoveSelected(anAIS, false);
138 aContext->AddOrRemoveSelected(anAIS, false);
139 //aContext->SetSelected(anAIS, false);
144 anAIS = new AIS_Shape(theShape);
145 myFeature2AISObjectMap[theFeature] = anAIS;
146 aContext->Display(anAIS, false);
150 void XGUI_Displayer::ActivateInLocalContext(boost::shared_ptr<ModelAPI_Feature> theFeature,
151 const std::list<int>& theModes, const bool isUpdateViewer)
153 Handle(AIS_InteractiveContext) aContext = AISContext();
154 // Open local context if there is no one
155 if (!aContext->HasOpenedContext()) {
156 aContext->ClearCurrents(false);
157 aContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
159 // display or redisplay presentation
160 Handle(AIS_Shape) anAIS;
161 if (IsVisible(theFeature))
162 anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[theFeature]);
164 // Activate selection of objects from prs
165 if (!anAIS.IsNull()) {
166 aContext->Load(anAIS, -1, true/*allow decomposition*/);
167 aContext->Deactivate(anAIS);
169 std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
170 for (; anIt != aLast; anIt++)
172 aContext->Activate(anAIS, AIS_Shape::SelectionMode((TopAbs_ShapeEnum)*anIt));
177 aContext->UpdateCurrentViewer();
180 void XGUI_Displayer::StopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop,
181 const bool isUpdateViewer)
183 Handle(AIS_InteractiveContext) aContext = AISContext();
185 Handle(AIS_Shape) anAIS;
186 std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
187 boost::shared_ptr<ModelAPI_Feature> aFeature;
188 for (; anIt != aLast; anIt++) {
189 aFeature = (*anIt).feature();
190 if (IsVisible(aFeature))
191 anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[aFeature]);
196 QColor aColor(Qt::white);
197 anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
201 QColor aColor(Qt::red);
202 anAIS->SetColor(Quantity_Color(aColor.red()/255., aColor.green()/255., aColor.blue()/255., Quantity_TOC_RGB));
207 aContext->UpdateCurrentViewer();
210 void XGUI_Displayer::SetSelected(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isUpdateViewer)
212 Handle(AIS_InteractiveContext) aContext = AISContext();
214 std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
215 boost::shared_ptr<ModelAPI_Feature> aFeature;
217 Handle(AIS_Shape) anAIS;
218 // we need to unhighligth objects manually in the current local context
219 // in couple with the selection clear (TODO)
220 Handle(AIS_LocalContext) aLocalContext = aContext->LocalContext();
221 if (!aLocalContext.IsNull())
222 aLocalContext->UnhilightLastDetected(myWorkshop->viewer()->activeView());
223 aContext->ClearSelected(false);
225 for (; anIt != aLast; anIt++) {
226 aFeature = (*anIt).feature();
227 if (IsVisible(aFeature))
228 anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[aFeature]);
231 aContext->AddOrRemoveSelected(anAIS, false);
235 aContext->UpdateCurrentViewer();
238 void XGUI_Displayer::EraseAll(const bool isUpdateViewer)
240 Handle(AIS_InteractiveContext) ic = AISContext();
242 AIS_ListOfInteractive aList;
243 ic->DisplayedObjects(aList);
244 AIS_ListIteratorOfListOfInteractive anIter(aList);
245 for (; anIter.More(); anIter.Next()) {
246 if ((anIter.Value()->DynamicType() == STANDARD_TYPE(AIS_Trihedron)))
250 Handle(AIS_InteractiveObject) anIO = anIter.Value();
251 ic->Erase(anIO, false);
253 myFeature2AISObjectMap.clear();
255 ic->UpdateCurrentViewer();
258 void XGUI_Displayer::EraseDeletedFeatures(const bool isUpdateViewer)
260 Handle(AIS_InteractiveContext) aContext = AISContext();
262 FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
263 aFLast = myFeature2AISObjectMap.end();
264 std::list<boost::shared_ptr<ModelAPI_Feature>> aRemoved;
265 for (; aFIt != aFLast; aFIt++)
267 boost::shared_ptr<ModelAPI_Feature> aFeature = (*aFIt).first;
268 if (!aFeature || !aFeature->data() || !aFeature->data()->isValid()) {
269 Handle(AIS_InteractiveObject) anAIS = (*aFIt).second;
270 if (!anAIS.IsNull()) {
271 aContext->Erase(anAIS, false);
272 aRemoved.push_back(aFeature);
276 std::list<boost::shared_ptr<ModelAPI_Feature>>::const_iterator anIt = aRemoved.begin(),
277 aLast = aRemoved.end();
278 for (; anIt != aLast; anIt++) {
279 myFeature2AISObjectMap.erase(myFeature2AISObjectMap.find(*anIt));
283 aContext->UpdateCurrentViewer();
286 void XGUI_Displayer::CloseLocalContexts(const bool isUpdateViewer)
288 CloseAllContexts(true);
291 boost::shared_ptr<ModelAPI_Feature> XGUI_Displayer::GetFeature(Handle(AIS_InteractiveObject) theIO)
293 boost::shared_ptr<ModelAPI_Feature> aFeature;
294 FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
295 aFLast = myFeature2AISObjectMap.end();
296 for (; aFIt != aFLast && !aFeature; aFIt++) {
297 Handle(AIS_InteractiveObject) anAIS = (*aFIt).second;
300 aFeature = (*aFIt).first;
305 void XGUI_Displayer::CloseAllContexts(const bool isUpdateViewer)
307 Handle(AIS_InteractiveContext) ic = AISContext();
309 ic->CloseAllContexts(false);
311 ic->UpdateCurrentViewer();
315 void XGUI_Displayer::UpdateViewer()
317 Handle(AIS_InteractiveContext) ic = AISContext();
319 ic->UpdateCurrentViewer();
322 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
324 return myWorkshop->viewer()->AISContext();