Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[modules/shaper.git] / src / PartSet / PartSet_ConstraintDistancePrs.cpp
index 29a0d19c27f4508a707f9a9a8e689c38c7869445..6bd7510c9b29c1d4841b0dc500a9ba7c1807d10d 100644 (file)
 #include <SketchPlugin_Feature.h>
 #include <SketchPlugin_Sketch.h>
 #include <SketchPlugin_Line.h>
+#include <SketchPlugin_Point.h>
 #include <SketchPlugin_ConstraintDistance.h>
 
 #include <GeomDataAPI_Point2D.h>
 #include <GeomAPI_Pnt2d.h>
 #include <GeomAPI_Lin2d.h>
+#include <GeomAPI_Pln.h>
 
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Document.h>
 #include <ModelAPI_AttributeDouble.h>
 
 #include <AIS_InteractiveObject.hxx>
+#include <AIS_LengthDimension.hxx>
+
 #include <Precision.hxx>
+#include <gp_Pln.hxx>
 
 using namespace std;
 
@@ -37,29 +42,42 @@ std::string PartSet_ConstraintDistancePrs::getKind()
   return SKETCH_CONSTRAINT_DISTANCE_KIND;
 }
 
-bool PartSet_ConstraintDistancePrs::setFeature(FeaturePtr theFeature, const PartSet_SelectionMode& theMode)
+PartSet_SelectionMode PartSet_ConstraintDistancePrs::setFeature(FeaturePtr theFeature, const PartSet_SelectionMode& theMode)
 {
-  bool aResult = false;
-  if (feature() && theFeature && theFeature->getKind() == SKETCH_LINE_KIND && theMode == SM_FirstPoint)
+  PartSet_SelectionMode aMode = theMode;
+  if (!feature() || !theFeature)
+    return aMode;
+
+  std::string anArgument;
+  if (theMode == SM_FirstPoint) {
+    anArgument = CONSTRAINT_ATTR_ENTITY_A;
+    aMode = SM_SecondPoint;
+  }
+  else if (theMode == SM_SecondPoint) {
+    anArgument = CONSTRAINT_ATTR_ENTITY_B;
+    aMode = SM_LastPoint;
+  }
+  if (theFeature->getKind() == SKETCH_POINT_KIND)
   {
     // set length feature
     boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
     boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef =
-          boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
+          boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(anArgument));
     aRef->setFeature(theFeature);
+  }
 
+  if (theMode == SM_SecondPoint) {
     // set length value
-    aData = theFeature->data();
-    boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
-          boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
-    boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
-          boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
-
-    double aLenght = aPoint1->pnt()->distance(aPoint2->pnt());
-    PartSet_Tools::setFeatureValue(feature(), aLenght, CONSTRAINT_ATTR_VALUE);
-    aResult = true;
+    boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(feature(), CONSTRAINT_ATTR_ENTITY_A);
+    boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(feature(), CONSTRAINT_ATTR_ENTITY_B);
+
+    if (aPoint_A && aPoint_B) {
+      double aLenght = aPoint_A->pnt()->distance(aPoint_B->pnt());
+      PartSet_Tools::setFeatureValue(feature(), aLenght, CONSTRAINT_ATTR_VALUE);
+    }
   }
