Salome HOME
Debug and optimization of selection of elements of the sketch.
[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 #include <SketchPlugin_ConstraintCoincidence.h>
37
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)), myIsCustomColor(false)
53 {
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 aExternalColor = aIsEdge ? Quantity_NOC_BLACK : Quantity_NOC_YELLOW;
142   Quantity_Color aInternalColor = aIsEdge ? Quantity_NOC_YELLOW : Quantity_NOC_BLACK;
143
144   // Create the presentation as a combination of standard point markers
145   bool aCustomColor = myIsCustomColor;
146   // The external yellow contour
147   Handle(Graphic3d_AspectMarker3d) aPtA = new Graphic3d_AspectMarker3d();
148   aPtA->SetType(Aspect_TOM_RING3);
149   aPtA->SetScale(2.);
150   aPtA->SetColor(!aCustomColor ? aExternalColor : myCustomColor);
151
152   Handle(Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup(thePresentation);
153   aGroup->SetPrimitivesAspect(aPtA);
154   Handle(Graphic3d_ArrayOfPoints) aPntArray = new Graphic3d_ArrayOfPoints(1);
155   aPntArray->AddVertex (myPoint.X(), myPoint.Y(), myPoint.Z());
156   aGroup->AddPrimitiveArray (aPntArray);
157
158   // Make a black mid ring
159   aPtA = new Graphic3d_AspectMarker3d();
160   aPtA->SetType(aIsEdge ? Aspect_TOM_STAR : Aspect_TOM_RING1);
161   aPtA->SetScale(1.);
162   aPtA->SetColor(!aCustomColor ? aInternalColor : myCustomColor);
163   aGroup->SetPrimitivesAspect(aPtA);
164   aGroup->AddPrimitiveArray (aPntArray);
165
166   // Make an internal ring
167   aPtA = new Graphic3d_AspectMarker3d();
168   aPtA->SetType(Aspect_TOM_POINT);
169   aPtA->SetScale(5.);
170   aPtA->SetColor(!aCustomColor ? aInternalColor : myCustomColor);
171   aGroup->SetPrimitivesAspect(aPtA);
172   aGroup->AddPrimitiveArray (aPntArray);
173
174   if (!aReadyToDisplay)
175     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
176                               "An empty AIS presentation: SketcherPrs_Coincident");
177 }
178
179
180 void SketcherPrs_Coincident::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
181                                             const Standard_Integer aMode)
182 {
183   // There is no selection of coincident - a point is selected instead of coincidence
184 }
185
186 void SketcherPrs_Coincident::SetColor(const Quantity_NameOfColor aCol)
187 {
188   SetColor(Quantity_Color(aCol));
189 }
190
191 void SketcherPrs_Coincident::SetColor(const Quantity_Color &aCol)
192 {
193   hasOwnColor=Standard_True;
194 }
195
196 void SketcherPrs_Coincident::SetCustomColor(const std::vector<int>& theColor)
197 {
198   myIsCustomColor = !theColor.empty();
199   if (myIsCustomColor)
200     myCustomColor = Quantity_Color(theColor[0] / 255., theColor[1] / 255.,
201                                    theColor[2] / 255., Quantity_TOC_RGB);
202   else
203     myCustomColor = Quantity_Color();
204 }