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