-  return aResult;
+
+  return aMode;
 }
 
 PartSet_SelectionMode PartSet_ConstraintDistancePrs::setPoint(double theX, double theY,
@@ -68,31 +86,25 @@ PartSet_SelectionMode PartSet_ConstraintDistancePrs::setPoint(double theX, doubl
   PartSet_SelectionMode aMode = theMode;
   switch (theMode)
   {
-    case SM_SecondPoint: {
-      boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
-      boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
-              boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
-      FeaturePtr aFeature;
-      if (anAttr) {
-        aFeature = anAttr->feature();
-        if (aFeature->getKind() != SKETCH_LINE_KIND) {
-          aFeature = FeaturePtr();
-        }
-      }
+    case SM_LastPoint: {
+      boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(feature(),
+                                                                        CONSTRAINT_ATTR_ENTITY_A);
+      boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(feature(),
+                                                                        CONSTRAINT_ATTR_ENTITY_B);
+
       boost::shared_ptr<GeomAPI_Pnt2d> aPoint = boost::shared_ptr<GeomAPI_Pnt2d>
                                                              (new GeomAPI_Pnt2d(theX, theY));
-      boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin = PartSet_FeatureLinePrs::createLin2d(aFeature);
+      boost::shared_ptr<GeomAPI_Lin2d> aFeatureLin = boost::shared_ptr<GeomAPI_Lin2d>
+                                        (new GeomAPI_Lin2d(aPoint_A->x(), aPoint_A->y(),
+                                                           aPoint_B->x(), aPoint_B->y()));
       boost::shared_ptr<GeomAPI_Pnt2d> aResult = aFeatureLin->project(aPoint);
       double aDistance = aPoint->distance(aResult);
 
-      double aStartX, aStartY;
-      PartSet_FeatureLinePrs::getLinePoint(aFeature, LINE_ATTR_START, aStartX, aStartY);
-
       if (!aFeatureLin->isRight(aPoint))
         aDistance = -aDistance;
 
-      AttributeDoublePtr aFlyoutAttr = 
-          boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
+      AttributeDoublePtr aFlyoutAttr = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>
+                                      (feature()->data()->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
       aFlyoutAttr->setValue(aDistance);
 
       aMode = SM_DonePoint;
@@ -111,39 +123,26 @@ Handle(AIS_InteractiveObject) PartSet_ConstraintDistancePrs::createPresentation(
   Handle(AIS_InteractiveObject) anAIS = thePreviuos;
   if (!theFeature || !theSketch)
     return anAIS;
-  /*
-  boost::shared_ptr<ModelAPI_Data> aData = theSketch->data();
-  boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
-    boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
-  boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
-    boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
-  gp_Pln aPlane(aNormal->x(), aNormal->y(), aNormal->z(), 0);
-
-  aData = theFeature->data();
-  boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
-          boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
-  if (!anAttr)
-    return thePreviuos;
-  FeaturePtr aFeature = anAttr->feature();
-  if (!aFeature || aFeature->getKind() != SKETCH_LINE_KIND)
-    return thePreviuos;
+  boost::shared_ptr<GeomAPI_Pln> aGPlane = PartSet_Tools::sketchPlane(theSketch);
+  gp_Pln aPlane = aGPlane->impl<gp_Pln>();
 
+  boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(theFeature, CONSTRAINT_ATTR_ENTITY_A);
+  boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(theFeature, CONSTRAINT_ATTR_ENTITY_B);
+  if (!aPoint_A || !aPoint_B)
+    return anAIS;
+
+  boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
   boost::shared_ptr<ModelAPI_AttributeDouble> aFlyoutAttr = 
           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_FLYOUT_VALUE));
   double aFlyout = aFlyoutAttr->value();
 
-  aData = aFeature->data();
-  if (!aData->isValid())
-    return thePreviuos;
-
-  boost::shared_ptr<GeomDataAPI_Point2D> aPointStart =
-        boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
-  boost::shared_ptr<GeomDataAPI_Point2D> aPointEnd =
-        boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
+  boost::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = 
+          boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
+  double aValue = aValueAttr->value();
 
   gp_Pnt aPoint1, aPoint2;
-  PartSet_Tools::convertTo3D(aPointStart->x(), aPointStart->y(), theSketch, aPoint1);
-  PartSet_Tools::convertTo3D(aPointEnd->x(), aPointEnd->y(), theSketch, aPoint2);
+  PartSet_Tools::convertTo3D(aPoint_A->x(), aPoint_A->y(), theSketch, aPoint1);
+  PartSet_Tools::convertTo3D(aPoint_B->x(), aPoint_B->y(), theSketch, aPoint2);
 
   //Build dimension here
   gp_Pnt aP1 = aPoint1;
@@ -153,10 +152,10 @@ Handle(AIS_InteractiveObject) PartSet_ConstraintDistancePrs::createPresentation(
     aP2 = aPoint1;
   }
 
-  Handle(AIS_InteractiveObject) anAIS = thePreviuos;
   if (anAIS.IsNull())
   {
-    Handle(AIS_LengthDimension) aDimAIS = new AIS_LengthDimension (aP1, aP2, aPlane);
+    Handle(AIS_LengthDimension) aDimAIS = new AIS_LengthDimension(aP1, aP2, aPlane);
+    aDimAIS->SetCustomValue(aValue);
 
     Handle(Prs3d_DimensionAspect) anAspect = new Prs3d_DimensionAspect();
     anAspect->MakeArrows3d (Standard_False);
@@ -164,10 +163,6 @@ Handle(AIS_InteractiveObject) PartSet_ConstraintDistancePrs::createPresentation(
     anAspect->TextAspect()->SetHeight(CONSTRAINT_TEXT_HEIGHT);
     anAspect->MakeTextShaded(false);
     aDimAIS->DimensionAspect()->MakeUnitsDisplayed(false);
-    //if (isUnitsDisplayed)
-    //{
-    //  aDimAIS->SetDisplayUnits (aDimDlg->GetUnits ());
-    //}
     aDimAIS->SetDimensionAspect (anAspect);
     aDimAIS->SetFlyout(aFlyout);
     aDimAIS->SetSelToleranceForText2d(CONSTRAINT_TEXT_SELECTION_TOLERANCE);
@@ -179,33 +174,36 @@ Handle(AIS_InteractiveObject) PartSet_ConstraintDistancePrs::createPresentation(
     Handle(AIS_LengthDimension) aDimAIS = Handle(AIS_LengthDimension)::DownCast(anAIS);
     if (!aDimAIS.IsNull()) {
       aDimAIS->SetMeasuredGeometry(aPoint1, aPoint2, aPlane);
+      aDimAIS->SetCustomValue(aValue);
       aDimAIS->SetFlyout(aFlyout);
 
       aDimAIS->Redisplay(Standard_True);
     }
   }
-*/
   return anAIS;
 }
 
-std::string PartSet_ConstraintDistancePrs::getAttribute(const PartSet_SelectionMode& theMode) const
+boost::shared_ptr<GeomDataAPI_Point2D> PartSet_ConstraintDistancePrs::getFeaturePoint
+                                                               (FeaturePtr theFeature,
+                                                                const std::string& theAttribute)
 {
-  return "";
-}
+  boost::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
 
-PartSet_SelectionMode PartSet_ConstraintDistancePrs::getNextMode(const std::string& theAttribute) const
-{
-  return SM_FirstPoint;
+  FeaturePtr aFeature = PartSet_Tools::feature(theFeature, theAttribute, SKETCH_POINT_KIND);
+  if (aFeature)
+    aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
+                                           (aFeature->data()->attribute(POINT_ATTR_COORD));
+  return aPointAttr;
 }
 
-void PartSet_ConstraintDistancePrs::move(double theDeltaX, double theDeltaY)
+std::string PartSet_ConstraintDistancePrs::getAttribute(const PartSet_SelectionMode& theMode) const
 {
+  return "";
 }
 
-double PartSet_ConstraintDistancePrs::distanceToPoint(FeaturePtr theFeature,
-                                                 double theX, double theY)
+PartSet_SelectionMode PartSet_ConstraintDistancePrs::getNextMode(const std::string& theAttribute) const
 {
-  return 0;
+  return SM_FirstPoint;
 }
 
 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_ConstraintDistancePrs::findPoint(FeaturePtr theFeature,