Salome HOME
Issue #1393 Angle constraint : incorrect angle displayed. solution: do not select...
[modules/shaper.git] / src / PartSet / PartSet_WidgetPoint2d.cpp
index 072143b246a068559e3934899894b8e41a769ef1..a2c0245033bddbf26d787e7d2d61bba0607907e8 100644 (file)
@@ -17,6 +17,8 @@
 #include <ModuleBase_IViewer.h>
 #include <ModuleBase_IViewWindow.h>
 #include <ModuleBase_ISelection.h>
+#include <ModuleBase_ViewerPrs.h>
+#include <ModuleBase_WidgetValidator.h>
 
 #include <Config_Keywords.h>
 #include <Config_WidgetAPI.h>
@@ -58,9 +60,8 @@ static QStringList MyFeaturesForCoincedence;
 
 PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent, 
                                              ModuleBase_IWorkshop* theWorkshop,
-                                             const Config_WidgetAPI* theData,
-                                             const std::string& theParentId)
-: ModuleBase_ModelWidget(theParent, theData, theParentId), myWorkshop(theWorkshop),
+                                             const Config_WidgetAPI* theData)
+: ModuleBase_ModelWidget(theParent, theData), myWorkshop(theWorkshop),
   myValueIsCashed(false), myIsFeatureVisibleInCash(true),
   myXValueInCash(0), myYValueInCash(0)
 {
@@ -116,6 +117,22 @@ PartSet_WidgetPoint2D::PartSet_WidgetPoint2D(QWidget* theParent,
   ModuleBase_Tools::zeroMargins(aLayout);
   aLayout->addWidget(myGroupBox);
   setLayout(aLayout);
+
+  myWidgetValidator = new ModuleBase_WidgetValidator(this, myWorkshop);
+}
+
+bool PartSet_WidgetPoint2D::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& theValue)
+{
+  bool aValid = true;
+  /*if (getValidState(theValue, aValid)) {
+    return aValid;
+  }
+  aValid = isValidSelectionCustom(theValue);
+  if (aValid)
+    aValid = isValidSelectionForAttribute(theValue, attribute());
+
+  storeValidState(theValue, aValid);
+  */return aValid;
 }
 
 bool PartSet_WidgetPoint2D::resetCustom()
@@ -148,26 +165,27 @@ PartSet_WidgetPoint2D::~PartSet_WidgetPoint2D()
 {
 }
 
