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