Salome HOME
Debug of CompSolid selection
[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 <BRep_Builder.hxx>
15 #include <Prs3d_Drawer.hxx>
16 #include <Prs3d.hxx>
17 #include <Prs3d_PointAspect.hxx>
18 #include <Prs3d_IsoAspect.hxx>
19 #include <TopoDS_Builder.hxx>
20 #include <SelectMgr_SequenceOfOwner.hxx>
21 #include <SelectMgr_EntityOwner.hxx>
22 #include <SelectMgr_SelectionManager.hxx>
23 #include <StdPrs_WFDeflectionShape.hxx>
24 #include <StdSelect_BRepSelectionTool.hxx>
25 #include <AIS_InteractiveContext.hxx>
26 #include <AIS_Selection.hxx>
27 #include <TColStd_ListIteratorOfListOfInteger.hxx>
28
29
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), myIsSketchMode(false)
45 {
46   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(theResult);
47   std::shared_ptr<GeomAPI_PlanarEdges> aWirePtr = 
48     std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aShapePtr);
49   if (aWirePtr) {
50     if (aWirePtr->hasPlane() ) {
51       // If this is a wire with plane defined thin it is a sketch-like object
52       // It must have invisible faces
53       myIsSketchMode = true;
54     }
55   }
56   TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
57   Set(aShape);
58   Handle(Prs3d_Drawer) aDrawer = Attributes();
59   if (aDrawer->HasOwnPointAspect()) 
60     aDrawer->PointAspect()->SetTypeOfMarker(Aspect_TOM_PLUS);
61   else
62     aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_PLUS, Quantity_NOC_YELLOW, 1.));
63
64   // Activate individual repaintng if this is a part of compsolid
65   ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
66   SetAutoHilight(aCompSolid.get() == NULL);
67 }
68
69
70 void ModuleBase_ResultPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
71                                    const Handle(Prs3d_Presentation)& thePresentation, 
72                                    const Standard_Integer theMode)
73 {
74   std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(myResult);
75   if (!aShapePtr)
76     return;
77
78   if (myIsSketchMode) {
79     myFacesList.clear();
80     ResultConstructionPtr aConstruction = 
81       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myResult);
82     if (aConstruction.get()) {
83       int aFacesNum = aConstruction->facesNum();
84       for(int aFaceIndex = 0; aFaceIndex < aFacesNum; aFaceIndex++) {
85         myFacesList.push_back(aConstruction->face(aFaceIndex));
86       }
87     }
88   }
89   myOriginalShape = aShapePtr->impl<TopoDS_Shape>();
90   if (!myOriginalShape.IsNull()) {
91     Set(myOriginalShape);
92
93     // change deviation coefficient to provide more precise circle
94     ModuleBase_Tools::setDefaultDeviationCoefficient(myOriginalShape, Attributes());
95     AIS_Shape::Compute(thePresentationManager, thePresentation, theMode);
96   }
97 }
98
99
100 void ModuleBase_ResultPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
101                                             const Standard_Integer aMode)
102 {
103   if (aMode > 8)
104     // In order to avoid using custom selection modes
105     return;
106
107   
108   if (myIsSketchMode) {
109     if (aMode == AIS_Shape::SelectionMode(TopAbs_FACE)) {
110       BRep_Builder aBuilder;
111       TopoDS_Compound aComp;
112       aBuilder.MakeCompound(aComp);
113       aBuilder.Add(aComp, myOriginalShape);
114       std::list<std::shared_ptr<GeomAPI_Shape>>::const_iterator aIt;
115       for (aIt = myFacesList.cbegin(); aIt != myFacesList.cend(); ++aIt) {
116         TopoDS_Shape aFace = (*aIt)->impl<TopoDS_Shape>();
117         aBuilder.Add(aComp, aFace);
118       }
119       Set(aComp);
120     } else
121       Set(myOriginalShape);
122   } 
123   if (aMode == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) {
124     // Limit selection area only by actual object (Shape)
125     ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
126     if (aCompSolid.get()) {
127       std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
128       if (aShapePtr.get()) {
129         TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
130         int aPriority = StdSelect_BRepSelectionTool::GetStandardPriority(aShape, TopAbs_SHAPE);
131         double aDeflection = Prs3d::GetDeflection(aShape, myDrawer);
132
133         Handle(ModuleBase_BRepOwner) aOwner = new ModuleBase_BRepOwner(aShape, aPriority);
134         StdSelect_BRepSelectionTool::ComputeSensitive(aShape, aOwner, aSelection, 
135           aDeflection, myDrawer->HLRAngle(), 9, 500);
136
137         for (aSelection->Init(); aSelection->More(); aSelection->Next()) {
138           Handle(SelectMgr_EntityOwner) anOwner =
139             Handle(SelectMgr_EntityOwner)::DownCast(aSelection->Sensitive()->BaseSensitive()->OwnerId());
140           anOwner->Set(this);
141         }
142         return;
143       }
144     }
145     //AIS_Shape::ComputeSelection(aSelection, 0);
146   } 
147   AIS_Shape::ComputeSelection(aSelection, aMode);
148 }
149
150
151 bool ModuleBase_ResultPrs::hasCompSolidSelectionMode() const
152 {
153   if (!HasInteractiveContext()) 
154     return false;
155
156   Handle(AIS_InteractiveContext) aContext = GetContext();
157   TColStd_ListOfInteger aModes;
158   aContext->ActivatedModes(this, aModes);
159
160   TColStd_ListIteratorOfListOfInteger aIt(aModes);
161   for (; aIt.More(); aIt.Next()) {
162     if (aIt.Value() == AIS_Shape::SelectionMode(TopAbs_COMPSOLID)) 
163       return true;
164   }
165   return false;
166 }
167
168
169 TopoDS_Shape ModuleBase_ResultPrs::getSelectionShape() const
170 {
171   if (hasCompSolidSelectionMode()) {
172     // In case of CompSolid mode use shape from Parent for highlighting
173     ResultCompSolidPtr aCompSolid = ModelAPI_Tools::compSolidOwner(myResult);
174     if (aCompSolid.get()) {
175       std::shared_ptr<GeomAPI_Shape> aShapePtr = ModelAPI_Tools::shape(aCompSolid);
176       if (aShapePtr.get()) 
177         return aShapePtr->impl<TopoDS_Shape>();
178     }
179   } 
180   return myOriginalShape;
181 }
182
183
184 void ModuleBase_ResultPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM, 
185                                            const SelectMgr_SequenceOfOwner& theOwners)
186 {
187   Handle(SelectMgr_EntityOwner) anOwner;
188   Handle(ModuleBase_BRepOwner) aCompOwner;
189   for (int i = 1; i <= theOwners.Length(); i++) {
190     anOwner = theOwners.Value(i);
191     aCompOwner = Handle(ModuleBase_BRepOwner)::DownCast(anOwner);
192     if (aCompOwner.IsNull())
193       anOwner->Hilight(thePM);
194     else {
195       TopoDS_Shape aShape = aCompOwner->Shape();
196       Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
197       aSelectionPrs->Clear();
198
199       StdPrs_WFDeflectionShape::Add(aSelectionPrs, aShape, myDrawer);
200
201       aSelectionPrs->SetDisplayPriority(9);
202       aSelectionPrs->Highlight(Aspect_TOHM_COLOR, aSelectionPrs->HighlightColor());
203       aSelectionPrs->Display();
204       thePM->Highlight(this);
205     }
206   }
207 }
208   
209 void ModuleBase_ResultPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM, 
210                                                  const Quantity_NameOfColor theColor, 
211                                                  const Handle(SelectMgr_EntityOwner)& theOwner)
212 {
213   Handle(StdSelect_BRepOwner) aOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
214   if (aOwner.IsNull())
215     return;
216
217   TopoDS_Shape aShape = aOwner->Shape();
218   if (!aShape.IsNull()) {
219     thePM->Color(this, theColor);
220
221     Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
222     aHilightPrs->Clear();
223
224     StdPrs_WFDeflectionShape::Add(aHilightPrs, aShape, myDrawer);
225     aHilightPrs->Highlight(Aspect_TOHM_COLOR, theColor);
226
227     if (thePM->IsImmediateModeOn())
228       thePM->AddToImmediateList(aHilightPrs);
229   }
230 }