Salome HOME
Issue #3041: SIGSEGV when changing sketch plane
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Angle.cpp
index c7273954548de55d0a188beb3dcb891e2c22177d..f5bcf8e3de258adabb2791c2ae02b9090f5b3ed4 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 //
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/ or
-// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 #include "SketcherPrs_Angle.h"
 #include <TopExp.hxx>
 #include <BRep_Tool.hxx>
 
+/// Update variable aspect parameters (depending on viewer scale)
+/// \param theDimAspect an aspect to be changed
+/// \param theDimValue an arrow value
+/// \param theTextSize an arrow value
+extern void updateArrows(Handle(Prs3d_DimensionAspect) theDimAspect,
+  double theDimValue, double theTextSize, SketcherPrs_Tools::LocationType theLocationType);
+
 #define PI 3.1415926535897932
 
 //#ifndef WNT
@@ -48,9 +54,9 @@
 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Angle, AIS_AngleDimension);
 
 SketcherPrs_Angle::SketcherPrs_Angle(ModelAPI_Feature* theConstraint,
-                                     const std::shared_ptr<GeomAPI_Ax3>& thePlane)
+  SketchPlugin_Sketch* theSketcher)
 : AIS_AngleDimension(gp_Pnt(0,0,0), gp_Pnt(1,0,0), gp_Pnt(0,1,0)), myConstraint(theConstraint),
-  mySketcherPlane(thePlane),
+  mySketcher(theSketcher),
   myFirstPoint(gp_Pnt(0,0,0)), myCenterPoint(gp_Pnt(1,0,0)), mySecondPoint(gp_Pnt(0,1,0)),
   myValue(90., false, ""), myFlyOutPoint(0, 0.5, 0)
 {
@@ -59,7 +65,7 @@ SketcherPrs_Angle::SketcherPrs_Angle(ModelAPI_Feature* theConstraint,
   myAspect->MakeText3d(false);
   myAspect->MakeTextShaded(false);
   myAspect->MakeUnitsDisplayed(false);
-  myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getDefaultTextHeight());
+  myAspect->TextAspect()->SetHeight(SketcherPrs_Tools::getConfigTextHeight());
   myAspect->ArrowAspect()->SetLength(SketcherPrs_Tools::getArrowSize());
 
   SetDimensionAspect(myAspect);
@@ -159,8 +165,10 @@ void SketcherPrs_Angle::Compute(const Handle(PrsMgr_PresentationManager3d)& theP
                                 const Handle(Prs3d_Presentation)& thePresentation,
                                 const Standard_Integer theMode)
 {
+  if (!plane().get())
+    return;
   gp_Pnt aFirstPoint, aSecondPoint, aCenterPoint;
-  bool aReadyToDisplay = readyToDisplay(myConstraint, mySketcherPlane,
+  bool aReadyToDisplay = readyToDisplay(myConstraint, plane(),
                                         aFirstPoint, aSecondPoint, aCenterPoint);
   if (aReadyToDisplay) {
     myFirstPoint = aFirstPoint;
@@ -176,7 +184,7 @@ void SketcherPrs_Angle::Compute(const Handle(PrsMgr_PresentationManager3d)& theP
                                 std::dynamic_pointer_cast<GeomDataAPI_Point2D>
                                 (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
     std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt =
-      mySketcherPlane->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
+      plane()->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
     myFlyOutPoint = aFlyoutPnt->impl<gp_Pnt>();
   }
 
@@ -230,8 +238,19 @@ void SketcherPrs_Angle::Compute(const Handle(PrsMgr_PresentationManager3d)& theP
   // Update text visualization: parameter value or parameter text
   myStyleListener->updateDimensions(this, myValue);
 
-  myAspect->SetExtensionSize(myAspect->ArrowAspect()->Length());
-  myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
+  double aTextSize = 0.0;
+  GetValueString(aTextSize);
+  AttributeIntegerPtr aLocAttr = std::dynamic_pointer_cast<ModelAPI_AttributeInteger>
+    (myConstraint->data()->attribute(SketchPlugin_ConstraintAngle::LOCATION_TYPE_ID()));
+  SketcherPrs_Tools::LocationType aLocationType = aLocAttr->isInitialized() ?
+    (SketcherPrs_Tools::LocationType)(aLocAttr->value()) : SketcherPrs_Tools::LOCATION_AUTOMATIC;
+
+  double aRadius = myCenterPoint.Translated(
+    gp_Vec(myCenterPoint, myFirstPoint).Normalized()*aDist).Distance(myCenterPoint);
+  double anAngleValue = myValue.myDoubleValue;
+  double anAngleCircleLength = aRadius * anAngleValue * PI / 180.;
+
+  updateArrows(myAspect, anAngleCircleLength, aTextSize, aLocationType);
 
   AIS_AngleDimension::Compute(thePresentationManager, thePresentation, theMode);
 
@@ -263,19 +282,19 @@ void SketcherPrs_Angle::ComputeSelection(const Handle(SelectMgr_Selection)& aSel
     return;
   }
   }
-  SetSelToleranceForText2d(SketcherPrs_Tools::getTextHeight());
+  SetSelToleranceForText2d(SketcherPrs_Tools::getArrowSize()/5.);
   AIS_AngleDimension::ComputeSelection(aSelection, aMode);
 }
 
 bool SketcherPrs_Angle::isAnglePlaneReversedToSketchPlane()
 {
   bool aReversed = false;
-  if (!mySketcherPlane.get())
+  if (!plane().get())
     return aReversed;
 
   gp_Pln aPlane = GetPlane();
   gp_Dir aDir = aPlane.Axis().Direction();
-  double aDot = mySketcherPlane->normal()->dot(
+  double aDot = plane()->normal()->dot(
                 std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z())));
   return aDot < 0;
 }