Salome HOME
Merge branch 'csgroup_IS2'
[modules/shaper.git] / src / PartSet / PartSet_CustomPrs.cpp
1 // Copyright (C) 2014-2021  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "PartSet_CustomPrs.h"
21 #include "PartSet_Module.h"
22 #include "PartSet_OperationPrs.h"
23 #include "PartSet_OverconstraintListener.h"
24 #include "PartSet_SketcherMgr.h"
25
26 #include <XGUI_ModuleConnector.h>
27 #include <XGUI_Workshop.h>
28 #include <XGUI_Displayer.h>
29
30 #include <ModuleBase_IWorkshop.h>
31 #include <ModuleBase_IViewer.h>
32 #include <ModuleBase_Tools.h>
33
34 #include <ModelAPI_Tools.h>
35 #include <SketchPlugin_Sketch.h>
36
37 #include <Config_PropManager.h>
38 #include <Events_Loop.h>
39 #include <ModelAPI_Events.h>
40 #include <GeomAlgoAPI_CompoundBuilder.h>
41
42 #include <AIS_InteractiveContext.hxx>
43 #include <AIS_InteractiveObject.hxx>
44 #include <Prs3d_PointAspect.hxx>
45
46 //#define DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
47
48 PartSet_CustomPrs::PartSet_CustomPrs(ModuleBase_IWorkshop* theWorkshop)
49   : myWorkshop(theWorkshop), myFeature(FeaturePtr()), myPresentationIsEmpty(false),
50   myDisabledMode(-1)
51 {
52   Events_Loop* aLoop = Events_Loop::loop();
53   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION));
54   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OPERATION_SHAPES_FAILED));
55
56   initPresentation(ModuleBase_IModule::CustomizeArguments);
57   initPresentation(ModuleBase_IModule::CustomizeResults);
58   initPresentation(ModuleBase_IModule::CustomizeHighlightedObjects);
59
60   myIsActive[ModuleBase_IModule::CustomizeArguments] = false;
61   myIsActive[ModuleBase_IModule::CustomizeResults] = false;
62   myIsActive[ModuleBase_IModule::CustomizeHighlightedObjects] = false;
63 }
64
65 bool PartSet_CustomPrs::isActive(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
66 {
67   return myIsActive[theFlag];
68 }
69
70 bool PartSet_CustomPrs::activate(const FeaturePtr& theFeature,
71                                  const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
72                                  const bool theUpdateViewer)
73 {
74 #ifdef DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
75   return false;
76 #endif
77   bool isModified = false;
78
79   // Do not call customisation for sketcher and all its sub-objects
80   if (theFeature->getKind() == SketchPlugin_Sketch::ID())
81     return isModified;
82
83   FeaturePtr aParent = ModelAPI_Tools::compositeOwner(theFeature);
84   if (aParent.get()) {
85     std::string aType = aParent->getKind();
86     if (aType == SketchPlugin_Sketch::ID())
87       return isModified;
88   }
89
90   if (theFeature.get()) {
91     myIsActive[theFlag] = true;
92     myFeature = theFeature;
93     displayPresentation(theFlag, theUpdateViewer);
94     isModified = true;
95   }
96   return isModified;
97 }
98
99 bool PartSet_CustomPrs::deactivate(const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
100                                    const bool theUpdateViewer)
101 {
102   myIsActive[theFlag] = false;
103   erasePresentation(theFlag, theUpdateViewer);
104   if (theFlag == ModuleBase_IModule::CustomizeResults)
105     clearErrorShape();
106   return true;
107 }
108
109 bool PartSet_CustomPrs::displayPresentation(
110                                   const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
111                                   const bool theUpdateViewer)
112 {
113   bool isModified = false;
114
115   if (myDisabledMode == theFlag)
116     return isModified;
117
118   // update the AIS objects content
119   AISObjectPtr aPresentation = getPresentation(theFlag, true);
120   Handle(AIS_InteractiveObject) anAISIO = aPresentation->impl<Handle(AIS_InteractiveObject)>();
121   Handle(PartSet_OperationPrs) anOperationPrs = Handle(PartSet_OperationPrs)::DownCast(anAISIO);
122
123   // do nothing if the feature can not be displayed [is moved from presentation, to be checked]
124   if (!myFeature.get())
125     return isModified;
126
127   QMap<ObjectPtr, QList<GeomShapePtr> > aFeatureShapes;
128   switch (theFlag) {
129     case ModuleBase_IModule::CustomizeArguments:
130       PartSet_OperationPrs::getFeatureShapes(myFeature, myWorkshop, aFeatureShapes);
131       break;
132     case ModuleBase_IModule::CustomizeResults:
133       PartSet_OperationPrs::getResultShapes(myFeature, myWorkshop, aFeatureShapes);
134       PartSet_OperationPrs::getPresentationShapes(myFeature, myWorkshop, aFeatureShapes, false);
135       break;
136     case ModuleBase_IModule::CustomizeHighlightedObjects:
137       PartSet_OperationPrs::getHighlightedShapes(myWorkshop, aFeatureShapes);
138       break;
139     default:
140       return isModified;
141   }
142   NCollection_DataMap<TopoDS_Shape, Handle(AIS_InteractiveObject)>& aShapeMap =
143                                                                  anOperationPrs->shapesMap();
144   PartSet_OperationPrs::fillShapeList(aFeatureShapes, myWorkshop, aShapeMap);
145
146   myPresentationIsEmpty = false;
147   // redisplay AIS objects
148   bool aRedisplayed = false;
149   Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
150   if (!aContext.IsNull() && !aContext->IsDisplayed(anOperationPrs)) {
151     // when the feature can not be visualized in the module, the operation preview should not
152     // be visualized also
153     if (anOperationPrs->hasShapes() && myWorkshop->module()->canDisplayObject(myFeature)) {
154       // set color here because it can be changed in preferences
155       Quantity_Color aShapeColor = getShapeColor(theFlag);
156       anOperationPrs->setShapeColor(aShapeColor);
157
158       PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
159       XGUI_Workshop* aWorkshop = workshop();
160       aRedisplayed = aWorkshop->displayer()->displayAIS(myPresentations[theFlag],
161                                          false/*load object in selection*/, 0, false);
162       aContext->SetZLayer(anOperationPrs, aModule->getVisualLayerId());
163       isModified = true;
164     }
165   }
166   else {
167     // when the feature can not be visualized in the module, the operation preview should not
168     // be visualized also
169     if (!anOperationPrs->hasShapes() || !myWorkshop->module()->canDisplayObject(myFeature)) {
170       aRedisplayed = erasePresentation(theFlag, false);
171       isModified = true;
172     }
173     else {
174       if (myFeature->firstResult().get() && myFeature->firstResult()->hasTextureFile())
175       {
176         PartSet_Module::setTexture( myFeature->firstResult()->getTextureFile(), aPresentation);
177       }
178       anOperationPrs->Redisplay();
179       isModified = true;
180       aRedisplayed = true;
181     }
182   }
183   if (myPresentationIsEmpty) {
184     aRedisplayed = erasePresentation(theFlag, false);
185   }
186   if (aRedisplayed && theUpdateViewer)
187     workshop()->displayer()->updateViewer();
188
189   return isModified;
190 }
191
192 bool PartSet_CustomPrs::erasePresentation(
193                           const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
194                           const bool theUpdateViewer)
195 {
196   bool isErased = false;
197   XGUI_Workshop* aWorkshop = workshop();
198   if (myPresentations.contains(theFlag))
199     isErased = aWorkshop->displayer()->eraseAIS(myPresentations[theFlag], theUpdateViewer);
200   return isErased;
201 }
202
203 void PartSet_CustomPrs::clearPresentation(
204   const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
205 {
206   AISObjectPtr aPresentation = getPresentation(theFlag, false);
207   if (aPresentation.get()) {
208     Handle(AIS_InteractiveObject) anAISIO = aPresentation->impl<Handle(AIS_InteractiveObject)>();
209     Handle(PartSet_OperationPrs) anOperationPrs = Handle(PartSet_OperationPrs)::DownCast(anAISIO);
210
211     anOperationPrs->shapesMap().Clear();
212     if (!anOperationPrs.IsNull())
213       anOperationPrs.Nullify();
214     myPresentations.remove(theFlag);
215   }
216 }
217
218 AISObjectPtr PartSet_CustomPrs::getPresentation(
219                                        const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
220                                        const bool theToCreate)
221 {
222   Handle(PartSet_OperationPrs) aPresentation;
223
224   AISObjectPtr anOperationPrs;
225   if (myPresentations.contains(theFlag))
226     anOperationPrs = myPresentations[theFlag];
227
228   if (!anOperationPrs.get() && theToCreate) {
229     initPresentation(theFlag);
230     anOperationPrs = myPresentations[theFlag];
231   }
232
233   return anOperationPrs;
234 }
235
236 bool PartSet_CustomPrs::redisplay(const ObjectPtr& theObject,
237                                   const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
238                                   const bool theUpdateViewer)
239 {
240 #ifdef DO_NOT_VISUALIZE_CUSTOM_PRESENTATION
241   return false;
242 #endif
243   bool aRedisplayed = false;
244   if (myIsActive[theFlag])
245     aRedisplayed = displayPresentation(theFlag, theUpdateViewer);
246
247   return aRedisplayed;
248 }
249
250 void PartSet_CustomPrs::clearPrs()
251 {
252   clearPresentation(ModuleBase_IModule::CustomizeArguments);
253   clearPresentation(ModuleBase_IModule::CustomizeResults);
254   clearPresentation(ModuleBase_IModule::CustomizeHighlightedObjects);
255   clearErrorShape();
256 }
257
258 void PartSet_CustomPrs::clearErrorShape()
259 {
260   if (!myErrorShapes.IsNull()) {
261     Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
262     if (aContext->IsDisplayed(myErrorShapes)) {
263       aContext->Remove(myErrorShapes, true);
264       myErrorShapes.Nullify();
265     }
266   }
267 }
268
269 void PartSet_CustomPrs::processEvent(const std::shared_ptr<Events_Message>& theMessage)
270 {
271   if (theMessage->eventID() == Events_Loop::eventByName(EVENT_EMPTY_OPERATION_PRESENTATION))
272     myPresentationIsEmpty = true; /// store state to analize it after display/erase is finished
273   else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_OPERATION_SHAPES_FAILED)) {
274     std::shared_ptr<ModelAPI_ShapesFailedMessage> aErrMsg =
275       std::dynamic_pointer_cast<ModelAPI_ShapesFailedMessage>(theMessage);
276     Handle(AIS_InteractiveContext) aContext = myWorkshop->viewer()->AISContext();
277     ListOfShape aShapes = aErrMsg->shapes();
278     if (aShapes.size() > 0) {
279       GeomShapePtr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
280       TopoDS_Shape aErrShape = aCompound->impl<TopoDS_Shape>();
281       if (myErrorShapes.IsNull()) {
282         myErrorShapes = new AIS_Shape(aErrShape);
283         myErrorShapes->SetColor(Quantity_NOC_RED);
284         Handle(Prs3d_Drawer) aDrawer = myErrorShapes->Attributes();
285         aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_RING1, Quantity_NOC_RED, 2.));
286         aDrawer->SetLineAspect(new Prs3d_LineAspect(Quantity_NOC_RED, Aspect_TOL_SOLID, 2.));
287         aContext->Display(myErrorShapes, true);
288         aContext->Deactivate(myErrorShapes);
289       }
290       else {
291         myErrorShapes->Set(aErrShape);
292         aContext->Redisplay(myErrorShapes, true);
293       }
294     }
295     else {
296       if (!myErrorShapes.IsNull()) {
297         aContext->Remove(myErrorShapes, true);
298         myErrorShapes.Nullify();
299       }
300     }
301   }
302 }
303
304 void PartSet_CustomPrs::initPresentation(
305   const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
306 {
307   AISObjectPtr anOperationPrs = AISObjectPtr(new GeomAPI_AISObject());
308   Handle(PartSet_OperationPrs) anAISPrs = new PartSet_OperationPrs(myWorkshop);
309   anOperationPrs->setImpl(new Handle(AIS_InteractiveObject)(anAISPrs));
310   if (theFlag == ModuleBase_IModule::CustomizeArguments ||
311       theFlag == ModuleBase_IModule::CustomizeResults) {
312     anOperationPrs->setPointMarker(5, 2.);
313     anOperationPrs->setWidth((theFlag == ModuleBase_IModule::CustomizeHighlightedObjects)? 2 : 1);
314   }
315   else if (theFlag == ModuleBase_IModule::CustomizeHighlightedObjects)
316     anAISPrs->useAISWidth();
317
318   if (anOperationPrs.get())
319     myPresentations[theFlag] = anOperationPrs;
320 }
321
322 Quantity_Color PartSet_CustomPrs::getShapeColor(
323                                   const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag)
324 {
325   Quantity_Color aColor;
326   switch(theFlag) {
327     case ModuleBase_IModule::CustomizeArguments:
328       aColor = ModuleBase_Tools::color("Visualization", "operation_parameter_color");
329     break;
330     case ModuleBase_IModule::CustomizeResults:
331       aColor = ModuleBase_Tools::color("Visualization", "operation_result_color");
332     break;
333     case ModuleBase_IModule::CustomizeHighlightedObjects:
334       aColor = ModuleBase_Tools::color("Visualization", "operation_highlight_color");
335     break;
336     default:
337     break;
338   }
339   return aColor;
340 }
341
342 XGUI_Workshop* PartSet_CustomPrs::workshop() const
343 {
344   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
345   return aConnector->workshop();
346 }