Salome HOME
Functionality for dynamic libraries loading is assigned to Config_ModuleReader
[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 std::list<XGUI_ViewerPrs> XGUI_Displayer::GetViewerPrs
68                                                 (const NCollection_List<TopoDS_Shape>& theShapes)
69 {
70   std::list<XGUI_ViewerPrs> aPresentations;
71   if (theShapes.IsEmpty())
72     return aPresentations;
73
74   NCollection_List<TopoDS_Shape>::Iterator anIt(theShapes);
75   for (; anIt.More(); anIt.Next()) {
76     const TopoDS_Shape& aShape = anIt.Value();
77     if (aShape.IsNull())
78       continue;
79     boost::shared_ptr<ModelAPI_Feature> aFeature = GetFeature(aShape);
80     aPresentations.push_back(XGUI_ViewerPrs(aFeature, aShape));
81   }
82
83   return aPresentations;
84 }
85
86 void XGUI_Displayer::Erase(boost::shared_ptr<ModelAPI_Feature> theFeature,
87                            const bool isUpdateViewer)
88 {
89   if (myFeature2AISObjectMap.find(theFeature) == myFeature2AISObjectMap.end())
90     return;
91
92   Handle(AIS_InteractiveContext) aContext = AISContext();
93   Handle(AIS_InteractiveObject) anAIS = myFeature2AISObjectMap[theFeature];
94   Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast(anAIS);
95   if (!anAISShape.IsNull())
96   {
97     aContext->Erase(anAISShape);
98   }
99   myFeature2AISObjectMap.erase(theFeature);
100
101   if (isUpdateViewer)
102     aContext->UpdateCurrentViewer();
103 }
104
105 void XGUI_Displayer::RedisplayInLocalContext(boost::shared_ptr<ModelAPI_Feature> theFeature,
106                                              const TopoDS_Shape& theShape,
107                                              const std::list<int>& theModes, const bool isUpdateViewer)
108 {
109   Handle(AIS_InteractiveContext) aContext = AISContext();
110   // Open local context if there is no one
111   if (!aContext->HasOpenedContext()) {
112     aContext->ClearCurrents(false);
113     aContext->OpenLocalContext(false/*use displayed objects*/, /*true*/false/*use displayed objects*/,
114                          true/*allow shape decomposition*/);
115   }
116   // display or redisplay presentation
117   Handle(AIS_Shape) anAIS;
118   if (IsVisible(theFeature)) {
119     anAIS = Handle(AIS_Shape)::DownCast(myFeature2AISObjectMap[theFeature]);
120     if (!anAIS.IsNull()) {
121       anAIS->Set(theShape);
122       anAIS->Redisplay();
123     }
124   }
125   else {
126     anAIS = new AIS_Shape(theShape);
127     myFeature2AISObjectMap[theFeature] = anAIS;
128     aContext->Display(anAIS, false);
129   }
130   // Activate selection of objects from prs
131   if (!anAIS.IsNull()) {
132     aContext->Load(anAIS, -1, true/*allow decomposition*/);
133     std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
134     for (; anIt != aLast; anIt++)
135     {
136       aContext->Activate(anAIS, AIS_Shape::SelectionMode((TopAbs_ShapeEnum)*anIt));
137     }
138   }
139
140   if (isUpdateViewer)
141     aContext->UpdateCurrentViewer();
142 }
143
144 void XGUI_Displayer::CloseLocalContexts(const bool isUpdateViewer)
145 {
146   closeAllContexts(true);
147 }
148
149 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
150 {
151   Handle(AIS_InteractiveContext) ic = AISContext();
152   if (!ic.IsNull()) {
153     ic->CloseAllContexts(false);
154     if (isUpdateViewer)
155       ic->UpdateCurrentViewer();
156   }
157 }
158
159 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const 
160
161   return myWorkshop->viewer()->AISContext(); 
162 }