Salome HOME
External edges for sketch: lines and circles
authormpv <mikhail.ponikarov@opencascade.com>
Thu, 30 Oct 2014 08:17:38 +0000 (11:17 +0300)
committermpv <mikhail.ponikarov@opencascade.com>
Thu, 30 Oct 2014 08:17:38 +0000 (11:17 +0300)
16 files changed:
src/FeaturesPlugin/FeaturesPlugin_Extrusion.cpp
src/GeomAPI/GeomAPI_Edge.cpp
src/GeomAPI/GeomAPI_Edge.h
src/GeomAPI/GeomAPI_Pnt.cpp
src/GeomAPI/GeomAPI_Pnt.h
src/Model/Model_Data.cpp
src/ModelAPI/ModelAPI_Feature.cpp
src/ModelAPI/ModelAPI_Feature.h
src/ModelAPI/ModelAPI_Object.h
src/PartSet/PartSet_Tools.cpp
src/SketchPlugin/SketchPlugin_Circle.cpp
src/SketchPlugin/SketchPlugin_Circle.h
src/SketchPlugin/SketchPlugin_Line.cpp
src/SketchPlugin/SketchPlugin_Line.h
src/SketchPlugin/SketchPlugin_Sketch.cpp
src/SketchPlugin/SketchPlugin_Sketch.h

index eeafb1db61e0e7557ff59706166146f75c41b219..7316f3c2c644ae24b260310ae2af6071170f346f 100644 (file)
@@ -65,6 +65,7 @@ void FeaturesPlugin_Extrusion::execute()
   if (data()->boolean(FeaturesPlugin_Extrusion::REVERSE_ID())->value())
     aSize = -aSize;
 
