Salome HOME
updated copyright message
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintRadius.cpp
index fea246b5c3f5a7e6e8e73977bb58dcb9b9522373..6dc1bd1dfd8d1225d9ecd1aca9b92aa3638001ba 100644 (file)
-// File:    SketchPlugin_ConstraintRadius.cpp
-// Created: 26 May 2014
-// Author:  Artem ZHIDKOV
+// 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 "SketchPlugin_ConstraintRadius.h"
 
 #include <SketchPlugin_Arc.h>
 #include <SketchPlugin_Circle.h>
 #include <SketchPlugin_Point.h>
+#include <SketchPlugin_Tools.h>
+
+#include <SketcherPrs_Factory.h>
+#include <SketcherPrs_Tools.h>
 
 #include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_AttributeInteger.h>
 #include <ModelAPI_Data.h>
+#include <ModelAPI_Session.h>
+#include <ModelAPI_Validator.h>
 
 #include <GeomAPI_Pnt2d.h>
 #include <GeomAPI_Circ.h>
+#include <GeomAPI_Circ2d.h>
+#include <GeomAPI_Dir.h>
+#include <GeomAPI_XY.h>
+#include <GeomAPI_XYZ.h>
 #include <GeomDataAPI_Point2D.h>
 #include <GeomDataAPI_Dir.h>
 
-#include <AIS_InteractiveObject.hxx>
-#include <AIS_RadiusDimension.hxx>
+#include <Config_PropManager.h>
+
+const double tolerance = 1.e-7;
 
 SketchPlugin_ConstraintRadius::SketchPlugin_ConstraintRadius()
 {
+  myFlyoutUpdate = false;
 }
 
 void SketchPlugin_ConstraintRadius::initAttributes()
 {
-  data()->addAttribute(CONSTRAINT_ATTR_VALUE,    ModelAPI_AttributeDouble::type());
-  data()->addAttribute(CONSTRAINT_ATTR_ENTITY_A, ModelAPI_AttributeRefAttr::type());
-  data()->addAttribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT, GeomDataAPI_Point2D::type());
+  data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
+  data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
+  data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
+
+  data()->addAttribute(SketchPlugin_ConstraintRadius::LOCATION_TYPE_ID(),
+                       ModelAPI_AttributeInteger::typeId());
+  ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), LOCATION_TYPE_ID());
+}
+
+void SketchPlugin_ConstraintRadius::colorConfigInfo(std::string& theSection, std::string& theName,
+                                                    std::string& theDefault)
+{
+  theSection = "Visualization";
+  theName = "sketch_dimension_color";
+  theDefault = SKETCH_DIMENSION_COLOR;
 }
 
 void SketchPlugin_ConstraintRadius::execute()
 {
-  if (data()->attribute(CONSTRAINT_ATTR_ENTITY_A)->isInitialized() &&
-      !data()->attribute(CONSTRAINT_ATTR_VALUE)->isInitialized()) {
-
-    boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
-      boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(data()->attribute(CONSTRAINT_ATTR_ENTITY_A));
-    FeaturePtr aFeature = aRef->feature();
-    if (aFeature) {
-      double aRadius = 0;
-      boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
-      if (aFeature->getKind() == SKETCH_CIRCLE_KIND) {
-        AttributeDoublePtr anAttribute = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>
-                                            (aData->attribute(CIRCLE_ATTR_RADIUS));
-        if (anAttribute)
-          aRadius = anAttribute->value();
-      }
-      else if (aFeature->getKind() == SKETCH_ARC_KIND) {
-        boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = 
-          boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
-        boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
-          boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
-        if (aCenterAttr && aStartAttr)
-          aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
-      }
-      boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = 
-        boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(CONSTRAINT_ATTR_VALUE));
-      aValueAttr->setValue(aRadius);
-    }
+  std::shared_ptr<ModelAPI_AttributeRefAttr> aRef = std::dynamic_pointer_cast<
+      ModelAPI_AttributeRefAttr>(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
+  FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
+  if (aFeature) {
+    // the value should to be computed here,
+    // not in the getAISObject in order to change the model value
+    // inside the object transaction. This is important for creating a constraint by preselection.
+    // The display of the presentation in this case happens after the transaction commit
+    std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
+        GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
+    if (!aFlyoutAttr->isInitialized())
+      compute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
+  }
+}
+
+bool SketchPlugin_ConstraintRadius::compute(const std::string& theAttributeId)
+{
+  if (theAttributeId != SketchPlugin_Constraint::FLYOUT_VALUE_PNT())
+    return false;
+
+  std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
+  double aRadius = circleRadius(aCyrcFeature);
+  if (aRadius < 0)
+    return false;
+
+  // Flyout point
+  std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr = std::dynamic_pointer_cast<
+      GeomDataAPI_Point2D>(data()->attribute(theAttributeId));
+  // Prepare a circle
+  if (aCyrcFeature->getKind() == SketchPlugin_Circle::ID()) { // circle
+    std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr =
+      std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+        aCyrcFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
+    double aShift = aRadius * 1.1;
+    std::shared_ptr<GeomAPI_Pnt2d> aPnt = aCenterAttr->pnt();
+    std::shared_ptr<GeomAPI_Pnt2d> aFPnt =
+      std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aPnt->x() + aShift, aPnt->y() + aShift));
+    aFlyoutAttr->setValue(aFPnt);
+  } else { // arc
+    std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
+      GeomDataAPI_Point2D>(aCyrcFeature->data()->attribute(SketchPlugin_Arc::START_ID()));
+    aFlyoutAttr->setValue(aStartAttr->pnt());
   }
