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