Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into SketchSolver
[modules/shaper.git] / src / PartSet / PartSet_OperationSketch.cpp
index d527cd0f3f1aee9d07598e9a2b2c36c40f1cbf6f..87f7635a58c1798cff6f4ec9b5fd86b94778829b 100644 (file)
@@ -4,10 +4,12 @@
 
 #include <PartSet_OperationSketch.h>
 
-#include <PartSet_OperationEditLine.h>
+#include <PartSet_OperationFeatureEdit.h>
+#include <PartSet_OperationFeatureEditMulti.h>
 #include <PartSet_Tools.h>
 
 #include <SketchPlugin_Sketch.h>
+#include <SketchPlugin_ConstraintLength.h>
 
 #include <ModelAPI_Data.h>
 #include <ModelAPI_AttributeDouble.h>
 #include <GeomAlgoAPI_FaceBuilder.h>
 #include <GeomDataAPI_Point.h>
 #include <GeomDataAPI_Dir.h>
+#include <GeomAPI_XYZ.h>
 
 #include <XGUI_ViewerPrs.h>
 
 #include <AIS_Shape.hxx>
 #include <AIS_ListOfInteractive.hxx>
+#include <AIS_InteractiveObject.hxx>
+#include <AIS_DimensionOwner.hxx>
+#include <AIS_LengthDimension.hxx>
 #include <V3d_View.hxx>
 
 #ifdef _DEBUG
@@ -33,7 +39,7 @@ using namespace std;
 
 PartSet_OperationSketch::PartSet_OperationSketch(const QString& theId,
                                                     QObject* theParent)
-: PartSet_OperationSketchBase(theId, theParent), myIsEditMode(false)
+: PartSet_OperationSketchBase(theId, theParent)
 {
 }
 
@@ -41,85 +47,105 @@ PartSet_OperationSketch::~PartSet_OperationSketch()
 {
 }
 
-std::list<int> PartSet_OperationSketch::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
+std::list<int> PartSet_OperationSketch::getSelectionModes(FeaturePtr theFeature) const
 {
   std::list<int> aModes;
-  if (!myIsEditMode)
+  if (!hasSketchPlane())
     aModes.push_back(TopAbs_FACE);
   else
     aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
+
   return aModes;
 }
 
-boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketch::sketch() const
+
+/// Initializes the operation with previously created feature. It is used in sequental operations
+void PartSet_OperationSketch::initFeature(FeaturePtr theFeature)
+{
+  if (theFeature)
+    setEditingFeature(theFeature);
+}
+
+FeaturePtr PartSet_OperationSketch::sketch() const
 {
   return feature();
 }
 
 void PartSet_OperationSketch::mousePressed(QMouseEvent* theEvent, Handle_V3d_View theView,
-                                           const std::list<XGUI_ViewerPrs>& theSelected)
+                                           const std::list<XGUI_ViewerPrs>& theSelected,
+                                           const std::list<XGUI_ViewerPrs>& theHighlighted)
 {
-  myFeatures = theSelected;
+  if (!hasSketchPlane()) {
+    if (!theHighlighted.empty()) {
+      XGUI_ViewerPrs aPrs = theHighlighted.front();
+      const TopoDS_Shape& aShape = aPrs.shape();
+      if (!aShape.IsNull())
+        setSketchPlane(aShape);
+    }
+  }
+  else {
+    // if shift button is pressed and there are some already selected objects, the operation should
+    // not be started. We just want to combine some selected objects.
+    bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
+    if (aHasShift && theSelected.size() > 0)
+      return;
+
+    if (theHighlighted.size() == 1) {
+      FeaturePtr aFeature = theHighlighted.front().feature();
+      if (aFeature) {
+        std::string anOperationType = PartSet_OperationFeatureEdit::Type();
+        if (theSelected.size() > 1)
+          anOperationType = PartSet_OperationFeatureEditMulti::Type();
+        restartOperation(anOperationType, aFeature);
+      }
+    }
+    else
+      myFeatures = theHighlighted;
+
+  }
 }
 
 void PartSet_OperationSketch::mouseReleased(QMouseEvent* theEvent, Handle_V3d_View theView,
-                                            const std::list<XGUI_ViewerPrs>& theSelected)
+                                            const std::list<XGUI_ViewerPrs>& theSelected,
+                                            const std::list<XGUI_ViewerPrs>& theHighlighted)
 {
-  if (theSelected.empty())
-    return;
-
-  if (!myIsEditMode) {
-    XGUI_ViewerPrs aPrs = theSelected.front();
-    const TopoDS_Shape& aShape = aPrs.shape();
-    if (!aShape.IsNull()) {
-      setSketchPlane(aShape);
-      myIsEditMode = true;
-    }
+  if (!hasSketchPlane()) {
   }
   else {
+    /// TODO: OCC bug: 25034 - the highlighted list should be filled not only for AIS_Shape
+    /// but for other IO, for example constraint dimensions.
+    /// It is empty and we have to use the process mouse release to start edition operation
+    /// for these objects
     if (theSelected.size() == 1) {
-      boost::shared_ptr<ModelAPI_Feature> aFeature = theSelected.front().feature();
+      FeaturePtr aFeature = theSelected.front().feature();
       if (aFeature)
-        emit launchOperation(PartSet_OperationEditLine::Type(), aFeature);
+        restartOperation(PartSet_OperationFeatureEdit::Type(), aFeature);
     }
   }
-  myFeatures.clear();
 }
 
 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
 {
-  if (!myIsEditMode || !(theEvent->buttons() &  Qt::LeftButton) || myFeatures.empty())
+  if (!hasSketchPlane() || !(theEvent->buttons() &  Qt::LeftButton) || myFeatures.empty())
     return;
 
   if (myFeatures.size() != 1) {
-    boost::shared_ptr<ModelAPI_Feature> aFeature = PartSet_Tools::NearestFeature(theEvent->pos(),
-                                                                theView, feature(), myFeatures);
+    FeaturePtr aFeature = PartSet_Tools::nearestFeature(theEvent->pos(), theView, feature(),
+                                                        myFeatures);
     if (aFeature)
-      emit launchOperation(PartSet_OperationEditLine::Type(), aFeature);
+               restartOperation(PartSet_OperationFeatureEditMulti::Type(), aFeature);
   }
 }
 
-std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
-                                                        PartSet_OperationSketch::subPreview() const
+std::list<FeaturePtr> PartSet_OperationSketch::subFeatures() const
 {
-  std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> > aPreviewMap;
-
-  boost::shared_ptr<SketchPlugin_Feature> aFeature;
-
   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
+  if (!aData->isValid())
+    return std::list<FeaturePtr>();
   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
         boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
 
-  std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
-  std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
-                                                                  aLast = aFeatures.end();
-  for (; anIt != aLast; anIt++) {
-    aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
-    boost::shared_ptr<GeomAPI_Shape> aPreview = aFeature->preview();
-    if (aPreview)
-      aPreviewMap[aFeature] = aPreview;
-  }
-  return aPreviewMap;
+  return aRefList->list();
 }
 
 void PartSet_OperationSketch::stopOperation()
@@ -129,6 +155,32 @@ void PartSet_OperationSketch::stopOperation()
   emit closeLocalContext();
 }
 
