Salome HOME
Fixes and unit-tests basing on the manual tests
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Trim.cpp
index aedc763322e4c4969e9a362fe366714debcf09d9..387f3a8b4e9edaf2ccfcffc9263c3f0b0066b81d 100644 (file)
@@ -45,7 +45,6 @@
 #include <SketchPlugin_Circle.h>
 #include <SketchPlugin_ConstraintCoincidence.h>
 #include <SketchPlugin_ConstraintEqual.h>
-//#include <SketchPlugin_ConstraintParallel.h>
 #include <SketchPlugin_ConstraintTangent.h>
 #include <SketchPlugin_ConstraintLength.h>
 #include <SketchPlugin_ConstraintMirror.h>
@@ -55,6 +54,7 @@
 #include <SketchPlugin_MultiTranslation.h>
 #include <SketchPlugin_Point.h>
 #include <SketchPlugin_Projection.h>
+#include <SketchPlugin_Tools.h>
 
 #include <ModelAPI_EventReentrantMessage.h>
 
@@ -68,9 +68,6 @@
 
 #include <cmath>
 
-//#define DEBUG_TRIM_METHODS
-//#define DEBUG_TRIM
-
 #ifdef DEBUG_TRIM
 #include <iostream>
 #endif
@@ -362,8 +359,9 @@ void SketchPlugin_Trim::execute()
     const std::list<ObjectPtr>& anObjects = anInfo.second;
     for (std::list<ObjectPtr>::const_iterator anObjectIt = anObjects.begin();
       anObjectIt != anObjects.end(); anObjectIt++) {
-      createConstraintToObject(SketchPlugin_ConstraintCoincidence::ID(), aPointAttribute,
-                               *anObjectIt);
+      SketchPlugin_Tools::createConstraintAttrObject(sketch(),
+            SketchPlugin_ConstraintCoincidence::ID(),
+            aPointAttribute, *anObjectIt);
     }
   }
 
@@ -379,7 +377,7 @@ void SketchPlugin_Trim::execute()
       anIt != aLast; anIt++) {
     AttributePtr anAttribute = *anIt;
 
-    if (setCoincidenceToAttribute(anAttribute, aFurtherCoincidences))
+    if (setCoincidenceToAttribute(anAttribute, aFurtherCoincidences, aFeaturesToDelete))
       continue;
 
     // move tangency constraint to the nearest feature if possible
@@ -533,7 +531,8 @@ std::string SketchPlugin_Trim::processEvent(const std::shared_ptr<Events_Message
 }
 
 bool SketchPlugin_Trim::setCoincidenceToAttribute(const AttributePtr& theAttribute,
-                                const std::set<AttributePoint2DPtr>& theFurtherCoincidences)
+                                const std::set<AttributePoint2DPtr>& theFurtherCoincidences,
+                                std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete)
 {
   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
   if (aFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
@@ -551,12 +550,11 @@ bool SketchPlugin_Trim::setCoincidenceToAttribute(const AttributePtr& theAttribu
     AttributePoint2DPtr aPointAttribute = (*anIt);
     std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
     if (aPoint2d->isEqual(aRefPnt2d)) {
-      AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
-                                                                           theAttribute);
-      if (aRefAttr.get()) {
-        aRefAttr->setAttr(aPointAttribute);
-        aFoundPoint = true;
-      }
+      // create new coincidence and then remove the old one
+      SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
+          SketchPlugin_ConstraintCoincidence::ID(),
+          aRefPointAttr, aPointAttribute);
+      theFeaturesToDelete.insert(aFeature);
     }
   }
   return aFoundPoint;
@@ -966,10 +964,10 @@ FeaturePtr SketchPlugin_Trim::trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& the
                                (aBaseFeature->attribute(aModifiedAttribute)));
 
     // Collinear constraint for lines
-    createConstraintForObjects(SketchPlugin_ConstraintCollinear::ID(),
-                               getFeatureResult(aBaseFeature),
-                               getFeatureResult(anNewFeature));
-
+    SketchPlugin_Tools::createConstraintObjectObject(sketch(),
+                                         SketchPlugin_ConstraintCollinear::ID(),
+                                         getFeatureResult(aBaseFeature),
+                                         getFeatureResult(anNewFeature));
   }
   return anNewFeature;
 }
@@ -1051,13 +1049,15 @@ FeaturePtr SketchPlugin_Trim::trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theS
                                (aBaseFeature->attribute(aModifiedAttribute)));
 
     // equal Radius constraint for arcs
-    createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(),
-                               getFeatureResult(aBaseFeature),
-                               getFeatureResult(anNewFeature));
+    SketchPlugin_Tools::createConstraintObjectObject(sketch(),
+                                         SketchPlugin_ConstraintEqual::ID(),
+                                         getFeatureResult(aBaseFeature),
+                                         getFeatureResult(anNewFeature));
     // coincident centers constraint
-    createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
-                     aBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
-                     anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
+    SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
+                                         SketchPlugin_ConstraintCoincidence::ID(),
+                                         aBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
+                                         anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
 
 #ifdef DEBUG_TRIM
     std::cout << "Created arc on points:" << std::endl;
@@ -1294,69 +1294,6 @@ FeaturePtr SketchPlugin_Trim::createArcFeature(const FeaturePtr& theBaseFeature,
   return aFeature;
 }
 
-FeaturePtr SketchPlugin_Trim::createConstraint(const std::string& theConstraintId,
-                                               const AttributePtr& theFirstAttribute,
-                                               const AttributePtr& theSecondAttribute)
-{
-  FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
-  AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
-                                 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
-  aRefAttr->setAttr(theFirstAttribute);
-
-  aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
-                                 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
-  aRefAttr->setAttr(theSecondAttribute);
-
-#ifdef DEBUG_TRIM
-  std::cout << "<createConstraint to attribute> :"
-            << "first attribute - " << theFirstAttribute->id()
-            << "second attribute - " << theSecondAttribute->id()
-            << std::endl;
-#endif
-
-  return aConstraint;
-}
-
-FeaturePtr SketchPlugin_Trim::createConstraintToObject(const std::string& theConstraintId,
-                                               const AttributePtr& theFirstAttribute,
-                                               const ObjectPtr& theSecondObject)
-{
-  FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
-  AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
-                                 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
-  aRefAttr->setAttr(theFirstAttribute);
-
-  aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
-                                 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
-  aRefAttr->setObject(theSecondObject);
-
-#ifdef DEBUG_TRIM
-  std::cout << "<createConstraint to attribute> :"
-            << "first attribute - " << theFirstAttribute->id()
-            << "second object - " << ModelAPI_Feature::feature(theSecondObject)->getKind()
-            << std::endl;
-#endif
-
-  return aConstraint;
-}
-
-FeaturePtr SketchPlugin_Trim::createConstraintForObjects(
-                                                    const std::string& theConstraintId,
-                                                    const ObjectPtr& theFirstObject,
-                                                    const ObjectPtr& theSecondObject)
-{
-  FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
-  AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
-                                 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
-  aRefAttr->setObject(theFirstObject);
-
-  aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
-                                 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
-  aRefAttr->setObject(theSecondObject);
-
-  return aConstraint;
-}
-
 std::shared_ptr<ModelAPI_Result> SketchPlugin_Trim::getFeatureResult(
                                     const std::shared_ptr<ModelAPI_Feature>& theFeature)
 {