Salome HOME
Issue #1299, Issue #1393 Angle constraint: support of additional and complementary...
[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 #include <Events_Error.h>
29
30 #define PI 3.1415926535897932
31
32 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Angle, AIS_AngleDimension);
33 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Angle, AIS_AngleDimension);
34
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)
38 {
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());
46   
47   SetDimensionAspect(myAspect);
48   SetSelToleranceForText2d(SketcherPrs_Tools::getDefaultTextHeight());
49
50   myStyleListener = new SketcherPrs_DimensionStyleListener();
51 }
52
53 SketcherPrs_Angle::~SketcherPrs_Angle()
54 {
55   delete myStyleListener;
56 }
57
58 bool SketcherPrs_Angle::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
59                                          const std::shared_ptr<GeomAPI_Ax3>&/* thePlane*/)
60 {
61   bool aReadyToDisplay = false;
62
63   DataPtr aData = theConstraint->data();
64
65   // Flyout point
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
71
72   AttributeRefAttrPtr anAttr1 = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
73   if (!anAttr1->isInitialized())
74     return aReadyToDisplay;
75
76   AttributeRefAttrPtr anAttr2 = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
77   if (!anAttr2->isInitialized())
78     return aReadyToDisplay;
79
80   // Get angle edges
81   ObjectPtr aObj1 = anAttr1->object();
82   ObjectPtr aObj2 = anAttr2->object();
83
84   std::shared_ptr<GeomAPI_Shape> aShape1 = SketcherPrs_Tools::getShape(aObj1);
85   std::shared_ptr<GeomAPI_Shape> aShape2 = SketcherPrs_Tools::getShape(aObj2);
86
87   aReadyToDisplay = aShape1.get() && aShape2.get();
88   return aReadyToDisplay;
89 }
90
91 bool SketcherPrs_Angle::getPoints(gp_Pnt& theFirstPoint, gp_Pnt& theSecondPoint,
92                                   gp_Pnt& theCenterPoint, double& theAngle) const
93 {
94   bool aReadyToDisplay = false;
95
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());
100
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()));
109
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()));
115   else {
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));
121   }
122   theAngle = anAng->angleDegree();
123
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>();
127
128   gp_Pnt2d aCenterPoint = anAng->center()->impl<gp_Pnt2d>();
129   aPoint = myPlane->to3D(aCenterPoint.X(), aCenterPoint.Y());
130   theCenterPoint = aPoint->impl<gp_Pnt>();
131
132   gp_Pnt2d aSecondPoint = anAng->secondPoint()->impl<gp_Pnt2d>();
133   aPoint = myPlane->to3D(aSecondPoint.X(), aSecondPoint.Y());
134   theSecondPoint = aPoint->impl<gp_Pnt>();
135
136   return aReadyToDisplay;
137 }
138
139 void SketcherPrs_Angle::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
140                                 const Handle(Prs3d_Presentation)& thePresentation, 
141                                 const Standard_Integer theMode)
142 {
143   DataPtr aData = myConstraint->data();
144
145   if (!IsReadyToDisplay(myConstraint, myPlane)) {
146     Events_Error::throwException("An empty AIS presentation: SketcherPrs_Angle");
147     return; // can not create a good presentation
148   }
149
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());
153
154   gp_Pnt aFirstPoint, aSecondPoint, aCenterPoint;
155   double aValue;
156   getPoints(aFirstPoint, aSecondPoint, aCenterPoint, aValue);
157
158   double aDist = -1;
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);
165     }
166     break;
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);
172     }
173     break;
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);
179     }
180     break;
181     default:
182       break;
183   }
184   if (aDist < 0) /// it was not calculated yet
185     aDist = calculateDistanceToFlyoutPoint();
186   SetFlyout(aDist);
187
188   // Angle value is in degrees
189   AttributeDoublePtr aVal = aData->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
190   SetCustomValue(aValue);
191
192   myAspect->SetExtensionSize(myAspect->ArrowAspect()->Length());
193   myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
194
195   SketcherPrs_Tools::setDisplaySpecialSymbol(this, aVal->usedParameters().size() > 0);
196   myStyleListener->updateDimensions(this, aVal);
197
198   AIS_AngleDimension::Compute(thePresentationManager, thePresentation, theMode);
199 }
200
201 void SketcherPrs_Angle::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
202                                                    const Standard_Integer theMode)
203 {
204   Standard_Integer aMode;
205   switch (theMode) {
206   case 0: // we should use selection of all objects
207     aMode = 0;
208     break;
209   case SketcherPrs_Tools::Sel_Dimension_All:
210     aMode = 0;
211     break;
212   case SketcherPrs_Tools::Sel_Dimension_Line:
213     aMode = 1;
214     break;
215   case SketcherPrs_Tools::Sel_Dimension_Text:
216     aMode = 2;
217     break;
218   default: {
219     // there are own selection modes, so the others should be ignored
220     // otherwise, the text selection appears in the viewer
221     return; 
222   }
223   }
224   AIS_AngleDimension::ComputeSelection(aSelection, aMode);
225 }
226
227 bool SketcherPrs_Angle::isAnglePlaneReversedToSketchPlane()
228 {
229   bool aReversed = false;
230   if (!myPlane.get())
231     return aReversed;
232
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())));
237   return aDot < 0;
238 }
239
240 double SketcherPrs_Angle::calculateDistanceToFlyoutPoint()
241 {
242   const gp_Pnt& aCenter = CenterPoint();
243   const gp_Pnt& aFirst = FirstPoint();
244   const gp_Pnt& aSecond = SecondPoint();
245
246   // Flyout point
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());
252
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);
259   return aDistance;
260 }