Salome HOME
updated copyright message
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_Angle.cpp
index bd79f8b4b29846fe75698e4021a7a796d350d1aa..88d57dd1ad221a45737fbe9d091e878a1cb7e8bd 100644 (file)
@@ -1,19 +1,35 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:        SketcherPrs_Angle.cpp
-// Created:     20 August 2015
-// Author:      Vitaly SMETANNIKOV
+// Copyright (C) 2014-2023  CEA, EDF
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// 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
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include "SketcherPrs_Angle.h"
+#include "SketcherPrs_DimensionStyle.h"
 #include "SketcherPrs_Tools.h"
-#include "SketcherPrs_DimensionStyleListener.h"
 
 #include <SketchPlugin_ConstraintAngle.h>
 #include <SketchPlugin_Constraint.h>
+#include <SketchPlugin_Line.h>
 
 #include <GeomAPI_Edge.h>
 #include <GeomAPI_Lin.h>
 #include <GeomDataAPI_Point2D.h>
+#include <GeomAPI_Angle2d.h>
+#include <GeomAPI_Lin2d.h>
 
 #include <ModelAPI_AttributeRefAttr.h>
 #include <ModelAPI_AttributeDouble.h>
 #include <TopExp.hxx>
 #include <BRep_Tool.hxx>
 
-#include <Events_Error.h>
+#include <Standard_Version.hxx>
 
-#define PI 3.1415926535897932
+#if OCC_VERSION_HEX > 0x070400
+#include <PrsDim_TypeOfAngleArrowVisibility.hxx>
+#endif
+
+/// 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);
 
-IMPLEMENT_STANDARD_HANDLE(SketcherPrs_Angle, AIS_AngleDimension);
-IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Angle, AIS_AngleDimension);
+#define PI 3.1415926535897932
 
-SketcherPrs_Angle::SketcherPrs_Angle(ModelAPI_Feature* theConstraint, 
-                                     const std::shared_ptr<GeomAPI_Ax3>& thePlane)
-: AIS_AngleDimension(gp_Pnt(0,0,0), gp_Pnt(1,0,0), gp_Pnt(0,1,0)), myConstraint(theConstraint), myPlane(thePlane)
+//#ifndef WNT
+//  #define COMPILATION_CORRECTION
+//#endif
+
+IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_Angle, PrsDim_AngleDimension);
+
+SketcherPrs_Angle::SketcherPrs_Angle(ModelAPI_Feature* theConstraint,
+                                     SketchPlugin_Sketch* theSketcher)
+: PrsDim_AngleDimension(gp_Pnt(0,0,0), gp_Pnt(1,0,0), gp_Pnt(0,1,0)),
+  myConstraint(theConstraint),
+  mySketcher(theSketcher),
+  myFirstPoint(gp_Pnt(0,0,0)),
+  mySecondPoint(gp_Pnt(0,1,0)),
+  myCenterPoint(gp_Pnt(1, 0, 0)),
+  myFlyOutPoint(0, 0.5, 0),
+  myValue(90., false, "")
 {
   myAspect = new Prs3d_DimensionAspect();
   myAspect->MakeArrows3d(false);
   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);
-  SetSelToleranceForText2d(SketcherPrs_Tools::getDefaultTextHeight());
 
-  myStyleListener = new SketcherPrs_DimensionStyleListener();
+  myStyleListener = new SketcherPrs_DimensionStyle();
 }
 
 SketcherPrs_Angle::~SketcherPrs_Angle()
@@ -53,14 +89,25 @@ SketcherPrs_Angle::~SketcherPrs_Angle()
 }
 
 bool SketcherPrs_Angle::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
