Salome HOME
Added test for creation arc by three points
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Line.cpp
index 3c4e19a1a369fe247dc140a81cc63c6815924cc3..7c3f528d50330ded1ed7bf8ad97efefb0100e1a7 100644 (file)
@@ -6,7 +6,10 @@
 
 #include "SketchPlugin_Line.h"
 #include "SketchPlugin_Sketch.h"
+#include "SketchPlugin_ConstraintCoincidence.h"
+
 #include <ModelAPI_Data.h>
+#include <ModelAPI_EventReentrantMessage.h>
 #include <ModelAPI_ResultConstruction.h>
 #include <ModelAPI_AttributeSelection.h>
 #include <ModelAPI_AttributeBoolean.h>
 #include <GeomAlgoAPI_EdgeBuilder.h>
 #include <GeomDataAPI_Point2D.h>
 
-using namespace std;
-
 SketchPlugin_Line::SketchPlugin_Line()
     : SketchPlugin_SketchEntity()
 {}
 
+void SketchPlugin_Line::initAttributes()
+{
+  SketchPlugin_SketchEntity::initAttributes();
+  /// new attributes should be added to end of the feature in order to provide
+  /// correct attribute values in previous saved studies
+  data()->addAttribute(LENGTH_ID(), ModelAPI_AttributeDouble::typeId());
+}
+
 void SketchPlugin_Line::initDerivedClassAttributes()
 {
   data()->addAttribute(START_ID(), GeomDataAPI_Point2D::typeId());
   data()->addAttribute(END_ID(), GeomDataAPI_Point2D::typeId());
   data()->addAttribute(EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
-#ifndef LINE_LENGHT_BLOCKED
-  data()->addAttribute(LENGTH_ID(), ModelAPI_AttributeDouble::typeId());
-#endif
   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), EXTERNAL_ID());
 }
 
@@ -61,6 +67,13 @@ void SketchPlugin_Line::execute()
       aConstr->setShape(anEdge);
       aConstr->setIsInHistory(false);
       setResult(aConstr);
+
+      static Events_ID anId = ModelAPI_EventReentrantMessage::eventId();
+      std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage = std::shared_ptr
+          <ModelAPI_EventReentrantMessage>(new ModelAPI_EventReentrantMessage(anId, this));
+      aMessage->setCreatedFeature(ModelAPI_Feature::feature(
+                                  data()->attribute(START_ID())->owner()));
+      Events_Loop::loop()->send(aMessage);
     }
   }
 }
@@ -80,14 +93,36 @@ void SketchPlugin_Line::move(double theDeltaX, double theDeltaY)
   aPoint2->move(theDeltaX, theDeltaY);
 }
 
+std::string SketchPlugin_Line::processEvent(const std::shared_ptr<Events_Message>& theMessage)
+{
+  std::string aFilledAttributeName;
+
+  std::shared_ptr<ModelAPI_EventReentrantMessage> aReentrantMessage =
+        std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
+  if (aReentrantMessage.get()) {
+    FeaturePtr aCreatedFeature = aReentrantMessage->createdFeature();
+
+    // Initialize new line with first point equal to end of previous
+    std::shared_ptr<ModelAPI_Data> aSFData = aCreatedFeature->data();
+    std::shared_ptr<GeomDataAPI_Point2D> aSPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+                                                 aSFData->attribute(SketchPlugin_Line::END_ID()));
+    std::shared_ptr<ModelAPI_Data> aNFData = data();
+    std::shared_ptr<GeomDataAPI_Point2D> aNPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+                                                 aNFData->attribute(SketchPlugin_Line::START_ID()));
+    aNPoint->setValue(aSPoint->x(), aSPoint->y());
+    SketchPlugin_ConstraintCoincidence::createCoincidenceFeature(sketch(), aSPoint, aNPoint);
+  }
+  return aFilledAttributeName;
+}
+
 double SketchPlugin_Line::distanceToPoint(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
 {
   double aDelta = 0;
 
   std::shared_ptr<ModelAPI_Data> aData = data();
-  std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = 
+  std::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(START_ID()));
-  std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = 
+  std::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
     std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(END_ID()));
 
   GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
@@ -118,13 +153,19 @@ void SketchPlugin_Line::attributeChanged(const std::string& theID) {
   // to be removed after debug
   if ((theID == EXTERNAL_ID() || isFixed()) && !isCopy()) {
     std::shared_ptr<GeomAPI_Shape> aSelection = data()->selection(EXTERNAL_ID())->value();
-     // update arguments due to the selection value
+    if (!aSelection) {
+      // empty shape in selection shows that the shape is equal to context
+      ResultPtr anExtRes = selection(EXTERNAL_ID())->context();
+      if (anExtRes)
+        aSelection = anExtRes->shape();
+    }
+    // update arguments due to the selection value
     if (aSelection && !aSelection->isNull() && aSelection->isEdge()) {
       std::shared_ptr<GeomAPI_Edge> anEdge( new GeomAPI_Edge(aSelection));
-      std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = 
+      std::shared_ptr<GeomDataAPI_Point2D> aStartAttr =
         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(START_ID()));
       aStartAttr->setValue(sketch()->to2D(anEdge->firstPoint()));
-      std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = 
+      std::shared_ptr<GeomDataAPI_Point2D> anEndAttr =
         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(attribute(END_ID()));
       anEndAttr->setValue(sketch()->to2D(anEdge->lastPoint()));
       updateLenghtValue();
@@ -137,7 +178,6 @@ void SketchPlugin_Line::attributeChanged(const std::string& theID) {
 
 void SketchPlugin_Line::updateLenghtValue()
 {
-#ifndef LINE_LENGHT_BLOCKED
   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<
       GeomDataAPI_Point2D>(data()->attribute(START_ID()));
   std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = std::dynamic_pointer_cast<
@@ -146,5 +186,4 @@ void SketchPlugin_Line::updateLenghtValue()
     double aDistance = aStartAttr->pnt()->distance(anEndAttr->pnt());
     data()->real(LENGTH_ID())->setValue(aDistance);
   }
-#endif
 }