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