Salome HOME
Merge with PythonAPI branch
[modules/shaper.git] / src / PartSet / PartSet_CustomPrs.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_CustomPrs.cpp
4 // Created:     30 Jun 2015
5 // Author:      Natalia ERMOLAEVA
6
7 #include <PartSet_CustomPrs.h>
8 #include <PartSet_Module.h>
9 #include "PartSet_OperationPrs.h"
10
11 #include <XGUI_ModuleConnector.h>
12 #include <XGUI_Workshop.h>
13 #include <XGUI_Displayer.h>
14
15 #include <ModuleBase_IWorkshop.h>
16 #include <ModuleBase_IViewer.h>
17
18 #include <Config_PropManager.h>
19
20 #include <AIS_InteractiveContext.hxx>
21 #include <AIS_InteractiveObject.hxx>
22 #include <Prs3d_PointAspect.hxx>
23
24 //#define DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
25
26 #define OPERATION_PARAMETER_COLOR "255, 255, 0"
27
28 PartSet_CustomPrs::PartSet_CustomPrs(ModuleBase_IWorkshop* theWorkshop)
29   : myWorkshop(theWorkshop), myIsActive(false)
30 {
31   initPrs();
32 }
33
34 bool PartSet_CustomPrs::isActive()
35 {
36   return myIsActive;
37   /*Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
38   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
39
40   return !aContext.IsNull() && aContext->IsDisplayed(anOperationPrs);*/
41 }
42
43 bool PartSet_CustomPrs::activate(const FeaturePtr& theFeature, const bool theUpdateViewer)
44 {
45 #ifdef DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
46   return false;
47 #endif
48
49   bool isModified = false;
50   Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
51
52   if (anOperationPrs->canActivate(theFeature)) {
53     myIsActive = true;
54     anOperationPrs->setFeature(theFeature);
55     if (theFeature.get()) {
56       displayPresentation(theUpdateViewer);
57       isModified = true;
58     }
59   }
60   return isModified;
61 }
62
63 bool PartSet_CustomPrs::deactivate(const bool theUpdateViewer)
64 {
65   myIsActive = false;
66   bool isModified = false;
67
68   Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
69   anOperationPrs->setFeature(FeaturePtr());
70
71   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
72   if (!aContext.IsNull() && aContext->IsDisplayed(anOperationPrs)) {
73     erasePresentation(theUpdateViewer);
74     isModified = true;
75   }
76
77   return isModified;
78 }
79
80
81 void PartSet_CustomPrs::displayPresentation(const bool theUpdateViewer)
82 {
83   Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
84
85   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
86   if (!aContext.IsNull() && !aContext->IsDisplayed(anOperationPrs)) {
87     if (anOperationPrs->hasShapes()) {
88       PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
89       XGUI_Workshop* aWorkshop = workshop();
90       aWorkshop->displayer()->displayAIS(myOperationPrs, false/*load object in selection*/, theUpdateViewer);
91       aContext->SetZLayer(anOperationPrs, aModule->getVisualLayerId());
92     }
93   }
94   else {
95     if (!anOperationPrs->hasShapes())
96       erasePresentation(theUpdateViewer);
97     else {
98       anOperationPrs->Redisplay();
99       if (theUpdateViewer)
100         workshop()->displayer()->updateViewer();
101     }
102   }
103 }
104
105 void PartSet_CustomPrs::erasePresentation(const bool theUpdateViewer)
106 {
107   XGUI_Workshop* aWorkshop = workshop();
108   aWorkshop->displayer()->eraseAIS(myOperationPrs, theUpdateViewer);
109 }
110
111 Handle(PartSet_OperationPrs) PartSet_CustomPrs::getPresentation()
112 {
113   if (!myOperationPrs.get())
114     initPrs();
115   Handle(AIS_InteractiveObject) anAISIO = myOperationPrs->impl<Handle(AIS_InteractiveObject)>();
116   return Handle(PartSet_OperationPrs)::DownCast(anAISIO);
117 }
118
119 bool PartSet_CustomPrs::redisplay(const ObjectPtr& theObject, const bool theUpdateViewer)
120 {
121 #ifdef DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
122   return false;
123 #endif
124
125   bool isModified = false;
126   // the presentation should be recomputed if the previous AIS depend on the result
127   // [it should be hiddend] or the new AIS depend on it [it should be visualized]
128   Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
129   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
130   if (!aContext.IsNull()) {
131     if (aContext->IsDisplayed(anOperationPrs)) {
132       // if there are performance poblems, to improve them, the necessity of redisplay can be checked
133       //bool aChanged = anOperationPrs->dependOn(theObject);
134       anOperationPrs->updateShapes();
135       //aChanged = aChanged || anOperationPrs->dependOn(theObject);
136       //if (aChanged)
137       anOperationPrs->Redisplay();
138       isModified = true;
139       if (theUpdateViewer)
140         workshop()->displayer()->updateViewer();
141     }
142     else {
143       anOperationPrs->updateShapes();
144       displayPresentation(theUpdateViewer);
145     }
146   }
147   return isModified;
148 }
149
150 void PartSet_CustomPrs::clearPrs()
151 {
152   Handle(PartSet_OperationPrs) anOperationPrs = getPresentation();
153   if (!anOperationPrs.IsNull())
154     anOperationPrs.Nullify();
155
156   myOperationPrs.reset();
157 }
158
159 void PartSet_CustomPrs::initPrs()
160 {
161   myOperationPrs = AISObjectPtr(new GeomAPI_AISObject());
162   myOperationPrs->setImpl(new Handle(AIS_InteractiveObject)(new PartSet_OperationPrs(myWorkshop)));
163
164   std::vector<int> aColor = Config_PropManager::color("Visualization", "operation_parameter_color",
165                                                       OPERATION_PARAMETER_COLOR);
166   myOperationPrs->setColor(aColor[0], aColor[1], aColor[2]);
167
168   myOperationPrs->setPointMarker(5, 2.);
169   myOperationPrs->setWidth(1);
170 }
171
172 XGUI_Workshop* PartSet_CustomPrs::workshop() const
173 {
174   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
175   return aConnector->workshop();
176 }