Salome HOME
refs #30 - Sketch base GUI: create, draw lines
[modules/shaper.git] / src / PartSet / PartSet_OperationSketchLine.cpp
index 21f09e9c1f125e3004b8dbb6a4f78e6fde177541..03912db526aef928e604e76ae73206545d818dee 100644 (file)
@@ -4,20 +4,31 @@
 
 #include <PartSet_OperationSketchLine.h>
 
+#include <PartSet_Tools.h>
+
 #include <SketchPlugin_Feature.h>
 #include <GeomDataAPI_Point2D.h>
-#include <GeomDataAPI_Point.h>
-#include <GeomDataAPI_Dir.h>
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Document.h>
 
-#include <SketchPlugin_Sketch.h>
+#include <Geom_Line.hxx>
+#include <gp_Lin.hxx>
+
+#include <XGUI_ViewerPrs.h>
+
 #include <SketchPlugin_Line.h>
 
+#include <V3d_View.hxx>
+#include <TopoDS_Vertex.hxx>
+#include <TopoDS.hxx>
+#include <BRep_Tool.hxx>
+
 #ifdef _DEBUG
 #include <QDebug>
 #endif
 
+#include <QMouseEvent>
+
 using namespace std;
 
 PartSet_OperationSketchLine::PartSet_OperationSketchLine(const QString& theId,
@@ -39,28 +50,71 @@ bool PartSet_OperationSketchLine::isGranted() const
 
 std::list<int> PartSet_OperationSketchLine::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
 {
-  std::list<int> aModes;
-  if (theFeature != feature())
-    aModes.push_back(TopAbs_VERTEX);
-  return aModes;
+  return std::list<int>();
 }
 
-void PartSet_OperationSketchLine::mouseReleased(const gp_Pnt& thePoint)
+void PartSet_OperationSketchLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature)
 {
+  if (!theFeature)
+    return;
+  // use the last point of the previous feature as the first of the new one
+  boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
+  myInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
+}
+
+void PartSet_OperationSketchLine::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
+                                                const std::list<XGUI_ViewerPrs>& theSelected)
+{
+  gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
+
+  if (!theSelected.empty()) {
+    XGUI_ViewerPrs aPrs = theSelected.front();
+    const TopoDS_Shape& aShape = aPrs.shape();
+    if (!aShape.IsNull()) {
+      if (aShape.ShapeType() == TopAbs_VERTEX) {
+        const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
+        if (!aVertex.IsNull())
+          aPoint = BRep_Tool::Pnt(aVertex);
+      }
+      else if (aShape.ShapeType() == TopAbs_EDGE) {
+        boost::shared_ptr<ModelAPI_Feature> aFeature = aPrs.feature();
+        if (!aFeature)
+          return;
+        double X0, X1, X2, X3;
+        double Y0, Y1, Y2, Y3;
+        getLinePoint(aFeature, LINE_ATTR_START, X2, Y2);
+        getLinePoint(aFeature, LINE_ATTR_END, X3, Y3);
+
+        if (myPointSelectionMode == SM_SecondPoint) {
+          getLinePoint(feature(), LINE_ATTR_START, X0, Y0);
+          PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, X1, Y1);
+
+          double aX, anY;
+          PartSet_Tools::IntersectLines(X0, Y0, X1, Y1, X2, Y2, X3, Y3, aX, anY);
+
+          setLinePoint(aX, anY, LINE_ATTR_END);
+          commit();
+          emit featureConstructed(feature(), FM_Deactivation);
+          emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
+          return;
+        }
+
+      }
+    }
+  }
+
   switch (myPointSelectionMode)
   {
     case SM_FirstPoint: {
-      setLinePoint(thePoint, LINE_ATTR_START);
+      setLinePoint(aPoint, theView, LINE_ATTR_START);
       myPointSelectionMode = SM_SecondPoint;
     }
     break;
     case SM_SecondPoint: {
-      setLinePoint(thePoint, LINE_ATTR_END);
-      myPointSelectionMode = SM_None;
-    }
-    break;
-    case SM_None: {
-
+      setLinePoint(aPoint, theView, LINE_ATTR_END);
+      commit();
+      emit featureConstructed(feature(), FM_Deactivation);
+      emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
     }
     break;
     default:
@@ -68,27 +122,14 @@ void PartSet_OperationSketchLine::mouseReleased(const gp_Pnt& thePoint)
   }
 }
 
-void PartSet_OperationSketchLine::mouseMoved(const gp_Pnt& thePoint)
+void PartSet_OperationSketchLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
 {
   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, FM_Deactivation);
