]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_ResultPrs.cpp
Salome HOME
Set highlighting without ISO lines
[modules/shaper.git] / src / ModuleBase / ModuleBase_ResultPrs.cpp
1 // Copyright (C) 2014-2019  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
18 //
19
20 #include "ModuleBase_ResultPrs.h"
21 #include "ModuleBase_IViewer.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_ResultBody.h>
29 #include <ModelAPI_AttributeIntArray.h>
30
31 #include "ModuleBase_Tools.h"
32 #include "ModuleBase_BRepOwner.h"
33
34 #include <Events_InfoMessage.h>
35 #include <Events_Loop.h>
36
37 #include <AIS_ColoredDrawer.hxx>
38 #include <AIS_InteractiveContext.hxx>
39 #include <AIS_Selection.hxx>
40 #include <BOPTools_AlgoTools3D.hxx>
41 #include <BRep_Builder.hxx>
42 #include <Graphic3d_AspectMarker3d.hxx>
43 #include <Prs3d_Drawer.hxx>
44 #include <Prs3d.hxx>
45 #include <Prs3d_PointAspect.hxx>
46 #include <Prs3d_IsoAspect.hxx>
47 #include <Prs3d_ShadingAspect.hxx>
48 #include <SelectMgr_SequenceOfOwner.hxx>
49 #include <SelectMgr_EntityOwner.hxx>
50 #include <SelectMgr_SelectionManager.hxx>
51 #include <StdPrs_WFShape.hxx>
52 #include <StdPrs_ShadedShape.hxx>
53 #include <StdSelect_BRepSelectionTool.hxx>
54 #include <TColStd_ListIteratorOfListOfInteger.hxx>
55 #include <TopExp_Explorer.hxx>
56 #include <TopoDS.hxx>
57 #include <TopoDS_Builder.hxx>
58
59 //*******************************************************************************************
60
61 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
62
63
64
65 //********************************************************************
66 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
67   : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult), myAdditionalSelectionPriority(0),
68   myTransparency(1), myIsSubstituted(false)
69 {
70   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
71   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
72   Set(aShape);
73
74   // Activate individual repaintng if this is a part of compsolid
75   ResultBodyPtr aResOwner = ModelAPI_Tools::bodyOwner(myResult);
76   SetAutoHilight(aResOwner.get() == NULL);
77
78   // Set own free boundaries aspect in order to have free
79   // and unfree boundaries with different colors
80   Handle(Prs3d_Drawer) aDrawer = Attributes();
81   Handle(Prs3d_LineAspect) aFreeBndAspect =
82     new Prs3d_LineAspect(Quantity_NOC_GREEN, Aspect_TOL_SOLID, 1);
83   aDrawer->SetFreeBoundaryAspect(aFreeBndAspect);
84
85   aDrawer->VIsoAspect()->SetNumber(0);
86   aDrawer->UIsoAspect()->SetNumber(0);
87
88   if (aDrawer->HasOwnPointAspect())
89     aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
90   else
91     aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
92
93   aDrawer = DynamicHilightAttributes();
94   if (aDrawer.IsNull()) {
95     if (!ModuleBase_IViewer::DefaultHighlightDrawer.IsNull()) {
96       aDrawer = new Prs3d_Drawer(*ModuleBase_IViewer::DefaultHighlightDrawer);
97       aDrawer->VIsoAspect()->SetNumber(0);
98       aDrawer->UIsoAspect()->SetNumber(0);
99       SetDynamicHilightAttributes(aDrawer);
100     }
101   } else {
102     aDrawer->VIsoAspect()->SetNumber(0);
103     aDrawer->UIsoAspect()->SetNumber(0);
104   }
105   myHiddenSubShapesDrawer = new AIS_ColoredDrawer(myDrawer);
106   Handle(Prs3d_ShadingAspect) aShadingAspect = new Prs3d_ShadingAspect();
107   aShadingAspect->SetMaterial(Graphic3d_NOM_BRASS); //default value of context material
108   myHiddenSubShapesDrawer->SetShadingAspect(aShadingAspect);
109
110   ModuleBase_Tools::setPointBallHighlighting(this);
111
112   // Define colors for wireframe mode
113   setEdgesDefaultColor();
114 }
115
116 //********************************************************************
117 void ModuleBase_ResultPrs::setAdditionalSelectionPriority(const int thePriority)
118 {
119   myAdditionalSelectionPriority = thePriority;
120 }
121
122 //********************************************************************
123 void ModuleBase_ResultPrs::SetColor (const Quantity_Color& theColor)
124 {
125   ViewerData_AISShape::SetColor(theColor);
126   myHiddenSubShapesDrawer->ShadingAspect()->SetColor (theColor, myCurrentFacingModel);
127   setEdgesDefaultColor();
128 }
129
130 void ModuleBase_ResultPrs::setEdgesDefaultColor()
131 {
132   if (myResult.get()) {
133     AttributeIntArrayPtr aColorAttr = myResult->data()->intArray(ModelAPI_Result::COLOR_ID());
134     bool aHasColor = aColorAttr.get() && aColorAttr->isInitialized();
135
136     if (!aHasColor) {
137       Handle(Prs3d_Drawer) aDrawer = Attributes();
138       Handle(Prs3d_LineAspect) anAspect; // = aDrawer->LineAspect();
139       //anAspect->SetColor(Quantity_NOC_YELLOW);
140       //aDrawer->SetLineAspect(anAspect);
141
142       // - unfree boundaries color
143       anAspect = aDrawer->UnFreeBoundaryAspect();
144       anAspect->SetColor(Quantity_NOC_YELLOW);
145       aDrawer->SetUnFreeBoundaryAspect(anAspect);
146       aDrawer->SetUnFreeBoundaryDraw(true);
147
148       // - free boundaries color
149       anAspect = aDrawer->FreeBoundaryAspect();
150       anAspect->SetColor(Quantity_NOC_GREEN);
151       aDrawer->SetFreeBoundaryAspect(anAspect);
152       aDrawer->SetFreeBoundaryDraw(true);
153
154       // - standalone edges color
155       anAspect = aDrawer->WireAspect();
156       anAspect->SetColor(Quantity_NOC_RED);
157       aDrawer->SetWireAspect(anAspect);
158     }
159   }
160 }
161
162
163 //********************************************************************
164 void ModuleBase_ResultPrs::setSubShapeHidden(const TopoDS_ListOfShape& theShapes)
165 {
166   bool isModified = false;
167
168   TopoDS_Compound aCompound;
169   BRep_Builder aBBuilder;
170   aBBuilder.MakeCompound (aCompound);
171
172   myHiddenSubShapes = theShapes;
173   collectSubShapes(aBBuilder, aCompound, myOriginalShape, myHiddenSubShapes);
174   myVisibleCompound = aCompound;
175
176   aBBuilder.MakeCompound (aCompound);
177   TopoDS_ListOfShape::Iterator aIt(myHiddenSubShapes);
178   for (; aIt.More(); aIt.Next()) {
179     aBBuilder.Add(aCompound, aIt.Value());
180   }
181   myHiddenCompound = aCompound;
182 }
183
184 //********************************************************************
185 bool ModuleBase_ResultPrs::isSubShapeHidden(const TopoDS_Shape& theShape)
186 {
187   if (theShape.IsNull() || theShape.ShapeType() != TopAbs_FACE) // only face shape can be hidden
188     return false;
189
190   // orientation of parameter shape(come from selection) may be wrong, check isEqual() to be sure
191   TopoDS_ListOfShape::Iterator aShapeIt(myHiddenSubShapes);
192   for (; aShapeIt.More(); aShapeIt.Next()) {
193     if (theShape.IsSame(aShapeIt.Value()))
194       return true;
195   }
196
197   return true;
198 }
199
200 //********************************************************************
201 bool ModuleBase_ResultPrs::hasSubShapeVisible(
202   const TopoDS_ListOfShape& theShapesToSkip)
203 {
204   TopoDS_Compound aCompound;
205   BRep_Builder aBuilder;
206   aBuilder.MakeCompound (aCompound);
207   TopoDS_ListOfShape aShapesToSkip;
208   TopoDS_ListOfShape aHiddenCopy(myHiddenSubShapes);
209   aShapesToSkip.Append(aHiddenCopy);
210   for (TopoDS_ListOfShape::Iterator anIt(theShapesToSkip); anIt.More(); anIt.Next())
211     aShapesToSkip.Append(anIt.Value());
212
213   collectSubShapes(aBuilder, aCompound, myOriginalShape, aShapesToSkip);
214   return !BOPTools_AlgoTools3D::IsEmptyShape(aCompound);
215 }
216
217 //********************************************************************
218 bool ModuleBase_ResultPrs::setHiddenSubShapeTransparency(double theTransparency)
219 {
220   if (myTransparency == theTransparency || theTransparency > 1 || theTransparency < 0)
221     return false;
222
223   myTransparency = theTransparency;
224   myHiddenSubShapesDrawer->ShadingAspect()->SetTransparency (theTransparency, myCurrentFacingModel);
225   return true;
226 }
227
228 //********************************************************************
229 void ModuleBase_ResultPrs::Compute(
230           const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
231           const Handle(Prs3d_Presentation)& thePresentation,
232           const Standard_Integer theMode)
233 {
234   std::shared_ptr<GeomAPI_Shape> aShapePtr = myResult->shape();
235   bool aReadyToDisplay = aShapePtr.get();
236   if (aReadyToDisplay) {
237     myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
238     if (myHiddenSubShapes.IsEmpty() || myOriginalShape.ShapeType() > TopAbs_FACE ) {
239       if (!myOriginalShape.IsNull()) {
240         Set(myOriginalShape);
241         myIsSubstituted = false;
242       }
243     }
244     else { // convert shape into SHELL
245       bool isEmptyShape = BOPTools_AlgoTools3D::IsEmptyShape(myVisibleCompound);
246       Set(myVisibleCompound);
247       myIsSubstituted = true;
248       if (isEmptyShape)
249         aReadyToDisplay = false;
250     }
251   }
252   try {
253     AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
254   }
255   catch (...) {
256     return;
257   }
258
259   // visualize hidden sub-shapes transparent
260   if (myResult.get()) {
261     if (myTransparency < 1 && !myHiddenSubShapes.IsEmpty())
262     {
263       StdPrs_ShadedShape::Add(thePresentation, myHiddenCompound, myHiddenSubShapesDrawer);
264       aReadyToDisplay = true;
265     }
266
267     if (!aReadyToDisplay) {
268       Events_InfoMessage("ModuleBase_ResultPrs",
269         "An empty AIS presentation: ModuleBase_ResultPrs").send();
270       static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
271       ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
272     }
273   }
274 }
275
276 //********************************************************************
277 void ModuleBase_ResultPrs::collectSubShapes(BRep_Builder& theBuilder,
278   TopoDS_Shape& theCompound, const TopoDS_Shape& theShape,
279   const TopoDS_ListOfShape& theHiddenSubShapes)
280 {
281   switch (theShape.ShapeType()) {
282     case TopAbs_COMPSOLID:
283     case TopAbs_COMPOUND: {
284       for (TopoDS_Iterator aChildIter (theShape); aChildIter.More(); aChildIter.Next())
285         collectSubShapes(theBuilder, theCompound, aChildIter.Value(), theHiddenSubShapes);
286     }
287     break;
288     case TopAbs_SOLID:
289     case TopAbs_SHELL: {
290       for (TopExp_Explorer anExp (theShape, TopAbs_FACE); anExp.More(); anExp.Next()) {
291         collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
292       }
293     }
294     break;
295     case TopAbs_WIRE: {
296       for (TopExp_Explorer anExp (theShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
297         collectSubShapes(theBuilder, theCompound, anExp.Current(), theHiddenSubShapes);
298       }
299     }
300     break;
301     case TopAbs_FACE: {
302       if (theHiddenSubShapes.Contains(theShape))
303         return; // remove hidden shape
304       theBuilder.Add(theCompound, theShape);
305     }
306     break;
307     case TopAbs_EDGE:
308     case TopAbs_VERTEX: {
309       theBuilder.Add(theCompound, theShape);
310     }
311     default:
312       break;
313   }
314 }
315
316 //********************************************************************
317 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
318                                             const Standard_Integer theMode)
319 {
320   if (appendVertexSelection(aSelection, theMode))
321     return;
322
323   if (theMode > TopAbs_SHAPE) {
324     // In order to avoid using custom selection modes
325     if (theMode == ModuleBase_ResultPrs::Sel_Result) {
326       AIS_Shape::ComputeSelection(aSelection, TopAbs_COMPOUND);
327     }
328     return;
329   }
330
331   // TODO: OCCT issue should be created for the COMPOUND processing
332   // before it is fixed, the next workaround in necessary
333   if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPOUND)) {
334     const TopoDS_Shape& aShape = Shape();
335     TopExp_Explorer aCompExp(aShape, TopAbs_COMPOUND);
336     // do not activate in compound mode shapes which do not contain compounds
337     if (!aCompExp.More())
338       return;
339   }
340
341   // bug 2110: if (theMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
342   //  // Limit selection area only by actual object (Shape)
343   //  ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
344   //  if (aCompSolid.get()) {
345   //    std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
346   //    if (aShapePtr.get()) {
347   //      TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
348   //   int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_COMPSOLID);
349   //     /// It is important to have priority for the shape of comp solid result less than priority
350   //      /// for the presentation shape which is a sub-result.
351   //      /// Reason is to select the sub-objects before: #1592
352   //      aPriority = aPriority - 1;
353   //      double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
354
355   //      Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
356   //      StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
357   //        aDeflection, myDrawer->HLRAngle(), 9, 500);
358
359   //      for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
360   //        Handle(SelectMgr_EntityOwner) anOwner =
361   //          Handle(SelectMgr_EntityOwner)
362   //          ::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
363   //        anOwner->Set(this);
364   //      }
365   //      return;
366   //    }
367   //  }
368   //}
369   AIS_Shape::ComputeSelection(aSelection, theMode);
370
371   if (myAdditionalSelectionPriority > 0) {
372     NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
373     for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
374          anIt.More();
375          anIt.Next()) {
376       Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
377       Handle(SelectBasics_EntityOwner) aBasicsOwner = anEntity->BaseSensitive()->OwnerId();
378       if (!aBasicsOwner.IsNull())
379         aBasicsOwner->Set(aBasicsOwner->Priority() + myAdditionalSelectionPriority);
380     }
381   }
382 }
383
384 //********************************************************************
385 bool ModuleBase_ResultPrs::appendVertexSelection(const Handle(SelectMgr_Selection)& aSelection,
386                                                  const Standard_Integer theMode)
387 {
388   if (Shape().ShapeType() == TopAbs_VERTEX) {
389     const TopoDS_Shape& aShape = Shape();
390
391     int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_VERTEX);
392     double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
393
394     /// The cause of this method is the last parameter of BRep owner setting into True.
395     /// That means that owner should behave like it comes from decomposition. (In this case, OCCT
396     /// visualizes it in Ring style) OCCT version is 7.0.0 with path for SHAPER module.
397     Handle(StdSelect_BRepOwner) aOwner = new StdSelect_BRepOwner(aShape, aPriority, Standard_True);
398     StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection,
399                                                   aDeflection, myDrawer->HLRAngle(), 9, 500);
400
401
402     NCollection_Vector<Handle(SelectMgr_SensitiveEntity)> anEntities = aSelection->Entities();
403     for (NCollection_Vector<Handle(SelectMgr_SensitiveEntity)>::Iterator anIt(anEntities);
404          anIt.More();
405          anIt.Next()) {
406       Handle(SelectMgr_SensitiveEntity) anEntity = anIt.Value();
407       Handle(SelectMgr_EntityOwner) anOwner =
408         Handle(SelectMgr_EntityOwner)
409         ::DownCast(anEntity->BaseSensitive()->OwnerId());
410       anOwner->Set(this);
411     }
412
413     return true;
414   }
415   return false;
416 }
417
418 //********************************************************************
419 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
420                                            const SelectMgr_SequenceOfOwner& theOwners)
421 {
422   Handle(SelectMgr_EntityOwner) anOwner;
423   Handle(ModuleBase_BRepOwner) aCompOwner;
424   for (int i = 1; i <= theOwners.Length(); i++) {
425     anOwner = theOwners.Value(i);
426     aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
427     if (aCompOwner.IsNull()) {
428       thePM->Color(anOwner->Selectable(), GetContext()->SelectionStyle());
429     }
430     else {
431       TopoDS_Shape aShape = aCompOwner->Shape();
432       Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
433       aSelectionPrs->Clear();
434
435       StdPrs_WFShape::Add(aSelectionPrs, aShape, myDrawer);
436
437       aSelectionPrs->SetDisplayPriority(9);
438       aSelectionPrs->Highlight(GetContext()->SelectionStyle());
439       aSelectionPrs->Display();
440       thePM->Color(this, GetContext()->SelectionStyle());
441     }
442   }
443 }
444
445 //********************************************************************
446 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
447                                                  const Handle(Prs3d_Drawer)& theStyle,
448                                                  const Handle(SelectMgr_EntityOwner)& theOwner)
449 {
450   Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
451   if (aOwner.IsNull())
452     return;
453
454   TopoDS_Shape aShape = aOwner->Shape();
455   if (!aShape.IsNull()) {
456     thePM->Color(this, theStyle);
457
458     Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
459     aHilightPrs->Clear();
460
461     StdPrs_WFShape::Add(aHilightPrs, aShape, myDrawer);
462     aHilightPrs->Highlight(theStyle);
463
464     if (thePM->IsImmediateModeOn())
465       thePM->AddToImmediateList(aHilightPrs);
466   }
467 }