]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Precise calculation of the flyout point for the Radius constraint
authorazv <azv@opencascade.com>
Thu, 16 Apr 2015 04:25:24 +0000 (07:25 +0300)
committerazv <azv@opencascade.com>
Thu, 16 Apr 2015 09:50:42 +0000 (12:50 +0300)
src/SketchPlugin/SketchPlugin_ConstraintRadius.cpp
src/SketchPlugin/SketchPlugin_ConstraintRadius.h
src/SketcherPrs/SketcherPrs_LengthDimension.cpp
src/SketcherPrs/SketcherPrs_Tools.cpp
src/SketcherPrs/SketcherPrs_Tools.h

index af53efa5d5424e0ba4a0ebfce0cd607c9cdd2f66..9530bc2add833bcc01071a6d07379deaa65276b6 100644 (file)
 #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 <Config_PropManager.h>
 
+const double tolerance = 1.e-7;
+
 SketchPlugin_ConstraintRadius::SketchPlugin_ConstraintRadius()
 {
 }
@@ -158,19 +161,9 @@ void SketchPlugin_ConstraintRadius::move(double theDeltaX, double theDeltaY)
   if (!aData->isValid())
     return;
 
-  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)
-    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();
+  // The flyout point is calculated in local coordinates of the shape,
+  // so the center should be coincident with origin
+  std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(0.0, 0.0));
 
   // The specified delta applied on the circle curve, 
   // so it will be scaled due to distance between flyout and center points
@@ -187,7 +180,9 @@ void SketchPlugin_ConstraintRadius::move(double theDeltaX, double theDeltaY)
   aFlyout->setY(aFlyout->y() + aScale * theDeltaY);
   aFlyout = aCircle->project(aFlyout);
 
+  myFlyoutUpdate = true;
   aFlyoutAttr->setValue(aFlyout->x(), aFlyout->y());
+  myFlyoutUpdate = false;
 }
 
 void SketchPlugin_ConstraintRadius::attributeChanged(const std::string& theID) {
@@ -201,5 +196,41 @@ void SketchPlugin_ConstraintRadius::attributeChanged(const std::string& theID) {
         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;
   }
 }
index 5400bc443188de718808239569a561b9aa6db7eb..cc2c96eb293f3e672c80ddd389573e8bd5703c53 100644 (file)
@@ -65,6 +65,9 @@ private:
   /// Checks and gets the radius of referenced circle (or arc) otherwise returns -1.
   /// \param theCircData the found referenced circle returned by this method
   double circleRadius(std::shared_ptr<ModelAPI_Feature>& theCirc);
+
+private:
+  bool myFlyoutUpdate; ///< to avoid cyclic dependencies on automatic updates of flyout point
 };
 
 #endif
index d2b062bfd10b70f8c0d30629f677de10584378a5..99cd15c37b176b488c3c403f855ccad6bff2ca53 100644 (file)
@@ -85,7 +85,7 @@ void SketcherPrs_LengthDimension::Compute(const Handle(PrsMgr_PresentationManage
   //aFlyout = aDist;
 
   //SetFlyout(aFlyout);
-  SetFlyout(SketcherPrs_Tools::getFlyoutDistance(myConstraint, myPlane));
+  SetFlyout(SketcherPrs_Tools::getFlyoutDistance(myConstraint));
   SetMeasuredGeometry(aPnt1, aPnt2, myPlane->impl<gp_Ax3>());
   AIS_LengthDimension::Compute(thePresentationManager, thePresentation, theMode);
 }
index 8497ec08edf453c50161989aab466db832a3a96f..980b28f2329d4e32d61981abe2440aa68ce3b63c 100644 (file)
 #include <SketchPlugin_Point.h>
 #include <SketchPlugin_Circle.h>
 #include <SketchPlugin_Line.h>
+#include <SketchPlugin_Arc.h>
 
 #include <ModelAPI_ResultConstruction.h>
 #include <ModelAPI_AttributeRefAttr.h>
+#include <ModelAPI_AttributeDouble.h>
 
 #include <GeomDataAPI_Point2D.h>
 #include <GeomAPI_Lin2d.h>
@@ -142,8 +144,7 @@ void setArrowSize(double theSize)
   MyArrowSize = theSize;
 }
 
-double getFlyoutDistance(const ModelAPI_Feature* theConstraint, 
-                         const std::shared_ptr<GeomAPI_Ax3>& thePlane)
+double getFlyoutDistance(const ModelAPI_Feature* theConstraint)
 {
   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutPoint =
       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
@@ -152,5 +153,35 @@ double getFlyoutDistance(const ModelAPI_Feature* theConstraint,
   return aFlyoutPoint->y();
 }
 
+std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(const ModelAPI_Feature* theConstraint,
+                                            const std::shared_ptr<GeomAPI_Ax3>& thePlane)
+{
+  ModelAPI_Feature* aConstraint = const_cast<ModelAPI_Feature*>(theConstraint);
+  AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
+      aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
+  if (!aRefAttr || !aRefAttr->isObject() || !aRefAttr->object())
+    return std::shared_ptr<GeomAPI_Pnt>();
+
+  FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
+  std::shared_ptr<GeomAPI_Pnt2d> aCenter;
+  if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
+    aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+        aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
+  } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
+    aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+        aFeature->attribute(SketchPlugin_Circle::CENTER_ID()))->pnt();
+  } else
+    return std::shared_ptr<GeomAPI_Pnt>();
+
+  std::shared_ptr<GeomAPI_Pnt2d> anOrigin(new GeomAPI_Pnt2d(0.0, 0.0));
+  std::shared_ptr<GeomAPI_Pnt2d> aFlyoutPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+      aConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()))->pnt();
+  double aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+      aConstraint->attribute(SketchPlugin_Constraint::VALUE()))->value();
+  double aLen = aFlyoutPnt->distance(anOrigin);
+  aFlyoutPnt->setX(aCenter->x() + aFlyoutPnt->x() * aRadius / aLen);
+  aFlyoutPnt->setY(aCenter->y() + aFlyoutPnt->y() * aRadius / aLen);
+  return thePlane->to3D(aFlyoutPnt->x(), aFlyoutPnt->y());
+}
 
 };
index bda6b2a73e9559e987df4028ea4ffe2076ae91a8..b2ed89c5bdc56689593a691628fc3dc9603c2df8 100644 (file)
@@ -62,7 +62,10 @@ enum SelectionModes {
 
   SKETCHERPRS_EXPORT void setArrowSize(double theSize);
 
-  SKETCHERPRS_EXPORT double getFlyoutDistance(const ModelAPI_Feature* theConstraint, 
+  SKETCHERPRS_EXPORT double getFlyoutDistance(const ModelAPI_Feature* theConstraint);
+
+  SKETCHERPRS_EXPORT std::shared_ptr<GeomAPI_Pnt> getAnchorPoint(
+                                              const ModelAPI_Feature* theConstraint,
                                               const std::shared_ptr<GeomAPI_Ax3>& thePlane);
 };