Salome HOME
Pipe validator fix
[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 #include "PartSet_OverconstraintListener.h"
11
12 #include <XGUI_ModuleConnector.h>
13 #include <XGUI_Workshop.h>
14 #include <XGUI_Displayer.h>
15
16 #include <ModuleBase_IWorkshop.h>
17 #include <ModuleBase_IViewer.h>
18 #include <ModuleBase_Tools.h>
19
20 #include <Config_PropManager.h>
21
22 #include <AIS_InteractiveContext.hxx>
23 #include <AIS_InteractiveObject.hxx>
24 #include <Prs3d_PointAspect.hxx>
25
26 //#define DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
27
28 PartSet_CustomPrs::PartSet_CustomPrs(ModuleBase_IWorkshop* theWorkshop)
29   : myWorkshop(theWorkshop), myFeature(FeaturePtr())
30 {
31   initPresentation(ModuleBase_IModule::CustomizeArguments);
32   initPresentation(ModuleBase_IModule::CustomizeResults);
33   initPresentation(ModuleBase_IModule::CustomizeHighlightedObjects);
34
35   myIsActive[ModuleBase_IModule::CustomizeArguments] = false;
36   myIsActive[ModuleBase_IModule::CustomizeResults] = false;
37   myIsActive[ModuleBase_IModule::CustomizeHighlightedObjects] = false;
38 }
39
40 bool PartSet_CustomPrs::isActive(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
41 {
42   return myIsActive[theFlag];
43 }
44
45 bool PartSet_CustomPrs::activate(const FeaturePtr& theFeature,
46                                  const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
47                                  const bool theUpdateViewer)
48 {
49 #ifdef DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
50   return false;
51 #endif
52
53   myIsActive[theFlag] = true;
54   myFeature = theFeature;
55
56   bool isModified = false;
57   if (theFeature.get()) {
58     displayPresentation(ModuleBase_IModule::CustomizeArguments, theUpdateViewer);
59     displayPresentation(ModuleBase_IModule::CustomizeResults, theUpdateViewer);
60     displayPresentation(ModuleBase_IModule::CustomizeHighlightedObjects, theUpdateViewer);
61     isModified = true;
62   }
63   return isModified;
64 }
65
66 bool PartSet_CustomPrs::deactivate(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
67                                    const bool theUpdateViewer)
68 {
69   myIsActive[theFlag] = false;
70   bool isModified = false;
71
72   erasePresentation(ModuleBase_IModule::CustomizeArguments, theUpdateViewer);
73   erasePresentation(ModuleBase_IModule::CustomizeResults, theUpdateViewer);
74   erasePresentation(ModuleBase_IModule::CustomizeHighlightedObjects, theUpdateViewer);
75   isModified = true;
76
77   return isModified;
78 }
79
80 bool PartSet_CustomPrs::displayPresentation(
81                                   const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
82                                   const bool theUpdateViewer)
83 {
84   bool isModified = false;
85
86   // update the AIS objects content
87   Handle(PartSet_OperationPrs) anOperationPrs = getPresentation(theFlag);
88   // do nothing if the feature can not be displayed [is moved from presentation, to be checked]
89   if (!myFeature.get())
90     return isModified;
91
92   switch (theFlag) {
93     case ModuleBase_IModule::CustomizeArguments:
94       PartSet_OperationPrs::getFeatureShapes(myFeature, myWorkshop, anOperationPrs->featureShapes());
95       break;
96     case ModuleBase_IModule::CustomizeResults:
97       PartSet_OperationPrs::getResultShapes(myFeature, myWorkshop, anOperationPrs->featureShapes());
98       break;
99     case ModuleBase_IModule::CustomizeHighlightedObjects:
100       PartSet_OperationPrs::getHighlightedShapes(myWorkshop, anOperationPrs->featureShapes());
101       break;
102     default:
103       return isModified;
104   }
105
106   // redisplay AIS objects
107   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
108   if (!aContext.IsNull() && !aContext->IsDisplayed(anOperationPrs)) {
109     // when the feature can not be visualized in the module, the operation preview should not
110     // be visualized also
111     if (anOperationPrs->hasShapes() && myWorkshop->module()->canDisplayObject(myFeature)) {
112       // set color here because it can be changed in preferences
113       Quantity_Color aShapeColor = getShapeColor(theFlag);
114       anOperationPrs->setShapeColor(aShapeColor);
115
116       PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
117       XGUI_Workshop* aWorkshop = workshop();
118       aWorkshop->displayer()->displayAIS(myPresentations[theFlag], false/*load object in selection*/,
119                                          theUpdateViewer);
120       aContext->SetZLayer(anOperationPrs, aModule->getVisualLayerId());
121       isModified = true;
122     }
123   }
124   else {
125     // when the feature can not be visualized in the module, the operation preview should not
126     // be visualized also
127     if (!anOperationPrs->hasShapes() || !myWorkshop->module()->canDisplayObject(myFeature)) {
128       erasePresentation(theFlag, theUpdateViewer);
129       isModified = true;
130     }
131     else {
132       anOperationPrs->Redisplay();
133       isModified = true;
134       if (theUpdateViewer)
135         workshop()->displayer()->updateViewer();
136     }
137   }
138   return isModified;
139 }
140
141 void PartSet_CustomPrs::erasePresentation(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
142                                           const bool theUpdateViewer)
143 {
144   XGUI_Workshop* aWorkshop = workshop();
145   aWorkshop->displayer()->eraseAIS(myPresentations[theFlag], theUpdateViewer);
146 }
147
148 void PartSet_CustomPrs::clearPresentation(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
149 {
150   Handle(PartSet_OperationPrs) anOperationPrs = getPresentation(theFlag);
151   anOperationPrs->featureShapes().clear();
152   if (!anOperationPrs.IsNull())
153     anOperationPrs.Nullify();
154   myPresentations[theFlag] = AISObjectPtr();
155 }
156
157 Handle(PartSet_OperationPrs) PartSet_CustomPrs::getPresentation(
158                                          const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
159 {
160   Handle(PartSet_OperationPrs) aPresentation;
161
162   if (myPresentations.contains(theFlag)) {
163     AISObjectPtr anOperationPrs = myPresentations[theFlag];
164     if (!anOperationPrs.get()) {
165       initPresentation(theFlag);
166       anOperationPrs = myPresentations[theFlag];
167     }
168     Handle(AIS_InteractiveObject) anAISIO = anOperationPrs->impl<Handle(AIS_InteractiveObject)>();
169     aPresentation = Handle(PartSet_OperationPrs)::DownCast(anAISIO);
170   }
171   return aPresentation;
172 }
173
174 bool PartSet_CustomPrs::redisplay(const ObjectPtr& theObject,
175                                   const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
176                                   const bool theUpdateViewer)
177 {
178 #ifdef DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
179   return false;
180 #endif
181   bool aRedisplayed = false;
182   if (myIsActive[theFlag])
183     aRedisplayed = displayPresentation(theFlag, theUpdateViewer);
184
185   return aRedisplayed;
186 }
187
188 void PartSet_CustomPrs::clearPrs()
189 {
190   clearPresentation(ModuleBase_IModule::CustomizeArguments);
191   clearPresentation(ModuleBase_IModule::CustomizeResults);
192   clearPresentation(ModuleBase_IModule::CustomizeHighlightedObjects);
193 }
194
195 void PartSet_CustomPrs::initPresentation(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
196 {
197   AISObjectPtr anOperationPrs = AISObjectPtr(new GeomAPI_AISObject());
198   Handle(PartSet_OperationPrs) anAISPrs = new PartSet_OperationPrs(myWorkshop);
199   anOperationPrs->setImpl(new Handle(AIS_InteractiveObject)(anAISPrs));
200   if (theFlag == ModuleBase_IModule::CustomizeArguments ||
201       theFlag == ModuleBase_IModule::CustomizeResults) {
202     anOperationPrs->setPointMarker(5, 2.);
203     anOperationPrs->setWidth(1);
204   }
205   else if (theFlag == ModuleBase_IModule::CustomizeHighlightedObjects)
206     anAISPrs->useAISWidth();
207
208   if (anOperationPrs.get())
209     myPresentations[theFlag] = anOperationPrs;
210 }
211
212 Quantity_Color PartSet_CustomPrs::getShapeColor(
213                                   const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
214 {
215   Quantity_Color aColor;
216   switch(theFlag) {
217     case ModuleBase_IModule::CustomizeArguments:
218       aColor = ModuleBase_Tools::color("Visualization", "operation_parameter_color",
219                                        OPERATION_PARAMETER_COLOR());
220     break;
221     case ModuleBase_IModule::CustomizeResults:
222       aColor = ModuleBase_Tools::color("Visualization", "operation_result_color",
223                                        OPERATION_RESULT_COLOR());
224     break;
225     case ModuleBase_IModule::CustomizeHighlightedObjects:
226       aColor = ModuleBase_Tools::color("Visualization", "operation_highlight_color",
227                                        OPERATION_HIGHLIGHT_COLOR());
228     break;
229     default:
230     break;
231   }
232   return aColor;
233 }
234
235 XGUI_Workshop* PartSet_CustomPrs::workshop() const
236 {
237   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
238   return aConnector->workshop();
239 }