-                                         const std::shared_ptr<GeomAPI_Ax3>&/* thePlane*/)
+                                         const std::shared_ptr<GeomAPI_Ax3>& thePlane)
+{
+  gp_Pnt aFirstPoint, aSecondPoint, aCenterPoint;
+  return readyToDisplay(theConstraint, thePlane, aFirstPoint, aSecondPoint, aCenterPoint);
+}
+
+bool SketcherPrs_Angle::readyToDisplay(ModelAPI_Feature* theConstraint,
+                                       const std::shared_ptr<GeomAPI_Ax3>& thePlane,
+                                       gp_Pnt& theFirstPoint, gp_Pnt& theSecondPoint,
+                                       gp_Pnt& theCenterPoint)
 {
   bool aReadyToDisplay = false;
+  if (!thePlane)
+    return aReadyToDisplay;
 
   DataPtr aData = theConstraint->data();
 
   // Flyout point
-  std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
+  std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
     std::dynamic_pointer_cast<GeomDataAPI_Point2D>
     (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
   if (!aFlyoutAttr->isInitialized())
@@ -74,111 +121,153 @@ bool SketcherPrs_Angle::IsReadyToDisplay(ModelAPI_Feature* theConstraint,
   if (!anAttr2->isInitialized())
     return aReadyToDisplay;
 
-  // Get angle edges
-  ObjectPtr aObj1 = anAttr1->object();
-  ObjectPtr aObj2 = anAttr2->object();
-
-  std::shared_ptr<GeomAPI_Shape> aShape1 = SketcherPrs_Tools::getShape(aObj1);
-  std::shared_ptr<GeomAPI_Shape> aShape2 = SketcherPrs_Tools::getShape(aObj2);
-
-  aReadyToDisplay = aShape1.get() && aShape2.get();
-  return aReadyToDisplay;
-}
+  FeaturePtr aLineA =
+    SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
+  FeaturePtr aLineB =
+    SketcherPrs_Tools::getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
 
-void SketcherPrs_Angle::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
-                                const Handle(Prs3d_Presentation)& thePresentation, 
-                                const Standard_Integer theMode)
-{
-  DataPtr aData = myConstraint->data();
+  if (!aLineA.get() || !aLineB.get())
+    return aReadyToDisplay;
 
-  if (!IsReadyToDisplay(myConstraint, myPlane)) {
-    Events_Error::throwException("An empty AIS presentation: SketcherPrs_Angle");
-    return; // can not create a good presentation
+  std::shared_ptr<GeomDataAPI_Point2D> aStartA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+      aLineA->attribute(SketchPlugin_Line::START_ID()));
+  std::shared_ptr<GeomDataAPI_Point2D> aEndA = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+      aLineA->attribute(SketchPlugin_Line::END_ID()));
+  std::shared_ptr<GeomDataAPI_Point2D> aStartB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+      aLineB->attribute(SketchPlugin_Line::START_ID()));
+  std::shared_ptr<GeomDataAPI_Point2D> aEndB = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+      aLineB->attribute(SketchPlugin_Line::END_ID()));
+
+  std::shared_ptr<GeomAPI_Angle2d> anAng;
+  bool isFirstPnt = aData->attribute(
+    SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID())->isInitialized();
+  bool isSecondPnt = aData->attribute(
+    SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID())->isInitialized();
+  if (!isFirstPnt || !isSecondPnt)
+    anAng = std::shared_ptr<GeomAPI_Angle2d>(new GeomAPI_Angle2d(
+        aStartA->pnt(), aEndA->pnt(), aStartB->pnt(), aEndB->pnt()));
+  else {
+    std::shared_ptr<GeomAPI_Lin2d> aLine1(new GeomAPI_Lin2d(aStartA->pnt(), aEndA->pnt()));
+    bool isReversed1 =
+      aData->boolean(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_FIRST_LINE_ID())->value();
+    std::shared_ptr<GeomAPI_Lin2d> aLine2(new GeomAPI_Lin2d(aStartB->pnt(), aEndB->pnt()));
+    bool isReversed2 =
+      aData->boolean(SketchPlugin_ConstraintAngle::ANGLE_REVERSED_SECOND_LINE_ID())->value();
+
+    if (aData->integer(SketchPlugin_ConstraintAngle::TYPE_ID())->value() ==
+        (int)SketcherPrs_Tools::ANGLE_COMPLEMENTARY)
+      isReversed1 = !isReversed1;
+
+    anAng = std::shared_ptr<GeomAPI_Angle2d>(
+      new GeomAPI_Angle2d(aLine1, isReversed1, aLine2, isReversed2));
   }
 
