Salome HOME
Abort previous operation by Edit of Parameter. Scenario: Create a parameter, start...
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Sketch.cpp
index 84170754a6385fc5d4d7f2ee6998c63795640024..21247d13a37b631f049f8327f5cf77e5452d0456 100644 (file)
@@ -9,16 +9,14 @@
 #include <GeomAlgoAPI_CompoundBuilder.h>
 #include <GeomAlgoAPI_FaceBuilder.h>
 
-#include <GeomAPI_AISObject.h>
 #include <GeomAPI_Dir.h>
 #include <GeomAPI_PlanarEdges.h>
 
-#include <GeomAlgoAPI_FaceBuilder.h>
-
 #include <GeomDataAPI_Point2D.h>
 #include <GeomAlgoAPI_PointBuilder.h>
 
 #include <ModelAPI_AttributeRefList.h>
+#include <ModelAPI_AttributeString.h>
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Document.h>
 #include <ModelAPI_Feature.h>
@@ -56,6 +54,9 @@ void SketchPlugin_Sketch::initAttributes()
   data()->addAttribute(SketchPlugin_SketchEntity::EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
   ModelAPI_Session::get()->validators()->registerNotObligatory(
     getKind(), SketchPlugin_SketchEntity::EXTERNAL_ID());
+  data()->addAttribute(SketchPlugin_Sketch::SOLVER_ERROR(), ModelAPI_AttributeString::typeId());
+  ModelAPI_Session::get()->validators()->registerNotObligatory(
+    getKind(), SketchPlugin_Sketch::SOLVER_ERROR());
 }
 
 void SketchPlugin_Sketch::execute()
@@ -73,7 +74,7 @@ void SketchPlugin_Sketch::execute()
       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
 
   std::list<ObjectPtr> aFeatures = aRefList->list();
-  if (aFeatures.empty())
+  if (aFeatures.empty()) // actually, this must be avoided by the validators
     return;
 
   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
@@ -109,8 +110,12 @@ void SketchPlugin_Sketch::execute()
     }
   }
 
-  if (aFeaturesPreview.empty())
+  if (aFeaturesPreview.empty()) {
+    // no good features for generation of preview => erase result if exists
+    if (firstResult().get() && !firstResult()->isDisabled())
+      removeResults(0, false);
     return;
+  }
 
   // Collect all edges as one big wire
   std::shared_ptr<GeomAPI_PlanarEdges> aBigWire(new GeomAPI_PlanarEdges);
@@ -140,39 +145,45 @@ void SketchPlugin_Sketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFea
 {
   if (!data()->isValid()) // sketch is already removed (case on undo of sketch), sync is not needed
     return;
-  list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
-  list<ObjectPtr>::iterator aSubIt = aSubs.begin(), aLastIt = aSubs.end();
-  bool isRemoved = false;
-  bool aHasEmtpyFeature = false;
-  for(; aSubIt != aLastIt && !isRemoved; aSubIt++) {
-    std::shared_ptr<ModelAPI_Feature> aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aSubIt);
-    if (aFeature.get() != NULL && aFeature == theFeature) {
-      data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(aFeature);
-      isRemoved = true;
-    }
-    else if (aFeature.get() == NULL)
-      aHasEmtpyFeature = true;
-  }
-  // if the object is not found in the sketch sub-elements, that means that the object is removed already.
-  // Find the first empty element and remove it
-  if (!isRemoved && aHasEmtpyFeature)
-    data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(ObjectPtr());
+  // to keep the persistent sub-elements indexing, do not remove elements from list,
+  // but substitute by nulls
+  reflist(SketchPlugin_Sketch::FEATURES_ID())->substitute(theFeature, ObjectPtr());
 }
 
 int SketchPlugin_Sketch::numberOfSubs(bool forTree) const
 {
-  return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->size();
+  if (forTree)
+    return 0;
+  return data()->reflist(FEATURES_ID())->size(false);
 }
 
-std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(const int theIndex, bool forTree) const
+std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(
+  const int theIndex, bool forTree)
 {
-  ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex);
-  return std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
+  if (forTree)
+    return FeaturePtr();
+
+  ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex, false);
+  FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
+  return aRes;
 }
 
 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
 {
-  return subFeature(theIndex)->data()->featureId();
+  std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
+      ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
+  std::list<ObjectPtr> aFeatures = aRefList->list();
+  std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
+  int aResultIndex = 1; // number of the counted (created) features, started from 1
+  int aFeatureIndex = -1; // number of the not-empty features in the list
+  for (; anIt != aFeatures.end(); anIt++) {
+    if (anIt->get()) 
+      aFeatureIndex++;
+    if (aFeatureIndex == theIndex)
+      break;
+    aResultIndex++;
+  }
+  return aResultIndex;
 }
 
 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
@@ -251,7 +262,8 @@ void SketchPlugin_Sketch::attributeChanged(const std::string& theID) {
     std::list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
     std::list<ObjectPtr>::iterator aSub = aSubs.begin();
     for(; aSub != aSubs.end(); aSub++) {
-      ModelAPI_EventCreator::get()->sendUpdated(*aSub, anUpdateEvent);
+      if (aSub->get())
+        ModelAPI_EventCreator::get()->sendUpdated(*aSub, anUpdateEvent);
     }
   }
 }
@@ -307,3 +319,9 @@ std::shared_ptr<GeomAPI_Ax3> SketchPlugin_Sketch::plane(SketchPlugin_Sketch* the
 
   return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(anOrigin->pnt(), aDirX->dir(), aNorm->dir()));
 }
+
+void SketchPlugin_Sketch::exchangeIDs(
+  std::shared_ptr<ModelAPI_Feature> theFeature1, std::shared_ptr<ModelAPI_Feature> theFeature2)
+{
+  reflist(SketchPlugin_Sketch::FEATURES_ID())->exchange(theFeature1, theFeature2);
+}