Salome HOME
a216008562a0e602c56eeadbfd659c57d3e2764e
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Coincident.cpp
1 // Copyright (C) 2014-2020  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
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 #include <SketchPlugin_ConstraintCoincidence.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   SketchPlugin_Sketch* theSketch)
50 : AIS_InteractiveObject(), myConstraint(theConstraint), mySketch(theSketch),
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 std::shared_ptr<GeomAPI_Pnt2d> getCoincidencePoint(ModelAPI_Feature* theConstraint)
63 {
64   std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theConstraint,
65     SketchPlugin_Constraint::ENTITY_A());
66   if (aPnt.get() == NULL)
67     aPnt = SketcherPrs_Tools::getPoint(theConstraint, SketchPlugin_Constraint::ENTITY_B());
68
69   return aPnt;
70 }
71
72
73 bool SketcherPrs_Coincident::readyToDisplay(ModelAPI_Feature* theConstraint,
74                                             const std::shared_ptr<GeomAPI_Ax3>& thePlane,
75                                             gp_Pnt& thePoint)
76 {
77   bool aReadyToDisplay = false;
78   // Get point of the presentation
79   std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincidencePoint(theConstraint);
80   aReadyToDisplay = aPnt.get() != NULL;
81   if (aReadyToDisplay) {
82     std::shared_ptr<GeomAPI_Pnt> aPoint = thePlane->to3D(aPnt->x(), aPnt->y());
83     thePoint = aPoint->impl<gp_Pnt>();
84   }
85   return aReadyToDisplay;
86 }
87
88
89 bool hasEdge(ModelAPI_Feature* theConstraint)
90 {
91   ObjectPtr aObjA =
92     SketcherPrs_Tools::getResult(theConstraint, SketchPlugin_Constraint::ENTITY_A());
93   ObjectPtr aObjB =
94     SketcherPrs_Tools::getResult(theConstraint, SketchPlugin_Constraint::ENTITY_B());
95   ResultPtr aResA = std::dynamic_pointer_cast<ModelAPI_Result>(aObjA);
96   ResultPtr aResB = std::dynamic_pointer_cast<ModelAPI_Result>(aObjB);
97   if (aResA.get()) {
98     GeomAPI_Shape::ShapeType aTypeA = aResA->shape()->shapeType();
99     if (aTypeA == GeomAPI_Shape::EDGE)
100       return true;
101   }
102   if (aResB.get()) {
103     GeomAPI_Shape::ShapeType aTypeB = aResB->shape()->shapeType();
104     if (aTypeB == GeomAPI_Shape::EDGE)
105       return true;
106   }
107   return false;
108 }
109
110
111 void SketcherPrs_Coincident::Compute(
112   const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
113   const Handle(Prs3d_Presentation)& thePresentation,
114   const Standard_Integer theMode)
115 {
116   gp_Pnt aPoint;
117   bool aReadyToDisplay = readyToDisplay(myConstraint, mySketch->coordinatePlane(), aPoint);
118   if (aReadyToDisplay)
119     myPoint = aPoint;
120
121   bool aIsEdge = hasEdge(myConstraint);
122   if (!aIsEdge) {
123     std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincidencePoint(myConstraint);
124     std::shared_ptr<GeomAPI_Pnt2d> aP;
125     FeaturePtr aSub;
126     int aNumberOfSubs = mySketch->numberOfSubs();
127     for (int i = 0; i < aNumberOfSubs; i++) {
128       aSub = mySketch->subFeature(i);
129       if (aSub->getKind() == SketchPlugin_ConstraintCoincidence::ID() &&
130         aSub.get() != myConstraint) {
131         aP = getCoincidencePoint(aSub.get());
132         if (aP && aP->isEqual(aPnt)) {
133           aIsEdge = hasEdge(aSub.get());
134           if (aIsEdge)
135             break;
136         }
137       }
138     }
139   }
140   Quantity_Color aExternalColor = aIsEdge ? Quantity_NOC_BLACK : Quantity_NOC_YELLOW;
141   Quantity_Color aInternalColor = aIsEdge ? Quantity_NOC_YELLOW : Quantity_NOC_BLACK;
142
143   int aRatio = SketcherPrs_Tools::pixelRatio();
144
145   // Create the presentation as a combination of standard point markers
146   bool aCustomColor = myIsCustomColor;
147   // The external yellow contour
148   Handle(Graphic3d_AspectMarker3d) aPtA = new Graphic3d_AspectMarker3d();
149   aPtA->SetType(Aspect_TOM_RING3);
150   aPtA->SetScale(2. * aRatio);
151   aPtA->SetColor(!aCustomColor ? aExternalColor : myCustomColor);
152
153   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup(thePresentation);
154   aGroup->SetPrimitivesAspect(aPtA);
155   Handle(Graphic3d_ArrayOfPoints) aPntArray = new Graphic3d_ArrayOfPoints(1);
156   aPntArray->AddVertex (myPoint.X(), myPoint.Y(), myPoint.Z());
157   aGroup->AddPrimitiveArray (aPntArray);
158
159   // Make a black mid ring
160   aPtA = new Graphic3d_AspectMarker3d();
161   aPtA->SetType(aIsEdge ? Aspect_TOM_STAR : Aspect_TOM_RING1);
162   aPtA->SetScale(1. * aRatio);
163   aPtA->SetColor(!aCustomColor ? aInternalColor : myCustomColor);
164   aGroup->SetPrimitivesAspect(aPtA);
165   aGroup->AddPrimitiveArray (aPntArray);
166
167   // Make an internal ring
168   aPtA = new Graphic3d_AspectMarker3d();
169   aPtA->SetType(Aspect_TOM_POINT);
170   aPtA->SetScale(5. * aRatio);
171   aPtA->SetColor(!aCustomColor ? aInternalColor : myCustomColor);
172   aGroup->SetPrimitivesAspect(aPtA);
173   aGroup->AddPrimitiveArray (aPntArray);
174
175   if (!aReadyToDisplay)
176     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
177                               "An empty AIS presentation: SketcherPrs_Coincident");
178 }
179
180
181 void SketcherPrs_Coincident::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
182                                             const Standard_Integer aMode)
183 {
184   // There is no selection of coincident - a point is selected instead of coincidence
185 }
186
187 void SketcherPrs_Coincident::SetColor(const Quantity_NameOfColor aCol)
188 {
189   SetColor(Quantity_Color(aCol));
190 }
191
192 void SketcherPrs_Coincident::SetColor(const Quantity_Color &aCol)
193 {
194   hasOwnColor=Standard_True;
195 }
196
197 void SketcherPrs_Coincident::SetCustomColor(const std::vector<int>& theColor)
198 {
199   myIsCustomColor = !theColor.empty();
200   if (myIsCustomColor)
201     myCustomColor = Quantity_Color(theColor[0] / 255., theColor[1] / 255.,
202                                    theColor[2] / 255., Quantity_TOC_RGB);
203   else
204     myCustomColor = Quantity_Color();
205 }