Salome HOME
Improve updating "Multi" constraints
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMultiRotation.cpp
index 5fc487e120e199815f7fae6f6b9486fbbaa03603..78bf3312e9d5df3cd74cda047b07a0d4eb65af8e 100644 (file)
@@ -1,23 +1,30 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
 #include <SketchSolver_ConstraintMultiRotation.h>
 #include <SketchSolver_Error.h>
 #include <SketchSolver_Manager.h>
 
+#include <PlaneGCSSolver_AttributeBuilder.h>
+#include <PlaneGCSSolver_PointWrapper.h>
+#include <PlaneGCSSolver_ScalarWrapper.h>
+#include <PlaneGCSSolver_UpdateFeature.h>
+
 #include <SketchPlugin_MultiRotation.h>
 
 #include <ModelAPI_AttributeString.h>
 
-#include <math.h>
+#include <cmath>
 
 void SketchSolver_ConstraintMultiRotation::getAttributes(
-    EntityWrapperPtr& theCenter, double& theAngle,
-    bool& theFullValue,
-    std::list< std::list<EntityWrapperPtr> >& theEntities)
+    EntityWrapperPtr& theCenter, EntityWrapperPtr& theAngle,
+    bool& theFullValue, std::list<EntityWrapperPtr>& theEntities)
 {
-  DataPtr aData = myBaseConstraint->data();
-  theAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
-      aData->attribute(SketchPlugin_MultiRotation::ANGLE_ID()))->value();
+  AttributePtr anAngleAttr = myBaseConstraint->attribute(SketchPlugin_MultiRotation::ANGLE_ID());
+  PlaneGCSSolver_AttributeBuilder aValueBuilder;
+  theAngle = aValueBuilder.createAttribute(anAngleAttr);
+  myStorage->addEntity(anAngleAttr, theAngle);
 