-  std::shared_ptr<ModelAPI_AttributeInteger> aTypeAttr = std::dynamic_pointer_cast<
-      ModelAPI_AttributeInteger>(aData->attribute(SketchPlugin_ConstraintAngle::TYPE_ID()));
-  SketcherPrs_Tools::AngleType anAngleType = (SketcherPrs_Tools::AngleType)(aTypeAttr->value());
-
-  // Flyout point
-  std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = 
-    std::dynamic_pointer_cast<GeomDataAPI_Point2D>
-    (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
-  std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = myPlane->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
+  gp_Pnt2d aCenterPoint = anAng->center()->impl<gp_Pnt2d>();
+  gp_Pnt2d aFirstPoint, aSecondPoint;
+  try {
+    if (anAng->angleRadian() > 0.0) {
+      aFirstPoint = anAng->firstPoint()->impl<gp_Pnt2d>();
+      aSecondPoint = anAng->secondPoint()->impl<gp_Pnt2d>();
+    }
+    else {
+      aFirstPoint = anAng->secondPoint()->impl<gp_Pnt2d>();
+      aSecondPoint = anAng->firstPoint()->impl<gp_Pnt2d>();
+    }
+  }
+  catch (...) {
+    // one of angle lines is degenerated, the presentation cannot be shown
+    return false;
+  }
 
-  AttributeRefAttrPtr anAttr1 = aData->refattr(SketchPlugin_Constraint::ENTITY_A());
-  AttributeRefAttrPtr anAttr2 = aData->refattr(SketchPlugin_Constraint::ENTITY_B());
+  if (aData->integer(SketchPlugin_ConstraintAngle::TYPE_ID())->value() ==
+      (int)SketcherPrs_Tools::ANGLE_BACKWARD)
+    std::swap(aFirstPoint, aSecondPoint);
 
-  // Get angle edges
-  ObjectPtr aObj1 = anAttr1->object();
-  ObjectPtr aObj2 = anAttr2->object();
+  std::shared_ptr<GeomAPI_Pnt> aPoint = thePlane->to3D(aFirstPoint.X(), aFirstPoint.Y());
+  theFirstPoint = aPoint->impl<gp_Pnt>();
 
-  std::shared_ptr<GeomAPI_Shape> aShape1 = SketcherPrs_Tools::getShape(aObj1);
-  std::shared_ptr<GeomAPI_Shape> aShape2 = SketcherPrs_Tools::getShape(aObj2);
+  aPoint = thePlane->to3D(aCenterPoint.X(), aCenterPoint.Y());
+  theCenterPoint = aPoint->impl<gp_Pnt>();
 
-  TopoDS_Shape aTEdge1 = aShape1->impl<TopoDS_Shape>();
-  TopoDS_Shape aTEdge2 = aShape2->impl<TopoDS_Shape>();
+  aPoint = thePlane->to3D(aSecondPoint.X(), aSecondPoint.Y());
+  theSecondPoint = aPoint->impl<gp_Pnt>();
 
-  TopoDS_Edge aEdge1 = TopoDS::Edge(aTEdge1);
-  TopoDS_Edge aEdge2 = TopoDS::Edge(aTEdge2);
+  return true;
+}
 
-  switch (anAngleType) {
-    case SketcherPrs_Tools::ANGLE_DIRECT: {
-      SetGeometryOrientedAngle(true, false);
-      SetArrowVisible(Standard_False, Standard_True);
-      SetMeasuredGeometry(aEdge1, aEdge2);
-    }
-    break;
-    case SketcherPrs_Tools::ANGLE_COMPLEMENTARY: {
-      // to calculate center, first and end points
-      SetGeometryOrientedAngle(false, false);
-      SetMeasuredGeometry(aEdge1, aEdge2);
-      gp_Pnt aCenterPnt = CenterPoint();
-      gp_Pnt aFirstPnt = FirstPoint();
-      gp_Pnt aSecondPnt = SecondPoint();
-      double anEdge2Length = aCenterPnt.Distance(aSecondPnt);
-      aSecondPnt = aCenterPnt.Translated (gp_Vec(aCenterPnt, aSecondPnt).Normalized() * (-anEdge2Length));
-      SetArrowVisible(Standard_True, Standard_False);
-      SetMeasuredGeometry(aFirstPnt, aCenterPnt, aSecondPnt);
-    }
-    break;
-    case SketcherPrs_Tools::ANGLE_BACKWARD: {
-      SetGeometryOrientedAngle(true, true);
-      SetArrowVisible(Standard_False, Standard_True);
-      SetMeasuredGeometry(aEdge1, aEdge2);
-    }
-    break;
-    default:
-      break;
-  }
 
