Salome HOME
updated copyright message
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMulti.cpp
index 0ee95310fd3844a9d9d3cb148acebcc09400e0a7..871a76e2e9ffc604b83a187d28e50fece2c82df6 100644 (file)
@@ -1,3 +1,22 @@
+// Copyright (C) 2014-2023  CEA, EDF
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// 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
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+
 #include <SketchSolver_ConstraintMulti.h>
 #include <SketchSolver_Error.h>
 #include <SketchSolver_Manager.h>
 #include <SketchPlugin_Point.h>
 #include <SketchPlugin_IntersectionPoint.h>
 
+static void createCopiedEntity(const FeaturePtr& theFeature, const StoragePtr& theStorage)
+{
+  EntityWrapperPtr anEntity = theStorage->entity(theFeature);
+  if (anEntity) {
+    std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
+        std::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
+    if (!anEntity->isExternal() && aSketchFeature->isCopy())
+      theStorage->makeExternal(anEntity);
+  }
+}
+
 void SketchSolver_ConstraintMulti::getEntities(std::list<EntityWrapperPtr>& theEntities)
 {
   myAdjusted = false;
-  DataPtr aData = myBaseConstraint->data();
 
   // Lists of objects and number of copies
-  AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
-      aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
+  AttributeRefListPtr anInitialRefList =
+      myBaseConstraint->reflist(SketchPlugin_Constraint::ENTITY_A());
   myNumberOfObjects = anInitialRefList->size();
-  myNumberOfCopies = aData->integer(nameNbObjects())->value() - 1;
+  myNumberOfCopies = myBaseConstraint->integer(nameNbObjects())->value() - 1;
   if (myNumberOfCopies <= 0)
     return;
 
-  AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
-      aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
+  AttributeRefListPtr aRefList =
+      myBaseConstraint->reflist(SketchPlugin_Constraint::ENTITY_B());
   if (!aRefList || aRefList->size() == 0) {
     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
     return;
@@ -35,7 +64,8 @@ void SketchSolver_ConstraintMulti::getEntities(std::list<EntityWrapperPtr>& theE
   FeaturePtr aFeature;
   std::list<ObjectPtr> anObjectList = aRefList->list();
   std::list<ObjectPtr>::iterator anObjIt = anObjectList.begin();
-  if ((myNumberOfCopies + 1) * myNumberOfObjects != aRefList->size()) // execute for the feature is not called yet
+  // execute for the feature is not called yet
+  if ((myNumberOfCopies + 1) * myNumberOfObjects != aRefList->size())
     myNumberOfCopies = aRefList->size() / myNumberOfObjects - 1;
 
   while (anObjIt != anObjectList.end()) {
@@ -43,50 +73,67 @@ void SketchSolver_ConstraintMulti::getEntities(std::list<EntityWrapperPtr>& theE
     if (!aFeature)
       continue;
 
-    if (!myStorage->update(aFeature)) // the entity is not created, so it is a copy in "multi" constraint, force its creation
-      myStorage->update(aFeature, myGroupID, true);
+    // the entity is not created, so it is a copy in "multi" constraint, force its creation
+    if (!myStorage->update(aFeature))
+      myStorage->update(aFeature, true);
     theEntities.push_back(myStorage->entity(aFeature));
-    myFeatures.insert(aFeature);
+    myOriginalFeatures.insert(aFeature);
     for (int i = 0; i < myNumberOfCopies && anObjIt != anObjectList.end(); ++i, ++anObjIt) {
       // just add copied features into the list of objects
       aFeature = ModelAPI_Feature::feature(*anObjIt);
-      if (aFeature)
-        myFeatures.insert(aFeature);
+      if (aFeature) {
+        createCopiedEntity(aFeature, myStorage);
+        myCopiedFeatures.insert(aFeature);
+      }
     }
   }
 }
 
 bool SketchSolver_ConstraintMulti::remove()
 {
-  myFeatures.clear();
+  myStorage->unsubscribeUpdates(this);
+
+  // "Multi" constraint has been removed, thus all copy features become non-copied,
+  // add them once again to be a common feature
+  std::set<FeaturePtr>::iterator anIt = myCopiedFeatures.begin();
+  for (; anIt != myCopiedFeatures.end(); ++anIt) {
+    EntityWrapperPtr anEntity = myStorage->entity(*anIt);
+    if (anEntity) {
+      std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
+          std::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
+      if (anEntity->isExternal() && !aSketchFeature->isExternal())
+        myStorage->makeNonExternal(anEntity);
+    } else if ((*anIt)->data() && (*anIt)->data()->isValid())
+      myStorage->update(*anIt, true);
+  }
+
+  myOriginalFeatures.clear();
+  myCopiedFeatures.clear();
   return SketchSolver_Constraint::remove();
 }
 
 void SketchSolver_ConstraintMulti::update()
-{
-  update(false);
-}
-
-void SketchSolver_ConstraintMulti::update(bool isForce)
 {
   cleanErrorMsg();
-  AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
-      myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
+  AttributeRefListPtr anInitialRefList =
+      myBaseConstraint->reflist(SketchPlugin_Constraint::ENTITY_A());
   AttributeIntegerPtr aNbObjects = myBaseConstraint->integer(nameNbObjects());
   if (!anInitialRefList || !aNbObjects)
     return; // the "Multi" constraint is in queue to remove
-  bool isUpdated= anInitialRefList->size() != myNumberOfObjects || aNbObjects->value()-1 != myNumberOfCopies;
+  bool isUpdated =
+    anInitialRefList->size() != myNumberOfObjects || aNbObjects->value()-1 != myNumberOfCopies;
   if (!isUpdated) {
     // additional check that the features and their copies are changed
-    AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
-        myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
+    AttributeRefListPtr aRefList =
+        myBaseConstraint->reflist(SketchPlugin_Constraint::ENTITY_B());
     if (aRefList && aRefList->size() != 0) {
       FeaturePtr aFeature;
       std::list<ObjectPtr> anObjectList = aRefList->list();
       std::list<ObjectPtr>::iterator anObjIt = anObjectList.begin();
       for (; anObjIt != anObjectList.end(); ++anObjIt) {
         aFeature = ModelAPI_Feature::feature(*anObjIt);
-        if (aFeature && myFeatures.find(aFeature) == myFeatures.end()) {
+        if (aFeature && myOriginalFeatures.find(aFeature) == myOriginalFeatures.end() &&
+            myCopiedFeatures.find(aFeature) == myCopiedFeatures.end()) {
           isUpdated = true;
           break;
         }
@@ -97,26 +144,25 @@ void SketchSolver_ConstraintMulti::update(bool isForce)
   if (isUpdated) {
     remove();
     process();
-    return;
   }
 
-  // update derivative object
+  // update derived object
   updateLocal();
-  if (isForce)
-    myAdjusted = false;
   adjustConstraint();
 }
 
 void SketchSolver_ConstraintMulti::adjustConstraint()
 {
-  AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
-      myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
+  AttributeRefListPtr aRefList =
+      myBaseConstraint->reflist(SketchPlugin_Constraint::ENTITY_B());
   if (!aRefList || aRefList->size() == 0) {
     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
     return;
   }
 
   FeaturePtr anOriginal, aFeature;
+  std::list<double>::iterator aXIt, aYIt;
+
   std::list<ObjectPtr> anObjectList = aRefList->list();
   std::list<ObjectPtr>::iterator anObjIt = anObjectList.begin();
   while (anObjIt != anObjectList.end()) {
@@ -126,25 +172,19 @@ void SketchSolver_ConstraintMulti::adjustConstraint()
 
     // Fill lists of coordinates of points composing a feature
     std::list<double> aX, aY;
-    std::list<double>::iterator aXIt, aYIt;
     double aXCoord, aYCoord;
-    EntityWrapperPtr anEntity = myStorage->entity(anOriginal);
-    std::list<EntityWrapperPtr> aSubs = anEntity->subEntities();
-    std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
-    for (; aSIt != aSubs.end(); ++aSIt) {
-      if ((*aSIt)->type() != ENTITY_POINT)
-        continue;
-      AttributePoint2DPtr aPoint =
-          std::dynamic_pointer_cast<GeomDataAPI_Point2D>((*aSIt)->baseAttribute());
-      if (aPoint) {
-        aXCoord = aPoint->x();
-        aYCoord = aPoint->y();
-      } else {
-        std::list<ParameterWrapperPtr> aParameters = (*aSIt)->parameters();
-        aXCoord = aParameters.front()->value();
-        aYCoord = aParameters.back()->value();
-      }
-      getRelative(aXCoord, aYCoord, aXCoord, aYCoord);
+    std::list<AttributePtr> aPoints =
+        anOriginal->data()->attributes(GeomDataAPI_Point2D::typeId());
+    std::list<AttributePtr>::iterator aPtIt = aPoints.begin();
+    for (; aPtIt != aPoints.end(); ++aPtIt) {
+      AttributePoint2DPtr aPoint2D = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aPtIt);
+      if (aPoint2D->isInitialized()) {
+        aXCoord = aPoint2D->x();
+        aYCoord = aPoint2D->y();
+        getRelative(aXCoord, aYCoord, aXCoord, aYCoord);
+      } else
+        aXCoord = aYCoord = 0;
+
       aX.push_back(aXCoord);
       aY.push_back(aYCoord);
     }
@@ -154,77 +194,69 @@ void SketchSolver_ConstraintMulti::adjustConstraint()
       aFeature = ModelAPI_Feature::feature(*anObjIt);
       if (!aFeature)
         continue;
-      anEntity = myStorage->entity(aFeature);
 
-      if (!anEntity || !myStorage->isEventsBlocked())
+      if (myIsEventsBlocked)
         aFeature->data()->blockSendAttributeUpdated(true);
 
-      std::list<AttributePtr> aPoints;
-      if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
-        aPoints.push_back(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
-        aPoints.push_back(aFeature->attribute(SketchPlugin_Arc::START_ID()));
-        aPoints.push_back(aFeature->attribute(SketchPlugin_Arc::END_ID()));
-      } else if (aFeature->getKind() == SketchPlugin_Line::ID()) {
-        aPoints.push_back(aFeature->attribute(SketchPlugin_Line::START_ID()));
-        aPoints.push_back(aFeature->attribute(SketchPlugin_Line::END_ID()));
-      } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
-        aPoints.push_back(aFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
-        // update circle's radius
+      if (aFeature->getKind() == SketchPlugin_Circle::ID()) // update circle's radius
         aFeature->real(SketchPlugin_Circle::RADIUS_ID())->setValue(
             anOriginal->real(SketchPlugin_Circle::RADIUS_ID())->value());
-      } else if (aFeature->getKind() == SketchPlugin_Point::ID() ||
-               aFeature->getKind() == SketchPlugin_IntersectionPoint::ID())
-        aPoints.push_back(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
 
-      std::list<AttributePtr>::iterator aPtIt = aPoints.begin();
-      for (aXIt = aX.begin(), aYIt = aY.begin(); aPtIt != aPoints.end(); ++aXIt, ++aYIt, ++aPtIt) {
+      aPoints = aFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
+      for (aPtIt = aPoints.begin(), aXIt = aX.begin(), aYIt = aY.begin();
+           aPtIt != aPoints.end(); ++aXIt, ++aYIt, ++aPtIt) {
+        if (!(*aPtIt)->isInitialized())
+          continue;
         transformRelative(*aXIt, *aYIt);
         getAbsolute(*aXIt, *aYIt, aXCoord, aYCoord);
 
-        std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
+        AttributePoint2DPtr aPoint2D =
             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aPtIt);
         aPoint2D->setValue(aXCoord, aYCoord);
       }
 
-      // update feature in the storage if it is used by another constraints
-      if (anEntity)
+      // update transformed entity if it exists in the storage
+      if (myStorage->entity(aFeature))
         myStorage->update(aFeature);
-      else { // update attributes, if they exist in the storage
-        for (aPtIt = aPoints.begin(); aPtIt != aPoints.end(); ++aPtIt) {
-          EntityWrapperPtr aPntEnt = myStorage->entity(*aPtIt);
-          if (aPntEnt)
-            myStorage->update(*aPtIt);
-        }
-      }
-
-      if (!anEntity || !myStorage->isEventsBlocked())
-        aFeature->data()->blockSendAttributeUpdated(false);
     }
   }
 
   myAdjusted = true;
 }
 
-bool SketchSolver_ConstraintMulti::isUsed(FeaturePtr theFeature) const
+void SketchSolver_ConstraintMulti::notify(const FeaturePtr& theFeature,
+                                          PlaneGCSSolver_Update*)
 {
-  return theFeature && (myFeatures.find(theFeature) != myFeatures.end() ||
-         SketchSolver_Constraint::isUsed(theFeature));
+  if (myOriginalFeatures.find(theFeature) == myOriginalFeatures.end() &&
+      myCopiedFeatures.find(theFeature) == myCopiedFeatures.end())
+    return; // the feature is not used by constraint => nothing to update
+
+  if (myIsProcessingNotify)
+    return; // "notify" is already processing
+
+  // do not adjust "multi"-constraint if the number of objects is changed,
+  // wait until the constraint is updated (issue #2425: changing number of copies by parameter)
+  if (myNumberOfCopies + 1 == myBaseConstraint->integer(nameNbObjects())->value()) {
+    myIsProcessingNotify = true;
+
+    // update derivative object
+    updateLocal();
+    myAdjusted = false;
+    adjustConstraint();
+
+    myIsProcessingNotify = false;
+  }
 }
 
-bool SketchSolver_ConstraintMulti::isUsed(AttributePtr theAttribute) const
+void SketchSolver_ConstraintMulti::blockEvents(bool isBlocked)
 {
-  AttributePtr anAttribute = theAttribute;
-  AttributeRefAttrPtr aRefAttr =
-      std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
-  if (aRefAttr) {
-    if (aRefAttr->isObject())
-      return isUsed(ModelAPI_Feature::feature(aRefAttr->object()));
-    else
-      anAttribute = aRefAttr->attr();
-  }
-  if (!anAttribute)
-    return false;
+  myIsEventsBlocked = isBlocked;
+
+  std::set<FeaturePtr>::iterator anIt = myOriginalFeatures.begin();
+  for (; anIt != myOriginalFeatures.end(); ++anIt)
+    (*anIt)->data()->blockSendAttributeUpdated(isBlocked);
+  for (anIt = myCopiedFeatures.begin(); anIt != myCopiedFeatures.end(); ++anIt)
+    (*anIt)->data()->blockSendAttributeUpdated(isBlocked);
 
-  FeaturePtr anOwner = ModelAPI_Feature::feature(anAttribute->owner());
-  return myFeatures.find(anOwner) != myFeatures.end();
+  SketchSolver_Constraint::blockEvents(isBlocked);
 }