Salome HOME
#1404 Random crash with Shaper: AIS presentations: avoid IsReadyToDisplay calling...
[modules/shaper.git] / src / ModuleBase / ModuleBase_ResultPrs.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_ResultPrs.cpp
4 // Created:     21 October 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "ModuleBase_ResultPrs.h"
8 #include "ModuleBase_Tools.h"
9
10 #include <ModelAPI_Events.h>
11 #include <ModelAPI_Tools.h>
12 #include <ModelAPI_ResultConstruction.h>
13 #include <GeomAPI_PlanarEdges.h>
14
15 #include <Events_Error.h>
16 #include <Events_Loop.h>
17
18 #include <BRep_Builder.hxx>
19 #include <Prs3d_Drawer.hxx>
20 #include <Prs3d.hxx>
21 #include <Prs3d_PointAspect.hxx>
22 #include <Prs3d_IsoAspect.hxx>
23 #include <TopoDS_Builder.hxx>
24 #include <SelectMgr_SequenceOfOwner.hxx>
25 #include <SelectMgr_EntityOwner.hxx>
26 #include <SelectMgr_SelectionManager.hxx>
27 #include <StdPrs_WFDeflectionShape.hxx>
28 #include <StdSelect_BRepSelectionTool.hxx>
29 #include <AIS_InteractiveContext.hxx>
30 #include <AIS_Selection.hxx>
31 #include <TColStd_ListIteratorOfListOfInteger.hxx>
32
33 IMPLEMENT_STANDARD_HANDLE(ModuleBase_BRepOwner, StdSelect_BRepOwner);
34 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_BRepOwner, StdSelect_BRepOwner);
35
36 //*******************************************************************************************
37
38 IMPLEMENT_STANDARD_HANDLE(ModuleBase_ResultPrs, ViewerData_AISShape);
39 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
40
41
42
43
44
45 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
46   : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult)
47 {
48   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
49   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
50   Set(aShape);
51   Handle(Prs3d_Drawer) aDrawer = Attributes();
52   if (aDrawer->HasOwnPointAspect()) 
53     aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
54   else
55     aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
56
57   // Activate individual repaintng if this is a part of compsolid
58   ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
59   SetAutoHilight(aCompSolid.get() == NULL);
60 }
61
62 bool ModuleBase_ResultPrs::isValidShapeType(const TopAbs_ShapeEnum& theBaseType,
63                                             const TopAbs_ShapeEnum& theCheckedType)
64 {
65   bool aValid = theBaseType == theCheckedType;
66   if (!aValid) {
67     // currently this functionality is for all, as we have no separate wire selection mode
68     // lately it should be corrected to have the following check only for sketch presentations
69     aValid = theBaseType == TopAbs_FACE && theCheckedType == TopAbs_WIRE;
70   }
71   return aValid;
72 }
73
74 void ModuleBase_ResultPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
75                                    const Handle(Prs3d_Presentation)& thePresentation, 
76                                    const Standard_Integer theMode)
77 {
78   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
79   bool aReadyToDisplay = aShapePtr.get();
80   if (aReadyToDisplay) {
81     myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
82     if (!myOriginalShape.IsNull())
83       Set(myOriginalShape);
84   }
85   // change deviation coefficient to provide more precise circle
86   ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), Attributes());
87   AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
88
89   if (!aReadyToDisplay) {
90     Events_Error::throwException("An empty AIS presentation: ModuleBase_ResultPrs");
91     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
92     ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
93   }
94 }
95
96 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
97                                             const Standard_Integer aMode)
98 {
99   if (aMode > 8)
100     // In order to avoid using custom selection modes
101     return;
102
103   if (aMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
104     // Limit selection area only by actual object (Shape)
105     ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
106     if (aCompSolid.get()) {
107       std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
108       if (aShapePtr.get()) {
109         TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
110         int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_SHAPE);
111         double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
112
113         Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
114         StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection, 
115           aDeflection, myDrawer->HLRAngle(), 9, 500);
116
117         for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
118           Handle(SelectMgr_EntityOwner) anOwner =
119             Handle(SelectMgr_EntityOwner)::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
120           anOwner->Set(this);
121         }
122         return;
123       }
124     }
125     //AIS_Shape::ComputeSelection(aSelection, 0);
126   } 
127   AIS_Shape::ComputeSelection(aSelection, aMode);
128 }
129
130 void ModuleBase_ResultPrs::appendWiresSelection(const Handle(SelectMgr_Selection)& theSelection,
131                                                 const TopoDS_Shape& theShape)
132 {
133   static TopAbs_ShapeEnum TypOfSel
134           = AIS_Shape::SelectionType(AIS_Shape::SelectionMode(TopAbs_WIRE));
135   // POP protection against crash in low layers
136   Standard_Real aDeflection = Prs3d::GetDeflection(theShape, myDrawer);
137   try {
138     StdSelect_BRepSelectionTool::Load(theSelection,
139                                       this,
140                                       theShape,
141                                       TypOfSel,
142                                       aDeflection,
143                                       myDrawer->HLRAngle(),
144                                       myDrawer->IsAutoTriangulation());
145   } catch ( Standard_Failure ) {
146   }
147 }
148
149 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM, 
150                                            const SelectMgr_SequenceOfOwner& theOwners)
151 {
152   Handle(SelectMgr_EntityOwner) anOwner;
153   Handle(ModuleBase_BRepOwner) aCompOwner;
154   for (int i = 1; i <= theOwners.Length(); i++) {
155     anOwner = theOwners.Value(i);
156     aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
157     if (aCompOwner.IsNull())
158       anOwner->Hilight(thePM);
159     else {
160       TopoDS_Shape aShape = aCompOwner->Shape();
161       Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
162       aSelectionPrs->Clear();
163
164       StdPrs_WFDeflectionShape::Add(aSelectionPrs, aShape, myDrawer);
165
166       aSelectionPrs->SetDisplayPriority(9);
167       aSelectionPrs->Highlight(Aspect_TOHM_COLOR, aSelectionPrs->HighlightColor());
168       aSelectionPrs->Display();
169       thePM->Highlight(this);
170     }
171   }
172 }
173   
174 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM, 
175                                                  const Quantity_NameOfColor theColor, 
176                                                  const Handle(SelectMgr_EntityOwner)& theOwner)
177 {
178   Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
179   if (aOwner.IsNull())
180     return;
181
182   TopoDS_Shape aShape = aOwner->Shape();
183   if (!aShape.IsNull()) {
184     thePM->Color(this, theColor);
185
186     Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
187     aHilightPrs->Clear();
188
189     StdPrs_WFDeflectionShape::Add(aHilightPrs, aShape, myDrawer);
190     aHilightPrs->Highlight(Aspect_TOHM_COLOR, theColor);
191
192     if (thePM->IsImmediateModeOn())
193       thePM->AddToImmediateList(aHilightPrs);
194   }
195 }