1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_CustomPrs.cpp
4 // Created: 30 Jun 2015
5 // Author: Natalia ERMOLAEVA
7 #include <PartSet_CustomPrs.h>
8 #include <PartSet_Module.h>
9 #include "PartSet_OperationPrs.h"
10 #include "PartSet_OverconstraintListener.h"
12 #include <XGUI_ModuleConnector.h>
13 #include <XGUI_Workshop.h>
14 #include <XGUI_Displayer.h>
16 #include <ModuleBase_IWorkshop.h>
17 #include <ModuleBase_IViewer.h>
18 #include <ModuleBase_Tools.h>
20 #include <Config_PropManager.h>
21 #include <Events_Loop.h>
22 #include <ModelAPI_Events.h>
24 #include <AIS_InteractiveContext.hxx>
25 #include <AIS_InteractiveObject.hxx>
26 #include <Prs3d_PointAspect.hxx>
28 //#define DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
30 PartSet_CustomPrs::PartSet_CustomPrs(ModuleBase_IWorkshop* theWorkshop)
31 : myWorkshop(theWorkshop), myFeature(FeaturePtr()), myPresentationIsEmpty(false)
33 Events_Loop* aLoop = Events_Loop::loop();
34 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION));
36 initPresentation(ModuleBase_IModule::CustomizeArguments);
37 initPresentation(ModuleBase_IModule::CustomizeResults);
38 initPresentation(ModuleBase_IModule::CustomizeHighlightedObjects);
40 myIsActive[ModuleBase_IModule::CustomizeArguments] = false;
41 myIsActive[ModuleBase_IModule::CustomizeResults] = false;
42 myIsActive[ModuleBase_IModule::CustomizeHighlightedObjects] = false;
45 bool PartSet_CustomPrs::isActive(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
47 return myIsActive[theFlag];
50 bool PartSet_CustomPrs::activate(const FeaturePtr& theFeature,
51 const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
52 const bool theUpdateViewer)
54 #ifdef DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
58 myIsActive[theFlag] = true;
59 myFeature = theFeature;
61 bool isModified = false;
62 if (theFeature.get()) {
63 displayPresentation(ModuleBase_IModule::CustomizeArguments, theUpdateViewer);
64 displayPresentation(ModuleBase_IModule::CustomizeResults, theUpdateViewer);
65 displayPresentation(ModuleBase_IModule::CustomizeHighlightedObjects, theUpdateViewer);
71 bool PartSet_CustomPrs::deactivate(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
72 const bool theUpdateViewer)
74 myIsActive[theFlag] = false;
75 bool isModified = false;
77 erasePresentation(ModuleBase_IModule::CustomizeArguments, theUpdateViewer);
78 erasePresentation(ModuleBase_IModule::CustomizeResults, theUpdateViewer);
79 erasePresentation(ModuleBase_IModule::CustomizeHighlightedObjects, theUpdateViewer);
85 bool PartSet_CustomPrs::displayPresentation(
86 const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
87 const bool theUpdateViewer)
89 bool isModified = false;
91 // update the AIS objects content
92 AISObjectPtr aPresentation = getPresentation(theFlag, true);
93 Handle(AIS_InteractiveObject) anAISIO = aPresentation->impl<Handle(AIS_InteractiveObject)>();
94 Handle(PartSet_OperationPrs) anOperationPrs = Handle(PartSet_OperationPrs)::DownCast(anAISIO);
96 // do nothing if the feature can not be displayed [is moved from presentation, to be checked]
101 case ModuleBase_IModule::CustomizeArguments:
102 PartSet_OperationPrs::getFeatureShapes(myFeature, myWorkshop, anOperationPrs->featureShapes());
104 case ModuleBase_IModule::CustomizeResults:
105 PartSet_OperationPrs::getResultShapes(myFeature, myWorkshop, anOperationPrs->featureShapes());
107 case ModuleBase_IModule::CustomizeHighlightedObjects:
108 PartSet_OperationPrs::getHighlightedShapes(myWorkshop, anOperationPrs->featureShapes());
114 myPresentationIsEmpty = false;
115 // redisplay AIS objects
116 bool aRedisplayed = false;
117 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
118 if (!aContext.IsNull() && !aContext->IsDisplayed(anOperationPrs)) {
119 // when the feature can not be visualized in the module, the operation preview should not
120 // be visualized also
121 if (anOperationPrs->hasShapes() && myWorkshop->module()->canDisplayObject(myFeature)) {
122 // set color here because it can be changed in preferences
123 Quantity_Color aShapeColor = getShapeColor(theFlag);
124 anOperationPrs->setShapeColor(aShapeColor);
126 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
127 XGUI_Workshop* aWorkshop = workshop();
128 aRedisplayed = aWorkshop->displayer()->displayAIS(myPresentations[theFlag],
129 false/*load object in selection*/, false);
130 aContext->SetZLayer(anOperationPrs, aModule->getVisualLayerId());
135 // when the feature can not be visualized in the module, the operation preview should not
136 // be visualized also
137 if (!anOperationPrs->hasShapes() || !myWorkshop->module()->canDisplayObject(myFeature)) {
138 aRedisplayed = erasePresentation(theFlag, false);
142 anOperationPrs->Redisplay();
147 if (myPresentationIsEmpty) {
148 aRedisplayed = erasePresentation(theFlag, false);
150 if (aRedisplayed && theUpdateViewer)
151 workshop()->displayer()->updateViewer();
156 bool PartSet_CustomPrs::erasePresentation(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
157 const bool theUpdateViewer)
159 bool isErased = false;
160 XGUI_Workshop* aWorkshop = workshop();
161 if (myPresentations.contains(theFlag))
162 isErased = aWorkshop->displayer()->eraseAIS(myPresentations[theFlag], theUpdateViewer);
166 void PartSet_CustomPrs::clearPresentation(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
168 AISObjectPtr aPresentation = getPresentation(theFlag, false);
169 if (aPresentation.get()) {
170 Handle(AIS_InteractiveObject) anAISIO = aPresentation->impl<Handle(AIS_InteractiveObject)>();
171 Handle(PartSet_OperationPrs) anOperationPrs = Handle(PartSet_OperationPrs)::DownCast(anAISIO);
173 anOperationPrs->featureShapes().clear();
174 if (!anOperationPrs.IsNull())
175 anOperationPrs.Nullify();
176 myPresentations.remove(theFlag);
180 AISObjectPtr PartSet_CustomPrs::getPresentation(
181 const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
182 const bool theToCreate)
184 Handle(PartSet_OperationPrs) aPresentation;
186 AISObjectPtr anOperationPrs;
187 if (myPresentations.contains(theFlag))
188 anOperationPrs = myPresentations[theFlag];
190 if (!anOperationPrs.get() && theToCreate) {
191 initPresentation(theFlag);
192 anOperationPrs = myPresentations[theFlag];
195 return anOperationPrs;
198 bool PartSet_CustomPrs::redisplay(const ObjectPtr& theObject,
199 const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
200 const bool theUpdateViewer)
202 #ifdef DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
205 bool aRedisplayed = false;
206 if (myIsActive[theFlag])
207 aRedisplayed = displayPresentation(theFlag, theUpdateViewer);
212 void PartSet_CustomPrs::clearPrs()
214 clearPresentation(ModuleBase_IModule::CustomizeArguments);
215 clearPresentation(ModuleBase_IModule::CustomizeResults);
216 clearPresentation(ModuleBase_IModule::CustomizeHighlightedObjects);
219 void PartSet_CustomPrs::processEvent(const std::shared_ptr<Events_Message>& theMessage)
221 if (theMessage->eventID() == Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION))
222 myPresentationIsEmpty = true; /// store state to analize it after display/erase is finished
225 void PartSet_CustomPrs::initPresentation(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
227 AISObjectPtr anOperationPrs = AISObjectPtr(new GeomAPI_AISObject());
228 Handle(PartSet_OperationPrs) anAISPrs = new PartSet_OperationPrs(myWorkshop);
229 anOperationPrs->setImpl(new Handle(AIS_InteractiveObject)(anAISPrs));
230 if (theFlag == ModuleBase_IModule::CustomizeArguments ||
231 theFlag == ModuleBase_IModule::CustomizeResults) {
232 anOperationPrs->setPointMarker(5, 2.);
233 anOperationPrs->setWidth(1);
235 else if (theFlag == ModuleBase_IModule::CustomizeHighlightedObjects)
236 anAISPrs->useAISWidth();
238 if (anOperationPrs.get())
239 myPresentations[theFlag] = anOperationPrs;
242 Quantity_Color PartSet_CustomPrs::getShapeColor(
243 const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
245 Quantity_Color aColor;
247 case ModuleBase_IModule::CustomizeArguments:
248 aColor = ModuleBase_Tools::color("Visualization", "operation_parameter_color",
249 OPERATION_PARAMETER_COLOR());
251 case ModuleBase_IModule::CustomizeResults:
252 aColor = ModuleBase_Tools::color("Visualization", "operation_result_color",
253 OPERATION_RESULT_COLOR());
255 case ModuleBase_IModule::CustomizeHighlightedObjects:
256 aColor = ModuleBase_Tools::color("Visualization", "operation_highlight_color",
257 OPERATION_HIGHLIGHT_COLOR());
265 XGUI_Workshop* PartSet_CustomPrs::workshop() const
267 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
268 return aConnector->workshop();