Salome HOME
Merge branch 'V9_4_BR'
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Angle.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "SketcherPrs_Angle.h"
21 #include "SketcherPrs_DimensionStyleListener.h"
22 #include "SketcherPrs_Tools.h"
23
24 #include <SketchPlugin_ConstraintAngle.h>
25 #include <SketchPlugin_Constraint.h>
26 #include <SketchPlugin_Line.h>
27
28 #include <GeomAPI_Edge.h>
29 #include <GeomAPI_Lin.h>
30 #include <GeomDataAPI_Point2D.h>
31 #include <GeomAPI_Angle2d.h>
32 #include <GeomAPI_Lin2d.h>
33
34 #include <ModelAPI_AttributeRefAttr.h>
35 #include <ModelAPI_AttributeDouble.h>
36 #include <ModelAPI_AttributeInteger.h>
37
38 #include <TopExp.hxx>
39 #include <BRep_Tool.hxx>
40
41 /// Update variable aspect parameters (depending on viewer scale)
42 /// \param theDimAspect an aspect to be changed
43 /// \param theDimValue an arrow value
44 /// \param theTextSize an arrow value
45 extern void updateArrows(Handle(Prs3d_DimensionAspect) theDimAspect,
46   double theDimValue, double theTextSize, SketcherPrs_Tools::LocationType theLocationType);
47
48 #define PI 3.1415926535897932
49
50 //#ifndef WNT
51 //  #define COMPILATION_CORRECTION
52 //#endif
53
54 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Angle, AIS_AngleDimension);
55
56 SketcherPrs_Angle::SketcherPrs_Angle(ModelAPI_Feature* theConstraint,
57   SketchPlugin_Sketch* theSketcher)
58 : AIS_AngleDimension(gp_Pnt(0,0,0), gp_Pnt(1,0,0), gp_Pnt(0,1,0)), myConstraint(theConstraint),
59   mySketcher(theSketcher),
60   myFirstPoint(gp_Pnt(0,0,0)), myCenterPoint(gp_Pnt(1,0,0)), mySecondPoint(gp_Pnt(0,1,0)),
61   myValue(90., false, ""), myFlyOutPoint(0, 0.5, 0)
62 {
63   myAspect = new Prs3d_DimensionAspect();
64   myAspect->MakeArrows3d(false);
65   myAspect->MakeText3d(false);
66   myAspect->MakeTextShaded(false);
67   myAspect->MakeUnitsDisplayed(false);
68   myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getConfigTextHeight());
69   myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
70
71   SetDimensionAspect(myAspect);
72
73   myStyleListener = new SketcherPrs_DimensionStyleListener();
74 }
75
76 SketcherPrs_Angle::~SketcherPrs_Angle()
77 {
78   delete myStyleListener;
79 }
80
81 bool SketcherPrs_Angle::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
82                                          const std::shared_ptr<GeomAPI_Ax3>& thePlane)
83 {
84   gp_Pnt aFirstPoint, aSecondPoint, aCenterPoint;
85   return readyToDisplay(theConstraint, thePlane, aFirstPoint, aSecondPoint, aCenterPoint);
86 }
87
88 bool SketcherPrs_Angle::readyToDisplay(ModelAPI_Feature* theConstraint,
89                                        const std::shared_ptr<GeomAPI_Ax3>& thePlane,
90                                        gp_Pnt& theFirstPoint, gp_Pnt& theSecondPoint,
91                                        gp_Pnt& theCenterPoint)
92 {
93   bool aReadyToDisplay = false;
94
95   DataPtr aData = theConstraint->data();
96
97   // Flyout point
98   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
99     std::dynamic_pointer_cast<GeomDataAPI_Point2D>
100     (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
101   if (!aFlyoutAttr->isInitialized())
102     return aReadyToDisplay; // can not create a good presentation
103
104   AttributeRefAttrPtr anAttr1 = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
105   if (!anAttr1->isInitialized())
106     return aReadyToDisplay;
107
108   AttributeRefAttrPtr anAttr2 = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
109   if (!anAttr2->isInitialized())
110     return aReadyToDisplay;
111
112   FeaturePtr aLineA =
113     SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
114   FeaturePtr aLineB =
115     SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
116
117   if (!aLineA.get() || !aLineB.get())
118     return aReadyToDisplay;
119
120   std::shared_ptr<GeomDataAPI_Point2D> aStartA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
121       aLineA->attribute(SketchPlugin_Line::START_ID()));
122   std::shared_ptr<GeomDataAPI_Point2D> aEndA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
123       aLineA->attribute(SketchPlugin_Line::END_ID()));
124   std::shared_ptr<GeomDataAPI_Point2D> aStartB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
125       aLineB->attribute(SketchPlugin_Line::START_ID()));
126   std::shared_ptr<GeomDataAPI_Point2D> aEndB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
127       aLineB->attribute(SketchPlugin_Line::END_ID()));
128
129   std::shared_ptr<GeomAPI_Angle2d> anAng;
130   bool isFirstPnt = aData->attribute(
131     SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID())->isInitialized();
132   bool isSecondPnt = aData->attribute(
133     SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID())->isInitialized();
134   if (!isFirstPnt || !isSecondPnt)
135     anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(
136         aStartA->pnt(), aEndA->pnt(), aStartB->pnt(), aEndB->pnt()));
137   else {
138     std::shared_ptr<GeomAPI_Lin2d> aLine1(new GeomAPI_Lin2d(aStartA->pnt(), aEndA->pnt()));
139     bool isReversed1 =
140       aData->boolean(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID())->value();
141     std::shared_ptr<GeomAPI_Lin2d> aLine2(new GeomAPI_Lin2d(aStartB->pnt(), aEndB->pnt()));
142     bool isReversed2 =
143       aData->boolean(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID())->value();
144     anAng = std::shared_ptr<GeomAPI_Angle2d>(
145       new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2));
146   }
147
148   gp_Pnt2d aCenterPoint = anAng->center()->impl<gp_Pnt2d>();
149   gp_Pnt2d aFirstPoint, aSecondPoint;
150   if (anAng->angleRadian() > 0.0) {
151     aFirstPoint = anAng->firstPoint()->impl<gp_Pnt2d>();
152     aSecondPoint = anAng->secondPoint()->impl<gp_Pnt2d>();
153   }
154   else {
155     aFirstPoint = anAng->secondPoint()->impl<gp_Pnt2d>();
156     aSecondPoint = anAng->firstPoint()->impl<gp_Pnt2d>();
157   }
158
159
160   std::shared_ptr<GeomAPI_Pnt> aPoint = thePlane->to3D(aFirstPoint.X(), aFirstPoint.Y());
161   theFirstPoint = aPoint->impl<gp_Pnt>();
162
163   aPoint = thePlane->to3D(aCenterPoint.X(), aCenterPoint.Y());
164   theCenterPoint = aPoint->impl<gp_Pnt>();
165
166   aPoint = thePlane->to3D(aSecondPoint.X(), aSecondPoint.Y());
167   theSecondPoint = aPoint->impl<gp_Pnt>();
168
169   return true;
170 }
171
172
173 void SketcherPrs_Angle::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
174                                 const Handle(Prs3d_Presentation)& thePresentation,
175                                 const Standard_Integer theMode)
176 {
177   if (!plane().get())
178     return;
179
180   DataPtr aData = myConstraint->data();
181
182   gp_Pnt aFirstPoint, aSecondPoint, aCenterPoint;
183   bool aReadyToDisplay = readyToDisplay(myConstraint, plane(),
184                                         aFirstPoint, aSecondPoint, aCenterPoint);
185   if (aReadyToDisplay) {
186     myFirstPoint = aFirstPoint;
187     mySecondPoint = aSecondPoint;
188     myCenterPoint = aCenterPoint;
189
190     AttributeDoublePtr anAttributeValue =
191       aData->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
192     myValue.init(anAttributeValue);
193
194     std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
195                                 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
196                                 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
197     std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt =
198       plane()->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
199     myFlyOutPoint = aFlyoutPnt->impl<gp_Pnt>();
200   }
201
202   std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
203       ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID()));
204   SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value());
205
206   double aDist = -1;
207   switch (anAngleType) {
208     case SketcherPrs_Tools::ANGLE_DIRECT: {
209 #ifndef COMPILATION_CORRECTION
210       SetArrowsVisibility(AIS_TOAV_Both);
211 #endif
212       SetMeasuredGeometry(myFirstPoint, myCenterPoint, mySecondPoint);
213 #ifndef COMPILATION_CORRECTION
214       bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
215       SetType(!isReversedPlanes ? AIS_TOA_Exterior : AIS_TOA_Interior);
216 #endif
217     }
218     break;
219     case SketcherPrs_Tools::ANGLE_COMPLEMENTARY: {
220       double anEdge1Length = aCenterPoint.Distance(myFirstPoint);
221       //aDist = calculateDistanceToFlyoutPoint();
222       gp_Pnt aFirstPoint = aCenterPoint.Translated(
223                           gp_Vec(myCenterPoint, myFirstPoint).Normalized() * (-anEdge1Length));
224       SetMeasuredGeometry(aFirstPoint, myCenterPoint, mySecondPoint);
225 #ifndef COMPILATION_CORRECTION
226       SetType(AIS_TOA_Interior);
227 #endif
228     }
229     break;
230     case SketcherPrs_Tools::ANGLE_BACKWARD: {
231 #ifndef COMPILATION_CORRECTION
232       SetArrowsVisibility(AIS_TOAV_Both);
233 #endif
234       SetMeasuredGeometry(myFirstPoint, myCenterPoint, mySecondPoint);
235       bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
236 #ifndef COMPILATION_CORRECTION
237       SetType(isReversedPlanes ? AIS_TOA_Exterior : AIS_TOA_Interior);
238 #endif
239     }
240     break;
241     default:
242       break;
243   }
244   if (aDist < 0) /// it was not calculated yet
245     aDist = calculateDistanceToFlyoutPoint();
246   SetFlyout(aDist);
247
248   // Update text visualization: parameter value or parameter text
249   myStyleListener->updateDimensions(this, myValue);
250
251   double aTextSize = 0.0;
252   GetValueString(aTextSize);
253   AttributeIntegerPtr aLocAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>
254     (myConstraint->data()->attribute(SketchPlugin_ConstraintAngle::LOCATION_TYPE_ID()));
255   SketcherPrs_Tools::LocationType aLocationType = aLocAttr->isInitialized() ?
256     (SketcherPrs_Tools::LocationType)(aLocAttr->value()) : SketcherPrs_Tools::LOCATION_AUTOMATIC;
257
258   double aRadius = myCenterPoint.Translated(
259     gp_Vec(myCenterPoint, myFirstPoint).Normalized()*aDist).Distance(myCenterPoint);
260   double anAngleValue = myValue.myDoubleValue;
261   double anAngleCircleLength = aRadius * anAngleValue * PI / 180.;
262
263   updateArrows(myAspect, anAngleCircleLength, aTextSize, aLocationType);
264
265   AIS_AngleDimension::Compute(thePresentationManager, thePresentation, theMode);
266
267   if (!aReadyToDisplay)
268     SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
269                               "An empty AIS presentation: SketcherPrs_Angle");
270 }
271
272 void SketcherPrs_Angle::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
273                                                    const Standard_Integer theMode)
274 {
275   Standard_Integer aMode;
276   switch (theMode) {
277   case 0: // we should use selection of all objects
278     aMode = 0;
279     break;
280   case SketcherPrs_Tools::Sel_Dimension_All:
281     aMode = 0;
282     break;
283   case SketcherPrs_Tools::Sel_Dimension_Line:
284     aMode = 1;
285     break;
286   case SketcherPrs_Tools::Sel_Dimension_Text:
287     aMode = 2;
288     break;
289   default: {
290     // there are own selection modes, so the others should be ignored
291     // otherwise, the text selection appears in the viewer
292     return;
293   }
294   }
295   SetSelToleranceForText2d(SketcherPrs_Tools::getArrowSize()/5.);
296   AIS_AngleDimension::ComputeSelection(aSelection, aMode);
297 }
298
299 bool SketcherPrs_Angle::isAnglePlaneReversedToSketchPlane()
300 {
301   bool aReversed = false;
302   if (!plane().get())
303     return aReversed;
304
305   gp_Pln aPlane = GetPlane();
306   gp_Dir aDir = aPlane.Axis().Direction();
307   double aDot = plane()->normal()->dot(
308                 std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z())));
309   return aDot < 0;
310 }
311
312 double SketcherPrs_Angle::calculateDistanceToFlyoutPoint()
313 {
314   return myFlyOutPoint.Distance(myCenterPoint);
315 }