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