+  return true;
 }
 
-Handle(AIS_InteractiveObject) SketchPlugin_ConstraintRadius::getAISShape(
-  Handle_AIS_InteractiveObject thePrevious)
+double SketchPlugin_ConstraintRadius::circleRadius(std::shared_ptr<ModelAPI_Feature>& theCirc)
 {
-  Handle(AIS_InteractiveObject) anAIS = thePrevious;
+  static const double kErrorResult = -1.;
   if (!sketch())
-    return anAIS;
+    return kErrorResult;
 
-  boost::shared_ptr<ModelAPI_Data> aData = data();
-  boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
-    boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
+  std::shared_ptr<ModelAPI_Data> aData = data();
+  std::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = std::dynamic_pointer_cast<
+      ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
   if (!anAttr)
-    return anAIS;
-  FeaturePtr aFeature = anAttr->feature();
-  std::string aKind = aFeature ? aFeature->getKind() : "";
-  if (aKind != SKETCH_CIRCLE_KIND && aKind != SKETCH_ARC_KIND)
-    return anAIS;
-
-  boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = 
-          boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
-  double aValue = aValueAttr->value();
-
-  // an anchor point
-  boost::shared_ptr<GeomDataAPI_Point2D> aAnchorAttr = 
-    boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
-  if (!aAnchorAttr->isInitialized())
-    return anAIS;
-  boost::shared_ptr<GeomAPI_Pnt2d> anAnchor2D = aAnchorAttr->pnt();
-  boost::shared_ptr<GeomAPI_Pnt> anAnchor = sketch()->to3D(anAnchor2D->x(), anAnchor2D->y());
-
-  aData = aFeature->data();
-  boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
-  double aRadius;
-  if (aKind == SKETCH_CIRCLE_KIND) {
-    aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CIRCLE_ATTR_CENTER));
-    AttributeDoublePtr aCircRadius = 
-      boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CIRCLE_ATTR_RADIUS));
-    aRadius = aCircRadius->value();
+    return kErrorResult;
+  theCirc = ModelAPI_Feature::feature(anAttr->object());
+  std::string aKind = theCirc ? theCirc->getKind() : "";
+  if (aKind != SketchPlugin_Circle::ID() && aKind != SketchPlugin_Arc::ID())
+    return kErrorResult;
+
+  DataPtr aCircData = theCirc->data();
+  std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr;
+  if (aKind == SketchPlugin_Circle::ID()) {
+    AttributeDoublePtr aCircRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+        aCircData->attribute(SketchPlugin_Circle::RADIUS_ID()));
+    return aCircRadius->value();
+  } else {
+    aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+        aCircData->attribute(SketchPlugin_Arc::CENTER_ID()));
+    std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
+        GeomDataAPI_Point2D>(aCircData->attribute(SketchPlugin_Arc::START_ID()));
+    return aCenterAttr->pnt()->distance(aStartAttr->pnt());
   }
