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