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