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>
14 #include <GeomAPI_Edge.h>
15 #include <GeomAPI_Lin.h>
16 #include <GeomDataAPI_Point2D.h>
18 #include <ModelAPI_AttributeRefAttr.h>
19 #include <ModelAPI_AttributeDouble.h>
20 #include <ModelAPI_AttributeInteger.h>
23 #include <BRep_Tool.hxx>
25 #include <Events_Error.h>
27 #define PI 3.1415926535897932
29 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Angle, AIS_AngleDimension);
30 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Angle, AIS_AngleDimension);
32 SketcherPrs_Angle::SketcherPrs_Angle(ModelAPI_Feature* theConstraint,
33 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
34 : AIS_AngleDimension(gp_Pnt(0,0,0), gp_Pnt(1,0,0), gp_Pnt(0,1,0)), myConstraint(theConstraint), myPlane(thePlane)
36 myAspect = new Prs3d_DimensionAspect();
37 myAspect->MakeArrows3d(false);
38 myAspect->MakeText3d(false);
39 myAspect->MakeTextShaded(false);
40 myAspect->MakeUnitsDisplayed(false);
41 myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
42 myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
44 SetDimensionAspect(myAspect);
45 SetSelToleranceForText2d(SketcherPrs_Tools::getDefaultTextHeight());
47 myStyleListener = new SketcherPrs_DimensionStyleListener();
50 SketcherPrs_Angle::~SketcherPrs_Angle()
52 delete myStyleListener;
55 bool SketcherPrs_Angle::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
56 const std::shared_ptr<GeomAPI_Ax3>&/* thePlane*/)
58 bool aReadyToDisplay = false;
60 DataPtr aData = theConstraint->data();
63 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
64 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
65 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
66 if (!aFlyoutAttr->isInitialized())
67 return aReadyToDisplay; // can not create a good presentation
69 AttributeRefAttrPtr anAttr1 = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
70 if (!anAttr1->isInitialized())
71 return aReadyToDisplay;
73 AttributeRefAttrPtr anAttr2 = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
74 if (!anAttr2->isInitialized())
75 return aReadyToDisplay;
78 ObjectPtr aObj1 = anAttr1->object();
79 ObjectPtr aObj2 = anAttr2->object();
81 std::shared_ptr<GeomAPI_Shape> aShape1 = SketcherPrs_Tools::getShape(aObj1);
82 std::shared_ptr<GeomAPI_Shape> aShape2 = SketcherPrs_Tools::getShape(aObj2);
84 aReadyToDisplay = aShape1.get() && aShape2.get();
85 return aReadyToDisplay;
88 void SketcherPrs_Angle::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
89 const Handle(Prs3d_Presentation)& thePresentation,
90 const Standard_Integer theMode)
92 DataPtr aData = myConstraint->data();
94 if (!IsReadyToDisplay(myConstraint, myPlane)) {
95 Events_Error::throwException("An empty AIS presentation: SketcherPrs_Angle");
96 return; // can not create a good presentation
99 std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
100 ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID()));
101 SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value());
103 AttributeRefAttrPtr anAttr1 = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
104 AttributeRefAttrPtr anAttr2 = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
107 ObjectPtr aObj1 = anAttr1->object();
108 ObjectPtr aObj2 = anAttr2->object();
110 std::shared_ptr<GeomAPI_Shape> aShape1 = SketcherPrs_Tools::getShape(aObj1);
111 std::shared_ptr<GeomAPI_Shape> aShape2 = SketcherPrs_Tools::getShape(aObj2);
113 TopoDS_Shape aTEdge1 = aShape1->impl<TopoDS_Shape>();
114 TopoDS_Shape aTEdge2 = aShape2->impl<TopoDS_Shape>();
116 TopoDS_Edge aEdge1 = TopoDS::Edge(aTEdge1);
117 TopoDS_Edge aEdge2 = TopoDS::Edge(aTEdge2);
121 switch (anAngleType) {
122 case SketcherPrs_Tools::ANGLE_DIRECT: {
123 SetArrowVisible(Standard_False/*first*/, Standard_True/*second*/);
125 SetMeasuredGeometry(aEdge1, aEdge2, Standard_False);
126 bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
127 SetAngleReversed(!isReversedPlanes);
130 case SketcherPrs_Tools::ANGLE_COMPLEMENTARY: {
131 SetArrowVisible(Standard_True/*first*/, Standard_False/*second*/);
132 // to calculate center, first and end points
133 SetAngleReversed(false);
134 SetMeasuredGeometry(aEdge1, aEdge2, Standard_False);
135 /// the first point will be moved, so it is necessary to find distance
136 /// after applying initial parameters of geometry but before correcting them
137 /// for the current type of angle(complementary)
138 aDist = calculateDistanceToFlyoutPoint();
139 /// invert the first edge according to the angle center
140 gp_Pnt aCenter = CenterPoint();
141 gp_Pnt aFirst = FirstPoint();
142 gp_Pnt aSecond = SecondPoint();
143 double anEdge1Length = aCenter.Distance(aFirst);
144 aFirst = aCenter.Translated (gp_Vec(aCenter, aFirst).Normalized() * (-anEdge1Length));
145 anEdge1Length = aCenter.Distance(aFirst);
147 SetMeasuredGeometry(aFirst, aCenter, aSecond);
150 case SketcherPrs_Tools::ANGLE_BACKWARD: {
151 SetArrowVisible(Standard_False/*first*/, Standard_True/*second*/);
153 SetMeasuredGeometry(aEdge1, aEdge2, Standard_False);
154 bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
155 SetAngleReversed(isReversedPlanes);
161 if (aDist < 0) /// it was not calculated yet
162 aDist = calculateDistanceToFlyoutPoint();
165 // Angle value is in degrees
166 AttributeDoublePtr aVal = aData->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
167 SetCustomValue(aVal->value());
169 myAspect->SetExtensionSize(myAspect->ArrowAspect()->Length());
170 myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
172 SketcherPrs_Tools::setDisplaySpecialSymbol(this, aVal->usedParameters().size() > 0);
173 myStyleListener->updateDimensions(this, aVal);
175 AIS_AngleDimension::Compute(thePresentationManager, thePresentation, theMode);
178 void SketcherPrs_Angle::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
179 const Standard_Integer theMode)
181 Standard_Integer aMode;
183 case 0: // we should use selection of all objects
186 case SketcherPrs_Tools::Sel_Dimension_All:
189 case SketcherPrs_Tools::Sel_Dimension_Line:
192 case SketcherPrs_Tools::Sel_Dimension_Text:
196 // there are own selection modes, so the others should be ignored
197 // otherwise, the text selection appears in the viewer
201 AIS_AngleDimension::ComputeSelection(aSelection, aMode);
204 bool SketcherPrs_Angle::isAnglePlaneReversedToSketchPlane()
206 bool aReversed = false;
210 gp_Pln aPlane = GetPlane();
211 gp_Dir aDir = aPlane.Axis().Direction();
212 double aDot = myPlane->normal()->dot(
213 std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z())));
217 double SketcherPrs_Angle::calculateDistanceToFlyoutPoint()
219 const gp_Pnt& aCenter = CenterPoint();
220 const gp_Pnt& aFirst = FirstPoint();
221 const gp_Pnt& aSecond = SecondPoint();
224 DataPtr aData = myConstraint->data();
225 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
226 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
227 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
228 std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = myPlane->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
230 gp_Dir aBisector((aFirst.XYZ() + aSecond.XYZ()) * 0.5 - aCenter.XYZ());
231 gp_Pnt aFlyPnt(aFlyoutPnt->x(), aFlyoutPnt->y(), aFlyoutPnt->z());
232 gp_XYZ aFlyDir = aFlyPnt.XYZ() - aCenter.XYZ();
233 double aDistance = aFlyDir.Dot(aBisector.XYZ());
234 // make a positive distance in order to AIS angle presentation is not reversed
235 aDistance = fabs(aDistance);