Salome HOME
e690ce0e533785b09e08867c135498d3eb187e72
[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 theMode)
109 {
110   if (appendVertexSelection(aSelection, theMode))
111     return;
112
113   if (theMode > TopAbs_SHAPE) {
114     // In order to avoid using custom selection modes
115     if (theMode == ModuleBase_ResultPrs::Sel_Result) {
116       AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
117     }
118     return;
119   }
120
121   // TODO: OCCT issue should be created for the COMPOUND processing
122   // before it is fixed, the next workaround in necessary
123   if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
124     const TopoDS_Shape& aShape = Shape();
125     TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
126     // do not activate in compound mode shapes which do not contain compounds
127     if (!aCompExp.More())
128       return;
129   }
130
131   if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
132     // Limit selection area only by actual object (Shape)
133     ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
134     if (aCompSolid.get()) {
135       std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
136       if (aShapePtr.get()) {
137         TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
138         int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
139         /// It is important to have priority for the shape of comp solid result less than priority
140         /// for the presentation shape which is a sub-result.
141         /// Reason is to select the sub-objects before: #1592
142         aPriority = aPriority - 1;
143         double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
144
145         Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
146         StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
147           aDeflection, myDrawer->HLRAngle(), 9, 500);
148
149         for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
150           Handle(SelectMgr_EntityOwner) anOwner =
151             Handle(SelectMgr_EntityOwner)
152             ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
153           anOwner->Set(this);
154         }
155         return;
156       }
157     }
158     //AIS_Shape::ComputeSelection(aSelection, 0);
159   }
160   AIS_Shape::ComputeSelection(aSelection, theMode);
161
162   if (myAdditionalSelectionPriority > 0) {
163     for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
164       Handle(SelectBasics_EntityOwner) aBasicsOwner =
165         aSelection->Sensitive()->BaseSensitive()->OwnerId();
166       if (!aBasicsOwner.IsNull())
167         aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
168     }
169   }
170 }
171
172 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
173                                                  const Standard_Integer theMode)
174 {
175   if (Shape().ShapeType() == TopAbs_VERTEX) {
176     const TopoDS_Shape& aShape = Shape();
177
178     int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
179     double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
180
181     /// The cause of this method is the last parameter of BRep owner setting into True.
182     /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
183     /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
184     Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
185     StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
186                                                   aDeflection, myDrawer->HLRAngle(), 9, 500);
187
188     for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
189       Handle(SelectMgr_EntityOwner) anOwner =
190         Handle(SelectMgr_EntityOwner)
191         ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
192       anOwner->Set(this);
193     }
194     return true;
195   }
196   return false;
197 }
198
199 /* OBSOLETE
200 void ModuleBase_ResultPrs::appendWiresSelection(const Handle(SelectMgr_Selection)& theSelection,
201                                                 const TopoDS_Shape& theShape)
202 {
203   static TopAbs_ShapeEnum TypOfSel
204           = AIS_Shape::SelectionType(AIS_Shape::SelectionMode(TopAbs_WIRE));
205   // POP protection against crash in low layers
206   Standard_Real aDeflection = Prs3d::GetDeflection(theShape, myDrawer);
207   try {
208     StdSelect_BRepSelectionTool::Load(theSelection,
209                                       this,
210                                       theShape,
211                                       TypOfSel,
212                                       aDeflection,
213                                       myDrawer->HLRAngle(),
214                                       myDrawer->IsAutoTriangulation());
215   } catch ( Standard_Failure ) {
216   }
217 }*/
218
219 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
220                                            const SelectMgr_SequenceOfOwner& theOwners)
221 {
222   Handle(SelectMgr_EntityOwner) anOwner;
223   Handle(ModuleBase_BRepOwner) aCompOwner;
224   for (int i = 1; i <= theOwners.Length(); i++) {
225     anOwner = theOwners.Value(i);
226     aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
227     if (aCompOwner.IsNull()) {
228       thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
229     }
230     else {
231       TopoDS_Shape aShape = aCompOwner->Shape();
232       Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
233       aSelectionPrs->Clear();
234
235       StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
236
237       aSelectionPrs->SetDisplayPriority(9);
238       aSelectionPrs->Highlight(GetContext()->SelectionStyle());
239       aSelectionPrs->Display();
240       thePM->Color(this, GetContext()->SelectionStyle());
241     }
242   }
243 }
244
245 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
246                                                  const Handle(Graphic3d_HighlightStyle)& theStyle,
247                                                  const Handle(SelectMgr_EntityOwner)& theOwner)
248 {
249   Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
250   if (aOwner.IsNull())
251     return;
252
253   TopoDS_Shape aShape = aOwner->Shape();
254   if (!aShape.IsNull()) {
255     thePM->Color(this, theStyle);
256
257     Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
258     aHilightPrs->Clear();
259
260     StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
261     aHilightPrs->Highlight(theStyle);
262
263     if (thePM->IsImmediateModeOn())
264       thePM->AddToImmediateList(aHilightPrs);
265   }
266 }