]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_ResultPrs.cpp
Salome HOME
#2309 Possibility to hide faces : linux compilation correction
[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
23 #include <GeomAPI_PlanarEdges.h>
24
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_Tools.h>
27 #include <ModelAPI_ResultConstruction.h>
28 #include <ModelAPI_ResultCompSolid.h>
29
30 #include "ModuleBase_Tools.h"
31 #include "ModuleBase_BRepOwner.h"
32
33 #include <Events_InfoMessage.h>
34 #include <Events_Loop.h>
35
36 #include <BRep_Builder.hxx>
37 #include <Prs3d_Drawer.hxx>
38 #include <Prs3d.hxx>
39 #include <Prs3d_PointAspect.hxx>
40 #include <Prs3d_IsoAspect.hxx>
41 #include <TopoDS_Builder.hxx>
42 #include <TopoDS.hxx>
43 #include <SelectMgr_SequenceOfOwner.hxx>
44 #include <SelectMgr_EntityOwner.hxx>
45 #include <SelectMgr_SelectionManager.hxx>
46 #include <StdPrs_WFShape.hxx>
47 #include <StdSelect_BRepSelectionTool.hxx>
48 #include <AIS_InteractiveContext.hxx>
49 #include <AIS_Selection.hxx>
50 #include <TColStd_ListIteratorOfListOfInteger.hxx>
51 #include <Graphic3d_AspectMarker3d.hxx>
52 #include <TopExp_Explorer.hxx>
53
54 //*******************************************************************************************
55
56 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
57
58
59
60 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
61   : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult), myAdditionalSelectionPriority(0)
62 {
63   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
64   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
65   Set(aShape);
66   Handle(Prs3d_Drawer) aDrawer = Attributes();
67   if (aDrawer->HasOwnPointAspect())
68     aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
69   else
70     aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
71
72   // Activate individual repaintng if this is a part of compsolid
73   ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
74   SetAutoHilight(aCompSolid.get() == NULL);
75
76   ModuleBase_Tools::setPointBallHighlighting(this);
77 }
78
79 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
80 {
81   myAdditionalSelectionPriority = thePriority;
82 }
83
84 bool ModuleBase_ResultPrs::setSubShapeHidden(const NCollection_List<TopoDS_Shape>& theShapes)
85 {
86   bool isModified = false;
87
88   // restore hidden shapes if there are not the shapes in parameter container
89   NCollection_List<TopoDS_Shape> aVisibleSubShapes;
90   for (NCollection_List<TopoDS_Shape>::Iterator aHiddenIt(myHiddenSubShapes); aHiddenIt.More();
91        aHiddenIt.Next()) {
92     if (!theShapes.Contains(aHiddenIt.Value()))
93       aVisibleSubShapes.Append(aHiddenIt.Value());
94   }
95   isModified = !aVisibleSubShapes.IsEmpty();
96   for (NCollection_List<TopoDS_Shape>::Iterator aVisibleIt(aVisibleSubShapes); aVisibleIt.More();
97        aVisibleIt.Next())
98     myHiddenSubShapes.Remove(aVisibleIt.Value());
99
100   // append hidden shapes into internal container if there are not these shapes
101   for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt(theShapes); aShapeIt.More();
102     aShapeIt.Next())
103   {
104     if (aShapeIt.Value().ShapeType() != TopAbs_FACE) // only face shape can be hidden
105       continue;
106
107     if (!myHiddenSubShapes.Contains(aShapeIt.Value())) {
108       myHiddenSubShapes.Append(aShapeIt.Value());
109       isModified = true;
110     }
111   }
112   return isModified;
113 }
114
115 bool ModuleBase_ResultPrs::hasSubShapeVisible(const TopoDS_Shape& theShape)
116 {
117   int aNbOfHiddenSubShapes = myHiddenSubShapes.Size();
118
119   if (!myHiddenSubShapes.Contains(theShape))
120     aNbOfHiddenSubShapes++; // the shape to be hidden later
121
122   TopExp_Explorer anExp(myOriginalShape, TopAbs_FACE);
123   bool aHasVisibleShape = false;
124   for(TopExp_Explorer anExp(myOriginalShape, TopAbs_FACE); anExp.More() && !aHasVisibleShape;
125       anExp.Next())
126   {
127     aNbOfHiddenSubShapes--;
128     if (aNbOfHiddenSubShapes < 0)
129       aHasVisibleShape = true;
130   }
131   return aHasVisibleShape;
132 }
133
134 void ModuleBase_ResultPrs::Compute(
135           const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
136           const Handle(Prs3d_Presentation)& thePresentation,
137           const Standard_Integer theMode)
138 {
139   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
140   bool aReadyToDisplay = aShapePtr.get();
141   if (aReadyToDisplay) {
142     myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
143     if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
144       if (!myOriginalShape.IsNull())
145         Set(myOriginalShape);
146     }
147     else { // convert shape into SHELL
148       TopoDS_Shell aShell;
149       BRep_Builder aShellBuilder;
150       aShellBuilder.MakeShell(aShell);
151       bool isEmptyShape = true;
152       for(TopExp_Explorer anExp(myOriginalShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
153         if (myHiddenSubShapes.Contains(anExp.Current()))
154           continue;
155         aShellBuilder.Add(aShell, anExp.Current());
156         isEmptyShape = false;
157       }
158       Set(aShell);
159       if (isEmptyShape)
160         aReadyToDisplay = false;
161     }
162   }
163   // change deviation coefficient to provide more precise circle
164   //ModuleBase_Tools::setDefaultDeviationCoefficient(myResult, Attributes());
165   AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
166
167   if (!aReadyToDisplay) {
168     Events_InfoMessage("ModuleBase_ResultPrs",
169                        "An empty AIS presentation: ModuleBase_ResultPrs").send();
170     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
171     ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
172   }
173 }
174
175 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
176                                             const Standard_Integer theMode)
177 {
178   if (appendVertexSelection(aSelection, theMode))
179     return;
180
181   if (theMode > TopAbs_SHAPE) {
182     // In order to avoid using custom selection modes
183     if (theMode == ModuleBase_ResultPrs::Sel_Result) {
184       AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
185     }
186     return;
187   }
188
189   // TODO: OCCT issue should be created for the COMPOUND processing
190   // before it is fixed, the next workaround in necessary
191   if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
192     const TopoDS_Shape& aShape = Shape();
193     TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
194     // do not activate in compound mode shapes which do not contain compounds
195     if (!aCompExp.More())
196       return;
197   }
198
199   if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
200     // Limit selection area only by actual object (Shape)
201     ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
202     if (aCompSolid.get()) {
203       std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
204       if (aShapePtr.get()) {
205         TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
206         int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
207         /// It is important to have priority for the shape of comp solid result less than priority
208         /// for the presentation shape which is a sub-result.
209         /// Reason is to select the sub-objects before: #1592
210         aPriority = aPriority - 1;
211         double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
212
213         Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
214         StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
215           aDeflection, myDrawer->HLRAngle(), 9, 500);
216
217         for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
218           Handle(SelectMgr_EntityOwner) anOwner =
219             Handle(SelectMgr_EntityOwner)
220             ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
221           anOwner->Set(this);
222         }
223         return;
224       }
225     }
226     //AIS_Shape::ComputeSelection(aSelection, 0);
227   }
228   AIS_Shape::ComputeSelection(aSelection, theMode);
229
230   if (myAdditionalSelectionPriority > 0) {
231     for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
232       Handle(SelectBasics_EntityOwner) aBasicsOwner =
233         aSelection->Sensitive()->BaseSensitive()->OwnerId();
234       if (!aBasicsOwner.IsNull())
235         aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
236     }
237   }
238 }
239
240 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
241                                                  const Standard_Integer theMode)
242 {
243   if (Shape().ShapeType() == TopAbs_VERTEX) {
244     const TopoDS_Shape& aShape = Shape();
245
246     int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
247     double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
248
249     /// The cause of this method is the last parameter of BRep owner setting into True.
250     /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
251     /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
252     Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
253     StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
254                                                   aDeflection, myDrawer->HLRAngle(), 9, 500);
255
256     for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
257       Handle(SelectMgr_EntityOwner) anOwner =
258         Handle(SelectMgr_EntityOwner)
259         ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
260       anOwner->Set(this);
261     }
262     return true;
263   }
264   return false;
265 }
266
267 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
268                                            const SelectMgr_SequenceOfOwner& theOwners)
269 {
270   Handle(SelectMgr_EntityOwner) anOwner;
271   Handle(ModuleBase_BRepOwner) aCompOwner;
272   for (int i = 1; i <= theOwners.Length(); i++) {
273     anOwner = theOwners.Value(i);
274     aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
275     if (aCompOwner.IsNull()) {
276       thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
277     }
278     else {
279       TopoDS_Shape aShape = aCompOwner->Shape();
280       Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
281       aSelectionPrs->Clear();
282
283       StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
284
285       aSelectionPrs->SetDisplayPriority(9);
286       aSelectionPrs->Highlight(GetContext()->SelectionStyle());
287       aSelectionPrs->Display();
288       thePM->Color(this, GetContext()->SelectionStyle());
289     }
290   }
291 }
292
293 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
294                                                  const Handle(Graphic3d_HighlightStyle)& theStyle,
295                                                  const Handle(SelectMgr_EntityOwner)& theOwner)
296 {
297   Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
298   if (aOwner.IsNull())
299     return;
300
301   TopoDS_Shape aShape = aOwner->Shape();
302   if (!aShape.IsNull()) {
303     thePM->Color(this, theStyle);
304
305     Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
306     aHilightPrs->Clear();
307
308     StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
309     aHilightPrs->Highlight(theStyle);
310
311     if (thePM->IsImmediateModeOn())
312       thePM->AddToImmediateList(aHilightPrs);
313   }
314 }