]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_ResultPrs.cpp
Salome HOME
Hide faces: checking presentation on having any shapes using a whole container of...
[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 <AIS_ColoredDrawer.hxx>
37 #include <AIS_InteractiveContext.hxx>
38 #include <AIS_Selection.hxx>
39 #include <BOPTools_AlgoTools3D.hxx>
40 #include <BRep_Builder.hxx>
41 #include <Graphic3d_AspectMarker3d.hxx>
42 #include <Prs3d_Drawer.hxx>
43 #include <Prs3d.hxx>
44 #include <Prs3d_PointAspect.hxx>
45 #include <Prs3d_IsoAspect.hxx>
46 #include <Prs3d_ShadingAspect.hxx>
47 #include <SelectMgr_SequenceOfOwner.hxx>
48 #include <SelectMgr_EntityOwner.hxx>
49 #include <SelectMgr_SelectionManager.hxx>
50 #include <StdPrs_WFShape.hxx>
51 #include <StdPrs_ShadedShape.hxx>
52 #include <StdSelect_BRepSelectionTool.hxx>
53 #include <TColStd_ListIteratorOfListOfInteger.hxx>
54 #include <TopExp_Explorer.hxx>
55 #include <TopoDS.hxx>
56 #include <TopoDS_Builder.hxx>
57
58 //*******************************************************************************************
59
60 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
61
62
63
64 //********************************************************************
65 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
66   : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult), myAdditionalSelectionPriority(0),
67   myTransparency(1)
68 {
69   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
70   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
71   Set(aShape);
72   Handle(Prs3d_Drawer) aDrawer = Attributes();
73   if (aDrawer->HasOwnPointAspect())
74     aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
75   else
76     aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
77
78   // Activate individual repaintng if this is a part of compsolid
79   ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
80   SetAutoHilight(aCompSolid.get() == NULL);
81
82   myHiddenSubShapesDrawer = new AIS_ColoredDrawer (myDrawer);
83   Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
84   aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
85   myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
86
87   ModuleBase_Tools::setPointBallHighlighting(this);
88 }
89
90 //********************************************************************
91 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
92 {
93   myAdditionalSelectionPriority = thePriority;
94 }
95
96 //********************************************************************
97 void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
98 {
99   ViewerData_AISShape::SetColor(theColor);
100   myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
101 }
102
103 //********************************************************************
104 bool ModuleBase_ResultPrs::setSubShapeHidden(const NCollection_List<TopoDS_Shape>& theShapes)
105 {
106   bool isModified = false;
107
108   TopoDS_Compound aCompound;
109   BRep_Builder aBBuilder;
110   aBBuilder.MakeCompound (aCompound);
111   // restore hidden shapes if there are not the shapes in parameter container
112   NCollection_List<TopoDS_Shape> aVisibleSubShapes;
113   for (NCollection_List<TopoDS_Shape>::Iterator aHiddenIt(myHiddenSubShapes); aHiddenIt.More();
114        aHiddenIt.Next()) {
115     if (!theShapes.Contains(aHiddenIt.Value()))
116       aVisibleSubShapes.Append(aHiddenIt.Value());
117     else
118       aBBuilder.Add (aCompound, aHiddenIt.Value());
119   }
120   isModified = !aVisibleSubShapes.IsEmpty();
121   for (NCollection_List<TopoDS_Shape>::Iterator aVisibleIt(aVisibleSubShapes); aVisibleIt.More();
122        aVisibleIt.Next())
123     myHiddenSubShapes.Remove(aVisibleIt.Value());
124
125   // append hidden shapes into internal container if there are not these shapes
126   for (NCollection_List<TopoDS_Shape>::Iterator aShapeIt(theShapes); aShapeIt.More();
127     aShapeIt.Next())
128   {
129     if (aShapeIt.Value().ShapeType() != TopAbs_FACE) // only face shape can be hidden
130       continue;
131
132     if (!myHiddenSubShapes.Contains(aShapeIt.Value())) {
133       myHiddenSubShapes.Append(aShapeIt.Value());
134       aBBuilder.Add (aCompound, aShapeIt.Value());
135       isModified = true;
136     }
137   }
138   myHiddenCompound = aCompound;
139   return isModified;
140 }
141
142 //********************************************************************
143 bool ModuleBase_ResultPrs::hasSubShapeVisible(
144   const NCollection_List<TopoDS_Shape>& theShapesToSkip)
145 {
146   TopoDS_Compound aCompound;
147   BRep_Builder aBuilder;
148   aBuilder.MakeCompound (aCompound);
149   collectSubShapes(aBuilder, aCompound, myOriginalShape, theShapesToSkip);
150   return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
151 }
152
153 //********************************************************************
154 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
155 {
156   if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
157     return false;
158
159   myTransparency = theTransparency;
160   myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
161   return true;
162 }
163
164 //********************************************************************
165 void ModuleBase_ResultPrs::Compute(
166           const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
167           const Handle(Prs3d_Presentation)& thePresentation,
168           const Standard_Integer theMode)
169 {
170   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
171   bool aReadyToDisplay = aShapePtr.get();
172   if (aReadyToDisplay) {
173     myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
174     if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
175       if (!myOriginalShape.IsNull())
176         Set(myOriginalShape);
177     }
178     else { // convert shape into SHELL
179       TopoDS_Compound aCompound;
180       BRep_Builder aBuilder;
181       aBuilder.MakeCompound (aCompound);
182       collectSubShapes(aBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
183       bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
184       Set(aCompound);
185       if (isEmptyShape)
186         aReadyToDisplay = false;
187     }
188   }
189   // change deviation coefficient to provide more precise circle
190   //ModuleBase_Tools::setDefaultDeviationCoefficient(myResult, Attributes());
191   AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
192
193   // visualize hidden sub-shapes transparent
194   if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
195   {
196     StdPrs_ShadedShape::Add (thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
197     aReadyToDisplay = true;
198   }
199
200   if (!aReadyToDisplay) {
201     Events_InfoMessage("ModuleBase_ResultPrs",
202                        "An empty AIS presentation: ModuleBase_ResultPrs").send();
203     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
204     ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
205   }
206 }
207
208 //********************************************************************
209 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
210   TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
211   const NCollection_List<TopoDS_Shape>& theHiddenSubShapes)
212 {
213   switch (theShape.ShapeType()) {
214     case TopAbs_COMPOUND: {
215       for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
216         collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
217     }
218     break;
219     case TopAbs_SOLID:
220     case TopAbs_SHELL: {
221       for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
222         collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
223       }
224     }
225     break;
226     case TopAbs_WIRE: {
227       for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
228         collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
229       }
230     }
231     break;
232     case TopAbs_FACE: {
233       if (theHiddenSubShapes.Contains(theShape))
234         return; // remove hidden shape
235       theBuilder.Add(theCompound, theShape);
236     }
237     break;
238     case TopAbs_EDGE:
239     case TopAbs_VERTEX: {
240       theBuilder.Add(theCompound, theShape);
241     }
242     default:
243       break;
244   }
245 }
246
247 //********************************************************************
248 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
249                                             const Standard_Integer theMode)
250 {
251   if (appendVertexSelection(aSelection, theMode))
252     return;
253
254   if (theMode > TopAbs_SHAPE) {
255     // In order to avoid using custom selection modes
256     if (theMode == ModuleBase_ResultPrs::Sel_Result) {
257       AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
258     }
259     return;
260   }
261
262   // TODO: OCCT issue should be created for the COMPOUND processing
263   // before it is fixed, the next workaround in necessary
264   if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
265     const TopoDS_Shape& aShape = Shape();
266     TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
267     // do not activate in compound mode shapes which do not contain compounds
268     if (!aCompExp.More())
269       return;
270   }
271
272   if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
273     // Limit selection area only by actual object (Shape)
274     ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
275     if (aCompSolid.get()) {
276       std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
277       if (aShapePtr.get()) {
278         TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
279         int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
280         /// It is important to have priority for the shape of comp solid result less than priority
281         /// for the presentation shape which is a sub-result.
282         /// Reason is to select the sub-objects before: #1592
283         aPriority = aPriority - 1;
284         double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
285
286         Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
287         StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
288           aDeflection, myDrawer->HLRAngle(), 9, 500);
289
290         for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
291           Handle(SelectMgr_EntityOwner) anOwner =
292             Handle(SelectMgr_EntityOwner)
293             ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
294           anOwner->Set(this);
295         }
296         return;
297       }
298     }
299     //AIS_Shape::ComputeSelection(aSelection, 0);
300   }
301   AIS_Shape::ComputeSelection(aSelection, theMode);
302
303   if (myAdditionalSelectionPriority > 0) {
304     for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
305       Handle(SelectBasics_EntityOwner) aBasicsOwner =
306         aSelection->Sensitive()->BaseSensitive()->OwnerId();
307       if (!aBasicsOwner.IsNull())
308         aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
309     }
310   }
311 }
312
313 //********************************************************************
314 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
315                                                  const Standard_Integer theMode)
316 {
317   if (Shape().ShapeType() == TopAbs_VERTEX) {
318     const TopoDS_Shape& aShape = Shape();
319
320     int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
321     double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
322
323     /// The cause of this method is the last parameter of BRep owner setting into True.
324     /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
325     /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
326     Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
327     StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
328                                                   aDeflection, myDrawer->HLRAngle(), 9, 500);
329
330     for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
331       Handle(SelectMgr_EntityOwner) anOwner =
332         Handle(SelectMgr_EntityOwner)
333         ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
334       anOwner->Set(this);
335     }
336     return true;
337   }
338   return false;
339 }
340
341 //********************************************************************
342 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
343                                            const SelectMgr_SequenceOfOwner& theOwners)
344 {
345   Handle(SelectMgr_EntityOwner) anOwner;
346   Handle(ModuleBase_BRepOwner) aCompOwner;
347   for (int i = 1; i <= theOwners.Length(); i++) {
348     anOwner = theOwners.Value(i);
349     aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
350     if (aCompOwner.IsNull()) {
351       thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
352     }
353     else {
354       TopoDS_Shape aShape = aCompOwner->Shape();
355       Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
356       aSelectionPrs->Clear();
357
358       StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
359
360       aSelectionPrs->SetDisplayPriority(9);
361       aSelectionPrs->Highlight(GetContext()->SelectionStyle());
362       aSelectionPrs->Display();
363       thePM->Color(this, GetContext()->SelectionStyle());
364     }
365   }
366 }
367
368 //********************************************************************
369 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
370                                                  const Handle(Graphic3d_HighlightStyle)& theStyle,
371                                                  const Handle(SelectMgr_EntityOwner)& theOwner)
372 {
373   Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
374   if (aOwner.IsNull())
375     return;
376
377   TopoDS_Shape aShape = aOwner->Shape();
378   if (!aShape.IsNull()) {
379     thePM->Color(this, theStyle);
380
381     Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
382     aHilightPrs->Clear();
383
384     StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
385     aHilightPrs->Highlight(theStyle);
386
387     if (thePM->IsImmediateModeOn())
388       thePM->AddToImmediateList(aHilightPrs);
389   }
390 }