Salome HOME
Remove extra files
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMulti.cpp
index e68a881be5b246c69e731d8dba9914e1746280d2..bd0a82ad3206962483ae6a6bffa4b5d5fe5b5fa8 100644 (file)
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
 #include <SketchSolver_ConstraintMulti.h>
-#include <SketchSolver_Group.h>
 #include <SketchSolver_Error.h>
+#include <SketchSolver_Manager.h>
 
-#include <SketchPlugin_Arc.h>
-
-#include <ModelAPI_AttributeDouble.h>
+#include <GeomDataAPI_Point2D.h>
 #include <ModelAPI_AttributeInteger.h>
 #include <ModelAPI_AttributeRefAttr.h>
 #include <ModelAPI_AttributeRefList.h>
-#include <ModelAPI_ResultConstruction.h>
-
-#include <GeomAPI_Dir2d.h>
-#include <GeomAPI_XY.h>
-
-#include <math.h>
+#include <SketchPlugin_Arc.h>
+#include <SketchPlugin_Circle.h>
+#include <SketchPlugin_Line.h>
+#include <SketchPlugin_Point.h>
+#include <SketchPlugin_IntersectionPoint.h>
 
-void SketchSolver_ConstraintMulti::processEntities(const std::vector< std::vector<Slvs_hEntity> >& theEntAndCopies)
+void SketchSolver_ConstraintMulti::getEntities(std::list<EntityWrapperPtr>& theEntities)
 {
-  // Keep all objects unchanged (only initial object may be changed by user)
-  myCircsAndCopies.clear();
-  std::vector<std::vector<Slvs_hEntity> >::const_iterator anEntIt = theEntAndCopies.begin();
-  std::vector<Slvs_hEntity>::const_iterator aCpIt;
-  for (; anEntIt != theEntAndCopies.end(); ++anEntIt) {
-    std::vector<Slvs_hEntity> aCircs;
-    aCpIt = anEntIt->begin();
-    // Obtain initial points
-    Slvs_Entity anInitial = myStorage->getEntity(*aCpIt);
-    if (anInitial.type == SLVS_E_POINT_IN_2D || anInitial.type == SLVS_E_POINT_IN_3D)
-      myInitialPoints.insert(anInitial.h);
-    else {
-      for (int i = 0; i < 4 && anInitial.point[i] != SLVS_E_UNKNOWN; i++)
-        myInitialPoints.insert(anInitial.point[i]);
-    }
+  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()));
+  myNumberOfObjects = anInitialRefList->size();
+  myNumberOfCopies = aData->integer(nameNbObjects())->value() - 1;
+  if (myNumberOfCopies <= 0)
+    return;
 
-    // Fix the copies
-    for (++aCpIt; aCpIt != anEntIt->end(); ++aCpIt) {
-      const Slvs_Entity& anEntity = myStorage->getEntity(*aCpIt);
-      std::vector<Slvs_hConstraint> aNewConstr;
-      if (anEntity.type == SLVS_E_CIRCLE) {
-        aCircs.push_back(anEntity.distance);
-        // for circles we fix only center
-        aNewConstr = myStorage->fixEntity(anEntity.point[0]);
-      } else
-        aNewConstr = myStorage->fixEntity(*aCpIt);
-      if (anEntity.type == SLVS_E_ARC_OF_CIRCLE)
-        aCircs.push_back(anEntity.h);
-      mySlvsConstraints.insert(mySlvsConstraints.end(), aNewConstr.begin(), aNewConstr.end());
-    }
+  AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
+      aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
+  if (!aRefList || aRefList->size() == 0) {
+    myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
+    return;
+  }
 
