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