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