-    if (!aCircs.empty()) {
-      if (anInitial.type == SLVS_E_CIRCLE)
-        aCircs.insert(aCircs.begin(), anInitial.distance);
-      else
-        aCircs.insert(aCircs.begin(), anInitial.h);
-      myCircsAndCopies.push_back(aCircs);
+  FeaturePtr aFeature;
+  std::list<ObjectPtr> anObjectList = aRefList->list();
+  std::list<ObjectPtr>::iterator anObjIt = anObjectList.begin();
+  // execute for the feature is not called yet
+  if ((myNumberOfCopies + 1) * myNumberOfObjects != aRefList->size()) 
+    myNumberOfCopies = aRefList->size() / myNumberOfObjects - 1;
+
+  while (anObjIt != anObjectList.end()) {
+    aFeature = ModelAPI_Feature::feature(*anObjIt++);
+    if (!aFeature)
+      continue;
+
+    // the entity is not created, so it is a copy in "multi" constraint, force its creation
+    if (!myStorage->update(aFeature)) 
+      myStorage->update(aFeature, myGroupID, true);
+    theEntities.push_back(myStorage->entity(aFeature));
+    myFeatures.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);
     }
   }
 }
 
-void SketchSolver_ConstraintMulti::update(ConstraintPtr theConstraint)
+bool SketchSolver_ConstraintMulti::remove()
 {
-  cleanErrorMsg();
-  if (!theConstraint || theConstraint == myBaseConstraint) {
-    AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
-        myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
-    AttributeIntegerPtr aNbCopies = myBaseConstraint->integer(nameNbCopies());
-    if (anInitialRefList->size() != myNumberOfObjects || aNbCopies->value() != myNumberOfCopies) {
-      remove(myBaseConstraint);
-      process();
-      return;
-    }
-  }
-
-  updateLocal();
-  SketchSolver_Constraint::update();
+  myFeatures.clear();
+  return SketchSolver_Constraint::remove();
 }
 
-bool SketchSolver_ConstraintMulti::remove(ConstraintPtr theConstraint)
+void SketchSolver_ConstraintMulti::update()
 {
-  cleanErrorMsg();
-  if (theConstraint && theConstraint != myBaseConstraint)
-    return false;
-  bool isFullyRemoved = true;
-  std::vector<Slvs_hEntity>::iterator aCIter = mySlvsConstraints.begin();
-  for (; aCIter != mySlvsConstraints.end(); aCIter++)
-   isFullyRemoved = myStorage->removeConstraint(*aCIter) && isFullyRemoved;
-  mySlvsConstraints.clear();
-
-  std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIt = myFeatureMap.begin();
-  for (; aFeatIt != myFeatureMap.end(); aFeatIt++)
-    myStorage->removeEntity(aFeatIt->second);
-  myStorage->removeUnusedEntities();
-
-  std::map<FeaturePtr, Slvs_hEntity> aFeatureMapCopy = myFeatureMap;
-
-  if (isFullyRemoved) {
-    myFeatureMap.clear();
-    myAttributeMap.clear();
-    myValueMap.clear();
-  } else
-    cleanRemovedEntities();
-
-  // Restore initial features
-  std::map<FeaturePtr, Slvs_hEntity>::iterator aFIter = aFeatureMapCopy.begin();
-  for (; aFIter != aFeatureMapCopy.end(); ++aFIter)
-  {
-    if (myFeatureMap.find(aFIter->first) != myFeatureMap.end())
-      continue; // the feature was not removed
-    Slvs_hEntity anEntity = myGroup->getFeatureId(aFIter->first);
-    if (anEntity != SLVS_E_UNKNOWN)
-      myFeatureMap[aFIter->first] = anEntity;
-  }
-
-  // Clear list of rotated points
-  myPointsAndCopies.clear();
-  myInitialPoints.clear();
-
-  return true;
+  update(false);
 }
 
-void SketchSolver_ConstraintMulti::addFeature(FeaturePtr theFeature)
+void SketchSolver_ConstraintMulti::update(bool isForce)
 {
-  SketchSolver_Constraint::addFeature(theFeature);
-
-  std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIt = myFeatureMap.find(theFeature);
-  if (aFeatIt == myFeatureMap.end())
+  cleanErrorMsg();
+  AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
+      myBaseConstraint->attribute(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;
+  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()));
+    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()) {
+          isUpdated = true;
+          break;
+        }
+      }
+    } else
+      isUpdated = true;
+  }
+  if (isUpdated) {
+    remove();
+    process();
     return;
+  }
 
