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