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