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