Salome HOME
#1404 Random crash with Shaper: AIS presentations: avoid IsReadyToDisplay calling...
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Coincident.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        SketcherPrs_Coincident.cpp
4 // Created:     12 February 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "SketcherPrs_Coincident.h"
8 #include "SketcherPrs_Tools.h"
9
10 #include <ModelAPI_AttributeRefAttr.h>
11 #include <ModelAPI_Events.h>
12
13 #include <GeomDataAPI_Point2D.h>
14 #include <GeomDataAPI_Dir.h>
15 #include <GeomDataAPI_Point.h>
16
17 #include <GeomAPI_XYZ.h>
18 #include <GeomAPI_Dir.h>
19 #include <GeomAPI_Pnt2d.h>
20
21 #include <Events_Error.h>
22
23 #include <SketchPlugin_Constraint.h>
24
25 #include <Graphic3d_AspectMarker3d.hxx>
26 #include <Graphic3d_ArrayOfPoints.hxx>
27 #include <Prs3d_PointAspect.hxx>
28 #include <Prs3d_Root.hxx>
29 #include <SelectMgr_EntityOwner.hxx>
30 #include <SelectMgr_Selection.hxx>
31 #include <Select3D_SensitivePoint.hxx>
32
33
34 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Coincident, AIS_InteractiveObject);
35 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Coincident, AIS_InteractiveObject);
36
37 SketcherPrs_Coincident::SketcherPrs_Coincident(ModelAPI_Feature* theConstraint, 
38                                                const std::shared_ptr<GeomAPI_Ax3>& thePlane) 
39  : AIS_InteractiveObject(), myConstraint(theConstraint), mySketcherPlane(thePlane)
40 {
41   
42 }  
43
44 bool SketcherPrs_Coincident::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
45                                               const std::shared_ptr<GeomAPI_Ax3>& thePlane)
46 {
47   gp_Pnt aPoint;
48   return readyToDisplay(theConstraint, thePlane, aPoint);
49 }
50
51 bool SketcherPrs_Coincident::readyToDisplay(ModelAPI_Feature* theConstraint,
52                                             const std::shared_ptr<GeomAPI_Ax3>& thePlane,
53                                             gp_Pnt& thePoint)
54 {
55   bool aReadyToDisplay = false;
56   // Get point of the presentation
57   std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theConstraint,
58                                                               SketchPlugin_Constraint::ENTITY_A());
59   if (aPnt.get() == NULL) {
60     aPnt = SketcherPrs_Tools::getPoint(theConstraint, SketchPlugin_Constraint::ENTITY_B());
61     aReadyToDisplay = aPnt.get() != NULL;
62     if (aReadyToDisplay)
63       thePoint = aPnt->impl<gp_Pnt>();
64   }
65   return aReadyToDisplay;
66 }
67
68
69 void SketcherPrs_Coincident::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
70                                    const Handle(Prs3d_Presentation)& thePresentation, 
71                                    const Standard_Integer theMode)
72 {
73   gp_Pnt aPoint;
74   bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane, aPoint);
75   if (aReadyToDisplay)
76     myPoint = aPoint;
77
78   // Get point of the presentation
79   static Handle(Graphic3d_AspectMarker3d) aPtA;
80   if (aPtA.IsNull()) {
81     aPtA = new Graphic3d_AspectMarker3d ();
82   }
83   // Create the presentation as a combination of standard point markers
84
85   // The external yellow contour
86   aPtA->SetType(Aspect_TOM_RING3);
87   aPtA->SetScale(2.);
88   aPtA->SetColor(Quantity_NOC_YELLOW);
89
90   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup(thePresentation);
91   aGroup->SetPrimitivesAspect(aPtA);
92   Handle(Graphic3d_ArrayOfPoints) aPntArray = new Graphic3d_ArrayOfPoints(1);
93   aPntArray->AddVertex (myPoint.X(), myPoint.Y(), myPoint.Z());
94   aGroup->AddPrimitiveArray (aPntArray);
95
96   // Make a black mid ring
97   aPtA->SetType(Aspect_TOM_RING1);
98   aPtA->SetScale(1.);
99   aPtA->SetColor(Quantity_NOC_BLACK);
100   aGroup->SetPrimitivesAspect(aPtA);
101   aGroup->AddPrimitiveArray (aPntArray);
102
103   // Make an internal ring
104   aPtA->SetType(Aspect_TOM_POINT);
105   aPtA->SetScale(5.);
106   aGroup->SetPrimitivesAspect(aPtA);
107   aGroup->AddPrimitiveArray (aPntArray);
108
109   if (!aReadyToDisplay)
110     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
111                               "An empty AIS presentation: SketcherPrs_Coincident");
112 }
113
114
115 void SketcherPrs_Coincident::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
116                                             const Standard_Integer aMode)
117 {
118   // There is no selection of coincident - a point is selected instead of coincidence
119 }
120
121 void SketcherPrs_Coincident::SetColor(const Quantity_NameOfColor aCol)
122 {
123   SetColor(Quantity_Color(aCol));
124 }
125
126 void SketcherPrs_Coincident::SetColor(const Quantity_Color &aCol)
127 {
128   hasOwnColor=Standard_True;
129   myOwnColor=aCol;
130 }