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