Salome HOME
Change icons for chamfer
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Split.cpp
index a7a8f69b224e7250d4ef0b9ad8ab173509c5f3f9..ac6a15d6d78d2094735b93585d9541e29e7ffb9f 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 //
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/ or
-// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 #include "SketchPlugin_Split.h"
@@ -55,6 +54,7 @@
 #include <SketchPlugin_MultiRotation.h>
 #include <SketchPlugin_MultiTranslation.h>
 #include <SketchPlugin_Point.h>
+#include <SketchPlugin_Tools.h>
 
 #include <ModelGeomAlgo_Point2D.h>
 #include <ModelAPI_EventReentrantMessage.h>
@@ -64,7 +64,6 @@
 
 //#define CREATE_CONSTRAINTS
 
-//#define DEBUG_SPLIT
 #ifdef DEBUG_SPLIT
 #include <iostream>
 #endif
@@ -235,6 +234,8 @@ void SketchPlugin_Split::execute()
   std::cout << std::endl;
 #endif
 
+  keepCurrentFeature();
+
   std::string aFeatureKind = aBaseFeature->getKind();
   FeaturePtr aSplitFeature, anAfterFeature;
   std::set<AttributePoint2DPtr> aFurtherCoincidences;
@@ -247,6 +248,9 @@ void SketchPlugin_Split::execute()
   else if (aFeatureKind == SketchPlugin_Arc::ID())
     aNewFeature = splitArc(aSplitFeature, aBaseFeature, anAfterFeature, aFurtherCoincidences,
                            aCreatedFeatures, aModifiedAttributes);
+
+  restoreCurrentFeature();
+
   if (aFeatureKind == SketchPlugin_Circle::ID()) {
     FeaturePtr aCircleFeature = aBaseFeature;
     aReplacingFeature = splitCircle(aSplitFeature, aBaseFeature, anAfterFeature,
@@ -317,7 +321,7 @@ void SketchPlugin_Split::execute()
 
   // coincidence to feature
   updateCoincidenceConstraintsToFeature(aCoincidenceToFeature, aFurtherCoincidences,
-                                        aFeatureResults, aSplitFeature);
+                                        aFeatureResults, aSplitFeature, aFeaturesToDelete);
 
   updateRefAttConstraints(aBaseRefAttributes, aModifiedAttributes);
 
@@ -371,7 +375,7 @@ void SketchPlugin_Split::execute()
       //}
       //aBaseShape = aShape;
 
-#ifdef DEBUG_TRIM_METHODS
+#ifdef DEBUG_SPLIT
       if (!aSelectedShape.get())
         std::cout << "Set empty selected object" << std::endl;
       else
@@ -421,10 +425,11 @@ void SketchPlugin_Split::execute()
 #endif
 }
 
+// LCOV_EXCL_START
 std::string SketchPlugin_Split::processEvent(const std::shared_ptr<Events_Message>& theMessage)
 {
-#ifdef DEBUG_TRIM_METHODS
-  std::cout << "SketchPlugin_Trim::processEvent:" << data()->name() << std::endl;
+#ifdef DEBUG_SPLIT
+  std::cout << "SketchPlugin_Split::processEvent:" << data()->name() << std::endl;
 #endif
   std::string aFilledAttributeName;
 
@@ -462,18 +467,29 @@ std::string SketchPlugin_Split::processEvent(const std::shared_ptr<Events_Messag
         Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
 
         GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
-  #ifdef DEBUG_TRIM_METHODS
+        if (aSelectedShape.get()) {
+          aFilledAttributeName = SELECTED_OBJECT();
+        }
+        else {
+          // #2480 - sub shape is not initialized when split sketch
+          // If restarted operation use some selection on the shape that is split and
+          // result selectiona can not participate in new split(checked shape above is null),
+          // reset filled values of selection set in this method above
+          aRefSelectedAttr->setValue(ResultPtr());
+          aRefPreviewAttr->setValue(ResultPtr());
+        }
+  #ifdef DEBUG_SPLIT
         if (!aSelectedShape.get())
           std::cout << "Set empty selected object" << std::endl;
         else
           std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
   #endif
-        aFilledAttributeName = SELECTED_OBJECT();
       }
     }
   }
   return aFilledAttributeName;
 }
