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_ListOfInteractive.hxx>
16 #include <AIS_ListIteratorOfListOfInteractive.hxx>
18 #include <AIS_Shape.hxx>
22 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
24 myWorkshop = theWorkshop;
27 XGUI_Displayer::~XGUI_Displayer()
31 bool XGUI_Displayer::IsVisible(boost::shared_ptr<ModelAPI_Feature> theFeature)
33 return myFeature2AISObjectMap.find(theFeature) != myFeature2AISObjectMap.end();
36 void XGUI_Displayer::Display(boost::shared_ptr<ModelAPI_Feature> theFeature,
37 const bool isUpdateViewer)
41 /*void XGUI_Displayer::Display(boost::shared_ptr<ModelAPI_Feature> theFeature,
42 const TopoDS_Shape& theShape, const bool isUpdateViewer)
44 Handle(AIS_InteractiveContext) aContext = AISContext();
46 Handle(AIS_Shape) anAIS = new AIS_Shape(theShape);
47 myFeature2AISObjectMap[theFeature] = anAIS;
49 aContext->Display(anAIS, Standard_False);
51 aContext->UpdateCurrentViewer();
55 std::list<XGUI_ViewerPrs> XGUI_Displayer::GetViewerPrs()
57 std::set<boost::shared_ptr<ModelAPI_Feature> > aPrsFeatures;
58 std::list<XGUI_ViewerPrs> aPresentations;
60 Handle(AIS_InteractiveContext) aContext = AISContext();
61 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
62 Handle(AIS_InteractiveObject) anIO = aContext->SelectedInteractive();
63 TopoDS_Shape aShape = aContext->SelectedShape();
65 boost::shared_ptr<ModelAPI_Feature> aFeature;
66 FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
67 aFLast = myFeature2AISObjectMap.end();
68 for (; aFIt != aFLast && !aFeature; aFIt++) {
69 Handle(AIS_InteractiveObject) anAIS = (*aFIt).second;
72 aFeature = (*aFIt).first;
74 if (aPrsFeatures.find(aFeature) != aPrsFeatures.end())
76 aPresentations.push_back(XGUI_ViewerPrs(aFeature, aShape));
77 aPrsFeatures.insert(aFeature);
80 return aPresentations;
83 void XGUI_Displayer::Erase(boost::shared_ptr<ModelAPI_Feature> theFeature,
84 const bool isUpdateViewer)
86 if (myFeature2AISObjectMap.find(theFeature) == myFeature2AISObjectMap.end())
89 Handle(AIS_InteractiveContext) aContext = AISContext();
90 Handle(AIS_InteractiveObject) anAIS = myFeature2AISObjectMap[theFeature];
91 Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast(anAIS);
92 if (!anAISShape.IsNull())
94 aContext->Erase(anAISShape);
96 myFeature2AISObjectMap.erase(theFeature);
99 aContext->UpdateCurrentViewer();
102 void XGUI_Displayer::RedisplayInLocalContext(boost::shared_ptr<ModelAPI_Feature> theFeature,
103 const TopoDS_Shape& theShape,
104 const std::list<int>& theModes, const bool isUpdateViewer)
106 Handle(AIS_InteractiveContext) aContext = AISContext();
107 // Open local context if there is no one
108 if (!aContext->HasOpenedContext()) {
109 aContext->ClearCurrents(false);
110 aContext->OpenLocalContext(false/*use displayed objects*/, /*true*/false/*use displayed objects*/,
111 true/*allow shape decomposition*/);
113 // display or redisplay presentation
114 Handle(AIS_Shape) anAIS;
115 if (IsVisible(theFeature)) {
116 anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[theFeature]);
117 if (!anAIS.IsNull()) {
118 // if the AIS object is displayed in the opened local context in some mode, additional
119 // AIS sub objects are created there. They should be rebuild for correct selecting.
120 // It is possible to correct it by closing local context before the shape set and opening
121 // after. Another workaround to thrown down the selection and reselecting the AIS.
122 // If there was a problem here, try the first solution with close/open local context.
123 anAIS->Set(theShape);
125 if (aContext->IsSelected(anAIS)) {
126 aContext->AddOrRemoveSelected(anAIS, false);
127 aContext->AddOrRemoveSelected(anAIS, false);
128 //aContext->SetSelected(anAIS, false);
133 anAIS = new AIS_Shape(theShape);
134 myFeature2AISObjectMap[theFeature] = anAIS;
135 aContext->Display(anAIS, false);
137 // Activate selection of objects from prs
138 if (!anAIS.IsNull()) {
139 aContext->Load(anAIS, -1, true/*allow decomposition*/);
140 std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
141 for (; anIt != aLast; anIt++)
143 aContext->Activate(anAIS, AIS_Shape::SelectionMode((TopAbs_ShapeEnum)*anIt));
148 aContext->UpdateCurrentViewer();
151 void XGUI_Displayer::EraseAll(const bool isUpdateViewer)
153 Handle(AIS_InteractiveContext) ic = AISContext();
155 AIS_ListOfInteractive aList;
156 ic->DisplayedObjects(aList);
157 AIS_ListIteratorOfListOfInteractive anIter(aList);
158 for (; anIter.More(); anIter.Next()) {
159 if ((anIter.Value()->DynamicType() == STANDARD_TYPE(AIS_Trihedron)))
163 Handle(AIS_InteractiveObject) anIO = anIter.Value();
164 ic->Erase(anIO, false);
166 myFeature2AISObjectMap.clear();
168 ic->UpdateCurrentViewer();
171 void XGUI_Displayer::EraseDeletedFeatures(const bool isUpdateViewer)
173 Handle(AIS_InteractiveContext) aContext = AISContext();
175 FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
176 aFLast = myFeature2AISObjectMap.end();
177 std::list<boost::shared_ptr<ModelAPI_Feature>> aRemoved;
178 for (; aFIt != aFLast; aFIt++)
180 boost::shared_ptr<ModelAPI_Feature> aFeature = (*aFIt).first;
181 if (!aFeature || !aFeature->data()->isValid()) {
182 Handle(AIS_InteractiveObject) anAIS = (*aFIt).second;
183 if (!anAIS.IsNull()) {
184 aContext->Erase(anAIS, false);
185 aRemoved.push_back(aFeature);
189 std::list<boost::shared_ptr<ModelAPI_Feature>>::const_iterator anIt = aRemoved.begin(),
190 aLast = aRemoved.end();
191 for (; anIt != aLast; anIt++) {
192 myFeature2AISObjectMap.erase(myFeature2AISObjectMap.find(*anIt));
196 aContext->UpdateCurrentViewer();
199 void XGUI_Displayer::CloseLocalContexts(const bool isUpdateViewer)
201 closeAllContexts(true);
204 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
206 Handle(AIS_InteractiveContext) ic = AISContext();
208 ic->CloseAllContexts(false);
210 ic->UpdateCurrentViewer();
214 void XGUI_Displayer::UpdateViewer()
216 Handle(AIS_InteractiveContext) ic = AISContext();
218 ic->UpdateCurrentViewer();
221 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
223 return myWorkshop->viewer()->AISContext();