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