+void SketcherPrs_Angle::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
+                                const Handle(Prs3d_Presentation)& thePresentation,
+                                const Standard_Integer theMode)
+{
+  if (!plane().get())
+    return;
 
-  const gp_Pnt& aCenter = CenterPoint();
-  const gp_Pnt& aFirst = FirstPoint();
-  const gp_Pnt& aSecond = SecondPoint();
+  DataPtr aData = myConstraint->data();
 
-  gp_Dir aBisector((aFirst.XYZ() + aSecond.XYZ()) * 0.5 - aCenter.XYZ());
+  gp_Pnt aFirstPoint, aSecondPoint, aCenterPoint;
+  bool aReadyToDisplay = readyToDisplay(myConstraint, plane(),
+                                        aFirstPoint, aSecondPoint, aCenterPoint);
+  if (aReadyToDisplay) {
+    myFirstPoint = aFirstPoint;
+    mySecondPoint = aSecondPoint;
+    myCenterPoint = aCenterPoint;
+
+    AttributeDoublePtr anAttributeValue =
+      aData->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
+    myValue.init(anAttributeValue);
+
+    std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
+                                std::dynamic_pointer_cast<GeomDataAPI_Point2D>
+                                (aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
+    std::shared_ptr<GeomAPI_Pnt> aFlyoutPnt =
+      plane()->to3D(aFlyoutAttr->x(), aFlyoutAttr->y());
+    myFlyOutPoint = aFlyoutPnt->impl<gp_Pnt>();
+  }
 
-  gp_Pnt aFlyPnt(aFlyoutPnt->x(), aFlyoutPnt->y(), aFlyoutPnt->z());
-  gp_XYZ aFlyDir = aFlyPnt.XYZ() - aCenter.XYZ();
-  double aDist = aFlyDir.Dot(aBisector.XYZ());
-  // make a positive distance in order to AIS angle presentation is not reversed
-  aDist = fabs(aDist);
+  double aDist = -1;
+#ifndef COMPILATION_CORRECTION
+#if OCC_VERSION_HEX > 0x070400
+  SetArrowsVisibility(PrsDim_TypeOfAngleArrowVisibility_Both);
+#else
+  SetArrowsVisibility(AIS_TOAV_Both);
+#endif
+#endif
+  SetMeasuredGeometry(myFirstPoint, myCenterPoint, mySecondPoint);
+#ifndef COMPILATION_CORRECTION
+  bool isReversedPlanes = isAnglePlaneReversedToSketchPlane();
+#if OCC_VERSION_HEX > 0x070400
+  SetType(!isReversedPlanes ? PrsDim_TypeOfAngle_Exterior : PrsDim_TypeOfAngle_Interior);
+#else
+  SetType(!isReversedPlanes ? AIS_TOA_Exterior : AIS_TOA_Interior);
+#endif
+#endif
+  if (aDist < 0) /// it was not calculated yet
+    aDist = calculateDistanceToFlyoutPoint();
   SetFlyout(aDist);
 
-  // Angle value is in degrees
-  AttributeDoublePtr aVal = aData->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
-  SetCustomValue(aVal->value() * PI / 180.0);
+  // Update text visualization: parameter value or parameter text
+  myStyleListener->updateDimensions(this, myValue);
+
+  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;
 
-  myAspect->SetExtensionSize(myAspect->ArrowAspect()->Length());
-  myAspect->SetArrowTailSize(myAspect->ArrowAspect()->Length());
+  double aRadius = myCenterPoint.Translated(
+    gp_Vec(myCenterPoint, myFirstPoint).Normalized()*aDist).Distance(myCenterPoint);
+  double anAngleValue = myValue.myDoubleValue;
+  double anAngleCircleLength = aRadius * anAngleValue * PI / 180.;
 
-  AttributeDoublePtr aValue = myConstraint->data()->real(SketchPlugin_ConstraintAngle::ANGLE_VALUE_ID());
-  SketcherPrs_Tools::setDisplaySpecialSymbol(this, aValue->usedParameters().size() > 0);
+  updateArrows(myAspect, anAngleCircleLength, aTextSize, aLocationType);
 
-  myStyleListener->updateDimensions(this, aValue);
+  PrsDim_AngleDimension::Compute(thePresentationManager, thePresentation, theMode);
 
-  AIS_AngleDimension::Compute(thePresentationManager, thePresentation, theMode);
+  if (!aReadyToDisplay)
+    SketcherPrs_Tools::sendEmptyPresentationError(myConstraint,
+                              "An empty AIS presentation: SketcherPrs_Angle");
 }
 
 void SketcherPrs_Angle::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
@@ -201,8 +290,27 @@ void SketcherPrs_Angle::ComputeSelection(const Handle(SelectMgr_Selection)& aSel
   default: {
     // there are own selection modes, so the others should be ignored
     // otherwise, the text selection appears in the viewer
-    return; 
+    return;
   }
   }
-  AIS_AngleDimension::ComputeSelection(aSelection, aMode);
+  SetSelToleranceForText2d(SketcherPrs_Tools::getArrowSize()/5.);
+  PrsDim_AngleDimension::ComputeSelection(aSelection, aMode);
+}
+
+bool SketcherPrs_Angle::isAnglePlaneReversedToSketchPlane()
+{
+  bool aReversed = false;
+  if (!plane().get())
+    return aReversed;
+
+  gp_Pln aPlane = GetPlane();
+  gp_Dir aDir = aPlane.Axis().Direction();
+  double aDot = plane()->normal()->dot(
+                std::shared_ptr<GeomAPI_Dir>(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z())));
+  return aDot < 0;
+}
+
+double SketcherPrs_Angle::calculateDistanceToFlyoutPoint()
+{
+  return myFlyOutPoint.Distance(myCenterPoint);
 }