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