-bool PartSet_WidgetPoint2D::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
+bool PartSet_WidgetPoint2D::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
                                          const bool theToValidate)
 {
   bool isDone = false;
   if (theValues.empty())
     return isDone;
 
-  ModuleBase_ViewerPrs aValue = theValues.takeFirst();
-  TopoDS_Shape aShape = aValue.shape();
-  if (!aShape.IsNull()) {
+  ModuleBase_ViewerPrsPtr aValue = theValues.takeFirst();
+  GeomShapePtr aShape = aValue->shape();
+  if (aShape.get() && !aShape->isNull()) {
     Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
     double aX, aY;
-    if (getPoint2d(aView, aShape, aX, aY)) {
+    const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
+    if (getPoint2d(aView, aTDShape, aX, aY)) {
       isDone = setPoint(aX, aY);
       PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aX, aY);
     }
   }
   else if (canBeActivatedByMove()) {
     if (feature()->getKind() == SketchPlugin_Line::ID()) {
-      FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aValue.object());
+      FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aValue->object());
       // Initialize new line with first point equal to end of previous
       if (aFeature.get()) {
         std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
@@ -384,24 +402,44 @@ bool PartSet_WidgetPoint2D::getPoint2d(const Handle(V3d_View)& theView,
   return false;
 }
 
-void PartSet_WidgetPoint2D::setConstraintWith(const ObjectPtr& theObject)
+bool PartSet_WidgetPoint2D::setConstraintWith(const ObjectPtr& theObject)
 {
+  std::shared_ptr<GeomDataAPI_Point2D> aFeaturePoint;
+  if (feature()->isMacro()) {
+    AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
+    std::shared_ptr<GeomDataAPI_Point2D> anAttrPoint =
+                               std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
+    if (anAttrPoint.get()) {
+      // the macro feature will be removed after the operation is stopped, so we need to build
+      // coicidence to possible sub-features
+      aFeaturePoint = PartSet_Tools::findFirstEqualPointInArgumentFeatures(feature(),
+                                                                 anAttrPoint->pnt());
+    }
+  }
+  else {
+    AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
+    aFeaturePoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
+  }
+  if (!aFeaturePoint.get())
+    return false;
+
   // Create point-edge coincedence
   FeaturePtr aFeature = mySketch->addFeature(SketchPlugin_ConstraintCoincidence::ID());
   std::shared_ptr<ModelAPI_Data> aData = aFeature->data();
 
   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 = std::dynamic_pointer_cast<
       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
-  AttributePtr aThisAttr = feature()->data()->attribute(attributeID());
-  std::shared_ptr<GeomDataAPI_Point2D> aThisPoint = 
-    std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aThisAttr);
-  aRef1->setAttr(aThisPoint);
+
+  aRef1->setAttr(aFeaturePoint);
 
   std::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 = std::dynamic_pointer_cast<
       ModelAPI_AttributeRefAttr>(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
   aRef2->setObject(theObject);
 
-  aFeature->execute();
+  // we need to flush created signal in order to coincidence is processed by solver
+  Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
+
+  return true;
 }
 
 void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
@@ -427,11 +465,11 @@ void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMous
       aSPFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(aSelectedFeature);
       if ((!aSPFeature && !aShape.IsNull()) ||
           (aSPFeature.get() && aSPFeature->isExternal())) {
+        ResultPtr aFixedObject;
         anExternal = true;
-        ResultPtr aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
+        aFixedObject = PartSet_Tools::findFixedObjectByExternal(aShape, aObject, mySketch);
         if (!aFixedObject.get())
           aFixedObject = PartSet_Tools::createFixedObjectByExternal(aShape, aObject, mySketch);
-
         double aX, aY;
         if (getPoint2d(aView, aShape, aX, aY) && isFeatureContainsPoint(myFeature, aX, aY)) {
           // do not create a constraint to the point, which already used by the feature
@@ -451,12 +489,12 @@ void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMous
             if (aShape.ShapeType() == TopAbs_VERTEX) {
               FeaturePtr aFixedFeature = ModelAPI_Feature::feature(aFixedObject);
               if (aFixedFeature.get() && aFixedFeature->getKind() == SketchPlugin_Point::ID()) {
-                anOrphanPoint = isOrphanPoint(aFixedFeature, mySketch, aX, aY, true);
+                anOrphanPoint = isOrphanPoint(aFixedFeature, mySketch, aX, aY);
               }
             }
           }
-
-          setConstraintWith(aFixedObject);
+          if (aFixedObject.get())
+            setConstraintWith(aFixedObject);
           // fignal updated should be flushed in order to visualize possible created external objects
           // e.g. selection of trihedron axis when input end arc point
           updateObject(feature());
@@ -483,20 +521,17 @@ void PartSet_WidgetPoint2D::onMouseRelease(ModuleBase_IViewWindow* theWnd, QMous
         bool isAuxiliaryFeature = false;
         if (getPoint2d(aView, aShape, aX, aY)) {
           setPoint(aX, aY);
+          feature()->execute();
           PartSet_Tools::setConstraints(mySketch, feature(), attributeID(), aX, aY);
         }
         else if (aShape.ShapeType() == TopAbs_EDGE) {
-          if (MyFeaturesForCoincedence.contains(myFeature->getKind().c_str())) {
-            setConstraintWith(aObject);
-            setValueState(Stored); // in case of edge selection, Apply state should also be updated
-
-            FeaturePtr anObjectFeature = ModelAPI_Feature::feature(aObject);
-            std::string anAuxiliaryAttribute = SketchPlugin_SketchEntity::AUXILIARY_ID();
-            AttributeBooleanPtr anAuxiliaryAttr = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
-                                              anObjectFeature->data()->attribute(anAuxiliaryAttribute));
-            if (anAuxiliaryAttr.get())
-              isAuxiliaryFeature = anAuxiliaryAttr->value();
+          if (!setConstraintWith(aObject)) {
+            gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theWnd->v3dView());
+            PartSet_Tools::convertTo2D(aPoint, mySketch, aView, aX, aY);
+            setPoint(aX, aY);
           }
+          setValueState(Stored); // in case of edge selection, Apply state should also be updated
+          isAuxiliaryFeature = PartSet_Tools::isAuxiliarySketchEntity(aObject);
         }
         // it is important to perform updateObject() in order to the current value is 
         // processed by Sketch Solver. Test case: line is created from a previous point
@@ -628,7 +663,7 @@ bool PartSet_WidgetPoint2D::useSelectedShapes() const
 
 bool PartSet_WidgetPoint2D::isOrphanPoint(const FeaturePtr& theFeature,
                                           const CompositeFeaturePtr& theSketch,
-                                          double theX, double theY, const bool theSearchInResults)
+                                          double theX, double theY)
 {
   bool anOrphanPoint = false;
   if (theFeature.get()) {
@@ -659,14 +694,15 @@ bool PartSet_WidgetPoint2D::isOrphanPoint(const FeaturePtr& theFeature,
       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aPointAttr->pnt();
       // we need to find coincidence features in results also, because external object(point)
       // uses refs to me in another feature.
-      FeaturePtr aCoincidence = PartSet_Tools::findFirstCoincidence(theFeature, aPoint, theSearchInResults);
+      FeaturePtr aCoincidence = PartSet_Tools::findFirstCoincidence(theFeature, aPoint);
       anOrphanPoint = true;
       // if there is at least one concident line to the point, the point is not an orphant
       if (aCoincidence.get()) {
         QList<FeaturePtr> aCoinsideLines;
-        PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines,
+        QList<FeaturePtr> aCoins;
+        PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines, aCoins,
                                         SketchPlugin_ConstraintCoincidence::ENTITY_A());
-        PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines,
+        PartSet_Tools::findCoincidences(aCoincidence, aCoinsideLines, aCoins,
                                         SketchPlugin_ConstraintCoincidence::ENTITY_B());
         QList<FeaturePtr>::const_iterator anIt = aCoinsideLines.begin(),
                                           aLast = aCoinsideLines.end();