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