+  eraseResults(); // to erase the previously stored naming structures
   boost::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
   GeomAlgoAPI_Extrusion aFeature(aFace, aSize);
   if(!aFeature.isDone()) {
index 2cde7c24d5fa55284087a53b373e9ce5974e9740..a4b42b207dd60d9445b4de1189fb01110a71fab3 100644 (file)
@@ -3,6 +3,9 @@
 // Author:      Artem ZHIDKOV
 
 #include<GeomAPI_Edge.h>
+#include<GeomAPI_Pnt.h>
+#include<GeomAPI_Circ.h>
+#include<GeomAPI_Dir.h>
 
 #include <TopoDS_Shape.hxx>
 #include <TopoDS_Edge.hxx>
 #include <Geom_Curve.hxx>
 #include <Geom_Line.hxx>
 #include <Geom_Circle.hxx>
+#include <gp_Ax1.hxx>
 
 GeomAPI_Edge::GeomAPI_Edge()
-    : GeomAPI_Shape()
+  : GeomAPI_Shape()
 {
 }
 
+GeomAPI_Edge::GeomAPI_Edge(const boost::shared_ptr<GeomAPI_Shape>& theShape)
+{
+  if (!theShape->isNull() && theShape->isEdge()) {
+    setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
+  }
+}
+
 bool GeomAPI_Edge::isLine() const
 {
   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
@@ -45,3 +56,41 @@ bool GeomAPI_Edge::isArc() const
     return true;
   return false;
 }
+
+boost::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
+{
+  const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
+  double aFirst, aLast;
+  Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
+  gp_Pnt aPoint;
+  aCurve->D0(aFirst, aPoint);
+  return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
+}
+
+boost::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
+{
+  const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
+  double aFirst, aLast;
+  Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
+  gp_Pnt aPoint;
+  aCurve->D0(aLast, aPoint);
+  return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
+}
+
+boost::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle()
+{
+  const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
+  double aFirst, aLast;
+  Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
+  if (aCurve) {
+    Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
+    if (aCirc) {
+      gp_Pnt aLoc = aCirc->Location();
+      boost::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
+      gp_Dir anAxis = aCirc->Axis().Direction();
+      boost::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
+      return boost::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
+    }
+  }
+  return boost::shared_ptr<GeomAPI_Circ>(); // not circle
+}
index f1c74c5d55573b1f92310a1d306aa7cc7339611d..6814bb68777cce3c2f8000735a97b3989d45933d 100644 (file)
@@ -7,28 +7,22 @@
 
 #include <GeomAPI_Shape.h>
 
+class GeomAPI_Pnt;
+class GeomAPI_Circ;
+
 /**\class GeomAPI_Edge
- * \ingroup DataModel
+* \ingroup DataModel
  * \brief Interface to the edge object
  */
 
 class GEOMAPI_EXPORT GeomAPI_Edge : public GeomAPI_Shape
 {
- public:
-  /// Creation of empty (null) shape
-  GeomAPI_Edge();
-
-  /// Returns whether the shape is a vertex
-  virtual bool isVertex() const
-  {
-    return false;
-  }
-
-  /// Returns whether the shape is an edge
-  virtual bool isEdge() const
-  {
-    return true;
-  }
+public:
+   /// Creation of empty (null) shape
+   GeomAPI_Edge();
+
+   /// Creation of edge by the edge-shape
+   GeomAPI_Edge(const boost::shared_ptr<GeomAPI_Shape>& theShape);
 
   /// Verifies that the edge is a line
   bool isLine() const;
@@ -38,6 +32,15 @@ class GEOMAPI_EXPORT GeomAPI_Edge : public GeomAPI_Shape
 
   /// Verifies that the edge is an arc of circle
   bool isArc() const;
+
+  /// Returns the first vertex coordinates of the edge 
+  boost::shared_ptr<GeomAPI_Pnt> firstPoint();
+
+  /// Returns the Last vertex coordinates of the edge 
+  boost::shared_ptr<GeomAPI_Pnt> lastPoint();
+
+  /// Returns a circle if edge is based on the cirsle curve
+  boost::shared_ptr<GeomAPI_Circ> circle();
 };
 
 #endif
index 0455c15134d64eff166f37bdff38ccc3498053e2..aae0d1b8b03ebc64b9d29006e9b0c0489b4b5beb 100644 (file)
@@ -4,6 +4,8 @@
 
 #include<GeomAPI_Pnt.h>
 #include<GeomAPI_XYZ.h>
+#include<GeomAPI_Pnt2d.h>
+#include<GeomAPI_Dir.h>
 
 #include<gp_Pnt.hxx>
 
@@ -58,3 +60,14 @@ double GeomAPI_Pnt::distance(const boost::shared_ptr<GeomAPI_Pnt>& theOther) con
 {
   return MY_PNT->Distance(theOther->impl<gp_Pnt>());
 }
+
+boost::shared_ptr<GeomAPI_Pnt2d> GeomAPI_Pnt::to2D(const boost::shared_ptr<GeomAPI_Pnt>& theOrigin,
+  const boost::shared_ptr<GeomAPI_Dir>& theDirX, const boost::shared_ptr<GeomAPI_Dir>& theDirY)
+{
+  gp_Pnt anOriginPnt(theOrigin->x(), theOrigin->y(), theOrigin->z());
+  gp_Vec aVec(anOriginPnt, impl<gp_Pnt>());
+
+  double aX = aVec.X() * theDirX->x() + aVec.Y() * theDirX->y() + aVec.Z() * theDirX->z();
+  double aY = aVec.X() * theDirY->x() + aVec.Y() * theDirY->y() + aVec.Z() * theDirY->z();
+  return boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, aY));
+}
index 6e4e527d554ce6795f36f8113899b184e0b3ebc5..af4cce8493b180a4b189f733a3025b1b5c5f188c 100644 (file)
@@ -9,6 +9,8 @@
 #include <boost/shared_ptr.hpp>
 
 class GeomAPI_XYZ;
