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