+bool PartSet_OperationSketch::isNestedOperationsEnabled() const
+{
+  return hasSketchPlane();
+}
+
+void PartSet_OperationSketch::startOperation()
+{
+  PartSet_OperationSketchBase::startOperation();
+  if (!isEditOperation())
+    emit fitAllView();
+}
+
+bool PartSet_OperationSketch::hasSketchPlane() const
+{
+  bool aHasPlane = false;
+
+  if (feature()) {
+    boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
+    AttributeDoublePtr anAttr;
+    boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
+      boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
+    aHasPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
+  }
+  return aHasPlane;
+}
+
 void PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
 {
   if (theShape.IsNull())
@@ -146,27 +198,35 @@ void PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
   double anA, aB, aC, aD;
   aPlane->coefficients(anA, aB, aC, aD);
 
-  boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
-  /*
-  aData->real(SKETCH_ATTR_PLANE_A)->setValue(anA);
-  aData->real(SKETCH_ATTR_PLANE_B)->setValue(aB);
-  aData->real(SKETCH_ATTR_PLANE_C)->setValue(aC);
-  aData->real(SKETCH_ATTR_PLANE_D)->setValue(aD);
-  */
-  // temporary solution for main planes only
+  // calculate attributes of the sketch
+  boost::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
+  boost::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
+  boost::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
+  aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
+  boost::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
+  // X axis is preferable to be dirX on the sketch
+  const double tol = Precision::Confusion();
+  bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
+  boost::shared_ptr<GeomAPI_Dir> aTempDir(isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
+  boost::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
+  boost::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
+
   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
     boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
-  anOrigin->setValue(0, 0, 0);
+  anOrigin->setValue(anOrigPnt);
   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
-  aNormal->setValue(anA, aB, aC);
+  aNormal->setValue(aNormDir);
   boost::shared_ptr<GeomDataAPI_Dir> aDirX = 
     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
-  aDirX->setValue(aB, aC, anA);
+  aDirX->setValue(aXDir);
   boost::shared_ptr<GeomDataAPI_Dir> aDirY = 
     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
-  aDirY->setValue(aC, anA, aB);
+  aDirY->setValue(aYDir);
   boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
+
+  flushUpdated();
+
   emit featureConstructed(feature(), FM_Hide);
   emit closeLocalContext();
   emit planeSelected(aDir->x(), aDir->y(), aDir->z());