+class GeomAPI_Pnt2d;
+class GeomAPI_Dir;
 
 /**\class GeomAPI_Pnt
  * \ingroup DataModel
@@ -42,7 +44,11 @@ class GEOMAPI_EXPORT GeomAPI_Pnt : public GeomAPI_Interface
 
   /// Distance between two points
   double distance(const boost::shared_ptr<GeomAPI_Pnt>& theOther) const;
+
+  /// Projects a point to the plane defined by the origin and 2 axes vectors in this plane
+  boost::shared_ptr<GeomAPI_Pnt2d> to2D(const boost::shared_ptr<GeomAPI_Pnt>& theOrigin,
+                                        const boost::shared_ptr<GeomAPI_Dir>& theDirX,
+                                        const boost::shared_ptr<GeomAPI_Dir>& theDirY);
 };
 
 #endif
-
index a3a1177410a933b240cda98348d735da7eff1481..573086ed0a31d441a51f47b4fbf3efe5c2776df2 100644 (file)
@@ -328,6 +328,9 @@ void Model_Data::sendAttributeUpdated(ModelAPI_Attribute* theAttr)
   if (theAttr->isArgument()) {
     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
     ModelAPI_EventCreator::get()->sendUpdated(myObject, anEvent);
+    if (myObject) {
+      myObject->attributeChanged();
+    }
   }
 }
 
index 14a1e396f55f0ad3c252e315a7c2c8f93d1937d0..94b1a64e8fb1f79fe1512faf8c6a7641aa085041 100644 (file)
@@ -20,6 +20,11 @@ boost::shared_ptr<ModelAPI_Result> ModelAPI_Feature::firstResult()
   return myResults.empty() ? boost::shared_ptr<ModelAPI_Result>() : *(myResults.begin());
 }
 
+boost::shared_ptr<ModelAPI_Result> ModelAPI_Feature::lastResult()
+{
+  return myResults.empty() ? boost::shared_ptr<ModelAPI_Result>() : *(myResults.rbegin());
+}
+
 void ModelAPI_Feature::setResult(const boost::shared_ptr<ModelAPI_Result>& theResult)
 {
   if (firstResult() == theResult) {  // just updated
index 269471f84f9404277236c65c621970f087295e49..c266b505ea1a194910a7ab7a020a72b448717532 100644 (file)
@@ -72,6 +72,8 @@ class ModelAPI_Feature : public ModelAPI_Object
   MODELAPI_EXPORT const std::list<boost::shared_ptr<ModelAPI_Result> >& results();
   /// returns the first result in the list or NULL reference
   MODELAPI_EXPORT boost::shared_ptr<ModelAPI_Result> firstResult();
+  /// returns the last result in the list or NULL reference
+  MODELAPI_EXPORT boost::shared_ptr<ModelAPI_Result> lastResult();
   /// sets the alone result
   MODELAPI_EXPORT void setResult(const boost::shared_ptr<ModelAPI_Result>& theResult);
   /// sets the result by index (zero based), results before this must be set before
index 7ac6696f5ad5b0433b2d662bd2c5917f6858372f..aba6567eb3a0d643409a36dc40644d2811650511 100644 (file)
@@ -54,6 +54,10 @@ class ModelAPI_Object
   /// Returns the group identifier of this object
   virtual std::string groupName() = 0;
 
+  /// Called on change of any argument-attribute of this object
+  MODELAPI_EXPORT virtual void attributeChanged() 
+  {}
+
   /// To use virtuality for destructors
   virtual ~ModelAPI_Object() {}
 
index f11abff580ddcd1ed1a96725872a4e17487859e1..916a31a71f2a4e958e3ddbf706eb0ce35df99548 100644 (file)
@@ -364,13 +364,13 @@ ResultPtr PartSet_Tools::createFixedObjectByEdge(const ModuleBase_ViewerPrs& the
 
   Standard_Real aStart, aEnd;
   Handle(V3d_View) aNullView;
-  FeaturePtr myFeature;
+  FeaturePtr aMyFeature;
 
   Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aStart, aEnd);
   GeomAdaptor_Curve aAdaptor(aCurve);
   if (aAdaptor.GetType() == GeomAbs_Line) {
     // Create line
-    myFeature = theSketch->addFeature(SketchPlugin_Line::ID());
+    aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
 
     //DataPtr aData = myFeature->data();
     //boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr = 
@@ -387,7 +387,7 @@ ResultPtr PartSet_Tools::createFixedObjectByEdge(const ModuleBase_ViewerPrs& the
   } else if (aAdaptor.GetType() == GeomAbs_Circle) {
     if (aAdaptor.IsClosed()) {
       // Create circle
-      myFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
+      aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
       //gp_Circ aCirc = aAdaptor.Circle();
       //gp_Pnt aCenter = aCirc.Location();
 
@@ -397,11 +397,11 @@ ResultPtr PartSet_Tools::createFixedObjectByEdge(const ModuleBase_ViewerPrs& the
       //setFeatureValue(myFeature, aCirc.Radius(), SketchPlugin_Circle::RADIUS_ID());
     } else {
       // Create arc
-      myFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
+      aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
     }
   }
-  if (myFeature) {
-    DataPtr aData = myFeature->data();
+  if (aMyFeature) {
+    DataPtr aData = aMyFeature->data();
     AttributeSelectionPtr anAttr = 
       boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>
       (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
@@ -412,8 +412,8 @@ ResultPtr PartSet_Tools::createFixedObjectByEdge(const ModuleBase_ViewerPrs& the
       anEdge->setImpl(new TopoDS_Shape(aShape));
 
       anAttr->setValue(aRes, anEdge);
-      myFeature->execute();
-      return myFeature->firstResult();
+      aMyFeature->execute();
+      return aMyFeature->lastResult();
     }
   }
   return ResultPtr();
index 992fc321d58d504e5cbe0b916e64c0ff2256a230..06ab76d14be74af9e3e6e017afddb8f246776782 100644 (file)
@@ -12,6 +12,7 @@
 #include <ModelAPI_Session.h>
 
 #include <GeomAPI_Pnt2d.h>
+#include <GeomAPI_Circ.h>
 #include <GeomDataAPI_Point2D.h>
 #include <GeomDataAPI_Dir.h>
 #include <GeomAlgoAPI_PointBuilder.h>
@@ -25,8 +26,8 @@ SketchPlugin_Circle::SketchPlugin_Circle()
 
 void SketchPlugin_Circle::initAttributes()
 {
-  data()->addAttribute(SketchPlugin_Circle::CENTER_ID(), GeomDataAPI_Point2D::type());
-  data()->addAttribute(SketchPlugin_Circle::RADIUS_ID(), ModelAPI_AttributeDouble::type());
+  data()->addAttribute(CENTER_ID(), GeomDataAPI_Point2D::type());
+  data()->addAttribute(RADIUS_ID(), ModelAPI_AttributeDouble::type());
   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::type());
   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
 }
@@ -39,14 +40,13 @@ void SketchPlugin_Circle::execute()
 
     // compute a circle point in 3D view
     boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = boost::dynamic_pointer_cast<
-        GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Circle::CENTER_ID()));
-    AttributeDoublePtr aRadiusAttr = boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
-        data()->attribute(SketchPlugin_Circle::RADIUS_ID()));
+        GeomDataAPI_Point2D>(data()->attribute(CENTER_ID()));
+    AttributeDoublePtr aRadiusAttr = 
+      boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(data()->attribute(RADIUS_ID()));
     if (aCenterAttr->isInitialized() && aRadiusAttr->isInitialized()) {
       boost::shared_ptr<GeomAPI_Pnt> aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y()));
       // make a visible point
       boost::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
-      //aShapes.push_back(aCenterPointShape);
       boost::shared_ptr<ModelAPI_ResultConstruction> aConstr1 = document()->createConstruction(
           data(), 0);
       aConstr1->setShape(aCenterPointShape);
@@ -72,15 +72,6 @@ void SketchPlugin_Circle::execute()
         setResult(aConstr2, 1);
       }
     }
-    /*
-     boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
-     // store the result
-     boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = 
-     document()->createConstruction(data());
-     aConstr->setShape(aCompound);
-     aConstr->setIsInHistory(false);
-     setResult(aConstr);
-     */
   }
 }
 
