Salome HOME
Optimize color management of presentations
[modules/shaper.git] / src / ModuleBase / ModuleBase_ResultPrs.cpp
1 // Copyright (C) 2014-2019  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 "ModuleBase_ResultPrs.h"
21 #include "ModuleBase_IViewer.h"
22
23 #include <GeomAPI_PlanarEdges.h>
24
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_Tools.h>
27 #include <ModelAPI_ResultConstruction.h>
28 #include <ModelAPI_ResultBody.h>
29 #include <ModelAPI_AttributeIntArray.h>
30
31 #include "ModuleBase_Tools.h"
32 #include "ModuleBase_BRepOwner.h"
33
34 #include <Events_InfoMessage.h>
35 #include <Events_Loop.h>
36
37 #include <AIS_ColoredDrawer.hxx>
38 #include <AIS_InteractiveContext.hxx>
39 #include <AIS_Selection.hxx>
40 #include <BOPTools_AlgoTools3D.hxx>
41 #include <BRep_Builder.hxx>
42 #include <Graphic3d_AspectMarker3d.hxx>
43 #include <Prs3d_Drawer.hxx>
44 #include <Prs3d.hxx>
45 #include <Prs3d_PointAspect.hxx>
46 #include <Prs3d_IsoAspect.hxx>
47 #include <Prs3d_ShadingAspect.hxx>
48 #include <SelectMgr_SequenceOfOwner.hxx>
49 #include <SelectMgr_EntityOwner.hxx>
50 #include <SelectMgr_SelectionManager.hxx>
51 #include <StdPrs_WFShape.hxx>
52 #include <StdPrs_ShadedShape.hxx>
53 #include <StdSelect_BRepSelectionTool.hxx>
54 #include <TColStd_ListIteratorOfListOfInteger.hxx>
55 #include <TopExp_Explorer.hxx>
56 #include <TopoDS.hxx>
57 #include <TopoDS_Builder.hxx>
58
59 //*******************************************************************************************
60
61 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
62
63
64
65 //********************************************************************
66 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
67   : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult), myAdditionalSelectionPriority(0),
68   myTransparency(1), myIsSubstituted(false)
69 {
70   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
71   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
72   Set(aShape);
73
74   // Activate individual repaintng if this is a part of compsolid
75   ResultBodyPtr aResOwner = ModelAPI_Tools::bodyOwner(myResult);
76   SetAutoHilight(aResOwner.get() == NULL);
77
78   // Set own free boundaries aspect in order to have free
79   // and unfree boundaries with different colors
80   Handle(Prs3d_Drawer) aDrawer = Attributes();
81   aDrawer->SetUnFreeBoundaryAspect(
82     new Prs3d_LineAspect(Quantity_NOC_YELLOW, Aspect_TOL_SOLID, 1));
83   aDrawer->SetFreeBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_GREEN, Aspect_TOL_SOLID, 1));
84   aDrawer->SetFaceBoundaryAspect(new Prs3d_LineAspect(Quantity_NOC_BLACK, Aspect_TOL_SOLID, 1));
85
86   aDrawer->VIsoAspect()->SetNumber(0);
87   aDrawer->UIsoAspect()->SetNumber(0);
88
89   if (aDrawer->HasOwnPointAspect())
90     aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
91   else
92     aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
93
94   aDrawer = DynamicHilightAttributes();
95   if (aDrawer.IsNull()) {
96     if (!ModuleBase_IViewer::DefaultHighlightDrawer.IsNull()) {
97       aDrawer = new Prs3d_Drawer(*ModuleBase_IViewer::DefaultHighlightDrawer);
98       aDrawer->VIsoAspect()->SetNumber(0);
99       aDrawer->UIsoAspect()->SetNumber(0);
100       SetDynamicHilightAttributes(aDrawer);
101     }
102   } else {
103     aDrawer->VIsoAspect()->SetNumber(0);
104     aDrawer->UIsoAspect()->SetNumber(0);
105   }
106   myHiddenSubShapesDrawer = new AIS_ColoredDrawer(myDrawer);
107   Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
108   aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
109   aShadingAspect->Aspect()->SetEdgeColor(Quantity_NOC_BLACK);
110   myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
111
112   ModuleBase_Tools::setPointBallHighlighting(this);
113
114   // Define colors for wireframe mode
115   setEdgesDefaultColor();
116 }
117
118 //********************************************************************
119 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
120 {
121   myAdditionalSelectionPriority = thePriority;
122 }
123
124 //********************************************************************
125 void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
126 {
127   ViewerData_AISShape::SetColor(theColor);
128   myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
129   setEdgesDefaultColor();
130 }
131
132 void ModuleBase_ResultPrs::setEdgesDefaultColor()
133 {
134   if (myResult.get()) {
135     AttributeIntArrayPtr aColorAttr = myResult->data()->intArray(ModelAPI_Result::COLOR_ID());
136     bool aHasColor = aColorAttr.get() && aColorAttr->isInitialized();
137
138     Handle(Prs3d_Drawer) aDrawer = Attributes();
139     aDrawer->SetFaceBoundaryDraw(Standard_True);
140     aDrawer->FaceBoundaryAspect()->SetColor(Quantity_NOC_BLACK);
141
142     if (!aHasColor) {
143       aDrawer->UnFreeBoundaryAspect()->SetColor(Quantity_NOC_YELLOW);
144       aDrawer->FreeBoundaryAspect()->SetColor(Quantity_NOC_GREEN);
145       aDrawer->WireAspect()->SetColor(Quantity_NOC_RED);
146
147       aDrawer->SetUnFreeBoundaryDraw(Standard_True);
148       aDrawer->SetFreeBoundaryDraw(Standard_True);
149       aDrawer->SetWireDraw(Standard_True);
150     }
151   }
152 }
153
154
155 //********************************************************************
156 void ModuleBase_ResultPrs::setSubShapeHidden(const TopoDS_ListOfShape& theShapes)
157 {
158   bool isModified = false;
159
160   TopoDS_Compound aCompound;
161   BRep_Builder aBBuilder;
162   aBBuilder.MakeCompound (aCompound);
163
164   myHiddenSubShapes = theShapes;
165   collectSubShapes(aBBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
166   myVisibleCompound = aCompound;
167
168   aBBuilder.MakeCompound (aCompound);
169   TopoDS_ListOfShape::Iterator aIt(myHiddenSubShapes);
170   for (; aIt.More(); aIt.Next()) {
171     aBBuilder.Add(aCompound, aIt.Value());
172   }
173   myHiddenCompound = aCompound;
174 }
175
176 //********************************************************************
177 bool ModuleBase_ResultPrs::isSubShapeHidden(const TopoDS_Shape& theShape)
178 {
179   if (theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE) // only face shape can be hidden
180     return false;
181
182   // orientation of parameter shape(come from selection) may be wrong, check isEqual() to be sure
183   TopoDS_ListOfShape::Iterator aShapeIt(myHiddenSubShapes);
184   for (; aShapeIt.More(); aShapeIt.Next()) {
185     if (theShape.IsSame(aShapeIt.Value()))
186       return true;
187   }
188
189   return true;
190 }
191
192 //********************************************************************
193 bool ModuleBase_ResultPrs::hasSubShapeVisible(
194   const TopoDS_ListOfShape& theShapesToSkip)
195 {
196   TopoDS_Compound aCompound;
197   BRep_Builder aBuilder;
198   aBuilder.MakeCompound (aCompound);
199   TopoDS_ListOfShape aShapesToSkip;
200   TopoDS_ListOfShape aHiddenCopy(myHiddenSubShapes);
201   aShapesToSkip.Append(aHiddenCopy);
202   for (TopoDS_ListOfShape::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
203     aShapesToSkip.Append(anIt.Value());
204
205   collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
206   return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
207 }
208
209 //********************************************************************
210 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
211 {
212   if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
213     return false;
214
215   myTransparency = theTransparency;
216   myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
217   return true;
218 }
219
220 //********************************************************************
221 void ModuleBase_ResultPrs::Compute(
222           const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
223           const Handle(Prs3d_Presentation)& thePresentation,
224           const Standard_Integer theMode)
225 {
226   std::shared_ptr<GeomAPI_Shape> aShapePtr = myResult->shape();
227   bool aReadyToDisplay = aShapePtr.get();
228   if (aReadyToDisplay) {
229     myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
230     if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
231       if (!myOriginalShape.IsNull()) {
232         Set(myOriginalShape);
233         myIsSubstituted = false;
234       }
235     }
236     else { // convert shape into SHELL
237       bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(myVisibleCompound);
238       Set(myVisibleCompound);
239       myIsSubstituted = true;
240       if (isEmptyShape)
241         aReadyToDisplay = false;
242     }
243   }
244   try {
245     AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
246   }
247   catch (...) {
248     return;
249   }
250
251   // visualize hidden sub-shapes transparent
252   if (myResult.get()) {
253     if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
254     {
255       StdPrs_ShadedShape::Add(thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
256       aReadyToDisplay = true;
257     }
258
259     if (!aReadyToDisplay) {
260       Events_InfoMessage("ModuleBase_ResultPrs",
261         "An empty AIS presentation: ModuleBase_ResultPrs").send();
262       static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
263       ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
264     }
265   }
266 }
267
268 //********************************************************************
269 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
270   TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
271   const TopoDS_ListOfShape& theHiddenSubShapes)
272 {
273   switch (theShape.ShapeType()) {
274     case TopAbs_COMPSOLID:
275     case TopAbs_COMPOUND: {
276       for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
277         collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
278     }
279     break;
280     case TopAbs_SOLID:
281     case TopAbs_SHELL: {
282       for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
283         collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
284       }
285     }
286     break;
287     case TopAbs_WIRE: {
288       for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
289         collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
290       }
291     }
292     break;
293     case TopAbs_FACE: {
294       if (theHiddenSubShapes.Contains(theShape))
295         return; // remove hidden shape
296       theBuilder.Add(theCompound, theShape);
297     }
298     break;
299     case TopAbs_EDGE:
300     case TopAbs_VERTEX: {
301       theBuilder.Add(theCompound, theShape);
302     }
303     default:
304       break;
305   }
306 }
307
308 //********************************************************************
309 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
310                                             const Standard_Integer theMode)
311 {
312   if (appendVertexSelection(aSelection, theMode))
313     return;
314
315   if (theMode > TopAbs_SHAPE) {
316     // In order to avoid using custom selection modes
317     if (theMode == ModuleBase_ResultPrs::Sel_Result) {
318       AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
319     }
320     return;
321   }
322
323   // TODO: OCCT issue should be created for the COMPOUND processing
324   // before it is fixed, the next workaround in necessary
325   if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
326     const TopoDS_Shape& aShape = Shape();
327     TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
328     // do not activate in compound mode shapes which do not contain compounds
329     if (!aCompExp.More())
330       return;
331   }
332
333   // bug 2110: if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
334   //  // Limit selection area only by actual object (Shape)
335   //  ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
336   //  if (aCompSolid.get()) {
337   //    std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
338   //    if (aShapePtr.get()) {
339   //      TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
340   //   int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
341   //     /// It is important to have priority for the shape of comp solid result less than priority
342   //      /// for the presentation shape which is a sub-result.
343   //      /// Reason is to select the sub-objects before: #1592
344   //      aPriority = aPriority - 1;
345   //      double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
346
347   //      Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
348   //      StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
349   //        aDeflection, myDrawer->HLRAngle(), 9, 500);
350
351   //      for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
352   //        Handle(SelectMgr_EntityOwner) anOwner =
353   //          Handle(SelectMgr_EntityOwner)
354   //          ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
355   //        anOwner->Set(this);
356   //      }
357   //      return;
358   //    }
359   //  }
360   //}
361   AIS_Shape::ComputeSelection(aSelection, theMode);
362
363   if (myAdditionalSelectionPriority > 0) {
364     NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
365     for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
366          anIt.More();
367          anIt.Next()) {
368       Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
369       Handle(SelectBasics_EntityOwner) aBasicsOwner = anEntity->BaseSensitive()->OwnerId();
370       if (!aBasicsOwner.IsNull())
371         aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
372     }
373   }
374 }
375
376 //********************************************************************
377 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
378                                                  const Standard_Integer theMode)
379 {
380   if (Shape().ShapeType() == TopAbs_VERTEX) {
381     const TopoDS_Shape& aShape = Shape();
382
383     int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
384     double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
385
386     /// The cause of this method is the last parameter of BRep owner setting into True.
387     /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
388     /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
389     Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
390     StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
391                                                   aDeflection, myDrawer->HLRAngle(), 9, 500);
392
393
394     NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
395     for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
396          anIt.More();
397          anIt.Next()) {
398       Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
399       Handle(SelectMgr_EntityOwner) anOwner =
400         Handle(SelectMgr_EntityOwner)
401         ::DownCast(anEntity->BaseSensitive()->OwnerId());
402       anOwner->Set(this);
403     }
404
405     return true;
406   }
407   return false;
408 }
409
410 //********************************************************************
411 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
412                                            const SelectMgr_SequenceOfOwner& theOwners)
413 {
414   Handle(SelectMgr_EntityOwner) anOwner;
415   Handle(ModuleBase_BRepOwner) aCompOwner;
416   for (int i = 1; i <= theOwners.Length(); i++) {
417     anOwner = theOwners.Value(i);
418     aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
419     if (aCompOwner.IsNull()) {
420       thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
421     }
422     else {
423       TopoDS_Shape aShape = aCompOwner->Shape();
424       Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
425       aSelectionPrs->Clear();
426
427       StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
428
429       aSelectionPrs->SetDisplayPriority(9);
430       aSelectionPrs->Highlight(GetContext()->SelectionStyle());
431       aSelectionPrs->Display();
432       thePM->Color(this, GetContext()->SelectionStyle());
433     }
434   }
435 }
436
437 //********************************************************************
438 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
439                                                  const Handle(Prs3d_Drawer)& theStyle,
440                                                  const Handle(SelectMgr_EntityOwner)& theOwner)
441 {
442   Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
443   if (aOwner.IsNull())
444     return;
445
446   TopoDS_Shape aShape = aOwner->Shape();
447   if (!aShape.IsNull()) {
448     thePM->Color(this, theStyle);
449
450     Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
451     aHilightPrs->Clear();
452
453     StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
454     aHilightPrs->Highlight(theStyle);
455
456     if (thePM->IsImmediateModeOn())
457       thePM->AddToImmediateList(aHilightPrs);
458   }
459 }