Salome HOME
Merge remote-tracking branch 'remotes/origin/SketchSolver_Linux'
[modules/shaper.git] / src / PartSet / PartSet_OperationSketchLine.cpp
index 97fd78c7458142d2d08ae6f017575c402ab72b4c..6b64cd2759cff1930c142411f11a40d44ab5d1bc 100644 (file)
@@ -5,11 +5,15 @@
 #include <PartSet_OperationSketchLine.h>
 
 #include <PartSet_Tools.h>
+#include <PartSet_OperationSketch.h>
 
 #include <SketchPlugin_Feature.h>
 #include <SketchPlugin_Sketch.h>
 
 #include <GeomDataAPI_Point2D.h>
+
+#include <ModuleBase_OperationDescription.h>
+
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Document.h>
 #include <ModelAPI_AttributeRefAttr.h>
@@ -49,31 +53,34 @@ PartSet_OperationSketchLine::~PartSet_OperationSketchLine()
 {
 }
 
-bool PartSet_OperationSketchLine::isGranted() const
+bool PartSet_OperationSketchLine::isGranted(ModuleBase_IOperation* theOperation) const
 {
-  return true;
+  return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
 }
 
 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);
-    aModes.push_back(TopAbs_EDGE);
-  }
+    aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
   return aModes;
 }
 
-void PartSet_OperationSketchLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature)
+void PartSet_OperationSketchLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature,
+                                       const std::list<XGUI_ViewerPrs>& /*thePresentations*/)
 {
-  if (!theFeature)
+  if (!theFeature || theFeature->getKind() != "SketchLine")
     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));
 }
 
+boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::sketch() const
+{
+  return mySketch;
+}
+
 void PartSet_OperationSketchLine::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
                                                 const std::list<XGUI_ViewerPrs>& theSelected)
 {
@@ -82,7 +89,7 @@ void PartSet_OperationSketchLine::mouseReleased(QMouseEvent* theEvent, Handle(V3
   bool isFoundPoint = false;
   gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
   if (theSelected.empty()) {
-    PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, aX, anY);
+    PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
     isFoundPoint = true;
   }
   else {
@@ -94,7 +101,7 @@ void PartSet_OperationSketchLine::mouseReleased(QMouseEvent* theEvent, Handle(V3
         const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
         if (!aVertex.IsNull()) {
           aPoint = BRep_Tool::Pnt(aVertex);
-          PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, aX, anY);
+          PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
           isFoundPoint = true;
 
           setConstraints(aX, anY);
@@ -108,7 +115,7 @@ void PartSet_OperationSketchLine::mouseReleased(QMouseEvent* theEvent, Handle(V3
           double Y0, Y1, Y2, Y3;
           getLinePoint(aFeature, LINE_ATTR_START, X2, Y2);
           getLinePoint(aFeature, LINE_ATTR_END, X3, Y3);
-          PartSet_Tools::ConvertTo2D(aPoint, mySketch, theView, X1, Y1);
+          PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, X1, Y1);
 
           switch (myPointSelectionMode) {
             case SM_FirstPoint:
@@ -133,15 +140,14 @@ void PartSet_OperationSketchLine::mouseReleased(QMouseEvent* theEvent, Handle(V3
   switch (myPointSelectionMode)
   {
     case SM_FirstPoint: {
-      setLinePoint(aX, anY, LINE_ATTR_START);
+      setLinePoint(feature(), aX, anY, LINE_ATTR_START);
+      setLinePoint(feature(), aX, anY, LINE_ATTR_END);
       myPointSelectionMode = SM_SecondPoint;
     }
     break;
     case SM_SecondPoint: {
-      setLinePoint(aX, anY, LINE_ATTR_END);
-      commit();
-      emit featureConstructed(feature(), FM_Deactivation);
-      emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
+      setLinePoint(feature(), aX, anY, LINE_ATTR_END);
+      myPointSelectionMode = SM_DonePoint;
     }
     break;
     default:
@@ -149,17 +155,30 @@ void PartSet_OperationSketchLine::mouseReleased(QMouseEvent* theEvent, Handle(V3
   }
 }
 
-void PartSet_OperationSketchLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView,
-                                             const std::list<XGUI_ViewerPrs>& /*theSelected*/)
+void PartSet_OperationSketchLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
 {
   switch (myPointSelectionMode)
   {
+    case SM_FirstPoint: {
+      double aX, anY;
+      gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
+      PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
+      setLinePoint(feature(), aX, anY, LINE_ATTR_START);
+      setLinePoint(feature(), aX, anY, LINE_ATTR_END);
+    }
+    break;
     case SM_SecondPoint:
     {
       gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
       setLinePoint(aPoint, theView, LINE_ATTR_END);
     }
     break;
+    case SM_DonePoint:
+    {
+      commit();
+      emit featureConstructed(feature(), FM_Deactivation);
+      emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
+    }
     default:
       break;
   }
@@ -168,16 +187,19 @@ void PartSet_OperationSketchLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_V
 void PartSet_OperationSketchLine::keyReleased(const int theKey)
 {
   switch (theKey) {
-    case Qt::Key_Escape: {
-      abort();
-    }
-    break;
     case Qt::Key_Return: {
-      abort();
+      if (myPointSelectionMode == SM_DonePoint)
+      {
+        commit();
+        emit featureConstructed(feature(), FM_Deactivation);
+      }
+      else
+        abort();
       emit launchOperation(PartSet_OperationSketchLine::Type(), boost::shared_ptr<ModelAPI_Feature>());
     }
     break;
     default:
+      PartSet_OperationSketchBase::keyReleased(theKey); 
     break;
   }
 }
@@ -191,7 +213,7 @@ void PartSet_OperationSketchLine::startOperation()
 
 void PartSet_OperationSketchLine::abortOperation()
 {
-  emit featureConstructed(feature(), FM_Abort);
+  emit featureConstructed(feature(), FM_Hide);
   PartSet_OperationSketchBase::abortOperation();
 }
 
@@ -204,18 +226,19 @@ void PartSet_OperationSketchLine::stopOperation()
 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::createFeature()
 {
   boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature();
-  if (mySketch) {
+  if (sketch()) {
     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
-                           boost::dynamic_pointer_cast<SketchPlugin_Feature>(mySketch);
+                           boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
 
     aFeature->addSub(aNewFeature);
   }
   if (myInitPoint) {
+    setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_START);
+    setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_END);
+
     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());
-
     createConstraint(myInitPoint, aPoint);
   }
 
@@ -229,6 +252,12 @@ void PartSet_OperationSketchLine::createConstraint(boost::shared_ptr<GeomDataAPI
   boost::shared_ptr<ModelAPI_Document> aDoc = document();
   boost::shared_ptr<ModelAPI_Feature> aFeature = aDoc->addFeature("SketchConstraintCoincidence");
 
+  if (sketch()) {
+    boost::shared_ptr<SketchPlugin_Feature> aSketch = 
+                           boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
+    aSketch->addSub(aFeature);
+  }
+
   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
 
   boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
@@ -259,7 +288,7 @@ void PartSet_OperationSketchLine::setConstraints(double theX, double theY)
   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
                                                               (aData->attribute(aPointArg));
-  aData = mySketch->data();
+  aData = sketch()->data();
   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
         boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
 
@@ -278,7 +307,7 @@ void PartSet_OperationSketchLine::getLinePoint(boost::shared_ptr<ModelAPI_Featur
                                                const std::string& theAttribute,
                                                double& theX, double& theY)
 {
-  if (!theFeature)
+  if (!theFeature || theFeature->getKind() != "SketchLine")
     return;
   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
@@ -292,10 +321,10 @@ boost::shared_ptr<GeomDataAPI_Point2D> PartSet_OperationSketchLine::findLinePoin
                                                double theX, double theY)
 {
   boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
-  if (!theFeature)
+  if (!theFeature || theFeature->getKind() != "SketchLine")
     return aPoint2D;
   boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
-
+  
   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
   if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
@@ -308,10 +337,13 @@ boost::shared_ptr<GeomDataAPI_Point2D> PartSet_OperationSketchLine::findLinePoin
   return aPoint2D;
 }
 
-void PartSet_OperationSketchLine::setLinePoint(double theX, double theY,
+void PartSet_OperationSketchLine::setLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
+                                               double theX, double theY,
                                                const std::string& theAttribute)
 {
-  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));
   aPoint->setValue(theX, theY);
@@ -322,7 +354,7 @@ void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
                                                const std::string& theAttribute)
 {
   double aX, anY;
-  PartSet_Tools::ConvertTo2D(thePoint, mySketch, theView, aX, anY);
+  PartSet_Tools::ConvertTo2D(thePoint, sketch(), 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));