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"
10 #include <ModelAPI_Document.h>
11 #include <ModelAPI_Data.h>
13 #include <AIS_InteractiveContext.hxx>
14 #include <AIS_ListOfInteractive.hxx>
15 #include <AIS_ListIteratorOfListOfInteractive.hxx>
17 #include <AIS_Shape.hxx>
19 XGUI_Displayer::XGUI_Displayer(XGUI_Workshop* theWorkshop)
21 myWorkshop = theWorkshop;
24 XGUI_Displayer::~XGUI_Displayer()
28 bool XGUI_Displayer::IsVisible(boost::shared_ptr<ModelAPI_Feature> theFeature)
30 return myFeature2AISObjectMap.find(theFeature) != myFeature2AISObjectMap.end();
33 void XGUI_Displayer::Display(boost::shared_ptr<ModelAPI_Feature> theFeature,
34 const bool isUpdateViewer)
38 void XGUI_Displayer::Display(boost::shared_ptr<ModelAPI_Feature> theFeature,
39 const TopoDS_Shape& theShape, const bool isUpdateViewer)
41 Handle(AIS_InteractiveContext) aContext = AISContext();
43 Handle(AIS_Shape) anAIS = new AIS_Shape(theShape);
44 myFeature2AISObjectMap[theFeature] = anAIS;
46 aContext->Display(anAIS, Standard_False);
48 aContext->UpdateCurrentViewer();
51 boost::shared_ptr<ModelAPI_Feature> XGUI_Displayer::GetFeature(const TopoDS_Shape& theShape)
53 boost::shared_ptr<ModelAPI_Feature> aFeature;
55 FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
56 aFLast = myFeature2AISObjectMap.end();
57 for (; aFIt != aFLast && !aFeature; aFIt++)
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;
68 std::list<XGUI_ViewerPrs> XGUI_Displayer::GetViewerPrs
69 (const NCollection_List<TopoDS_Shape>& theShapes)
71 std::list<XGUI_ViewerPrs> aPresentations;
72 if (theShapes.IsEmpty())
73 return aPresentations;
75 NCollection_List<TopoDS_Shape>::Iterator anIt(theShapes);
76 for (; anIt.More(); anIt.Next()) {
77 const TopoDS_Shape& aShape = anIt.Value();
80 boost::shared_ptr<ModelAPI_Feature> aFeature = GetFeature(aShape);
81 aPresentations.push_back(XGUI_ViewerPrs(aFeature, aShape));
84 return aPresentations;
87 void XGUI_Displayer::Erase(boost::shared_ptr<ModelAPI_Feature> theFeature,
88 const bool isUpdateViewer)
90 if (myFeature2AISObjectMap.find(theFeature) == myFeature2AISObjectMap.end())
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())
98 aContext->Erase(anAISShape);
100 myFeature2AISObjectMap.erase(theFeature);
103 aContext->UpdateCurrentViewer();
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)
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*/);
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);
127 anAIS = new AIS_Shape(theShape);
128 myFeature2AISObjectMap[theFeature] = anAIS;
129 aContext->Display(anAIS, false);
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++)
137 aContext->Activate(anAIS, AIS_Shape::SelectionMode((TopAbs_ShapeEnum)*anIt));
142 aContext->UpdateCurrentViewer();
145 void XGUI_Displayer::EraseAll(const bool isUpdateViewer)
147 Handle(AIS_InteractiveContext) ic = AISContext();
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)))
157 Handle(AIS_InteractiveObject) anIO = anIter.Value();
158 ic->Erase(anIO, false);
160 myFeature2AISObjectMap.clear();
162 ic->UpdateCurrentViewer();
165 void XGUI_Displayer::EraseDeletedFeatures(const bool isUpdateViewer)
167 Handle(AIS_InteractiveContext) aContext = AISContext();
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++)
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);
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));
190 aContext->UpdateCurrentViewer();
193 void XGUI_Displayer::CloseLocalContexts(const bool isUpdateViewer)
195 closeAllContexts(true);
198 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
200 Handle(AIS_InteractiveContext) ic = AISContext();
202 ic->CloseAllContexts(false);
204 ic->UpdateCurrentViewer();
208 void XGUI_Displayer::UpdateViewer()
210 Handle(AIS_InteractiveContext) ic = AISContext();
212 ic->UpdateCurrentViewer();
215 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const
217 return myWorkshop->viewer()->AISContext();