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