Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / PartSet / PartSet_OperationSketchLine.cpp
index 366d650e8427a1220cafbe6208c8ebdc54de4916..5bc516c3954a89c2fb15789086d9b229f02a7627 100644 (file)
@@ -9,6 +9,7 @@
 #include <GeomDataAPI_Point.h>
 #include <GeomDataAPI_Dir.h>
 #include <ModelAPI_Data.h>
+#include <ModelAPI_Document.h>
 
 #include <SketchPlugin_Sketch.h>
 #include <SketchPlugin_Line.h>
@@ -36,9 +37,12 @@ bool PartSet_OperationSketchLine::isGranted() const
   return true;
 }
 
-int PartSet_OperationSketchLine::getSelectionMode() const
+int PartSet_OperationSketchLine::getSelectionMode(boost::shared_ptr<ModelAPI_Feature> theFeature) const
 {
-  return 0;//TopAbs_FACE;
+  int aMode = 0;
+  if (theFeature != feature())
+    aMode = TopAbs_VERTEX;
+  return aMode;
 }
 
 void PartSet_OperationSketchLine::mouseReleased(const gp_Pnt& thePoint)
@@ -66,8 +70,44 @@ void PartSet_OperationSketchLine::mouseReleased(const gp_Pnt& thePoint)
 
 void PartSet_OperationSketchLine::mouseMoved(const gp_Pnt& thePoint)
 {
-  if (myPointSelectionMode == SM_SecondPoint)
-    setLinePoint(thePoint, LINE_ATTR_END);
+  switch (myPointSelectionMode)
+  {
+    case SM_SecondPoint:
+      setLinePoint(thePoint, LINE_ATTR_END);
+      break;
+    case SM_None: {
+      boost::shared_ptr<ModelAPI_Feature> aPrevFeature = feature();
+      // stop the last operation
+      commitOperation();
+      document()->finishOperation();
+      //emit changeSelectionMode(aPrevFeature, TopAbs_VERTEX);
+      // start a new operation
+      document()->startOperation();
+      startOperation();
+      // use the last point of the previous feature as the first of the new one
+      setLinePoint(aPrevFeature, LINE_ATTR_END, LINE_ATTR_START);
+      myPointSelectionMode = SM_SecondPoint;
+
+      emit featureConstructed(aPrevFeature);
+    }
+    break;
+    default:
+      break;
+  }
+}
+
+void PartSet_OperationSketchLine::keyReleased(const int theKey)
+{
+  switch (theKey) {
+    case Qt::Key_Escape:
+      abort();
+      break;
+    case Qt::Key_Enter:
+      //myPointSelectionMode = myPointSelectionMode;
+      break;
+    default:
+      break;
+  }
 }
 
 void PartSet_OperationSketchLine::startOperation()
@@ -102,6 +142,21 @@ void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
   aPoint->setValue(aX, anY);
 }
 
+void PartSet_OperationSketchLine::setLinePoint(boost::shared_ptr<ModelAPI_Feature> theSourceFeature,
+                                               const std::string& theSourceAttribute,
+                                               const std::string& theAttribute)
+{
+  boost::shared_ptr<ModelAPI_Data> aData = theSourceFeature->data();
+  boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
+        boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theSourceAttribute));
+  double aX = aPoint->x();
+  double anY = aPoint->y();
+
+  aData = feature()->data();
+  aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
+  aPoint->setValue(aX, anY);
+}
+
 void PartSet_OperationSketchLine::convertTo2D(const gp_Pnt& thePoint, double& theX, double& theY)
 {
   if (!mySketch)