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