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