+    {
+      gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
+      setLinePoint(aPoint, theView, LINE_ATTR_END);
     }
     break;
     default:
@@ -100,19 +141,12 @@ void PartSet_OperationSketchLine::keyReleased(const int theKey)
 {
   switch (theKey) {
     case Qt::Key_Escape: {
-      if (myPointSelectionMode != SM_None)
-        emit featureConstructed(feature(), FM_Abort);
       abort();
     }
     break;
     case Qt::Key_Return: {
-      if (myPointSelectionMode != SM_None) {
-        emit featureConstructed(feature(), FM_Abort);
-        myPointSelectionMode = SM_FirstPoint;
-        document()->abortOperation();
-      }
-      else
-        myPointSelectionMode = SM_FirstPoint;
+      abort();
+      emit launchOperation(PartSet_OperationSketchLine::Type(), boost::shared_ptr<ModelAPI_Feature>());
     }
     break;
     default:
@@ -123,73 +157,72 @@ void PartSet_OperationSketchLine::keyReleased(const int theKey)
 void PartSet_OperationSketchLine::startOperation()
 {
   PartSet_OperationSketchBase::startOperation();
-  myPointSelectionMode = SM_FirstPoint;
+  myPointSelectionMode = !myInitPoint ? SM_FirstPoint : SM_SecondPoint;
+  emit multiSelectionEnabled(false);
+}
+
+void PartSet_OperationSketchLine::abortOperation()
+{
+  emit featureConstructed(feature(), FM_Abort);
+  PartSet_OperationSketchBase::abortOperation();
 }
 
 void PartSet_OperationSketchLine::stopOperation()
 {
   PartSet_OperationSketchBase::stopOperation();
-  myPointSelectionMode = SM_None;
+  emit multiSelectionEnabled(true);
 }
 
 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::createFeature()
 {
-  boost::shared_ptr<ModelAPI_Feature> aNewFeature = PartSet_OperationSketchBase::createFeature();
+  boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature();
   if (mySketch) {
     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(mySketch);
 
     aFeature->addSub(aNewFeature);
   }
-  //emit featureConstructed(aNewFeature, FM_Activation);
+  if (myInitPoint) {
+    boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
+    boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
+                                                                (aData->attribute(LINE_ATTR_START));
+    aPoint->setValue(myInitPoint->x(), myInitPoint->y());
+  }
+
+  emit featureConstructed(aNewFeature, FM_Activation);
   return aNewFeature;
 }
 
-void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
-                                               const std::string& theAttribute)
+void PartSet_OperationSketchLine::getLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
+                                               const std::string& theAttribute,
+                                               double& theX, double& theY)
 {
-  boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
+  if (!theFeature)
+    return;
+  boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
-
-  double aX = 0;
-  double anY = 0;
-  convertTo2D(thePoint, aX, anY);
-  aPoint->setValue(aX, anY);
+  theX = aPoint->x();
+  theY = aPoint->y();
 }
 
-void PartSet_OperationSketchLine::setLinePoint(boost::shared_ptr<ModelAPI_Feature> theSourceFeature,
-                                               const std::string& theSourceAttribute,
+void PartSet_OperationSketchLine::setLinePoint(double theX, double theY,
                                                const std::string& theAttribute)
 {
-  boost::shared_ptr<ModelAPI_Data> aData = theSourceFeature->data();
+  boost::shared_ptr<ModelAPI_Data> aData = feature()->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);
+        boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
+  aPoint->setValue(theX, theY);
 }
 
-void PartSet_OperationSketchLine::convertTo2D(const gp_Pnt& thePoint, double& theX, double& theY)
+void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
+                                               Handle(V3d_View) theView,
+                                               const std::string& theAttribute)
 {
-  if (!mySketch)
-    return;
-
-  boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
-  boost::shared_ptr<ModelAPI_Data> aData = mySketch->data();
-
-  boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
-    boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
-
-  boost::shared_ptr<GeomDataAPI_Dir> aX = 
-    boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
-  boost::shared_ptr<GeomDataAPI_Dir> anY = 
-    boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
-
-  gp_Pnt aVec(thePoint.X() - anOrigin->x(), thePoint.Y() - anOrigin->y(), thePoint.Z() - anOrigin->z());
-  theX = aVec.X() * aX->x() + aVec.Y() * aX->y() + aVec.Z() * aX->z();
-  theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
+  double aX, anY;
+  PartSet_Tools::ConvertTo2D(thePoint, mySketch, theView, aX, anY);
+  boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
+  boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
+        boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
+  aPoint->setValue(aX, anY);
 }