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