+// LCOV_EXCL_STOP
 
 AISObjectPtr SketchPlugin_Split::getAISObject(AISObjectPtr thePrevious)
 {
@@ -537,8 +553,8 @@ AISObjectPtr SketchPlugin_Split::getAISObject(AISObjectPtr thePrevious)
     return anAIS;
   }
   return AISObjectPtr();*/
-#ifdef DEBUG_TRIM_METHODS
-  std::cout << "SketchPlugin_Trim::getAISObject: " << data()->name() << std::endl;
+#ifdef DEBUG_SPLIT
+  std::cout << "SketchPlugin_Split::getAISObject: " << data()->name() << std::endl;
 #endif
 
   AISObjectPtr anAIS = thePrevious;
@@ -609,7 +625,8 @@ void SketchPlugin_Split::fillObjectShapes(const ObjectPtr& theObject,
     ModelGeomAlgo_Point2D::getPointsInsideShape_p(aFeatureShape, aRefAttributes, aC->pnt(),
                                                 aX->dir(), aY, aPoints, aPointToAttributes);
 
-    GeomAlgoAPI_ShapeTools::splitShape_p(aFeatureShape, aPoints, aShapes);
+    if (!aPoints.empty())
+      GeomAlgoAPI_ShapeTools::splitShape_p(aFeatureShape, aPoints, aShapes);
   }
   myCashedShapes[theObject] = aShapes;
   myCashedReferences[theObject] = aPointToAttributes;
@@ -703,23 +720,6 @@ GeomShapePtr SketchPlugin_Split::getSubShape(const std::string& theObjectAttribu
   return aBaseShape;
 }
 
-std::shared_ptr<GeomDataAPI_Point2D> SketchPlugin_Split::getPointOfRefAttr(
-                                                      const AttributePtr& theAttribute)
-{
-  AttributePoint2DPtr aPointAttribute;
-
-  if (theAttribute->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
-    AttributeRefAttrPtr aRefAttr =
-      std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
-    if (aRefAttr.get() && aRefAttr->isInitialized()) {
-      AttributePtr anAttribute = aRefAttr->attr();
-      if (anAttribute.get() && anAttribute->attributeType() == GeomDataAPI_Point2D::typeId())
-        aPointAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
-    }
-  }
-  return aPointAttribute;
-}
-
 void SketchPlugin_Split::getFeaturePoints(const FeaturePtr& theFeature,
                                                     AttributePoint2DPtr& theStartPointAttr,
                                                     AttributePoint2DPtr& theEndPointAttr)
@@ -875,7 +875,8 @@ void SketchPlugin_Split::updateCoincidenceConstraintsToFeature(
       const std::map<std::shared_ptr<ModelAPI_Feature>, IdToPointPair>& theCoincidenceToFeature,
       const std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theFurtherCoincidences,
       const std::set<ResultPtr>& theFeatureResults,
-      const FeaturePtr& theSplitFeature)
+      const FeaturePtr& theSplitFeature,
+      std::set<FeaturePtr>& theFeaturesToDelete)
 {
   if (theCoincidenceToFeature.empty())
     return;
@@ -898,6 +899,9 @@ void SketchPlugin_Split::updateCoincidenceConstraintsToFeature(
   for (; aCIt != aCLast; aCIt++) {
     FeaturePtr aCoincFeature = aCIt->first;
     std::string anAttributeId = aCIt->second.first;
+    std::string aSecondAttribute = anAttributeId == SketchPlugin_Constraint::ENTITY_A() ?
+        SketchPlugin_Constraint::ENTITY_B() : SketchPlugin_Constraint::ENTITY_A();
+
     AttributePoint2DPtr aCoincPoint = aCIt->second.second;
     std::set<AttributePoint2DPtr>::const_iterator aFCIt = theFurtherCoincidences.begin(),
                                                   aFCLast = theFurtherCoincidences.end();
@@ -909,18 +913,19 @@ void SketchPlugin_Split::updateCoincidenceConstraintsToFeature(
         aFeaturePointAttribute = aFCAttribute;
     }
     if (aFeaturePointAttribute.get()) {
-      aCoincFeature->refattr(anAttributeId)->setObject(ResultPtr());
-      aCoincFeature->refattr(anAttributeId)->setAttr(aFeaturePointAttribute);
+      // create new constraint and remove the current
+      aCoincFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
+          SketchPlugin_ConstraintCoincidence::ID(),
+          aFeaturePointAttribute, aCoincFeature->refattr(aSecondAttribute)->attr());
+      theFeaturesToDelete.insert(aCIt->first);
       // create new coincidences to split feature points
       std::set<AttributePoint2DPtr>::const_iterator aSFIt = aNewCoincidencesToSplitFeature.begin(),
                                                     aSFLast = aNewCoincidencesToSplitFeature.end();
       for (; aSFIt != aSFLast; aSFIt++) {
         AttributePoint2DPtr aSFAttribute = *aSFIt;
         if (aCoincPnt->isEqual(aSFAttribute->pnt())) {
-          std::string aSecondAttribute = SketchPlugin_Constraint::ENTITY_A();
-          if (anAttributeId == SketchPlugin_Constraint::ENTITY_A())
-            aSecondAttribute = SketchPlugin_Constraint::ENTITY_B();
-          createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
+          SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
+                           SketchPlugin_ConstraintCoincidence::ID(),
                            aSFAttribute, aCoincFeature->refattr(aSecondAttribute)->attr());
         }
       }
@@ -1078,7 +1083,8 @@ FeaturePtr SketchPlugin_Split::splitLine(FeaturePtr& theSplitFeature,
                                              aFeature->attribute(SketchPlugin_Line::END_ID())));
       anNewFeature = aFeature;
     }
