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