Salome HOME
Issue #1608 : Dimension in sketcher is not always correctly updated
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Angle.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        SketcherPrs_Angle.cpp
4 // Created:     20 August 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "SketcherPrs_Angle.h"
8 #include "SketcherPrs_Tools.h"
9 #include "SketcherPrs_DimensionStyleListener.h"
10
11 #include <SketchPlugin_ConstraintAngle.h>
12 #include <SketchPlugin_Constraint.h>
13 #include <SketchPlugin_Line.h>
14
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>
20
21 #include <ModelAPI_AttributeRefAttr.h>
22 #include <ModelAPI_AttributeDouble.h>
23 #include <ModelAPI_AttributeInteger.h>
24
25 #include <TopExp.hxx>
26 #include <BRep_Tool.hxx>
27
28 #define PI 3.1415926535897932
29
30 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Angle, AIS_AngleDimension);
31 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Angle, AIS_AngleDimension);
32
33 SketcherPrs_Angle::SketcherPrs_Angle(ModelAPI_Feature* theConstraint, 
34                                      const std::shared_ptr<GeomAPI_Ax3>& thePlane)
35 : AIS_AngleDimension(gp_Pnt(0,0,0), gp_Pnt(1,0,0), gp_Pnt(0,1,0)), myConstraint(theConstraint),
36   mySketcherPlane(thePlane),
37   myFirstPoint(gp_Pnt(0,0,0)), myCenterPoint(gp_Pnt(1,0,0)), mySecondPoint(gp_Pnt(0,1,0)),
38   myValue(90., false, ""), myFlyOutPoint(0, 0.5, 0)
39 {
40   myAspect = new Prs3d_DimensionAspect();
41   myAspect->MakeArrows3d(false);
42   myAspect->MakeText3d(false);
43   myAspect->MakeTextShaded(false);
44   myAspect->MakeUnitsDisplayed(false);
45   myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
46   myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
47   
48   SetDimensionAspect(myAspect);
49   SetSelToleranceForText2d(SketcherPrs_Tools::getDefaultTextHeight());
50
51   myStyleListener = new SketcherPrs_DimensionStyleListener();
52 }
53
54 SketcherPrs_Angle::~SketcherPrs_Angle()
55 {
56   delete myStyleListener;
57 }
58
59 bool SketcherPrs_Angle::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
60                                          const std::shared_ptr<GeomAPI_Ax3>& thePlane)
61 {
62   gp_Pnt aFirstPoint, aSecondPoint, aCenterPoint;
63   return readyToDisplay(theConstraint, thePlane, aFirstPoint, aSecondPoint, aCenterPoint);
64 }
65
66 bool SketcherPrs_Angle::readyToDisplay(ModelAPI_Feature* theConstraint,
67                                        const std::shared_ptr<GeomAPI_Ax3>& thePlane,
68                                        gp_Pnt& theFirstPoint, gp_Pnt& theSecondPoint,
69                                        gp_Pnt& theCenterPoint)
70 {
71   bool aReadyToDisplay = false;
72
73   DataPtr aData = theConstraint->data();
74
75   // Flyout point
76   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
77     std::dynamic_pointer_cast<GeomDataAPI_Point2D>
78     (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
79   if (!aFlyoutAttr->isInitialized())
80     return aReadyToDisplay; // can not create a good presentation
81
82   AttributeRefAttrPtr anAttr1 = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
83   if (!anAttr1->isInitialized())
84     return aReadyToDisplay;
85
86   AttributeRefAttrPtr anAttr2 = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
87   if (!anAttr2->isInitialized())
88     return aReadyToDisplay;
89
90   FeaturePtr aLineA = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
91   FeaturePtr aLineB = SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
92
93   if (!aLineA.get() || !aLineB.get())
94     return aReadyToDisplay;
95
96   std::shared_ptr<GeomDataAPI_Point2D> aStartA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
97       aLineA->attribute(SketchPlugin_Line::START_ID()));
98   std::shared_ptr<GeomDataAPI_Point2D> aEndA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
99       aLineA->attribute(SketchPlugin_Line::END_ID()));
100   std::shared_ptr<GeomDataAPI_Point2D> aStartB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
101       aLineB->attribute(SketchPlugin_Line::START_ID()));
102   std::shared_ptr<GeomDataAPI_Point2D> aEndB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
103       aLineB->attribute(SketchPlugin_Line::END_ID()));
104
105   std::shared_ptr<GeomAPI_Angle2d> anAng;
106   if (!aData->attribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID())->isInitialized() ||
107       !aData->attribute(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID())->isInitialized())
108     anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(
109         aStartA->pnt(), aEndA->pnt(), aStartB->pnt(), aEndB->pnt()));
110   else {
111     std::shared_ptr<GeomAPI_Lin2d> aLine1(new GeomAPI_Lin2d(aStartA->pnt(), aEndA->pnt()));
112     bool isReversed1 = aData->boolean(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID())->value();
113     std::shared_ptr<GeomAPI_Lin2d> aLine2(new GeomAPI_Lin2d(aStartB->pnt(), aEndB->pnt()));
114     bool isReversed2 = aData->boolean(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID())->value();
115     anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2));
116   }
117
118   gp_Pnt2d aFirstPoint = anAng->firstPoint()->impl<gp_Pnt2d>();
119   std::shared_ptr<GeomAPI_Pnt> aPoint = thePlane->to3D(aFirstPoint.X(), aFirstPoint.Y());
120   theFirstPoint = aPoint->impl<gp_Pnt>();
121
122   gp_Pnt2d aCenterPoint = anAng->center()->impl<gp_Pnt2d>();
123   aPoint = thePlane->to3D(aCenterPoint.X(), aCenterPoint.Y());
124   theCenterPoint = aPoint->impl<gp_Pnt>();
125
126   gp_Pnt2d aSecondPoint = anAng->secondPoint()->impl<gp_Pnt2d>();
127   aPoint = thePlane->to3D(aSecondPoint.X(), aSecondPoint.Y());
128   theSecondPoint = aPoint->impl<gp_Pnt>();
129
130   return true;
131 }
132
133
134 void SketcherPrs_Angle::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
135                                 const Handle(Prs3d_Presentation)& thePresentation, 
136                                 const Standard_Integer theMode)
137 {
138   gp_Pnt aFirstPoint, aSecondPoint, aCenterPoint;
139   bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane, aFirstPoint, aSecondPoint, aCenterPoint);
140   if (aReadyToDisplay) {
141     myFirstPoint = aFirstPoint;
142     mySecondPoint = aSecondPoint;
143     myCenterPoint = aCenterPoint;
144
145     DataPtr aData = myConstraint->data();
146     AttributeDoublePtr anAttributeValue = aData->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
147     myValue.init(anAttributeValue);
148
149     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
150                                 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
151                                 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
152     std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = mySketcherPlane->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
153     myFlyOutPoint = aFlyoutPnt->impl<gp_Pnt>();
154   }
155
156   DataPtr aData = myConstraint->data();
157   std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
158       ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID()));
159   SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value());
160
161   double aDist = -1;
162   switch (anAngleType) {
163     case SketcherPrs_Tools::ANGLE_DIRECT: {
164       SetArrowVisible(Standard_False/*first*/, Standard_True/*second*/);
165       SetMeasuredGeometry(myFirstPoint, myCenterPoint, mySecondPoint);
166       bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
167       SetAngleReversed(!isReversedPlanes);
168     }
169     break;
170     case SketcherPrs_Tools::ANGLE_COMPLEMENTARY: {
171       double anEdge1Length = aCenterPoint.Distance(myFirstPoint);
172       //aDist = calculateDistanceToFlyoutPoint();
173       gp_Pnt aFirstPoint = aCenterPoint.Translated(
174                           gp_Vec(myCenterPoint, myFirstPoint).Normalized() * (-anEdge1Length));
175       SetMeasuredGeometry(aFirstPoint, myCenterPoint, mySecondPoint);
176       SetAngleReversed(false);
177     }
178     break;
179     case SketcherPrs_Tools::ANGLE_BACKWARD: {
180       SetArrowVisible(Standard_False/*first*/, Standard_True/*second*/);
181       SetMeasuredGeometry(myFirstPoint, myCenterPoint, mySecondPoint);
182       bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
183       SetAngleReversed(isReversedPlanes);
184     }
185     break;
186     default:
187       break;
188   }
189   if (aDist < 0) /// it was not calculated yet
190     aDist = calculateDistanceToFlyoutPoint();
191   SetFlyout(aDist);
192
193   // Update text visualization: parameter value or parameter text
194   myStyleListener->updateDimensions(this, myValue);
195
196   myAspect->SetExtensionSize(myAspect->ArrowAspect()->Length());
197   myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
198
199   AIS_AngleDimension::Compute(thePresentationManager, thePresentation, theMode);
200
201   if (!aReadyToDisplay)
202     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
203                               "An empty AIS presentation: SketcherPrs_Angle");
204 }
205
206 void SketcherPrs_Angle::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
207                                                    const Standard_Integer theMode)
208 {
209   Standard_Integer aMode;
210   switch (theMode) {
211   case 0: // we should use selection of all objects
212     aMode = 0;
213     break;
214   case SketcherPrs_Tools::Sel_Dimension_All:
215     aMode = 0;
216     break;
217   case SketcherPrs_Tools::Sel_Dimension_Line:
218     aMode = 1;
219     break;
220   case SketcherPrs_Tools::Sel_Dimension_Text:
221     aMode = 2;
222     break;
223   default: {
224     // there are own selection modes, so the others should be ignored
225     // otherwise, the text selection appears in the viewer
226     return; 
227   }
228   }
229   AIS_AngleDimension::ComputeSelection(aSelection, aMode);
230 }
231
232 bool SketcherPrs_Angle::isAnglePlaneReversedToSketchPlane()
233 {
234   bool aReversed = false;
235   if (!mySketcherPlane.get())
236     return aReversed;
237
238   gp_Pln aPlane = GetPlane();
239   gp_Dir aDir = aPlane.Axis().Direction();
240   double aDot = mySketcherPlane->normal()->dot(
241                 std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z())));
242   return aDot < 0;
243 }
244
245 double SketcherPrs_Angle::calculateDistanceToFlyoutPoint()
246 {
247   gp_Dir aBisector((myFirstPoint.XYZ() + mySecondPoint.XYZ()) * 0.5 - myCenterPoint.XYZ());
248   //gp_Pnt aFlyPnt(aFlyoutPnt->x(), aFlyoutPnt->y(), aFlyoutPnt->z());
249   gp_XYZ aFlyDir = myFlyOutPoint.XYZ() - myCenterPoint.XYZ();
250   double aDistance = aFlyDir.Dot(aBisector.XYZ());
251   // make a positive distance in order to AIS angle presentation is not reversed
252   aDistance = fabs(aDistance);
253   return aDistance;
254 }