@@ -91,15 +82,15 @@ void SketchPlugin_Circle::move(double theDeltaX, double theDeltaY)
     return;
 
   boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-      aData->attribute(SketchPlugin_Circle::CENTER_ID()));
+      aData->attribute(CENTER_ID()));
   aPoint1->move(theDeltaX, theDeltaY);
 }
 
 double SketchPlugin_Circle::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
 {
   boost::shared_ptr<ModelAPI_Data> aData = data();
-  boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-      aData->attribute(SketchPlugin_Circle::CENTER_ID()));
+  boost::shared_ptr<GeomDataAPI_Point2D> aPoint = 
+    boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(CENTER_ID()));
 
   return aPoint->pnt()->distance(thePoint);
 }
@@ -107,3 +98,18 @@ double SketchPlugin_Circle::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2
 bool SketchPlugin_Circle::isFixed() {
   return data()->selection(EXTERNAL_ID())->context();
 }
+
+void SketchPlugin_Circle::attributeChanged() {
+  static bool myIsUpdated = false; // to avoid infinitive cycle on attrubtes change
+  boost::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
+  if (aSelection && !myIsUpdated) { // update arguments due to the selection value
+    myIsUpdated = true;
+    boost::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
+    boost::shared_ptr<GeomAPI_Circ> aCirc = anEdge->circle();
+    boost::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = 
+      boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(CENTER_ID()));
+    aCenterAttr->setValue(sketch()->to2D(aCirc->center()));
+    real(RADIUS_ID())->setValue(aCirc->radius());
+    myIsUpdated = false;
+  }
+}
index 11311dcb0da8c230e74ce32ee5623e8797ea2841..70ee0c892630546da0b394f6b79afacb0b570acd 100644 (file)
@@ -76,6 +76,9 @@ class SketchPlugin_Circle : public SketchPlugin_Feature  //, public GeomAPI_IPre
   /// \param thePoint the point
   virtual double distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint);
 
