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