Salome HOME
Issue #2029 Change the color of the Sketch when fully constrained
[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 <SketchPlugin_Constraint.h>
22
23 #include <Graphic3d_AspectMarker3d.hxx>
24 #include <Graphic3d_ArrayOfPoints.hxx>
25 #include <Prs3d_PointAspect.hxx>
26 #include <Prs3d_Root.hxx>
27 #include <SelectMgr_EntityOwner.hxx>
28 #include <SelectMgr_Selection.hxx>
29 #include <Select3D_SensitivePoint.hxx>
30
31
32 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Coincident, AIS_InteractiveObject);
33
34 SketcherPrs_Coincident::SketcherPrs_Coincident(ModelAPI_Feature* theConstraint,
35                                                const std::shared_ptr<GeomAPI_Ax3>& thePlane)
36 : AIS_InteractiveObject(), myConstraint(theConstraint), mySketcherPlane(thePlane),
37   myPoint(gp_Pnt(0.0, 0.0, 0.0)), myIsCustomColor(false)
38 {
39 }
40
41 bool SketcherPrs_Coincident::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
42                                               const std::shared_ptr<GeomAPI_Ax3>& thePlane)
43 {
44   gp_Pnt aPoint;
45   return readyToDisplay(theConstraint, thePlane, aPoint);
46 }
47
48 bool SketcherPrs_Coincident::readyToDisplay(ModelAPI_Feature* theConstraint,
49                                             const std::shared_ptr<GeomAPI_Ax3>& thePlane,
50                                             gp_Pnt& thePoint)
51 {
52   bool aReadyToDisplay = false;
53   // Get point of the presentation
54   std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theConstraint,
55                                                               SketchPlugin_Constraint::ENTITY_A());
56   if (aPnt.get() == NULL)
57     aPnt = SketcherPrs_Tools::getPoint(theConstraint, SketchPlugin_Constraint::ENTITY_B());
58
59   aReadyToDisplay = aPnt.get() != NULL;
60   if (aReadyToDisplay) {
61     std::shared_ptr<GeomAPI_Pnt> aPoint = thePlane->to3D(aPnt->x(), aPnt->y());
62     thePoint = aPoint->impl<gp_Pnt>();
63   }
64   return aReadyToDisplay;
65 }
66
67
68 void SketcherPrs_Coincident::Compute(
69   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   // Create the presentation as a combination of standard point markers
79   bool aCustomColor = myIsCustomColor;
80   // The external yellow contour
81   Handle(Graphic3d_AspectMarker3d) aPtA = new Graphic3d_AspectMarker3d();
82   aPtA->SetType(Aspect_TOM_RING3);
83   aPtA->SetScale(2.);
84   aPtA->SetColor(!aCustomColor ? Quantity_NOC_YELLOW : myCustomColor);
85
86   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup(thePresentation);
87   aGroup->SetPrimitivesAspect(aPtA);
88   Handle(Graphic3d_ArrayOfPoints) aPntArray = new Graphic3d_ArrayOfPoints(1);
89   aPntArray->AddVertex (myPoint.X(), myPoint.Y(), myPoint.Z());
90   aGroup->AddPrimitiveArray (aPntArray);
91
92   // Make a black mid ring
93   aPtA = new Graphic3d_AspectMarker3d();
94   aPtA->SetType(Aspect_TOM_RING1);
95   aPtA->SetScale(1.);
96   aPtA->SetColor(!aCustomColor ? Quantity_NOC_BLACK : myCustomColor);
97   aGroup->SetPrimitivesAspect(aPtA);
98   aGroup->AddPrimitiveArray (aPntArray);
99
100   // Make an internal ring
101   aPtA = new Graphic3d_AspectMarker3d();
102   aPtA->SetType(Aspect_TOM_POINT);
103   aPtA->SetScale(5.);
104   aPtA->SetColor(!aCustomColor ? Quantity_NOC_BLACK : myCustomColor);
105   aGroup->SetPrimitivesAspect(aPtA);
106   aGroup->AddPrimitiveArray (aPntArray);
107
108   if (!aReadyToDisplay)
109     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
110                               "An empty AIS presentation: SketcherPrs_Coincident");
111 }
112
113
114 void SketcherPrs_Coincident::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
115                                             const Standard_Integer aMode)
116 {
117   // There is no selection of coincident - a point is selected instead of coincidence
118 }
119
120 void SketcherPrs_Coincident::SetColor(const Quantity_NameOfColor aCol)
121 {
122   SetColor(Quantity_Color(aCol));
123 }
124
125 void SketcherPrs_Coincident::SetColor(const Quantity_Color &aCol)
126 {
127   hasOwnColor=Standard_True;
128   myOwnColor=aCol;
129 }
130
131 void SketcherPrs_Coincident::SetCustomColor(const std::vector<int>& theColor)
132 {
133   myIsCustomColor = !theColor.empty();
134   if (myIsCustomColor)
135     myCustomColor = Quantity_Color(theColor[0] / 255., theColor[1] / 255.,
136                                    theColor[2] / 255., Quantity_TOC_RGB);
137   else
138     myCustomColor = Quantity_Color();
139 }