-  // store list of points of the feature
-  const Slvs_Entity& theEntity = myStorage->getEntity(aFeatIt->second);
-  for (int i = 0; i < 4; i++)
-    if (theEntity.point[i] != SLVS_E_UNKNOWN)
-      myPointsJustUpdated.insert(theEntity.point[i]);
+  // update derivative object
+  updateLocal();
+  if (isForce)
+    myAdjusted = false;
+  adjustConstraint();
 }
 
 void SketchSolver_ConstraintMulti::adjustConstraint()
 {
-  if (myAdjusted)
-    return; // constraint already adjusted, don't do it once again
-
-  double aRelCoord[2]  = {0.0, 0.0}; // relative coordinates of point
-  double anAbsCoord[2] = {0.0, 0.0}; // absolute coordinates of point
-
-  std::list<Slvs_Constraint> aCoincident = myStorage->getConstraintsByType(SLVS_C_POINTS_COINCIDENT);
-  std::list<Slvs_Constraint>::const_iterator aCoIt;
-
-  // Update positions of all points to satisfy angles
-  std::vector< std::vector<Slvs_hEntity> >::const_iterator aPointsIter = myPointsAndCopies.begin();
-  std::vector<Slvs_hEntity>::const_iterator aCopyIter;
-  for (; aPointsIter != myPointsAndCopies.end(); ++aPointsIter) {
-    aCopyIter = aPointsIter->begin();
-    const Slvs_Entity& anInitial = myStorage->getEntity(*aCopyIter);
-    for (int i = 0; i < 2; i++)
-      anAbsCoord[i] = myStorage->getParameter(anInitial.param[i]).val;
-    getRelative(anAbsCoord[0], anAbsCoord[1], aRelCoord[0], aRelCoord[1]);
-
-    // if the point is coincident with another one which is temporary fixed (moved by user),
-    // we will update its position correspondingly
-    Slvs_hConstraint aFixed;
-    for (aCoIt = aCoincident.begin(); aCoIt != aCoincident.end(); ++aCoIt) {
-      if ((aCoIt->ptA == anInitial.h && myInitialPoints.find(aCoIt->ptB) != myInitialPoints.end()) ||
-          (aCoIt->ptB == anInitial.h && myInitialPoints.find(aCoIt->ptA) != myInitialPoints.end())) {
-        Slvs_hEntity anOtherId = aCoIt->ptA == anInitial.h ? aCoIt->ptB : aCoIt->ptA;
-        if (!myStorage->isTemporary(aFixed) &&
-            myPointsJustUpdated.find(anOtherId) == myPointsJustUpdated.end())
-          continue; // nothing to change
-
-        const Slvs_Entity& anOtherPnt = myStorage->getEntity(anOtherId);
-        for (int i = 0; i < 2; i++) {
-          Slvs_Param anInitParam = myStorage->getParameter(anInitial.param[i]);
-          const Slvs_Param& anOtherParam = myStorage->getParameter(anOtherPnt.param[i]);
-          anInitParam.val = anOtherParam.val;
-          myStorage->updateParameter(anInitParam);
-          anAbsCoord[i] = anOtherParam.val;
-        }
-        getRelative(anAbsCoord[0], anAbsCoord[1], aRelCoord[0], aRelCoord[1]);
-      }
-    }
+  AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
+      myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
+  if (!aRefList || aRefList->size() == 0) {
+    myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
+    return;
+  }
 
-    // update copied points
-    aCopyIter = aPointsIter->begin();
-    for (++aCopyIter; aCopyIter != aPointsIter->end(); ++aCopyIter) {
-      // transform coordinates
-      transformRelative(aRelCoord[0], aRelCoord[1]);
-      getAbsolute(aRelCoord[0], aRelCoord[1], anAbsCoord[0], anAbsCoord[1]);
-
-      const Slvs_Entity& aTarget = myStorage->getEntity(*aCopyIter);
-      for (int i = 0; i < 2; i++) {
-        Slvs_Param aParam = myStorage->getParameter(aTarget.param[i]);
-        aParam.val = anAbsCoord[i];
-        myStorage->updateParameter(aParam);
+  FeaturePtr anOriginal, aFeature;
+  std::list<ObjectPtr> anObjectList = aRefList->list();
+  std::list<ObjectPtr>::iterator anObjIt = anObjectList.begin();
+  while (anObjIt != anObjectList.end()) {
+    anOriginal = ModelAPI_Feature::feature(*anObjIt++);
+    if (!anOriginal)
+      continue;
+
+    // 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);
+      aX.push_back(aXCoord);
+      aY.push_back(aYCoord);
     }
-  }
 
-  std::list<Slvs_Constraint> aDiamConstr;
-  for (aPointsIter = myCircsAndCopies.begin(); aPointsIter != myCircsAndCopies.end(); ++aPointsIter) {
-    aCopyIter = aPointsIter->begin();
-    const Slvs_Entity& anInitial = myStorage->getEntity(*aCopyIter);
-    if (anInitial.type == SLVS_E_DISTANCE) {
-      const Slvs_Param& anInitRad = myStorage->getParameter(anInitial.param[0]);
-      for (++aCopyIter; aCopyIter != aPointsIter->end(); ++aCopyIter) {
-        const Slvs_Entity& aCopy = myStorage->getEntity(*aCopyIter);
-        Slvs_Param aCopyRad = myStorage->getParameter(aCopy.param[0]);
-        aCopyRad.val = anInitRad.val;
-        myStorage->updateParameter(aCopyRad);
-      }
-    } else if (anInitial.type == SLVS_E_ARC_OF_CIRCLE) {
-      const Slvs_Entity& aCenterEnt = myStorage->getEntity(anInitial.point[0]);
-      const Slvs_Entity& aStartEnt = myStorage->getEntity(anInitial.point[1]);
-
-      if (aDiamConstr.empty())
-        aDiamConstr = myStorage->getConstraintsByType(SLVS_C_DIAMETER);
-      // Calculate diameter of initial arc
-      double aDiam = 0.0;
-      for (int i = 0; i < 2; i++) {
-        double d = myStorage->getParameter(aStartEnt.param[i]).val -
-                   myStorage->getParameter(aCenterEnt.param[i]).val;
-        aDiam += d * d;
+    // Calculate positions of copied features
+    for (int i = 0; i < myNumberOfCopies && anObjIt != anObjectList.end(); ++i, ++anObjIt) {
+      aFeature = ModelAPI_Feature::feature(*anObjIt);
+      if (!aFeature)
+        continue;
+      anEntity = myStorage->entity(aFeature);
+
+      if (!anEntity || !myStorage->isEventsBlocked())
+        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
+        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) {
+        transformRelative(*aXIt, *aYIt);
+        getAbsolute(*aXIt, *aYIt, aXCoord, aYCoord);
+
+        std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
+            std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aPtIt);
+        aPoint2D->setValue(aXCoord, aYCoord);
       }
-      aDiam = sqrt(aDiam) * 2.0;
-      // Update the Diameter constraints of copied arcs
-      for (++aCopyIter; aCopyIter != aPointsIter->end(); ++aCopyIter) {
-        std::list<Slvs_Constraint>::iterator aDCIt = aDiamConstr.begin();
-        for (; aDCIt != aDiamConstr.end(); ++aDCIt)
-          if (aDCIt->entityA == *aCopyIter) {
-            aDCIt->valA = aDiam;
-            myStorage->updateConstraint(*aDCIt);
-            aDiamConstr.erase(aDCIt);
-            break;
-          }
+
+      // update feature in the storage if it is used by another constraints
+      if (anEntity)
+        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);
     }
   }
 
-  myPointsJustUpdated.clear();
   myAdjusted = true;
 }
+
+bool SketchSolver_ConstraintMulti::isUsed(FeaturePtr theFeature) const
+{
+  return theFeature && (myFeatures.find(theFeature) != myFeatures.end() ||
+         SketchSolver_Constraint::isUsed(theFeature));
+}
+
+bool SketchSolver_ConstraintMulti::isUsed(AttributePtr theAttribute) const
+{
+  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;
+
+  FeaturePtr anOwner = ModelAPI_Feature::feature(anAttribute->owner());
+  return myFeatures.find(anOwner) != myFeatures.end();
+}