Salome HOME
updated copyright message
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintDistanceVertical.cpp
index 6cebbd4f5f3ffcbf730cb2b3eb2e3f9727055fb1..ea6ce98cf203ec4a2a12088867505bf6dcfcc204 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2017  CEA/DEN, EDF R&D
+// Copyright (C) 2017-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
 //
 // 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
 //
 
 // File:    SketchPlugin_ConstraintDistanceVertical.cpp
 #include <SketchPlugin_ConstraintDistanceVertical.h>
 
 #include <SketcherPrs_Tools.h>
-#include <SketcherPrs_Factory.h>
 
-#include <GeomAPI_Dir2d.h>
 #include <GeomAPI_XY.h>
 #include <GeomDataAPI_Point2D.h>
 
-#include <ModelAPI_AttributeDouble.h>
-#include <ModelAPI_AttributeInteger.h>
-
-const double tolerance = 1e-7;
-
 
 SketchPlugin_ConstraintDistanceVertical::SketchPlugin_ConstraintDistanceVertical()
-  : SketchPlugin_ConstraintDistance()
+  : SketchPlugin_ConstraintDistanceAlongDir()
 {
 }
 
-//*************************************************************************************
-void SketchPlugin_ConstraintDistanceVertical::initAttributes()
-{
-  data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
-  data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
-  data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
-  data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
-
-  data()->addAttribute(SketchPlugin_ConstraintDistanceVertical::LOCATION_TYPE_ID(),
-                       ModelAPI_AttributeInteger::typeId());
-  data()->integer(LOCATION_TYPE_ID())->setValue(SketcherPrs_Tools::LOCATION_AUTOMATIC);
-}
-
-//*************************************************************************************
-void SketchPlugin_ConstraintDistanceVertical::execute()
-{
-  AttributeDoublePtr anAttrValue = real(SketchPlugin_Constraint::VALUE());
-  if (anAttrValue->isInitialized() || !areAttributesInitialized())
-    return;
-
-  double aDistance = calculateCurrentDistance();
-  anAttrValue->setValue(aDistance);
-}
-
-//*************************************************************************************
-AISObjectPtr SketchPlugin_ConstraintDistanceVertical::getAISObject(AISObjectPtr thePrevious)
-{
-  if (!sketch())
-    return thePrevious;
-
-  AISObjectPtr anAIS = SketcherPrs_Factory::lengthDimensionConstraint(this,
-                                                                      sketch()->coordinatePlane(),
-                                                                      thePrevious);
-  return anAIS;
-}
-
 double SketchPlugin_ConstraintDistanceVertical::calculateCurrentDistance()
 {
   std::shared_ptr<ModelAPI_Data> aData = data();
@@ -90,45 +46,19 @@ double SketchPlugin_ConstraintDistanceVertical::calculateCurrentDistance()
   return aPointB->y() - aPointA->y();
 }
 
-void SketchPlugin_ConstraintDistanceVertical::attributeChanged(const std::string& theID)
+void SketchPlugin_ConstraintDistanceVertical::updateFlyoutPoint()
 {
-  if (theID == SketchPlugin_Constraint::ENTITY_A() ||
-      theID == SketchPlugin_Constraint::ENTITY_B())
-  {
-    AttributeDoublePtr aValueAttr = real(SketchPlugin_Constraint::VALUE());
-    if (!aValueAttr->isInitialized() && areAttributesInitialized()) {
-      // only if it is not initialized, try to compute the current value
-      double aDistance = calculateCurrentDistance();
-      aValueAttr->setValue(aDistance);
-    }
-  } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
-    // Recalculate flyout point in local coordinates of the distance constraint:
-    // the X coordinate is a length of projection of the flyout point on the
-    //                  line binding two distanced points
-    //                  or a line of projection of the distanced point onto the distanced segment
-    // the Y coordinate is a distance from the flyout point to the line
-    std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
-        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-        attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
-    std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
-
-    std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
-    std::shared_ptr<GeomDataAPI_Point2D> aPointA = SketcherPrs_Tools::getFeaturePoint(
-        data(), SketchPlugin_Constraint::ENTITY_A(), aPlane);
-    std::shared_ptr<GeomDataAPI_Point2D> aPointB = SketcherPrs_Tools::getFeaturePoint(
-        data(), SketchPlugin_Constraint::ENTITY_B(), aPlane);
+  std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
+      std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+      attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
+  std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
 
-    std::shared_ptr<GeomAPI_XY> aStartPnt = aPointA->pnt()->xy();
-    std::shared_ptr<GeomAPI_XY> aEndPnt = aPointB->pnt()->xy();
-
-    if (aEndPnt->distance(aStartPnt) < tolerance)
-      return;
+  std::shared_ptr<GeomAPI_Ax3> aPlane = SketchPlugin_Sketch::plane(sketch());
+  std::shared_ptr<GeomDataAPI_Point2D> aEndPoint = SketcherPrs_Tools::getFeaturePoint(
+      data(), SketchPlugin_Constraint::ENTITY_B(), aPlane);
 
-    std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aEndPnt);
-    myFlyoutUpdate = true;
-    double X =  aFlyoutDir->y(); // Dot on OY axis
-    double Y = -aFlyoutDir->x(); // Cross to OY axis
-    aFlyoutAttr->setValue(X, Y);
-    myFlyoutUpdate = false;
-  }
+  std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aEndPoint->pnt()->xy());
+  double X =  aFlyoutDir->y(); // Dot on OY axis
+  double Y = -aFlyoutDir->x(); // Cross to OY axis
+  aFlyoutAttr->setValue(X, Y);
 }