]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
authornds <natalia.donis@opencascade.com>
Sat, 21 Jun 2014 11:51:23 +0000 (15:51 +0400)
committernds <natalia.donis@opencascade.com>
Sat, 21 Jun 2014 11:51:23 +0000 (15:51 +0400)
1. Distance constraint between points.

src/PartSet/PartSet_ConstraintDistancePrs.cpp
src/PartSet/PartSet_ConstraintDistancePrs.h
src/PartSet/PartSet_ConstraintLengthPrs.cpp
src/PartSet/PartSet_Tools.cpp
src/PartSet/PartSet_Tools.h

index 8ad377696cb400522742782eb0d34d1e4b508b8b..809ea8c3c7f3ca0fb26d96d74e5e2d2b444b77f6 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;
 
@@ -40,25 +45,38 @@ std::string PartSet_ConstraintDistancePrs::getKind()
 PartSet_SelectionMode PartSet_ConstraintDistancePrs::setFeature(FeaturePtr theFeature, const PartSet_SelectionMode& theMode)
 {
   PartSet_SelectionMode aMode = theMode;
-  if (feature() && theFeature && theFeature->getKind() == SKETCH_LINE_KIND && theMode == SM_FirstPoint)
+  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);
-    aMode = SM_SecondPoint;
+    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 aMode;
 }
 
@@ -69,30 +87,24 @@ PartSet_SelectionMode PartSet_ConstraintDistancePrs::setPoint(double theX, doubl
   switch (theMode)
   {
     case SM_LastPoint: {
-      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();
-        }
-      }
+      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,35 +123,26 @@ Handle(AIS_InteractiveObject) PartSet_ConstraintDistancePrs::createPresentation(
   Handle(AIS_InteractiveObject) anAIS = thePreviuos;
   if (!theFeature || !theSketch)
     return anAIS;
-  /*
   boost::shared_ptr<GeomAPI_Pln> aGPlane = PartSet_Tools::sketchPlane(theSketch);
   gp_Pln aPlane = aGPlane->impl<gp_Pln>();
 
-  boost::shared_ptr<ModelAPI_Data> 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<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;
@@ -149,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);
@@ -160,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);
@@ -175,15 +174,28 @@ 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;
 }
 
+boost::shared_ptr<GeomDataAPI_Point2D> PartSet_ConstraintDistancePrs::getFeaturePoint
+                                                               (FeaturePtr theFeature,
+                                                                const std::string& theAttribute)
+{
+  boost::shared_ptr<GeomDataAPI_Point2D> aPointAttr;
+
+  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;
+}
+
 std::string PartSet_ConstraintDistancePrs::getAttribute(const PartSet_SelectionMode& theMode) const
 {
   return "";
index 89cfb9051d7f3b791dadd0719bdfcddbfb68259d..7bac945e60c69720eb956e66bd1d0d8628446228 100644 (file)
@@ -85,6 +85,14 @@ protected:
   /// Returns the feature point in the selection mode position.
   /// \param theMode the current operation selection mode. The feature attribute depends on the mode
   virtual boost::shared_ptr<GeomDataAPI_Point2D> featurePoint(const PartSet_SelectionMode& theMode);
+
+private:
+  /// Find the feature point
+  /// \param theFeature the constraint feature
+  /// \param theAttribute a distance constraint attribute name of feature
+  /// \return the point in the feature
+  static boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(FeaturePtr theFeature,
+                                                                const std::string& theAttribute);
 };
 
 #endif
index 2f9c1d15ca4dc3130b3bd3f92bc07f15e4fa7247..a594bc6d30af287de0ac700ce1ab97e5176fdad7 100644 (file)
@@ -94,9 +94,6 @@ PartSet_SelectionMode PartSet_ConstraintLengthPrs::setPoint(double theX, double
       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;
 
index 8829dce4f4b6cf02f8250ad15fdbb9a23727f24b..58ed85bb2b67756170e6efa300b63ffc68a24f0c 100644 (file)
@@ -272,6 +272,25 @@ double PartSet_Tools::featureValue(FeaturePtr theFeature, const std::string& the
   return aValue;
 }
 
+FeaturePtr PartSet_Tools::feature(FeaturePtr theFeature, const std::string& theAttribute,
+                                  const std::string& theKind)
+{
+  FeaturePtr aFeature;
+  if (!theFeature)
+    return aFeature;
+
+  boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
+  boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
+          boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(theAttribute));
+  if (anAttr) {
+    aFeature = anAttr->feature();
+    if (!theKind.empty() && aFeature && aFeature->getKind() != theKind) {
+      aFeature = FeaturePtr();
+    }
+  }
+  return aFeature;
+}
+
 void PartSet_Tools::createConstraint(FeaturePtr theSketch,
                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
                                      boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
index deb4b4910756739355f004ce456af0dbe9ce8321..7b81b3f10015e3c7e444d50cd9ed490759c5647a 100644 (file)
@@ -100,6 +100,15 @@ public:
   static double featureValue(FeaturePtr theFeature, const std::string& theAttribute,
                              bool& isValid);
 
+  /// Find a feature in the attribute of the given feature. If the kind is not empty,
+  /// the return feature should be this type. Otherwise it is null
+  /// \param theFeature a source feature
+  /// \param theAttribute a name of the requried attribute attribute
+  /// \param theKind an output feature kind
+  /// \return the feature
+  static FeaturePtr feature(FeaturePtr theFeature, const std::string& theAttribute,
+                             const std::string& theKind);
+
   /// Creates a constraint on two points
   /// \param thePoint1 the first point
   /// \param thePoint1 the second point