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