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