+  /// Called on change of any argument-attribute of this object
+  SKETCHPLUGIN_EXPORT virtual void attributeChanged();
+
   /// Use plugin manager for features creation
   SketchPlugin_Circle();
 };
index dac4222679edb0f0193182709f9b132bfc74d247..75c404a772a12922e9bd6284a8b07e4d3a952d15 100644 (file)
@@ -26,8 +26,8 @@ SketchPlugin_Line::SketchPlugin_Line()
 
 void SketchPlugin_Line::initAttributes()
 {
-  data()->addAttribute(SketchPlugin_Line::START_ID(), GeomDataAPI_Point2D::type());
-  data()->addAttribute(SketchPlugin_Line::END_ID(), GeomDataAPI_Point2D::type());
+  data()->addAttribute(START_ID(), GeomDataAPI_Point2D::type());
+  data()->addAttribute(END_ID(), GeomDataAPI_Point2D::type());
   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::type());
   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
 }
@@ -38,15 +38,15 @@ void SketchPlugin_Line::execute()
   if (aSketch) {
     // compute a start point in 3D view
     boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = boost::dynamic_pointer_cast<
-        GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Line::START_ID()));
+        GeomDataAPI_Point2D>(data()->attribute(START_ID()));
     // compute an end point in 3D view
     boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr = boost::dynamic_pointer_cast<
