Salome HOME
Replace of Events_Error by Events_InfoMessage. Provide storing of non translated...
[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_InfoMessage.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 void ModuleBase_ResultPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
63                                    const Handle(Prs3d_Presentation)& thePresentation, 
64                                    const Standard_Integer theMode)
65 {
66   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
67   bool aReadyToDisplay = aShapePtr.get();
68   if (aReadyToDisplay) {
69     myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
70     if (!myOriginalShape.IsNull())
71       Set(myOriginalShape);
72   }
73   // change deviation coefficient to provide more precise circle
74   ModuleBase_Tools::setDefaultDeviationCoefficient(Shape(), Attributes());
75   AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
76
77   if (!aReadyToDisplay) {
78     Events_InfoMessage("ModuleBase_ResultPrs", "An empty AIS presentation: ModuleBase_ResultPrs").send();
79     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_EMPTY_AIS_PRESENTATION);
80     ModelAPI_EventCreator::get()->sendUpdated(myResult, anEvent);
81   }
82 }
83
84 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
85                                             const Standard_Integer aMode)
86 {
87   if (aMode > 8)
88     // In order to avoid using custom selection modes
89     return;
90
91   if (aMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
92     // Limit selection area only by actual object (Shape)
93     ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
94     if (aCompSolid.get()) {
95       std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
96       if (aShapePtr.get()) {
97         TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
98         int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_SHAPE);
99         double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
100
101         Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
102         StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection, 
103           aDeflection, myDrawer->HLRAngle(), 9, 500);
104
105         for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
106           Handle(SelectMgr_EntityOwner) anOwner =
107             Handle(SelectMgr_EntityOwner)::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
108           anOwner->Set(this);
109         }
110         return;
111       }
112     }
113     //AIS_Shape::ComputeSelection(aSelection, 0);
114   } 
115   AIS_Shape::ComputeSelection(aSelection, aMode);
116 }
117
118 void ModuleBase_ResultPrs::appendWiresSelection(const Handle(SelectMgr_Selection)& theSelection,
119                                                 const TopoDS_Shape& theShape)
120 {
121   static TopAbs_ShapeEnum TypOfSel
122           = AIS_Shape::SelectionType(AIS_Shape::SelectionMode(TopAbs_WIRE));
123   // POP protection against crash in low layers
124   Standard_Real aDeflection = Prs3d::GetDeflection(theShape, myDrawer);
125   try {
126     StdSelect_BRepSelectionTool::Load(theSelection,
127                                       this,
128                                       theShape,
129                                       TypOfSel,
130                                       aDeflection,
131                                       myDrawer->HLRAngle(),
132                                       myDrawer->IsAutoTriangulation());
133   } catch ( Standard_Failure ) {
134   }
135 }
136
137 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM, 
138                                            const SelectMgr_SequenceOfOwner& theOwners)
139 {
140   Handle(SelectMgr_EntityOwner) anOwner;
141   Handle(ModuleBase_BRepOwner) aCompOwner;
142   for (int i = 1; i <= theOwners.Length(); i++) {
143     anOwner = theOwners.Value(i);
144     aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
145     if (aCompOwner.IsNull())
146       anOwner->Hilight(thePM);
147     else {
148       TopoDS_Shape aShape = aCompOwner->Shape();
149       Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
150       aSelectionPrs->Clear();
151
152       StdPrs_WFDeflectionShape::Add(aSelectionPrs, aShape, myDrawer);
153
154       aSelectionPrs->SetDisplayPriority(9);
155       aSelectionPrs->Highlight(Aspect_TOHM_COLOR, aSelectionPrs->HighlightColor());
156       aSelectionPrs->Display();
157       thePM->Highlight(this);
158     }
159   }
160 }
161   
162 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM, 
163                                                  const Quantity_NameOfColor theColor, 
164                                                  const Handle(SelectMgr_EntityOwner)& theOwner)
165 {
166   Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
167   if (aOwner.IsNull())
168     return;
169
170   TopoDS_Shape aShape = aOwner->Shape();
171   if (!aShape.IsNull()) {
172     thePM->Color(this, theColor);
173
174     Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
175     aHilightPrs->Clear();
176
177     StdPrs_WFDeflectionShape::Add(aHilightPrs, aShape, myDrawer);
178     aHilightPrs->Highlight(Aspect_TOHM_COLOR, theColor);
179
180     if (thePM->IsImmediateModeOn())
181       thePM->AddToImmediateList(aHilightPrs);
182   }
183 }