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