]> SALOME platform Git repositories - modules/shaper.git/blob - src/XGUI/XGUI_Displayer.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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   std::vector<Handle(AIS_InteractiveObject)> aDispAIS;
44   if (myFeature2AISObjectMap.find(theFeature) != myFeature2AISObjectMap.end()) {
45     aDispAIS = myFeature2AISObjectMap[theFeature];
46   }
47   aDispAIS.push_back(anAIS);
48   myFeature2AISObjectMap[theFeature] = aDispAIS;
49
50   aContext->Display(anAIS, Standard_False);
51
52   if (isUpdateViewer)
53     aContext->UpdateCurrentViewer();
54 }
55
56 boost::shared_ptr<ModelAPI_Feature> XGUI_Displayer::GetFeature(const TopoDS_Shape& theShape)
57 {
58   boost::shared_ptr<ModelAPI_Feature> aFeature;
59
60   FeatureToAISMap::const_iterator aFIt = myFeature2AISObjectMap.begin(),
61                                   aFLast = myFeature2AISObjectMap.end();
62   for (; aFIt != aFLast && !aFeature; aFIt++)
63   {
64     std::vector<Handle(AIS_InteractiveObject)> aDispAIS = (*aFIt).second;
65     std::vector<Handle(AIS_InteractiveObject)>::const_iterator anIt = aDispAIS.begin(),
66                                                                 aLast = aDispAIS.end();
67     Handle(AIS_InteractiveContext) aContext = AISContext();
68     for (; anIt != aLast && !aFeature; anIt++) {
69       Handle(AIS_InteractiveObject) anAIS = *anIt;
70       Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast(anAIS);
71       if (!anAISShape.IsNull() && anAISShape->Shape() == theShape) {
72         aFeature = (*aFIt).first;
73       }
74     }
75   }
76
77   return aFeature;
78 }
79
80 void XGUI_Displayer::Erase(boost::shared_ptr<ModelAPI_Feature> theFeature,
81                            const bool isUpdateViewer)
82 {
83   if (myFeature2AISObjectMap.find(theFeature) == myFeature2AISObjectMap.end())
84     return;
85
86   std::vector<Handle(AIS_InteractiveObject)> aDispAIS = myFeature2AISObjectMap[theFeature];
87   std::vector<Handle(AIS_InteractiveObject)>::const_iterator anIt = aDispAIS.begin(),
88                                                              aLast = aDispAIS.end();
89   Handle(AIS_InteractiveContext) aContext = AISContext();
90   for (; anIt != aLast; anIt++) {
91     Handle(AIS_InteractiveObject) anAIS = *anIt;
92     Handle(AIS_Shape) anAISShape = Handle(AIS_Shape)::DownCast(anAIS);
93     if (anAISShape.IsNull())
94       continue;
95       aContext->Erase(anAISShape);
96   }
97   myFeature2AISObjectMap.erase(theFeature);
98
99   if (isUpdateViewer)
100     aContext->UpdateCurrentViewer();
101 }
102
103 void XGUI_Displayer::RedisplayInLocalContext(boost::shared_ptr<ModelAPI_Feature> theFeature,
104                                              const TopoDS_Shape& theShape,
105                                              const std::list<int>& theModes, const bool isUpdateViewer)
106 {
107   Handle(AIS_InteractiveContext) aContext = AISContext();
108   
109   if (IsVisible(theFeature)) {
110     Erase(theFeature, false);
111   }
112
113   Handle(AIS_Shape) anAIS = new AIS_Shape(theShape);
114   std::vector<Handle(AIS_InteractiveObject)> aDispAIS;
115   if (myFeature2AISObjectMap.find(theFeature) != myFeature2AISObjectMap.end()) {
116     aDispAIS = myFeature2AISObjectMap[theFeature];
117   }
118   aDispAIS.push_back(anAIS);
119   myFeature2AISObjectMap[theFeature] = aDispAIS;
120
121   Handle(AIS_InteractiveContext) ic = AISContext();
122
123   // Open local context if there is no one
124   if (!ic->HasOpenedContext()) {
125     ic->ClearCurrents(false);
126     ic->OpenLocalContext(false/*use displayed objects*/, /*true*/false/*use displayed objects*/,
127                          true/*allow shape decomposition*/);
128   }
129   // Activate selection of objects from prs
130   if (!anAIS.IsNull()) {
131     if (anAIS->IsKind(STANDARD_TYPE(AIS_Shape))) {
132       ic->Display(anAIS, false);
133       ic->Load(anAIS, -1, true/*allow decomposition*/);
134       std::list<int>::const_iterator anIt = theModes.begin(), aLast = theModes.end();
135       for (; anIt != aLast; anIt++) 
136         ic->Activate(anAIS, AIS_Shape::SelectionMode((TopAbs_ShapeEnum)*anIt));
137     }
138   }
139   if (isUpdateViewer)
140     ic->UpdateCurrentViewer();
141 }
142
143 void XGUI_Displayer::CloseLocalContexts(const bool isUpdateViewer)
144 {
145   closeAllContexts(true);
146 }
147
148 void XGUI_Displayer::closeAllContexts(const bool isUpdateViewer)
149 {
150   Handle(AIS_InteractiveContext) ic = AISContext();
151   if (!ic.IsNull()) {
152     ic->CloseAllContexts(false);
153     if (isUpdateViewer)
154       ic->UpdateCurrentViewer();
155   }
156 }
157
158 Handle(AIS_InteractiveContext) XGUI_Displayer::AISContext() const 
159
160   return myWorkshop->viewer()->AISContext(); 
161 }