-        GeomDataAPI_Point2D>(data()->attribute(SketchPlugin_Line::END_ID()));
+        GeomDataAPI_Point2D>(data()->attribute(END_ID()));
     if (aStartAttr->isInitialized() && anEndAttr->isInitialized()) {
       boost::shared_ptr<GeomAPI_Pnt> aStart(aSketch->to3D(aStartAttr->x(), aStartAttr->y()));
       boost::shared_ptr<GeomAPI_Pnt> anEnd(aSketch->to3D(anEndAttr->x(), anEndAttr->y()));
       // make linear edge
-      boost::shared_ptr<GeomAPI_Shape> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
+      boost::shared_ptr<GeomAPI_Edge> anEdge = GeomAlgoAPI_EdgeBuilder::line(aStart, anEnd);
       // store the result
       boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(
           data());
@@ -63,12 +63,12 @@ void SketchPlugin_Line::move(double theDeltaX, double theDeltaY)
   if (!aData->isValid())
     return;
 
-  boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-      aData->attribute(SketchPlugin_Line::START_ID()));
+  boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
+    (aData->attribute(START_ID()));
   aPoint1->move(theDeltaX, theDeltaY);
 
-  boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-      aData->attribute(SketchPlugin_Line::END_ID()));
+  boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
+    (aData->attribute(END_ID()));
   aPoint2->move(theDeltaX, theDeltaY);
 }
 
@@ -77,10 +77,10 @@ double SketchPlugin_Line::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>
   double aDelta = 0;
 
   boost::shared_ptr<ModelAPI_Data> aData = data();
-  boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-      aData->attribute(SketchPlugin_Line::START_ID()));
-  boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-      aData->attribute(SketchPlugin_Line::END_ID()));
+  boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 = 
+    boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(START_ID()));
+  boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 = 
+    boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(END_ID()));
 
   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
 
@@ -97,3 +97,19 @@ double SketchPlugin_Line::distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>
 bool SketchPlugin_Line::isFixed() {
   return data()->selection(EXTERNAL_ID())->context();
 }
+
+void SketchPlugin_Line::attributeChanged() {
+  static bool myIsUpdated = false; // to avoid infinitive cycle on attrubtes change
+  boost::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
+  if (aSelection && !myIsUpdated) { // update arguments due to the selection value
+    myIsUpdated = true;
+    boost::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
+    boost::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
+      boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_ID()));
+    aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
+    boost::shared_ptr<GeomDataAPI_Point2D> anEndAttr = 
+      boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_ID()));
+    anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
+    myIsUpdated = false;
+  }
+}
index d24f1af52701f79fe25d0ee6b9f6136fcd033f92..3943eafd3608869a6f708539e8f61b19ac82c085 100644 (file)
@@ -67,6 +67,9 @@ class SketchPlugin_Line : public SketchPlugin_Feature
   /// \param thePoint the point
   virtual double distanceToPoint(const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint);
 
+  /// Called on change of any argument-attribute of this object
+  SKETCHPLUGIN_EXPORT virtual void attributeChanged();
+
   /// Use plugin manager for features creation
   SketchPlugin_Line();
 };
index 5429b2b605668573097dbc9bd76ff09713971433..42b70c4c692a41d008b8ba8524318b711ca03c49 100644 (file)
@@ -165,6 +165,19 @@ boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, cons
   return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
 }
 
+boost::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Sketch::to2D(
+  const boost::shared_ptr<GeomAPI_Pnt>& thePnt)
+{
+  boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
+      data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
+  boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
+      data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
+  boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
+      data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
+  return thePnt->to2D(aC->pnt(), aX->dir(), aY->dir());
+}
+
+
 bool SketchPlugin_Sketch::isPlaneSet()
 {
   boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
index 23147d97f612f11077809ff5b8b773ad7d7f2690..637bd34a90e416f289a0f75a3cdfb44dabfb5b9e 100644 (file)
@@ -132,6 +132,9 @@ class SketchPlugin_Sketch : public ModelAPI_CompositeFeature, public GeomAPI_IPr
   /// Construction result is allways recomuted on the fly
   SKETCHPLUGIN_EXPORT virtual bool isPersistentResult() {return false;}
 
+  /// Returns the point projected into the sketch plane
+  boost::shared_ptr<GeomAPI_Pnt2d> to2D(const boost::shared_ptr<GeomAPI_Pnt>& thePnt);
+
 protected:
   /// Creates a plane and append it to the list
   /// \param theX the X normal value