Salome HOME
Merge branch 'Dev_GroupsRevision'
[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   NCollection_List<TopoDS_Shape> aShapesToSkip;
150   aShapesToSkip.Append(myHiddenSubShapes);
151   for (NCollection_List<TopoDS_Shape>::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
152     aShapesToSkip.Append(anIt.Value());
153
154   collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
155   return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
156 }
157
158 //********************************************************************
159 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
160 {
161   if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
162     return false;
163
164   myTransparency = theTransparency;
165   myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
166   return true;
167 }
168
169 //********************************************************************
170 void ModuleBase_ResultPrs::Compute(
171           const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
172           const Handle(Prs3d_Presentation)& thePresentation,
173           const Standard_Integer theMode)
174 {
175   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
176   bool aReadyToDisplay = aShapePtr.get();
177   if (aReadyToDisplay) {
178     myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
179     if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
180       if (!myOriginalShape.IsNull())
181         Set(myOriginalShape);
182     }
183     else { // convert shape into SHELL
184       TopoDS_Compound aCompound;
185       BRep_Builder aBuilder;
186       aBuilder.MakeCompound (aCompound);
187       collectSubShapes(aBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
188       bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
189       Set(aCompound);
190       if (isEmptyShape)
191         aReadyToDisplay = false;
192     }
193   }
194   // change deviation coefficient to provide more precise circle
195   //ModuleBase_Tools::setDefaultDeviationCoefficient(myResult, Attributes());
196   AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
197
198   // visualize hidden sub-shapes transparent
199   if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
200   {
201     StdPrs_ShadedShape::Add (thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
202     aReadyToDisplay = true;
203   }
204
205   if (!aReadyToDisplay) {
206     Events_InfoMessage("ModuleBase_ResultPrs",
207                        "An empty AIS presentation: ModuleBase_ResultPrs").send();
208     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
209     ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
210   }
211 }
212
213 //********************************************************************
214 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
215   TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
216   const NCollection_List<TopoDS_Shape>& theHiddenSubShapes)
217 {
218   switch (theShape.ShapeType()) {
219     case TopAbs_COMPOUND: {
220       for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
221         collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
222     }
223     break;
224     case TopAbs_SOLID:
225     case TopAbs_SHELL: {
226       for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
227         collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
228       }
229     }
230     break;
231     case TopAbs_WIRE: {
232       for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
233         collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
234       }
235     }
236     break;
237     case TopAbs_FACE: {
238       if (theHiddenSubShapes.Contains(theShape))
239         return; // remove hidden shape
240       theBuilder.Add(theCompound, theShape);
241     }
242     break;
243     case TopAbs_EDGE:
244     case TopAbs_VERTEX: {
245       theBuilder.Add(theCompound, theShape);
246     }
247     default:
248       break;
249   }
250 }
251
252 //********************************************************************
253 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
254                                             const Standard_Integer theMode)
255 {
256   if (appendVertexSelection(aSelection, theMode))
257     return;
258
259   if (theMode > TopAbs_SHAPE) {
260     // In order to avoid using custom selection modes
261     if (theMode == ModuleBase_ResultPrs::Sel_Result) {
262       AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
263     }
264     return;
265   }
266
267   // TODO: OCCT issue should be created for the COMPOUND processing
268   // before it is fixed, the next workaround in necessary
269   if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
270     const TopoDS_Shape& aShape = Shape();
271     TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
272     // do not activate in compound mode shapes which do not contain compounds
273     if (!aCompExp.More())
274       return;
275   }
276
277   if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
278     // Limit selection area only by actual object (Shape)
279     ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
280     if (aCompSolid.get()) {
281       std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
282       if (aShapePtr.get()) {
283         TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
284         int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
285         /// It is important to have priority for the shape of comp solid result less than priority
286         /// for the presentation shape which is a sub-result.
287         /// Reason is to select the sub-objects before: #1592
288         aPriority = aPriority - 1;
289         double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
290
291         Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
292         StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
293           aDeflection, myDrawer->HLRAngle(), 9, 500);
294
295         for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
296           Handle(SelectMgr_EntityOwner) anOwner =
297             Handle(SelectMgr_EntityOwner)
298             ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
299           anOwner->Set(this);
300         }
301         return;
302       }
303     }
304     //AIS_Shape::ComputeSelection(aSelection, 0);
305   }
306   AIS_Shape::ComputeSelection(aSelection, theMode);
307
308   if (myAdditionalSelectionPriority > 0) {
309     for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
310       Handle(SelectBasics_EntityOwner) aBasicsOwner =
311         aSelection->Sensitive()->BaseSensitive()->OwnerId();
312       if (!aBasicsOwner.IsNull())
313         aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
314     }
315   }
316 }
317
318 //********************************************************************
319 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
320                                                  const Standard_Integer theMode)
321 {
322   if (Shape().ShapeType() == TopAbs_VERTEX) {
323     const TopoDS_Shape& aShape = Shape();
324
325     int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
326     double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
327
328     /// The cause of this method is the last parameter of BRep owner setting into True.
329     /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
330     /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
331     Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
332     StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
333                                                   aDeflection, myDrawer->HLRAngle(), 9, 500);
334
335     for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
336       Handle(SelectMgr_EntityOwner) anOwner =
337         Handle(SelectMgr_EntityOwner)
338         ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
339       anOwner->Set(this);
340     }
341     return true;
342   }
343   return false;
344 }
345
346 //********************************************************************
347 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
348                                            const SelectMgr_SequenceOfOwner& theOwners)
349 {
350   Handle(SelectMgr_EntityOwner) anOwner;
351   Handle(ModuleBase_BRepOwner) aCompOwner;
352   for (int i = 1; i <= theOwners.Length(); i++) {
353     anOwner = theOwners.Value(i);
354     aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
355     if (aCompOwner.IsNull()) {
356       thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
357     }
358     else {
359       TopoDS_Shape aShape = aCompOwner->Shape();
360       Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
361       aSelectionPrs->Clear();
362
363       StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
364
365       aSelectionPrs->SetDisplayPriority(9);
366       aSelectionPrs->Highlight(GetContext()->SelectionStyle());
367       aSelectionPrs->Display();
368       thePM->Color(this, GetContext()->SelectionStyle());
369     }
370   }
371 }
372
373 //********************************************************************
374 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
375                                                  const Handle(Graphic3d_HighlightStyle)& theStyle,
376                                                  const Handle(SelectMgr_EntityOwner)& theOwner)
377 {
378   Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
379   if (aOwner.IsNull())
380     return;
381
382   TopoDS_Shape aShape = aOwner->Shape();
383   if (!aShape.IsNull()) {
384     thePM->Color(this, theStyle);
385
386     Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
387     aHilightPrs->Clear();
388
389     StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
390     aHilightPrs->Highlight(theStyle);
391
392     if (thePM->IsImmediateModeOn())
393       thePM->AddToImmediateList(aHilightPrs);
394   }
395 }