-    aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
+    aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
+                     SketchPlugin_ConstraintCoincidence::ID(),
                      theSplitFeature->attribute(SketchPlugin_Line::END_ID()),
                      aFeature->attribute(SketchPlugin_Line::START_ID()));
     theCreatedFeatures.insert(aConstraintFeature);
@@ -1107,7 +1113,8 @@ FeaturePtr SketchPlugin_Split::splitLine(FeaturePtr& theSplitFeature,
     fillAttribute(theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID()),
                                                     aFirstPointAttrOfSplit);
     theBaseFeatureModified->execute(); // to update result
-    aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
+    aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
+                     SketchPlugin_ConstraintCoincidence::ID(),
                      theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID()),
                      theSplitFeature->attribute(SketchPlugin_Line::START_ID()));
     theCreatedFeatures.insert(aConstraintFeature);
@@ -1123,14 +1130,16 @@ FeaturePtr SketchPlugin_Split::splitLine(FeaturePtr& theSplitFeature,
 
 #ifdef CREATE_CONSTRAINTS
   // additional constraints between split and base features
-  aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintParallel::ID(),
-                                                       getFeatureResult(aBaseFeature),
-                                                       getFeatureResult(theSplitFeature));
+  aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
+          SketchPlugin_ConstraintParallel::ID(),
+          getFeatureResult(aBaseFeature),
+          getFeatureResult(theSplitFeature));
   theCreatedFeatures.insert(aConstraintFeature);
   if (theAfterFeature.get()) {
-    aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintParallel::ID(),
-                                                    getFeatureResult(aBaseFeature),
-                                                    getFeatureResult(theAfterFeature));
+    aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
+          SketchPlugin_ConstraintParallel::ID(),
+          getFeatureResult(aBaseFeature),
+          getFeatureResult(theAfterFeature));
     theCreatedFeatures.insert(aConstraintFeature);
   }
 #endif
@@ -1217,7 +1226,8 @@ FeaturePtr SketchPlugin_Split::splitArc(FeaturePtr& theSplitFeature,
                                                   aFeature->attribute(SketchPlugin_Arc::END_ID())));
       anNewFeature = aFeature;
     }
-    aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
+    aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
+                     SketchPlugin_ConstraintCoincidence::ID(),
                      theSplitFeature->attribute(SketchPlugin_Arc::END_ID()),
                      aFeature->attribute(SketchPlugin_Arc::START_ID()));
     theCreatedFeatures.insert(aConstraintFeature);
@@ -1246,7 +1256,8 @@ FeaturePtr SketchPlugin_Split::splitArc(FeaturePtr& theSplitFeature,
     fillAttribute(theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
                                                     aFirstPointAttrOfSplit);
     theBaseFeatureModified->execute(); // to update result
-    aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
+    aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
+                     SketchPlugin_ConstraintCoincidence::ID(),
                      theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
                      theSplitFeature->attribute(SketchPlugin_Arc::START_ID()));
     theCreatedFeatures.insert(aConstraintFeature);
