Salome HOME
Merge branch 'Dev_1.4.0' of newgeom:newgeom into Dev_1.4.0
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Sketch.cpp
index 2190a6e4b550bcda625e40fdcf071cd3251024cf..7b993c2161e0ad1d3daa39ea59f25188c5490430 100644 (file)
@@ -31,6 +31,7 @@
 #include <SketchPlugin_Sketch.h>
 #include <SketchPlugin_Feature.h>
 #include <SketchPlugin_SketchEntity.h>
+#include <SketchPlugin_Tools.h>
 
 #include <Events_Loop.h>
 
@@ -125,51 +126,71 @@ void SketchPlugin_Sketch::execute()
 
 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
 {
-  std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID);
+  std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
   if (aNew) {
     std::dynamic_pointer_cast<SketchPlugin_Feature>(aNew)->setSketch(this);
     data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
   }
+   // set as current also after it becomes sub to set correctly enabled for other sketch subs
+  document()->setCurrentFeature(aNew, false);
   return aNew;
 }
 
 void SketchPlugin_Sketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
 {
-  if (!data().get()) // sketch is already removed (case on undo of sketch), sync is not needed
+  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;
+  // 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());
+
+  std::map<int, std::shared_ptr<ModelAPI_Feature> >::iterator aSubIter = mySubs.begin();
+  for(; aSubIter != mySubs.end(); aSubIter++) {
+    if (aSubIter->second == theFeature) {
+      mySubs.erase(aSubIter);
+      break;
     }
-    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());
 }
 
-int SketchPlugin_Sketch::numberOfSubs() const
+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) 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();
+
+  if (mySubs.find(theIndex) != mySubs.end())
+    return mySubs[theIndex];
+
+  ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex, false);
+  FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
+  mySubs[theIndex] = aRes;
+  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
@@ -182,11 +203,7 @@ bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
       aFeature = document()->feature(aRes);
   }
   if (aFeature) {
-    list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
-    for(list<ObjectPtr>::iterator aSubIt = aSubs.begin(); aSubIt != aSubs.end(); aSubIt++) {
-      if (*aSubIt == aFeature)
-        return true;
-    }
+    return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->isInList(aFeature);
   }
   return false;
 }
@@ -226,8 +243,8 @@ void SketchPlugin_Sketch::attributeChanged(const std::string& theID) {
         aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
         std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
         // X axis is preferable to be dirX on the sketch
-        static const double tol = 1.e-7;
-        bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
+        static const double tol = 0.1; // here can not be very small value to avoid very close to X normal axis (issue 595)
+        bool isX = fabs(anA) - 1.0 < tol && fabs(aB) < tol && fabs(aC) < tol;
         std::shared_ptr<GeomAPI_Dir> aTempDir(
           isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
         std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
@@ -252,7 +269,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);
     }
   }
 }
@@ -278,3 +296,33 @@ void SketchPlugin_Sketch::createPoint2DResult(ModelAPI_Feature* theFeature,
 
   theFeature->setResult(aResult, theIndex);
 }
+
+FeaturePtr SketchPlugin_Sketch::addUniqueNamedCopiedFeature(FeaturePtr theFeature,
+                                                            SketchPlugin_Sketch* theSketch)
+{
+  FeaturePtr aNewFeature = theSketch->addFeature(theFeature->getKind());
+  // addFeature generates a unique name for the feature, it caches the name
+  std::string aUniqueFeatureName = aNewFeature->data()->name();
+  // all attribute values are copied\pasted to the new feature, name is not an exception
+  theFeature->data()->copyTo(aNewFeature->data());
+  // as a name for the feature, the generated unique name is set
+  aNewFeature->data()->setName(aUniqueFeatureName);
+  // text expressions could block setValue of some attributes
+  clearExpressions(aNewFeature);
+
+  return aNewFeature;
+}
+
+std::shared_ptr<GeomAPI_Ax3> SketchPlugin_Sketch::plane(SketchPlugin_Sketch* theSketch)
+{
+  std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
+
+  std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
+      aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
+  std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
+      aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
+  std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
+      aData->attribute(SketchPlugin_Sketch::NORM_ID()));
+
+  return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(anOrigin->pnt(), aDirX->dir(), aNorm->dir()));
+}