Salome HOME
Selection priority in Sketch, clear selection when sketch goes from entity to neutral...
[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_InfoMessage.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 <TopoDS.hxx>
25 #include <SelectMgr_SequenceOfOwner.hxx>
26 #include <SelectMgr_EntityOwner.hxx>
27 #include <SelectMgr_SelectionManager.hxx>
28 #include <StdPrs_WFDeflectionShape.hxx>
29 #include <StdSelect_BRepSelectionTool.hxx>
30 #include <AIS_InteractiveContext.hxx>
31 #include <AIS_Selection.hxx>
32 #include <TColStd_ListIteratorOfListOfInteger.hxx>
33 #include <Graphic3d_AspectMarker3d.hxx>
34 #include <TopExp_Explorer.hxx>
35
36 IMPLEMENT_STANDARD_HANDLE(ModuleBase_BRepOwner, StdSelect_BRepOwner);
37 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_BRepOwner, StdSelect_BRepOwner);
38
39 //*******************************************************************************************
40
41 IMPLEMENT_STANDARD_HANDLE(ModuleBase_ResultPrs, ViewerData_AISShape);
42 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
43
44
45
46
47
48 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
49   : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult), myAdditionalSelectionPriority(0)
50 {
51   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
52   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
53   Set(aShape);
54   Handle(Prs3d_Drawer) aDrawer = Attributes();
55   if (aDrawer->HasOwnPointAspect()) 
56     aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
57   else
58     aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
59
60   // Activate individual repaintng if this is a part of compsolid
61   ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
62   SetAutoHilight(aCompSolid.get() == NULL);
63
64   ModuleBase_Tools::setPointBallHighlighting(this);
65 }
66
67 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
68 {
69   myAdditionalSelectionPriority = thePriority;
70 }
71
72 void ModuleBase_ResultPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
73                                    const Handle(Prs3d_Presentation)& thePresentation, 
74                                    const Standard_Integer theMode)
75 {
76   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
77   bool aReadyToDisplay = aShapePtr.get();
78   if (aReadyToDisplay) {
79     myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
80     if (!myOriginalShape.IsNull())
81       Set(myOriginalShape);
82   }
83   // change deviation coefficient to provide more precise circle
84   ModuleBase_Tools::setDefaultDeviationCoefficient(myResult, Attributes());
85   AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
86
87   if (!aReadyToDisplay) {
88     Events_InfoMessage("ModuleBase_ResultPrs", "An empty AIS presentation: ModuleBase_ResultPrs").send();
89     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
90     ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
91   }
92 }
93
94 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
95                                             const Standard_Integer aMode)
96 {
97   if (aMode > 8)
98     // In order to avoid using custom selection modes
99     return;
100
101   // TODO: OCCT issue should be created for the COMPOUND processing
102   // before it is fixed, the next workaround in necessary
103   if (aMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
104     const TopoDS_Shape& aShape = Shape();
105     TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
106     // do not activate in compound mode shapes which do not contain compounds
107     if (!aCompExp.More())
108       return;
109   }
110
111   if (aMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
112     // Limit selection area only by actual object (Shape)
113     ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
114     if (aCompSolid.get()) {
115       std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
116       if (aShapePtr.get()) {
117         TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
118         int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_SHAPE);
119         double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
120
121         Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
122         StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection, 
123           aDeflection, myDrawer->HLRAngle(), 9, 500);
124
125         for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
126           Handle(SelectMgr_EntityOwner) anOwner =
127             Handle(SelectMgr_EntityOwner)::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
128           anOwner->Set(this);
129         }
130         return;
131       }
132     }
133     //AIS_Shape::ComputeSelection(aSelection, 0);
134   } 
135   AIS_Shape::ComputeSelection(aSelection, aMode);
136
137   if (myAdditionalSelectionPriority > 0) {
138     for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
139       Handle(SelectBasics_EntityOwner) aBasicsOwner = aSelection->Sensitive()->BaseSensitive()->OwnerId();
140       if (!aBasicsOwner.IsNull())
141         aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
142     }
143   }
144 }
145
146 void ModuleBase_ResultPrs::appendWiresSelection(const Handle(SelectMgr_Selection)& theSelection,
147                                                 const TopoDS_Shape& theShape)
148 {
149   static TopAbs_ShapeEnum TypOfSel
150           = AIS_Shape::SelectionType(AIS_Shape::SelectionMode(TopAbs_WIRE));
151   // POP protection against crash in low layers
152   Standard_Real aDeflection = Prs3d::GetDeflection(theShape, myDrawer);
153   try {
154     StdSelect_BRepSelectionTool::Load(theSelection,
155                                       this,
156                                       theShape,
157                                       TypOfSel,
158                                       aDeflection,
159                                       myDrawer->HLRAngle(),
160                                       myDrawer->IsAutoTriangulation());
161   } catch ( Standard_Failure ) {
162   }
163 }
164
165 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM, 
166                                            const SelectMgr_SequenceOfOwner& theOwners)
167 {
168   Handle(SelectMgr_EntityOwner) anOwner;
169   Handle(ModuleBase_BRepOwner) aCompOwner;
170   for (int i = 1; i <= theOwners.Length(); i++) {
171     anOwner = theOwners.Value(i);
172     aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
173     if (aCompOwner.IsNull())
174       anOwner->Hilight(thePM);
175     else {
176       TopoDS_Shape aShape = aCompOwner->Shape();
177       Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
178       aSelectionPrs->Clear();
179
180       StdPrs_WFDeflectionShape::Add(aSelectionPrs, aShape, myDrawer);
181
182       aSelectionPrs->SetDisplayPriority(9);
183       aSelectionPrs->Highlight(Aspect_TOHM_COLOR, aSelectionPrs->HighlightColor());
184       aSelectionPrs->Display();
185       thePM->Highlight(this);
186     }
187   }
188 }
189   
190 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM, 
191                                                  const Quantity_NameOfColor theColor, 
192                                                  const Handle(SelectMgr_EntityOwner)& theOwner)
193 {
194   Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
195   if (aOwner.IsNull())
196     return;
197
198   TopoDS_Shape aShape = aOwner->Shape();
199   if (!aShape.IsNull()) {
200     thePM->Color(this, theColor);
201
202     Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
203     aHilightPrs->Clear();
204
205     StdPrs_WFDeflectionShape::Add(aHilightPrs, aShape, myDrawer);
206     aHilightPrs->Highlight(Aspect_TOHM_COLOR, theColor);
207
208     if (thePM->IsImmediateModeOn())
209       thePM->AddToImmediateList(aHilightPrs);
210   }
211 }