Salome HOME
Issue #2109 trim - wrong result due to constarints
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Trim.cpp
index 4e60668376998ee0c60c7a55317efdd15f1b99d3..9ad6f0db9ecb71e6b63ee53c4f49d71abd29d794 100644 (file)
@@ -189,8 +189,10 @@ void SketchPlugin_Trim::execute()
 #endif
 
   SketchPlugin_Sketch* aSketch = sketch();
-  if (!aSketch)
+  if (!aSketch) {
+    setError("Error: Sketch object is empty.");
     return;
+  }
 
   // Check the base objects are initialized.
   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
@@ -200,8 +202,10 @@ void SketchPlugin_Trim::execute()
     return;
   }
   ObjectPtr aBaseObject = aBaseObjectAttr->value();
-  if (!aBaseObject.get())
+  if (!aBaseObject.get()) {
+    setError("Error: Base object is not initialized.");
     return;
+  }
   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
 
   /// Remove reference of this feature to feature used in preview, it is not necessary anymore
@@ -229,8 +233,8 @@ void SketchPlugin_Trim::execute()
   std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint2d = convertPoint(aStartShapePoint);
   std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint2d = convertPoint(aLastShapePoint);
 
-  std::set<FeaturePtr> aFeaturesToDelete;
-  getConstraints(aFeaturesToDelete);
+  std::set<FeaturePtr> aFeaturesToDelete, aFeaturesToUpdate;
+  getConstraints(aFeaturesToDelete, aFeaturesToUpdate);
 
   std::map<AttributePtr, std::list<AttributePtr> > aBaseRefAttributes;
   std::list<AttributePtr> aRefsToFeature;
@@ -410,6 +414,8 @@ void SketchPlugin_Trim::execute()
   ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
 
+  updateFeaturesAfterTrim(aFeaturesToUpdate);
+
   // Send events to update the sub-features by the solver.
   if(isUpdateFlushed) {
     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
@@ -481,7 +487,6 @@ std::string SketchPlugin_Trim::processEvent(const std::shared_ptr<Events_Message
 #ifdef DEBUG_TRIM_METHODS
   std::cout << "SketchPlugin_Trim::processEvent:" << data()->name() << std::endl;
 #endif
-
   std::string aFilledAttributeName;
 
   std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage =
@@ -491,34 +496,39 @@ std::string SketchPlugin_Trim::processEvent(const std::shared_ptr<Events_Message
     std::shared_ptr<GeomAPI_Pnt2d> aPoint = aMessage->clickedPoint();
 
     if (anObject.get() && aPoint.get()) {
-      std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
-                            std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
-                            data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
-      std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
-                            std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
-                            data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
-      aRefSelectedAttr->setValue(anObject);
-      aRefPreviewAttr->setValue(anObject);
-
-      std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
-                            std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-                            data()->attribute(SketchPlugin_Trim::SELECTED_POINT()));
-      std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
-                            std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
-                            data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
-      aPointSelectedAttr->setValue(aPoint);
-      aPointPreviewAttr->setValue(aPoint);
-
-      Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
-
-      GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
-#ifdef DEBUG_TRIM_METHODS
-      if (!aSelectedShape.get())
-        std::cout << "Set empty selected object" << std::endl;
-      else
-        std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
-#endif
-      aFilledAttributeName = SketchPlugin_Trim::SELECTED_OBJECT();
+      if (myCashedShapes.find(anObject) == myCashedShapes.end())
+        fillObjectShapes(anObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
+      const std::set<GeomShapePtr>& aShapes = myCashedShapes[anObject];
+      if (aShapes.size() > 1) {
+        std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
+                              std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
+                              data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
+        std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
+                              std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
+                              data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
+        aRefSelectedAttr->setValue(anObject);
+        aRefPreviewAttr->setValue(anObject);
+
+        std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
+                              std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+                              data()->attribute(SketchPlugin_Trim::SELECTED_POINT()));
+        std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
+                              std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+                              data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
+        aPointSelectedAttr->setValue(aPoint);
+        aPointPreviewAttr->setValue(aPoint);
+
+        Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
+
+        GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
+  #ifdef DEBUG_TRIM_METHODS
+        if (!aSelectedShape.get())
+          std::cout << "Set empty selected object" << std::endl;
+        else
+          std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
+  #endif
+        aFilledAttributeName = SketchPlugin_Trim::SELECTED_OBJECT();
+      }
     }
   }
   return aFilledAttributeName;
@@ -689,7 +699,8 @@ void SketchPlugin_Trim::getFeaturePoints(const FeaturePtr& theFeature,
   }
 }
 
-void SketchPlugin_Trim::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete)
+void SketchPlugin_Trim::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
+                                       std::set<FeaturePtr>& theFeaturesToUpdate)
 {
   std::shared_ptr<ModelAPI_Data> aData = data();
 
@@ -713,6 +724,8 @@ void SketchPlugin_Trim::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete
         aRefFeatureKind == SketchPlugin_MultiTranslation::ID() ||
         aRefFeatureKind == SketchPlugin_ConstraintMiddle::ID())
       theFeaturesToDelete.insert(aRefFeature);
+    else if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID())
+      theFeaturesToUpdate.insert(aRefFeature);
   }
 }
 
@@ -869,6 +882,27 @@ void SketchPlugin_Trim::removeReferencesToAttribute(const AttributePtr& theAttri
   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
 }
 
+void SketchPlugin_Trim::updateFeaturesAfterTrim(const std::set<FeaturePtr>& theFeaturesToUpdate)
+{
+  std::set<FeaturePtr>::const_iterator anIt = theFeaturesToUpdate.begin(),
+                                       aLast = theFeaturesToUpdate.end();
+  for (; anIt != aLast; anIt++) {
+    FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
+    std::string aRefFeatureKind = aRefFeature->getKind();
+    if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID()) {
+      std::shared_ptr<SketchPlugin_ConstraintLength> aLenghtFeature =
+                              std::dynamic_pointer_cast<SketchPlugin_ConstraintLength>(*anIt);
+      if (aLenghtFeature.get()) {
+        std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
+            ModelAPI_AttributeDouble>(aLenghtFeature->attribute(SketchPlugin_Constraint::VALUE()));
+        double aValue;
+        if (aLenghtFeature->computeLenghtValue(aValue) && aValueAttr.get())
+          aValueAttr->setValue(aValue);
+      }
+    }
+  }
+}
+
 FeaturePtr SketchPlugin_Trim::trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
                   const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
                   std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
@@ -1037,7 +1071,6 @@ FeaturePtr SketchPlugin_Trim::trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theS
                                (aBaseFeature->attribute(aModifiedAttribute)));
 
     // equal Radius constraint for arcs
-    anNewFeature->execute(); // we need the created arc result to set equal constraint
     createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(),
                                getFeatureResult(aBaseFeature),
                                getFeatureResult(anNewFeature));
@@ -1271,7 +1304,7 @@ FeaturePtr SketchPlugin_Trim::createArcFeature(const FeaturePtr& theBaseFeature,
     bool aReversed = theBaseFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
     aFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(aReversed);
   }
-  //aFeature->execute(); // to obtain result
+  aFeature->execute(); // to obtain result (need to calculate arc parameters before sending Update)
   aFeature->data()->blockSendAttributeUpdated(aWasBlocked);
 
   #ifdef DEBUG_TRIM