1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SketcherPrs_Coincident.h"
21 #include "SketcherPrs_Tools.h"
23 #include <ModelAPI_AttributeRefAttr.h>
24 #include <ModelAPI_Events.h>
26 #include <GeomDataAPI_Point2D.h>
27 #include <GeomDataAPI_Dir.h>
28 #include <GeomDataAPI_Point.h>
30 #include <GeomAPI_XYZ.h>
31 #include <GeomAPI_Dir.h>
32 #include <GeomAPI_Pnt2d.h>
34 #include <SketchPlugin_Constraint.h>
35 #include <SketchPlugin_ConstraintCoincidence.h>
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>
46 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Coincident, AIS_InteractiveObject);
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)
55 bool SketcherPrs_Coincident::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
56 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
59 return readyToDisplay(theConstraint, thePlane, aPoint);
62 std::shared_ptr<GeomAPI_Pnt2d> getCoincidencePoint(ModelAPI_Feature* theConstraint)
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());
73 bool SketcherPrs_Coincident::readyToDisplay(ModelAPI_Feature* theConstraint,
74 const std::shared_ptr<GeomAPI_Ax3>& thePlane,
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>();
85 return aReadyToDisplay;
89 bool hasEdge(ModelAPI_Feature* theConstraint)
92 SketcherPrs_Tools::getResult(theConstraint, SketchPlugin_Constraint::ENTITY_A());
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);
98 GeomAPI_Shape::ShapeType aTypeA = aResA->shape()->shapeType();
99 if (aTypeA == GeomAPI_Shape::EDGE)
103 GeomAPI_Shape::ShapeType aTypeB = aResB->shape()->shapeType();
104 if (aTypeB == GeomAPI_Shape::EDGE)
111 void SketcherPrs_Coincident::Compute(
112 const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
113 const Handle(Prs3d_Presentation)& thePresentation,
114 const Standard_Integer theMode)
117 bool aReadyToDisplay = readyToDisplay(myConstraint, mySketch->coordinatePlane(), aPoint);
121 bool aIsEdge = hasEdge(myConstraint);
123 std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincidencePoint(myConstraint);
124 std::shared_ptr<GeomAPI_Pnt2d> aP;
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());
140 Quantity_Color aExternalColor = aIsEdge ? Quantity_NOC_BLACK : Quantity_NOC_YELLOW;
141 Quantity_Color aInternalColor = aIsEdge ? Quantity_NOC_YELLOW : Quantity_NOC_BLACK;
143 int aRatio = SketcherPrs_Tools::pixelRatio();
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);
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);
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);
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);
175 if (!aReadyToDisplay)
176 SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
177 "An empty AIS presentation: SketcherPrs_Coincident");
181 void SketcherPrs_Coincident::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
182 const Standard_Integer aMode)
184 // There is no selection of coincident - a point is selected instead of coincidence
187 void SketcherPrs_Coincident::SetColor(const Quantity_NameOfColor aCol)
189 SetColor(Quantity_Color(aCol));
192 void SketcherPrs_Coincident::SetColor(const Quantity_Color &aCol)
194 hasOwnColor=Standard_True;
197 void SketcherPrs_Coincident::SetCustomColor(const std::vector<int>& theColor)
199 myIsCustomColor = !theColor.empty();
201 myCustomColor = Quantity_Color(theColor[0] / 255., theColor[1] / 255.,
202 theColor[2] / 255., Quantity_TOC_RGB);
204 myCustomColor = Quantity_Color();