Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Coincident.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #include "SketcherPrs_Coincident.h"
21 #include "SketcherPrs_Tools.h"
22
23 #include <ModelAPI_AttributeRefAttr.h>
24 #include <ModelAPI_Events.h>
25
26 #include <GeomDataAPI_Point2D.h>
27 #include <GeomDataAPI_Dir.h>
28 #include <GeomDataAPI_Point.h>
29
30 #include <GeomAPI_XYZ.h>
31 #include <GeomAPI_Dir.h>
32 #include <GeomAPI_Pnt2d.h>
33
34 #include <SketchPlugin_Constraint.h>
35
36 #include <Graphic3d_AspectMarker3d.hxx>
37 #include <Graphic3d_ArrayOfPoints.hxx>
38 #include <Prs3d_PointAspect.hxx>
39 #include <Prs3d_Root.hxx>
40 #include <SelectMgr_EntityOwner.hxx>
41 #include <SelectMgr_Selection.hxx>
42 #include <Select3D_SensitivePoint.hxx>
43
44
45 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Coincident, AIS_InteractiveObject);
46
47 SketcherPrs_Coincident::SketcherPrs_Coincident(ModelAPI_Feature* theConstraint,
48                                                const std::shared_ptr<GeomAPI_Ax3>& thePlane)
49 : AIS_InteractiveObject(), myConstraint(theConstraint), mySketcherPlane(thePlane),
50   myPoint(gp_Pnt(0.0, 0.0, 0.0)), myIsCustomColor(false)
51 {
52 }
53
54 bool SketcherPrs_Coincident::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
55                                               const std::shared_ptr<GeomAPI_Ax3>& thePlane)
56 {
57   gp_Pnt aPoint;
58   return readyToDisplay(theConstraint, thePlane, aPoint);
59 }
60
61 bool SketcherPrs_Coincident::readyToDisplay(ModelAPI_Feature* theConstraint,
62                                             const std::shared_ptr<GeomAPI_Ax3>& thePlane,
63                                             gp_Pnt& thePoint)
64 {
65   bool aReadyToDisplay = false;
66   // Get point of the presentation
67   std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theConstraint,
68                                                               SketchPlugin_Constraint::ENTITY_A());
69   if (aPnt.get() == NULL)
70     aPnt = SketcherPrs_Tools::getPoint(theConstraint, SketchPlugin_Constraint::ENTITY_B());
71
72   aReadyToDisplay = aPnt.get() != NULL;
73   if (aReadyToDisplay) {
74     std::shared_ptr<GeomAPI_Pnt> aPoint = thePlane->to3D(aPnt->x(), aPnt->y());
75     thePoint = aPoint->impl<gp_Pnt>();
76   }
77   return aReadyToDisplay;
78 }
79
80
81 void SketcherPrs_Coincident::Compute(
82   const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
83   const Handle(Prs3d_Presentation)& thePresentation,
84   const Standard_Integer theMode)
85 {
86   gp_Pnt aPoint;
87   bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane, aPoint);
88   if (aReadyToDisplay)
89     myPoint = aPoint;
90
91   // Create the presentation as a combination of standard point markers
92   bool aCustomColor = myIsCustomColor;
93   // The external yellow contour
94   Handle(Graphic3d_AspectMarker3d) aPtA = new Graphic3d_AspectMarker3d();
95   aPtA->SetType(Aspect_TOM_RING3);
96   aPtA->SetScale(2.);
97   aPtA->SetColor(!aCustomColor ? Quantity_NOC_YELLOW : myCustomColor);
98
99   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup(thePresentation);
100   aGroup->SetPrimitivesAspect(aPtA);
101   Handle(Graphic3d_ArrayOfPoints) aPntArray = new Graphic3d_ArrayOfPoints(1);
102   aPntArray->AddVertex (myPoint.X(), myPoint.Y(), myPoint.Z());
103   aGroup->AddPrimitiveArray (aPntArray);
104
105   // Make a black mid ring
106   aPtA = new Graphic3d_AspectMarker3d();
107   aPtA->SetType(Aspect_TOM_RING1);
108   aPtA->SetScale(1.);
109   aPtA->SetColor(!aCustomColor ? Quantity_NOC_BLACK : myCustomColor);
110   aGroup->SetPrimitivesAspect(aPtA);
111   aGroup->AddPrimitiveArray (aPntArray);
112
113   // Make an internal ring
114   aPtA = new Graphic3d_AspectMarker3d();
115   aPtA->SetType(Aspect_TOM_POINT);
116   aPtA->SetScale(5.);
117   aPtA->SetColor(!aCustomColor ? Quantity_NOC_BLACK : myCustomColor);
118   aGroup->SetPrimitivesAspect(aPtA);
119   aGroup->AddPrimitiveArray (aPntArray);
120
121   if (!aReadyToDisplay)
122     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
123                               "An empty AIS presentation: SketcherPrs_Coincident");
124 }
125
126
127 void SketcherPrs_Coincident::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
128                                             const Standard_Integer aMode)
129 {
130   // There is no selection of coincident - a point is selected instead of coincidence
131 }
132
133 void SketcherPrs_Coincident::SetColor(const Quantity_NameOfColor aCol)
134 {
135   SetColor(Quantity_Color(aCol));
136 }
137
138 void SketcherPrs_Coincident::SetColor(const Quantity_Color &aCol)
139 {
140   hasOwnColor=Standard_True;
141   myOwnColor=aCol;
142 }
143
144 void SketcherPrs_Coincident::SetCustomColor(const std::vector<int>& theColor)
145 {
146   myIsCustomColor = !theColor.empty();
147   if (myIsCustomColor)
148     myCustomColor = Quantity_Color(theColor[0] / 255., theColor[1] / 255.,
149                                    theColor[2] / 255., Quantity_TOC_RGB);
150   else
151     myCustomColor = Quantity_Color();
152 }