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