1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketcherPrs_Angle.cpp
4 // Created: 20 August 2015
5 // Author: Vitaly SMETANNIKOV
7 #include "SketcherPrs_Angle.h"
8 #include "SketcherPrs_Tools.h"
9 #include "SketcherPrs_DimensionStyleListener.h"
11 #include <SketchPlugin_ConstraintAngle.h>
12 #include <SketchPlugin_Constraint.h>
13 #include <SketchPlugin_Line.h>
15 #include <GeomAPI_Edge.h>
16 #include <GeomAPI_Lin.h>
17 #include <GeomDataAPI_Point2D.h>
18 #include <GeomAPI_Angle2d.h>
19 #include <GeomAPI_Lin2d.h>
21 #include <ModelAPI_AttributeRefAttr.h>
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_AttributeInteger.h>
26 #include <BRep_Tool.hxx>
28 #include <Events_Error.h>
30 #define PI 3.1415926535897932
32 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Angle, AIS_AngleDimension);
33 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Angle, AIS_AngleDimension);
35 SketcherPrs_Angle::SketcherPrs_Angle(ModelAPI_Feature* theConstraint,
36 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
37 : AIS_AngleDimension(gp_Pnt(0,0,0), gp_Pnt(1,0,0), gp_Pnt(0,1,0)), myConstraint(theConstraint), myPlane(thePlane)
39 myAspect = new Prs3d_DimensionAspect();
40 myAspect->MakeArrows3d(false);
41 myAspect->MakeText3d(false);
42 myAspect->MakeTextShaded(false);
43 myAspect->MakeUnitsDisplayed(false);
44 myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
45 myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
47 SetDimensionAspect(myAspect);
48 SetSelToleranceForText2d(SketcherPrs_Tools::getDefaultTextHeight());
50 myStyleListener = new SketcherPrs_DimensionStyleListener();
53 SketcherPrs_Angle::~SketcherPrs_Angle()
55 delete myStyleListener;
58 bool SketcherPrs_Angle::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
59 const std::shared_ptr<GeomAPI_Ax3>&/* thePlane*/)
61 bool aReadyToDisplay = false;
63 DataPtr aData = theConstraint->data();
66 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
67 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
68 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
69 if (!aFlyoutAttr->isInitialized())
70 return aReadyToDisplay; // can not create a good presentation
72 AttributeRefAttrPtr anAttr1 = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
73 if (!anAttr1->isInitialized())
74 return aReadyToDisplay;
76 AttributeRefAttrPtr anAttr2 = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
77 if (!anAttr2->isInitialized())
78 return aReadyToDisplay;
81 ObjectPtr aObj1 = anAttr1->object();
82 ObjectPtr aObj2 = anAttr2->object();
84 std::shared_ptr<GeomAPI_Shape> aShape1 = SketcherPrs_Tools::getShape(aObj1);
85 std::shared_ptr<GeomAPI_Shape> aShape2 = SketcherPrs_Tools::getShape(aObj2);
87 aReadyToDisplay = aShape1.get() && aShape2.get();
88 return aReadyToDisplay;
91 bool SketcherPrs_Angle::getPoints(gp_Pnt& theFirstPoint, gp_Pnt& theSecondPoint,
92 gp_Pnt& theCenterPoint, double& theAngle) const
94 bool aReadyToDisplay = false;
96 std::shared_ptr<ModelAPI_Data> aData = myConstraint->data();
97 std::shared_ptr<GeomAPI_Ax3> aPlane = myPlane;
98 FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
99 FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
101 std::shared_ptr<GeomDataAPI_Point2D> aStartA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
102 aLineA->attribute(SketchPlugin_Line::START_ID()));
103 std::shared_ptr<GeomDataAPI_Point2D> aEndA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
104 aLineA->attribute(SketchPlugin_Line::END_ID()));
105 std::shared_ptr<GeomDataAPI_Point2D> aStartB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
106 aLineB->attribute(SketchPlugin_Line::START_ID()));
107 std::shared_ptr<GeomDataAPI_Point2D> aEndB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
108 aLineB->attribute(SketchPlugin_Line::END_ID()));
110 std::shared_ptr<GeomAPI_Angle2d> anAng;
111 if (!aData->attribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID())->isInitialized() ||
112 !aData->attribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID())->isInitialized())
113 anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(
114 aStartA->pnt(), aEndA->pnt(), aStartB->pnt(), aEndB->pnt()));
116 std::shared_ptr<GeomAPI_Lin2d> aLine1(new GeomAPI_Lin2d(aStartA->pnt(), aEndA->pnt()));
117 bool isReversed1 = aData->boolean(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID())->value();
118 std::shared_ptr<GeomAPI_Lin2d> aLine2(new GeomAPI_Lin2d(aStartB->pnt(), aEndB->pnt()));
119 bool isReversed2 = aData->boolean(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID())->value();
120 anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2));
122 theAngle = anAng->angleDegree();
124 gp_Pnt2d aFirstPoint = anAng->firstPoint()->impl<gp_Pnt2d>();
125 std::shared_ptr<GeomAPI_Pnt> aPoint = myPlane->to3D(aFirstPoint.X(), aFirstPoint.Y());
126 theFirstPoint = aPoint->impl<gp_Pnt>();
128 gp_Pnt2d aCenterPoint = anAng->center()->impl<gp_Pnt2d>();
129 aPoint = myPlane->to3D(aCenterPoint.X(), aCenterPoint.Y());
130 theCenterPoint = aPoint->impl<gp_Pnt>();
132 gp_Pnt2d aSecondPoint = anAng->secondPoint()->impl<gp_Pnt2d>();
133 aPoint = myPlane->to3D(aSecondPoint.X(), aSecondPoint.Y());
134 theSecondPoint = aPoint->impl<gp_Pnt>();
136 return aReadyToDisplay;
139 void SketcherPrs_Angle::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
140 const Handle(Prs3d_Presentation)& thePresentation,
141 const Standard_Integer theMode)
143 DataPtr aData = myConstraint->data();
145 if (!IsReadyToDisplay(myConstraint, myPlane)) {
146 Events_Error::throwException("An empty AIS presentation: SketcherPrs_Angle");
147 return; // can not create a good presentation
150 std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
151 ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID()));
152 SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value());
154 gp_Pnt aFirstPoint, aSecondPoint, aCenterPoint;
156 getPoints(aFirstPoint, aSecondPoint, aCenterPoint, aValue);
159 switch (anAngleType) {
160 case SketcherPrs_Tools::ANGLE_DIRECT: {
161 SetArrowVisible(Standard_False/*first*/, Standard_True/*second*/);
162 SetMeasuredGeometry(aFirstPoint, aCenterPoint, aSecondPoint);
163 bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
164 SetAngleReversed(!isReversedPlanes);
167 case SketcherPrs_Tools::ANGLE_COMPLEMENTARY: {
168 double anEdge1Length = aCenterPoint.Distance(aFirstPoint);
169 aFirstPoint = aCenterPoint.Translated (gp_Vec(aCenterPoint, aFirstPoint).Normalized() * (-anEdge1Length));
170 anEdge1Length = aCenterPoint.Distance(aFirstPoint);
171 SetMeasuredGeometry(aFirstPoint, aCenterPoint, aSecondPoint);
174 case SketcherPrs_Tools::ANGLE_BACKWARD: {
175 SetArrowVisible(Standard_False/*first*/, Standard_True/*second*/);
176 SetMeasuredGeometry(aFirstPoint, aCenterPoint, aSecondPoint);
177 bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
178 SetAngleReversed(isReversedPlanes);
184 if (aDist < 0) /// it was not calculated yet
185 aDist = calculateDistanceToFlyoutPoint();
188 // Angle value is in degrees
189 AttributeDoublePtr aVal = aData->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
190 SetCustomValue(aValue);
192 myAspect->SetExtensionSize(myAspect->ArrowAspect()->Length());
193 myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
195 SketcherPrs_Tools::setDisplaySpecialSymbol(this, aVal->usedParameters().size() > 0);
196 myStyleListener->updateDimensions(this, aVal);
198 AIS_AngleDimension::Compute(thePresentationManager, thePresentation, theMode);
201 void SketcherPrs_Angle::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
202 const Standard_Integer theMode)
204 Standard_Integer aMode;
206 case 0: // we should use selection of all objects
209 case SketcherPrs_Tools::Sel_Dimension_All:
212 case SketcherPrs_Tools::Sel_Dimension_Line:
215 case SketcherPrs_Tools::Sel_Dimension_Text:
219 // there are own selection modes, so the others should be ignored
220 // otherwise, the text selection appears in the viewer
224 AIS_AngleDimension::ComputeSelection(aSelection, aMode);
227 bool SketcherPrs_Angle::isAnglePlaneReversedToSketchPlane()
229 bool aReversed = false;
233 gp_Pln aPlane = GetPlane();
234 gp_Dir aDir = aPlane.Axis().Direction();
235 double aDot = myPlane->normal()->dot(
236 std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z())));
240 double SketcherPrs_Angle::calculateDistanceToFlyoutPoint()
242 const gp_Pnt& aCenter = CenterPoint();
243 const gp_Pnt& aFirst = FirstPoint();
244 const gp_Pnt& aSecond = SecondPoint();
247 DataPtr aData = myConstraint->data();
248 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
249 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
250 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
251 std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = myPlane->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
253 gp_Dir aBisector((aFirst.XYZ() + aSecond.XYZ()) * 0.5 - aCenter.XYZ());
254 gp_Pnt aFlyPnt(aFlyoutPnt->x(), aFlyoutPnt->y(), aFlyoutPnt->z());
255 gp_XYZ aFlyDir = aFlyPnt.XYZ() - aCenter.XYZ();
256 double aDistance = aFlyDir.Dot(aBisector.XYZ());
257 // make a positive distance in order to AIS angle presentation is not reversed
258 aDistance = fabs(aDistance);