-  AttributePtr aCenterAttr = aData->attribute(SketchPlugin_MultiRotation::CENTER_ID());
+  AttributeRefAttrPtr aCenterAttr = myBaseConstraint->refattr(SketchPlugin_MultiRotation::CENTER_ID());
   if (!aCenterAttr || !aCenterAttr->isInitialized()) {
     myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
     return;
@@ -25,50 +32,48 @@ void SketchSolver_ConstraintMultiRotation::getAttributes(
 
   myType = CONSTRAINT_MULTI_ROTATION;
 
-  myStorage->update(aCenterAttr, GID_OUTOFGROUP);
+  myStorage->update(aCenterAttr);
   theCenter = myStorage->entity(aCenterAttr);
 
-  AttributeStringPtr aMethodTypeAttr = aData->string(SketchPlugin_MultiRotation::ANGLE_TYPE());
+  AttributeStringPtr aMethodTypeAttr =
+      myBaseConstraint->string(SketchPlugin_MultiRotation::ANGLE_TYPE());
   theFullValue = aMethodTypeAttr->value() != "SingleAngle";
 
-  getEntitiesAndCopies(theEntities);
+  getEntities(theEntities);
+
+  // add owner of central point of Multi-Rotation to the list of monitored features
+  FeaturePtr anOwner = ModelAPI_Feature::feature(aCenterAttr->attr()->owner());
+  if (anOwner)
+    myFeatures.insert(anOwner);
 }
 
 void SketchSolver_ConstraintMultiRotation::process()
 {
   cleanErrorMsg();
-  if (!myBaseConstraint || !myStorage || myGroupID == GID_UNKNOWN) {
-    /// TODO: Put error message here
+  if (!myBaseConstraint || !myStorage) {
+    // Not enough parameters are assigned
     return;
   }
 
+  EntityWrapperPtr anAngle;
   EntityWrapperPtr aRotationCenter;
   bool isFullValue;
-  std::list<std::list<EntityWrapperPtr> > anEntitiesAndCopies;
-  getAttributes(aRotationCenter, myAngle, isFullValue, anEntitiesAndCopies);
+  std::list<EntityWrapperPtr> aBaseEntities;
+  getAttributes(aRotationCenter, anAngle, isFullValue, aBaseEntities);
   if (!myErrorMsg.empty())
     return;
 
-  BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
-  std::list<ConstraintWrapperPtr> aRotConstraints;
-
-  std::list< std::list<EntityWrapperPtr> >::iterator anEntIt = anEntitiesAndCopies.begin();
-  for (; anEntIt != anEntitiesAndCopies.end(); ++anEntIt) {
-    std::list<ConstraintWrapperPtr> aNewConstraints =
-        aBuilder->createConstraint(myBaseConstraint, myGroupID, mySketchID, myType,
-        myAngle, isFullValue, aRotationCenter, EntityWrapperPtr(), *anEntIt);
-    aRotConstraints.insert(aRotConstraints.end(), aNewConstraints.begin(), aNewConstraints.end());
-  }
-  myStorage->addConstraint(myBaseConstraint, aRotConstraints);
-
+  ScalarWrapperPtr anAngleVal = std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(anAngle);
+  myAngle = anAngleVal->value();
   myAdjusted = false;
   adjustConstraint();
+
+  myStorage->subscribeUpdates(this, PlaneGCSSolver_UpdateFeature::GROUP());
 }
 
 void SketchSolver_ConstraintMultiRotation::updateLocal()
 {
-  double aValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
-      myBaseConstraint->attribute(SketchPlugin_MultiRotation::ANGLE_ID()))->value();
+  double aValue = myBaseConstraint->real(SketchPlugin_MultiRotation::ANGLE_ID())->value();
   if (fabs(myAngle - aValue) > tolerance)
     myAdjusted = false;
   // update angle value
@@ -88,55 +93,61 @@ void SketchSolver_ConstraintMultiRotation::updateLocal()
   if (isMethodChanged)
     myIsFullValue = aFullValue;
 
-  if (aCenterPointChanged || isMethodChanged) {
-    DataPtr aData = myBaseConstraint->data();
-    std::list<ConstraintWrapperPtr> aConstraints = myStorage->constraint(myBaseConstraint);
-    std::list<ConstraintWrapperPtr>::const_iterator anIt = aConstraints.begin(),
-      aLast = aConstraints.end();
-    std::list<EntityWrapperPtr> anEntities;
-    for (; anIt != aLast; anIt++) {
-      ConstraintWrapperPtr aConstraint = *anIt;
-      aConstraint->setIsFullValue(myIsFullValue);
-      if (aCenterPointChanged) {
-        anEntities.clear();
-        const std::list<EntityWrapperPtr>& aConstraintEntities = aConstraint->entities();
-        std::list<EntityWrapperPtr>::const_iterator aSIt = aConstraintEntities.begin(),
-          aSLast = aConstraintEntities.end();
-        EntityWrapperPtr aCenterPointEntity = *aSIt++;
-        if (aCenterPointChanged) {
-          AttributePtr aCenterPointAttr = aData->attribute(SketchPlugin_MultiRotation::CENTER_ID());
-          myStorage->update(aCenterPointAttr);
-          aCenterPointEntity = myStorage->entity(aCenterPointAttr);
-        }
-        anEntities.push_back(aCenterPointEntity);
-
-        for (; aSIt != aSLast; ++aSIt)
-          anEntities.push_back(*aSIt);
-
-        aConstraint->setEntities(anEntities);
-      }
-    }
-    myStorage->addConstraint(myBaseConstraint, aConstraints);
-
+  if (aCenterPointChanged || isMethodChanged)
     myAdjusted = false;
-  }
 }
 
 void SketchSolver_ConstraintMultiRotation::adjustConstraint()
 {
+  if (myAdjusted)
+    return;
+
   if (fabs(myAngle) < tolerance) {
     myStorage->setNeedToResolve(false);
     return;
   }
 
-  const std::list<ConstraintWrapperPtr>& aConstraints = myStorage->constraint(myBaseConstraint);
-  std::list<ConstraintWrapperPtr>::const_iterator aCIt = aConstraints.begin();
-  for (; aCIt != aConstraints.end(); ++aCIt)
-    (*aCIt)->setValue(myAngle);
+  // Obtain coordinates of rotation center
+  std::shared_ptr<PlaneGCSSolver_PointWrapper> aRotCenter =
+      std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(myStorage->entity(
+      myBaseConstraint->attribute(SketchPlugin_MultiRotation::CENTER_ID())));
+  GCSPointPtr aCenterPoint = aRotCenter->point();
+  myCenterCoord[0] = *(aCenterPoint->x);
+  myCenterCoord[1] = *(aCenterPoint->y);
+
+  double anAngleValue = myAngle;
+  if (myIsFullValue && myNumberOfCopies > 0)
+    anAngleValue /= myNumberOfCopies;
+
+  myRotationVal[0] = sin(anAngleValue * PI / 180.0);
+  myRotationVal[1] = cos(anAngleValue * PI / 180.0);
 
   SketchSolver_ConstraintMulti::adjustConstraint();
 }
 
+void SketchSolver_ConstraintMultiRotation::getRelative(
+    double theAbsX, double theAbsY, double& theRelX, double& theRelY)
+{
+  theRelX = theAbsX - myCenterCoord[0];
+  theRelY = theAbsY - myCenterCoord[1];
+}
+
+void SketchSolver_ConstraintMultiRotation::getAbsolute(
+    double theRelX, double theRelY, double& theAbsX, double& theAbsY)
+{
+  theAbsX = theRelX + myCenterCoord[0];
+  theAbsY = theRelY + myCenterCoord[1];
+}
+
+void SketchSolver_ConstraintMultiRotation::transformRelative(double& theX, double& theY)
+{
+  // rotate direction
+  // myRotationVal[0] = sinA, myRotationVal[1] = cosA
+  double aTemp = theX * myRotationVal[1] - theY * myRotationVal[0];
+  theY = theX * myRotationVal[0] + theY * myRotationVal[1];
+  theX = aTemp;
+}
+
 const std::string& SketchSolver_ConstraintMultiRotation::nameNbObjects()
 {
   return SketchPlugin_MultiRotation::NUMBER_OF_OBJECTS_ID();