1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <PartSet_CustomPrs.h>
22 #include <PartSet_Module.h>
23 #include "PartSet_OperationPrs.h"
24 #include "PartSet_OverconstraintListener.h"
26 #include <XGUI_ModuleConnector.h>
27 #include <XGUI_Workshop.h>
28 #include <XGUI_Displayer.h>
30 #include <ModuleBase_IWorkshop.h>
31 #include <ModuleBase_IViewer.h>
32 #include <ModuleBase_Tools.h>
34 #include <Config_PropManager.h>
35 #include <Events_Loop.h>
36 #include <ModelAPI_Events.h>
38 #include <AIS_InteractiveContext.hxx>
39 #include <AIS_InteractiveObject.hxx>
40 #include <Prs3d_PointAspect.hxx>
42 //#define DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
44 PartSet_CustomPrs::PartSet_CustomPrs(ModuleBase_IWorkshop* theWorkshop)
45 : myWorkshop(theWorkshop), myFeature(FeaturePtr()), myPresentationIsEmpty(false)
47 Events_Loop* aLoop = Events_Loop::loop();
48 aLoop->registerListener(this, Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION));
50 initPresentation(ModuleBase_IModule::CustomizeArguments);
51 initPresentation(ModuleBase_IModule::CustomizeResults);
52 initPresentation(ModuleBase_IModule::CustomizeHighlightedObjects);
54 myIsActive[ModuleBase_IModule::CustomizeArguments] = false;
55 myIsActive[ModuleBase_IModule::CustomizeResults] = false;
56 myIsActive[ModuleBase_IModule::CustomizeHighlightedObjects] = false;
59 bool PartSet_CustomPrs::isActive(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
61 return myIsActive[theFlag];
64 bool PartSet_CustomPrs::activate(const FeaturePtr& theFeature,
65 const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
66 const bool theUpdateViewer)
68 #ifdef DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
72 myIsActive[theFlag] = true;
73 myFeature = theFeature;
75 bool isModified = false;
76 if (theFeature.get()) {
77 displayPresentation(theFlag, theUpdateViewer);
83 bool PartSet_CustomPrs::deactivate(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
84 const bool theUpdateViewer)
86 myIsActive[theFlag] = false;
87 erasePresentation(theFlag, theUpdateViewer);
91 bool PartSet_CustomPrs::displayPresentation(
92 const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
93 const bool theUpdateViewer)
95 bool isModified = false;
97 // update the AIS objects content
98 AISObjectPtr aPresentation = getPresentation(theFlag, true);
99 Handle(AIS_InteractiveObject) anAISIO = aPresentation->impl<Handle(AIS_InteractiveObject)>();
100 Handle(PartSet_OperationPrs) anOperationPrs = Handle(PartSet_OperationPrs)::DownCast(anAISIO);
102 // do nothing if the feature can not be displayed [is moved from presentation, to be checked]
103 if (!myFeature.get())
106 QMap<ObjectPtr, QList<GeomShapePtr> > aFeatureShapes;
108 case ModuleBase_IModule::CustomizeArguments:
109 PartSet_OperationPrs::getFeatureShapes(myFeature, myWorkshop, aFeatureShapes);
111 case ModuleBase_IModule::CustomizeResults:
112 PartSet_OperationPrs::getResultShapes(myFeature, myWorkshop, aFeatureShapes);
113 PartSet_OperationPrs::getPresentationShapes(myFeature, myWorkshop, aFeatureShapes, false);
115 case ModuleBase_IModule::CustomizeHighlightedObjects:
116 PartSet_OperationPrs::getHighlightedShapes(myWorkshop, aFeatureShapes);
121 NCollection_DataMap<TopoDS_Shape, Handle_AIS_InteractiveObject>& aShapeMap =
122 anOperationPrs->shapesMap();
123 PartSet_OperationPrs::fillShapeList(aFeatureShapes, myWorkshop, aShapeMap);
125 myPresentationIsEmpty = false;
126 // redisplay AIS objects
127 bool aRedisplayed = false;
128 Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
129 if (!aContext.IsNull() && !aContext->IsDisplayed(anOperationPrs)) {
130 // when the feature can not be visualized in the module, the operation preview should not
131 // be visualized also
132 if (anOperationPrs->hasShapes() && myWorkshop->module()->canDisplayObject(myFeature)) {
133 // set color here because it can be changed in preferences
134 Quantity_Color aShapeColor = getShapeColor(theFlag);
135 anOperationPrs->setShapeColor(aShapeColor);
137 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
138 XGUI_Workshop* aWorkshop = workshop();
139 aRedisplayed = aWorkshop->displayer()->displayAIS(myPresentations[theFlag],
140 false/*load object in selection*/, false);
141 aContext->SetZLayer(anOperationPrs, aModule->getVisualLayerId());
146 // when the feature can not be visualized in the module, the operation preview should not
147 // be visualized also
148 if (!anOperationPrs->hasShapes() || !myWorkshop->module()->canDisplayObject(myFeature)) {
149 aRedisplayed = erasePresentation(theFlag, false);
153 anOperationPrs->Redisplay();
158 if (myPresentationIsEmpty) {
159 aRedisplayed = erasePresentation(theFlag, false);
161 if (aRedisplayed && theUpdateViewer)
162 workshop()->displayer()->updateViewer();
167 bool PartSet_CustomPrs::erasePresentation(
168 const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
169 const bool theUpdateViewer)
171 bool isErased = false;
172 XGUI_Workshop* aWorkshop = workshop();
173 if (myPresentations.contains(theFlag))
174 isErased = aWorkshop->displayer()->eraseAIS(myPresentations[theFlag], theUpdateViewer);
178 void PartSet_CustomPrs::clearPresentation(
179 const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
181 AISObjectPtr aPresentation = getPresentation(theFlag, false);
182 if (aPresentation.get()) {
183 Handle(AIS_InteractiveObject) anAISIO = aPresentation->impl<Handle(AIS_InteractiveObject)>();
184 Handle(PartSet_OperationPrs) anOperationPrs = Handle(PartSet_OperationPrs)::DownCast(anAISIO);
186 anOperationPrs->shapesMap().Clear();
187 if (!anOperationPrs.IsNull())
188 anOperationPrs.Nullify();
189 myPresentations.remove(theFlag);
193 AISObjectPtr PartSet_CustomPrs::getPresentation(
194 const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
195 const bool theToCreate)
197 Handle(PartSet_OperationPrs) aPresentation;
199 AISObjectPtr anOperationPrs;
200 if (myPresentations.contains(theFlag))
201 anOperationPrs = myPresentations[theFlag];
203 if (!anOperationPrs.get() && theToCreate) {
204 initPresentation(theFlag);
205 anOperationPrs = myPresentations[theFlag];
208 return anOperationPrs;
211 bool PartSet_CustomPrs::redisplay(const ObjectPtr& theObject,
212 const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
213 const bool theUpdateViewer)
215 #ifdef DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
218 bool aRedisplayed = false;
219 if (myIsActive[theFlag])
220 aRedisplayed = displayPresentation(theFlag, theUpdateViewer);
225 void PartSet_CustomPrs::clearPrs()
227 clearPresentation(ModuleBase_IModule::CustomizeArguments);
228 clearPresentation(ModuleBase_IModule::CustomizeResults);
229 clearPresentation(ModuleBase_IModule::CustomizeHighlightedObjects);
232 void PartSet_CustomPrs::processEvent(const std::shared_ptr<Events_Message>& theMessage)
234 if (theMessage->eventID() == Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION))
235 myPresentationIsEmpty = true; /// store state to analize it after display/erase is finished
238 void PartSet_CustomPrs::initPresentation(
239 const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
241 AISObjectPtr anOperationPrs = AISObjectPtr(new GeomAPI_AISObject());
242 Handle(PartSet_OperationPrs) anAISPrs = new PartSet_OperationPrs(myWorkshop);
243 anOperationPrs->setImpl(new Handle(AIS_InteractiveObject)(anAISPrs));
244 if (theFlag == ModuleBase_IModule::CustomizeArguments ||
245 theFlag == ModuleBase_IModule::CustomizeResults) {
246 anOperationPrs->setPointMarker(5, 2.);
247 anOperationPrs->setWidth(1);
249 else if (theFlag == ModuleBase_IModule::CustomizeHighlightedObjects)
250 anAISPrs->useAISWidth();
252 if (anOperationPrs.get())
253 myPresentations[theFlag] = anOperationPrs;
256 Quantity_Color PartSet_CustomPrs::getShapeColor(
257 const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
259 Quantity_Color aColor;
261 case ModuleBase_IModule::CustomizeArguments:
262 aColor = ModuleBase_Tools::color("Visualization", "operation_parameter_color");
264 case ModuleBase_IModule::CustomizeResults:
265 aColor = ModuleBase_Tools::color("Visualization", "operation_result_color");
267 case ModuleBase_IModule::CustomizeHighlightedObjects:
268 aColor = ModuleBase_Tools::color("Visualization", "operation_highlight_color");
276 XGUI_Workshop* PartSet_CustomPrs::workshop() const
278 XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
279 return aConnector->workshop();