@@ -1262,22 +1273,26 @@ FeaturePtr SketchPlugin_Split::splitArc(FeaturePtr& theSplitFeature,
 
   // additional constraints between split and base features
 #ifdef CREATE_CONSTRAINTS
-  aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(),
-                                                       getFeatureResult(aBaseFeature),
-                                                       getFeatureResult(theSplitFeature));
+  aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
+          SketchPlugin_ConstraintEqual::ID(),
+          getFeatureResult(aBaseFeature),
+          getFeatureResult(theSplitFeature));
   theCreatedFeatures.insert(aConstraintFeature);
-  aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(),
-                                                       getFeatureResult(theSplitFeature),
-                                                       getFeatureResult(aBaseFeature));
+  aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
+          SketchPlugin_ConstraintTangent::ID(),
+          getFeatureResult(theSplitFeature),
+          getFeatureResult(aBaseFeature));
   theCreatedFeatures.insert(aConstraintFeature);
   if (theAfterFeature.get()) {
-    aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(),
-                                                    getFeatureResult(aBaseFeature),
-                                                    getFeatureResult(theAfterFeature));
+    aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
+                SketchPlugin_ConstraintEqual::ID(),
+                getFeatureResult(aBaseFeature),
+                getFeatureResult(theAfterFeature));
     theCreatedFeatures.insert(aConstraintFeature);
-    aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(),
-                                                    getFeatureResult(theSplitFeature),
-                                                    getFeatureResult(theAfterFeature));
+    aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
+                SketchPlugin_ConstraintTangent::ID(),
+                getFeatureResult(theSplitFeature),
+                getFeatureResult(theAfterFeature));
     theCreatedFeatures.insert(aConstraintFeature);
   }
 #endif
@@ -1341,19 +1356,22 @@ FeaturePtr SketchPlugin_Split::splitCircle(FeaturePtr& theSplitFeature,
                              (theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID())));
 
   // additional constraints between split and base features
-  aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
+  aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
+                     SketchPlugin_ConstraintCoincidence::ID(),
                      theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
                      theSplitFeature->attribute(SketchPlugin_Arc::END_ID()));
   theCreatedFeatures.insert(aConstraintFeature);
-  aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
+  aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
+                     SketchPlugin_ConstraintCoincidence::ID(),
                      theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID()),
                      theSplitFeature->attribute(SketchPlugin_Arc::START_ID()));
   theCreatedFeatures.insert(aConstraintFeature);
 
 #ifdef CREATE_CONSTRAINTS
-  aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(),
-                                                       getFeatureResult(theSplitFeature),
-                                                       getFeatureResult(theBaseFeatureModified));
+  aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
+                     SketchPlugin_ConstraintTangent::ID(),
+                     getFeatureResult(theSplitFeature),
+                     getFeatureResult(theBaseFeatureModified));
   theCreatedFeatures.insert(aConstraintFeature);
 #endif
   return anNewFeature;
@@ -1500,39 +1518,6 @@ FeaturePtr SketchPlugin_Split::createArcFeature(const FeaturePtr& theBaseFeature
   return aFeature;
 }
 
-FeaturePtr SketchPlugin_Split::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);
-
-  return aConstraint;
-}
-
-FeaturePtr SketchPlugin_Split::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;
-}
-
 void SketchPlugin_Split::updateFeaturesAfterSplit(
                                                    const std::set<FeaturePtr>& theFeaturesToUpdate)
 {
@@ -1571,6 +1556,7 @@ std::shared_ptr<ModelAPI_Result> SketchPlugin_Split::getFeatureResult(
   return aResult;
 }
 
+#ifdef _DEBUG
 std::set<std::shared_ptr<ModelAPI_Attribute> > SketchPlugin_Split::getEdgeAttributes(
                                            const std::shared_ptr<ModelAPI_Feature>& theFeature)
 {
@@ -1590,6 +1576,7 @@ std::set<std::shared_ptr<ModelAPI_Attribute> > SketchPlugin_Split::getEdgeAttrib
 
   return anAttributes;
 }
+#endif
 
 std::shared_ptr<GeomDataAPI_Point2D> SketchPlugin_Split::getPointAttribute
                                                               (const bool isFirstAttribute)