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