Salome HOME
Revert "Merge branch 'Pre_2.8.0_development'"
[modules/shaper.git] / src / ModuleBase / ModuleBase_ResultPrs.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "ModuleBase_ResultPrs.h"
22 #include "ModuleBase_Tools.h"
23
24 #include <ModelAPI_Events.h>
25 #include <ModelAPI_Tools.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_ResultCompSolid.h>
28 #include <GeomAPI_PlanarEdges.h>
29
30 #include <Events_InfoMessage.h>
31 #include <Events_Loop.h>
32
33 #include <BRep_Builder.hxx>
34 #include <Prs3d_Drawer.hxx>
35 #include <Prs3d.hxx>
36 #include <Prs3d_PointAspect.hxx>
37 #include <Prs3d_IsoAspect.hxx>
38 #include <TopoDS_Builder.hxx>
39 #include <TopoDS.hxx>
40 #include <SelectMgr_SequenceOfOwner.hxx>
41 #include <SelectMgr_EntityOwner.hxx>
42 #include <SelectMgr_SelectionManager.hxx>
43 #include <StdPrs_WFShape.hxx>
44 #include <StdSelect_BRepSelectionTool.hxx>
45 #include <AIS_InteractiveContext.hxx>
46 #include <AIS_Selection.hxx>
47 #include <TColStd_ListIteratorOfListOfInteger.hxx>
48 #include <Graphic3d_AspectMarker3d.hxx>
49 #include <TopExp_Explorer.hxx>
50
51 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_BRepOwner, StdSelect_BRepOwner);
52
53 //*******************************************************************************************
54
55 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
56
57
58
59 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
60   : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult), myAdditionalSelectionPriority(0)
61 {
62   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
63   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
64   Set(aShape);
65   Handle(Prs3d_Drawer) aDrawer = Attributes();
66   if (aDrawer->HasOwnPointAspect())
67     aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
68   else
69     aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
70
71   // Activate individual repaintng if this is a part of compsolid
72   ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
73   SetAutoHilight(aCompSolid.get() == NULL);
74
75   ModuleBase_Tools::setPointBallHighlighting(this);
76 }
77
78 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
79 {
80   myAdditionalSelectionPriority = thePriority;
81 }
82
83 void ModuleBase_ResultPrs::Compute(
84           const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
85           const Handle(Prs3d_Presentation)& thePresentation,
86           const Standard_Integer theMode)
87 {
88   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
89   bool aReadyToDisplay = aShapePtr.get();
90   if (aReadyToDisplay) {
91     myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
92     if (!myOriginalShape.IsNull())
93       Set(myOriginalShape);
94   }
95   // change deviation coefficient to provide more precise circle
96   //ModuleBase_Tools::setDefaultDeviationCoefficient(myResult, Attributes());
97   AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
98
99   if (!aReadyToDisplay) {
100     Events_InfoMessage("ModuleBase_ResultPrs",
101                        "An empty AIS presentation: ModuleBase_ResultPrs").send();
102     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
103     ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
104   }
105 }
106
107 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
108                                             const Standard_Integer aMode)
109 {
110   if (aMode > TopAbs_SHAPE) {
111     // In order to avoid using custom selection modes
112     if (aMode == ModuleBase_ResultPrs::Sel_Result) {
113       AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
114     }
115     return;
116   }
117
118   // TODO: OCCT issue should be created for the COMPOUND processing
119   // before it is fixed, the next workaround in necessary
120   if (aMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
121     const TopoDS_Shape& aShape = Shape();
122     TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
123     // do not activate in compound mode shapes which do not contain compounds
124     if (!aCompExp.More())
125       return;
126   }
127
128   if (aMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
129     // Limit selection area only by actual object (Shape)
130     ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
131     if (aCompSolid.get()) {
132       std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
133       if (aShapePtr.get()) {
134         TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
135         int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
136         /// It is important to have priority for the shape of comp solid result less than priority
137         /// for the presentation shape which is a sub-result.
138         /// Reason is to select the sub-objects before: #1592
139         aPriority = aPriority - 1;
140         double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
141
142         Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
143         StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
144           aDeflection, myDrawer->HLRAngle(), 9, 500);
145
146         for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
147           Handle(SelectMgr_EntityOwner) anOwner =
148             Handle(SelectMgr_EntityOwner)
149             ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
150           anOwner->Set(this);
151         }
152         return;
153       }
154     }
155     //AIS_Shape::ComputeSelection(aSelection, 0);
156   }
157   AIS_Shape::ComputeSelection(aSelection, aMode);
158
159   if (myAdditionalSelectionPriority > 0) {
160     for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
161       Handle(SelectBasics_EntityOwner) aBasicsOwner =
162         aSelection->Sensitive()->BaseSensitive()->OwnerId();
163       if (!aBasicsOwner.IsNull())
164         aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
165     }
166   }
167 }
168
169 void ModuleBase_ResultPrs::appendWiresSelection(const Handle(SelectMgr_Selection)& theSelection,
170                                                 const TopoDS_Shape& theShape)
171 {
172   static TopAbs_ShapeEnum TypOfSel
173           = AIS_Shape::SelectionType(AIS_Shape::SelectionMode(TopAbs_WIRE));
174   // POP protection against crash in low layers
175   Standard_Real aDeflection = Prs3d::GetDeflection(theShape, myDrawer);
176   try {
177     StdSelect_BRepSelectionTool::Load(theSelection,
178                                       this,
179                                       theShape,
180                                       TypOfSel,
181                                       aDeflection,
182                                       myDrawer->HLRAngle(),
183                                       myDrawer->IsAutoTriangulation());
184   } catch ( Standard_Failure ) {
185   }
186 }
187
188 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
189                                            const SelectMgr_SequenceOfOwner& theOwners)
190 {
191   Handle(SelectMgr_EntityOwner) anOwner;
192   Handle(ModuleBase_BRepOwner) aCompOwner;
193   for (int i = 1; i <= theOwners.Length(); i++) {
194     anOwner = theOwners.Value(i);
195     aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
196     if (aCompOwner.IsNull()) {
197       thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
198     }
199     else {
200       TopoDS_Shape aShape = aCompOwner->Shape();
201       Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
202       aSelectionPrs->Clear();
203
204       StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
205
206       aSelectionPrs->SetDisplayPriority(9);
207       aSelectionPrs->Highlight(GetContext()->SelectionStyle());
208       aSelectionPrs->Display();
209       thePM->Color(this, GetContext()->SelectionStyle());
210     }
211   }
212 }
213
214 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
215                                                  const Handle(Graphic3d_HighlightStyle)& theStyle,
216                                                  const Handle(SelectMgr_EntityOwner)& theOwner)
217 {
218   Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
219   if (aOwner.IsNull())
220     return;
221
222   TopoDS_Shape aShape = aOwner->Shape();
223   if (!aShape.IsNull()) {
224     thePM->Color(this, theStyle);
225
226     Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
227     aHilightPrs->Clear();
228
229     StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
230     aHilightPrs->Highlight(theStyle);
231
232     if (thePM->IsImmediateModeOn())
233       thePM->AddToImmediateList(aHilightPrs);
234   }
235 }