]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_ResultPrs.cpp
Salome HOME
#1404 Random crash with Shaper: AIS presentations: operation prs, result sketch prs...
[modules/shaper.git] / src / ModuleBase / ModuleBase_ResultPrs.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_ResultPrs.cpp
4 // Created:     21 October 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "ModuleBase_ResultPrs.h"
8 #include "ModuleBase_Tools.h"
9
10 #include <ModelAPI_Events.h>
11 #include <ModelAPI_Tools.h>
12 #include <ModelAPI_ResultConstruction.h>
13 #include <GeomAPI_PlanarEdges.h>
14
15 #include <Events_Error.h>
16 #include <Events_Loop.h>
17
18 #include <BRep_Builder.hxx>
19 #include <Prs3d_Drawer.hxx>
20 #include <Prs3d.hxx>
21 #include <Prs3d_PointAspect.hxx>
22 #include <Prs3d_IsoAspect.hxx>
23 #include <TopoDS_Builder.hxx>
24 #include <SelectMgr_SequenceOfOwner.hxx>
25 #include <SelectMgr_EntityOwner.hxx>
26 #include <SelectMgr_SelectionManager.hxx>
27 #include <StdPrs_WFDeflectionShape.hxx>
28 #include <StdSelect_BRepSelectionTool.hxx>
29 #include <AIS_InteractiveContext.hxx>
30 #include <AIS_Selection.hxx>
31 #include <TColStd_ListIteratorOfListOfInteger.hxx>
32
33 IMPLEMENT_STANDARD_HANDLE(ModuleBase_BRepOwner, StdSelect_BRepOwner);
34 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_BRepOwner, StdSelect_BRepOwner);
35
36 //*******************************************************************************************
37
38 IMPLEMENT_STANDARD_HANDLE(ModuleBase_ResultPrs, ViewerData_AISShape);
39 IMPLEMENT_STANDARD_RTTIEXT(ModuleBase_ResultPrs, ViewerData_AISShape);
40
41
42
43
44
45 ModuleBase_ResultPrs::ModuleBase_ResultPrs(ResultPtr theResult)
46   : ViewerData_AISShape(TopoDS_Shape()), myResult(theResult)
47 {
48   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
49   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
50   Set(aShape);
51   Handle(Prs3d_Drawer) aDrawer = Attributes();
52   if (aDrawer->HasOwnPointAspect()) 
53     aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
54   else
55     aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
56
57   // Activate individual repaintng if this is a part of compsolid
58   ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
59   SetAutoHilight(aCompSolid.get() == NULL);
60 }
61
62 bool ModuleBase_ResultPrs::isValidShapeType(const TopAbs_ShapeEnum& theBaseType,
63                                             const TopAbs_ShapeEnum& theCheckedType)
64 {
65   bool aValid = theBaseType == theCheckedType;
66   if (!aValid) {
67     // currently this functionality is for all, as we have no separate wire selection mode
68     // lately it should be corrected to have the following check only for sketch presentations
69     aValid = theBaseType == TopAbs_FACE && theCheckedType == TopAbs_WIRE;
70   }
71   return aValid;
72 }
73
74 void ModuleBase_ResultPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
75                                    const Handle(Prs3d_Presentation)& thePresentation, 
76                                    const Standard_Integer theMode)
77 {
78   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
79   if (aShapePtr.get()) {
80     myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
81     if (!myOriginalShape.IsNull())
82       Set(myOriginalShape);
83   }
84   else {
85     Events_Error::throwException("An empty AIS presentation: ModuleBase_ResultPrs");
86     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
87     ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
88   }
89   // change deviation coefficient to provide more precise circle
90   ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), Attributes());
91   AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
92 }
93
94 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
95                                             const Standard_Integer aMode)
96 {
97   if (aMode > 8)
98     // In order to avoid using custom selection modes
99     return;
100
101   if (aMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
102     // Limit selection area only by actual object (Shape)
103     ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
104     if (aCompSolid.get()) {
105       std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
106       if (aShapePtr.get()) {
107         TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
108         int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_SHAPE);
109         double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
110
111         Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
112         StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection, 
113           aDeflection, myDrawer->HLRAngle(), 9, 500);
114
115         for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
116           Handle(SelectMgr_EntityOwner) anOwner =
117             Handle(SelectMgr_EntityOwner)::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
118           anOwner->Set(this);
119         }
120         return;
121       }
122     }
123     //AIS_Shape::ComputeSelection(aSelection, 0);
124   } 
125   AIS_Shape::ComputeSelection(aSelection, aMode);
126 }
127
128 void ModuleBase_ResultPrs::appendWiresSelection(const Handle(SelectMgr_Selection)& theSelection,
129                                                 const TopoDS_Shape& theShape)
130 {
131   static TopAbs_ShapeEnum TypOfSel
132           = AIS_Shape::SelectionType(AIS_Shape::SelectionMode(TopAbs_WIRE));
133   // POP protection against crash in low layers
134   Standard_Real aDeflection = Prs3d::GetDeflection(theShape, myDrawer);
135   try {
136     StdSelect_BRepSelectionTool::Load(theSelection,
137                                       this,
138                                       theShape,
139                                       TypOfSel,
140                                       aDeflection,
141                                       myDrawer->HLRAngle(),
142                                       myDrawer->IsAutoTriangulation());
143   } catch ( Standard_Failure ) {
144   }
145 }
146
147 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM, 
148                                            const SelectMgr_SequenceOfOwner& theOwners)
149 {
150   Handle(SelectMgr_EntityOwner) anOwner;
151   Handle(ModuleBase_BRepOwner) aCompOwner;
152   for (int i = 1; i <= theOwners.Length(); i++) {
153     anOwner = theOwners.Value(i);
154     aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
155     if (aCompOwner.IsNull())
156       anOwner->Hilight(thePM);
157     else {
158       TopoDS_Shape aShape = aCompOwner->Shape();
159       Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
160       aSelectionPrs->Clear();
161
162       StdPrs_WFDeflectionShape::Add(aSelectionPrs, aShape, myDrawer);
163
164       aSelectionPrs->SetDisplayPriority(9);
165       aSelectionPrs->Highlight(Aspect_TOHM_COLOR, aSelectionPrs->HighlightColor());
166       aSelectionPrs->Display();
167       thePM->Highlight(this);
168     }
169   }
170 }
171   
172 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM, 
173                                                  const Quantity_NameOfColor theColor, 
174                                                  const Handle(SelectMgr_EntityOwner)& theOwner)
175 {
176   Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
177   if (aOwner.IsNull())
178     return;
179
180   TopoDS_Shape aShape = aOwner->Shape();
181   if (!aShape.IsNull()) {
182     thePM->Color(this, theColor);
183
184     Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
185     aHilightPrs->Clear();
186
187     StdPrs_WFDeflectionShape::Add(aHilightPrs, aShape, myDrawer);
188     aHilightPrs->Highlight(Aspect_TOHM_COLOR, theColor);
189
190     if (thePM->IsImmediateModeOn())
191       thePM->AddToImmediateList(aHilightPrs);
192   }
193 }