-  else if (aKind == SKETCH_ARC_KIND) {
-    aCenterAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_CENTER));
-    boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
-      boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(ARC_ATTR_START));
-    aRadius = aCenterAttr->pnt()->distance(aStartAttr->pnt());
-  }
-
-  // a circle
-  boost::shared_ptr<GeomAPI_Pnt> aCenter = sketch()->to3D(aCenterAttr->x(), aCenterAttr->y());
-
-  boost::shared_ptr<GeomDataAPI_Dir> aNDir = 
-    boost::dynamic_pointer_cast<GeomDataAPI_Dir>(sketch()->data()->attribute(SKETCH_ATTR_NORM));
-  boost::shared_ptr<GeomAPI_Dir> aNormal = aNDir->dir();
-
-  boost::shared_ptr<GeomAPI_Circ> aCircle(new GeomAPI_Circ(aCenter, aNormal, aRadius));
-
-  anAnchor = aCircle->project(anAnchor);
-  // TODO: a bug in AIS_RadiusDimension:
-  // The anchor point can't be myCirc.Location() - an exception is raised.
-  // But we need exactly this case...
-  // We want to show a radius dimension starting from the circle centre and 
-  // ending at the user-defined point.
-  // Also, if anchor point coincides with myP2, the radius dimension is not displayed at all.
-  gp_Pnt anAPnt = anAnchor->impl<gp_Pnt>();
-  double aDelta = 1/1000.0;
-  if (anAnchor->x() != 0)
-    anAnchor->setX(anAnchor->x() + aDelta);
-  if (anAnchor->y() != 0)
-    anAnchor->setY(anAnchor->y() + aDelta);
-  if (anAnchor->z() != 0)
-    anAnchor->setZ(anAnchor->z() + aDelta);
-
-  if (anAIS.IsNull())
-  {
-    Handle(AIS_RadiusDimension) aDimAIS = 
-      new AIS_RadiusDimension(aCircle->impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
-    aDimAIS->SetCustomValue(aValue);
-
-    Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
-    anAspect->MakeArrows3d (Standard_False);
-    anAspect->MakeText3d(false);
-    anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
-    anAspect->MakeTextShaded(false);
-    aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
-    aDimAIS->SetDimensionAspect (anAspect);
-    aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
-
-    anAIS = aDimAIS;
-  }
-  else
-  {
-    // update presentation
-    Handle(AIS_RadiusDimension) aDimAIS = Handle(AIS_RadiusDimension)::DownCast(anAIS);
-    if (!aDimAIS.IsNull())
-    {
-      aDimAIS->SetMeasuredGeometry(aCircle->impl<gp_Circ>(), anAnchor->impl<gp_Pnt>());
-      aDimAIS->SetCustomValue(aValue);
-      aDimAIS->Redisplay(Standard_True);
-    }
-  }
-  return anAIS;
+  return kErrorResult;
 }
 
-void SketchPlugin_ConstraintRadius::move(double theDeltaX, double theDeltaY)
+AISObjectPtr SketchPlugin_ConstraintRadius::getAISObject(AISObjectPtr thePrevious)
 {
-  boost::shared_ptr<ModelAPI_Data> aData = data();
-  if (!aData->isValid())
-    return;
+  if (!sketch())
+    return thePrevious;
 
-  //boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
-  //      boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE_PNT));
-  //aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
+  AISObjectPtr anAIS = SketcherPrs_Factory::radiusConstraint(this, sketch(),
+                                                             thePrevious);
+  if (anAIS.get() && !thePrevious.get())
+    SketchPlugin_Tools::setDimensionColor(anAIS);
+  return anAIS;
+}
+
+void SketchPlugin_ConstraintRadius::attributeChanged(const std::string& theID) {
+  if (theID == SketchPlugin_Constraint::ENTITY_A()) {
+    std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
+        ModelAPI_AttributeDouble>(data()->attribute(SketchPlugin_Constraint::VALUE()));
+    if (!aValueAttr->isInitialized()) {
+      // only if it is not initialized, try to compute the current value
+      std::shared_ptr<ModelAPI_Feature> aCyrcFeature;
+      double aRadius = circleRadius(aCyrcFeature);
+      if (aRadius > 0) { // set as value the radius of updated reference to another circle
+        aValueAttr->setValue(aRadius);
+      }
+    }
+  } else if (theID == SketchPlugin_Constraint::FLYOUT_VALUE_PNT() && !myFlyoutUpdate) {
+    // Recalculate flyout point in local coordinates of the circle (or arc):
+    // coordinates are calculated according to center of the shape
+    std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
+        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+        attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
+
+    AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
+        attribute(SketchPlugin_Constraint::ENTITY_A()));
+    if (!aRefAttr || !aRefAttr->isObject())
+      return;
+    FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
+    if (!aFeature || (aFeature->getKind() != SketchPlugin_Arc::ID() &&
+        aFeature->getKind() != SketchPlugin_Circle::ID()))
+      return;
+
+    std::string aCenterAttrName;
+    if (aFeature->getKind() == SketchPlugin_Circle::ID())
+      aCenterAttrName = SketchPlugin_Circle::CENTER_ID();
+    else if (aFeature->getKind() == SketchPlugin_Arc::ID())
+      aCenterAttrName = SketchPlugin_Arc::CENTER_ID();
+    std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<
+        GeomDataAPI_Point2D>(aFeature->data()->attribute(aCenterAttrName));
+    std::shared_ptr<GeomAPI_Pnt2d> aCenter = aCenterAttr->pnt();
+    std::shared_ptr<ModelAPI_AttributeDouble> aRadius = std::dynamic_pointer_cast<
+        ModelAPI_AttributeDouble>(attribute(SketchPlugin_Constraint::VALUE()));
+
+    std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = aFlyoutAttr->pnt();
+    double aDist = aFlyoutPnt->distance(aCenter);
+    if (aDist < tolerance)
+      return;
+
+    myFlyoutUpdate = true;
+    std::shared_ptr<GeomAPI_XY> aFlyoutDir = aFlyoutPnt->xy()->decreased(aCenter->xy());
+    aFlyoutAttr->setValue(aFlyoutDir->x(), aFlyoutDir->y());
+    myFlyoutUpdate = false;
+  }
 }