Salome HOME
Task "Make the size of the selection area even bigger, especially for points"
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMultiRotation.cpp
index da083caf78ce6f5f10ee05f58dab11c30d44fbef..bd53679d9ae635b2882bc62374c5703e5b936a76 100644 (file)
@@ -1,25 +1,16 @@
 #include <SketchSolver_ConstraintMultiRotation.h>
-#include <SketchSolver_Group.h>
 #include <SketchSolver_Error.h>
+#include <SketchSolver_Manager.h>
 
-#include <SketchPlugin_Arc.h>
 #include <SketchPlugin_MultiRotation.h>
 
-#include <ModelAPI_AttributeDouble.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 <ModelAPI_AttributeString.h>
 
 #include <math.h>
 
 void SketchSolver_ConstraintMultiRotation::getAttributes(
-    Slvs_hEntity& theCenter, double& theAngle,
-    std::vector< std::vector<Slvs_hEntity> >& thePoints,
-    std::vector< std::vector<Slvs_hEntity> >& theEntities)
+    EntityWrapperPtr& theCenter, double& theAngle,
+    bool& theFullValue, std::list<EntityWrapperPtr>& theEntities)
 {
   DataPtr aData = myBaseConstraint->data();
   theAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
@@ -30,148 +21,134 @@ void SketchSolver_ConstraintMultiRotation::getAttributes(
     myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
     return;
   }
-  int aType = SLVS_E_UNKNOWN; // type of created entity
-  Slvs_hEntity anEntityID = myGroup->getAttributeId(aCenterAttr);
-  if (anEntityID == SLVS_E_UNKNOWN)
-    anEntityID = changeEntity(aCenterAttr, aType);
-  theCenter = anEntityID;
-
-  // 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 = (size_t) aData->integer(SketchPlugin_MultiRotation::NUMBER_OF_COPIES_ID())->value();
-  AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
-      myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
-  if (!aRefList) {
-    myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
-    return;
-  }
 
-  // Obtain all points of initial features and store them into separate lists
-  // containing their translated copies.
-  // Also all circles and arc collected too, because they will be constrained by equal radii.
-  FeaturePtr aFeature;
-  ResultConstructionPtr aRC;
-  static const size_t MAX_POINTS = 3;
-  std::vector<Slvs_hEntity> aPoints[MAX_POINTS]; // lists of points of features
-  std::vector<Slvs_hEntity> anEntities;
-  std::list<ObjectPtr> anObjectList = aRefList->list();
-  std::list<ObjectPtr>::iterator anObjectIter = anObjectList.begin();
-  while (anObjectIter != anObjectList.end()) {
-    for (size_t i = 0; i < MAX_POINTS; ++i)
-      aPoints[i].clear();
-    anEntities.clear();
-
-    for (size_t i = 0; i <= myNumberOfCopies && anObjectIter != anObjectList.end(); i++, anObjectIter++) {
-      aFeature = ModelAPI_Feature::feature(*anObjectIter);
-      if (!aFeature)
-        continue;
-      anEntityID = changeEntity(aFeature, aType);
-      anEntities.push_back(anEntityID);
-      Slvs_Entity anEntity = myStorage->getEntity(anEntityID);
-      switch (aType) {
-      case SLVS_E_POINT_IN_2D:
-      case SLVS_E_POINT_IN_3D:
-        aPoints[0].push_back(anEntityID);
-        break;
-      case SLVS_E_LINE_SEGMENT:
-        aPoints[0].push_back(anEntity.point[0]); // start point of line
-        aPoints[1].push_back(anEntity.point[1]); // end point of line
-        break;
-      case SLVS_E_CIRCLE:
-        aPoints[0].push_back(anEntity.point[0]); // center of circle
-        break;
-      case SLVS_E_ARC_OF_CIRCLE:
-        aPoints[0].push_back(anEntity.point[0]); // center of arc
-        aPoints[1].push_back(anEntity.point[1]); // start point of arc
-        aPoints[2].push_back(anEntity.point[2]); // end point of arc
-        break;
-      default:
-        myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
-        return;
-      }
-    }
+  myType = CONSTRAINT_MULTI_ROTATION;
 
-    for (size_t i = 0; i < MAX_POINTS; ++i)
-      if (!aPoints[i].empty())
-        thePoints.push_back(aPoints[i]);
-    if (!anEntities.empty())
-      theEntities.push_back(anEntities);
-  }
+  myStorage->update(aCenterAttr);
+  theCenter = myStorage->entity(aCenterAttr);
+
+  AttributeStringPtr aMethodTypeAttr = aData->string(SketchPlugin_MultiRotation::ANGLE_TYPE());
+  theFullValue = aMethodTypeAttr->value() != "SingleAngle";
+
+  getEntities(theEntities);
 }
 
 void SketchSolver_ConstraintMultiRotation::process()
 {
   cleanErrorMsg();
-  if (!myBaseConstraint || !myStorage || myGroup == 0) {
+  if (!myBaseConstraint || !myStorage || myGroupID == GID_UNKNOWN) {
     /// TODO: Put error message here
     return;
   }
-  if (!mySlvsConstraints.empty()) // some data is changed, update constraint
-    update(myBaseConstraint);
 
-  std::vector<std::vector<Slvs_hEntity> > anEntitiesAndCopies;
-  getAttributes(myRotationCenter, myAngle, myPointsAndCopies, anEntitiesAndCopies);
+  EntityWrapperPtr aRotationCenter;
+  bool isFullValue;
+  std::list<EntityWrapperPtr> aBaseEntities;
+  getAttributes(aRotationCenter, myAngle, isFullValue, aBaseEntities);
   if (!myErrorMsg.empty())
     return;
 
-  // Set the rotation center unchanged during constraint recalculation
-  Slvs_Constraint aConstraint;
-  if (!myStorage->isPointFixed(myRotationCenter, aConstraint.h, true)) {
-    aConstraint = Slvs_MakeConstraint(
-        SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_WHERE_DRAGGED, myGroup->getWorkplaneId(), 0.0,
-        myRotationCenter, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
-    aConstraint.h = myStorage->addConstraint(aConstraint);
-    mySlvsConstraints.push_back(aConstraint.h);
+  BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
+  std::list<ConstraintWrapperPtr> aRotConstraints;
+
+  std::list<EntityWrapperPtr>::iterator anEntIt = aBaseEntities.begin();
+  for (; anEntIt != aBaseEntities.end(); ++anEntIt) {
+    std::list<ConstraintWrapperPtr> aNewConstraints =
+        aBuilder->createConstraint(myBaseConstraint, myGroupID, mySketchID, myType,
+        myAngle, isFullValue, aRotationCenter, EntityWrapperPtr(), std::list<EntityWrapperPtr>(1, *anEntIt));
+    aRotConstraints.insert(aRotConstraints.end(), aNewConstraints.begin(), aNewConstraints.end());
   }
+  myStorage->addConstraint(myBaseConstraint, aRotConstraints);
 
-  processEntities(anEntitiesAndCopies);
+  myAdjusted = false;
   adjustConstraint();
 }
 
 void SketchSolver_ConstraintMultiRotation::updateLocal()
 {
-  // update angle value
-  myAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+  double aValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
       myBaseConstraint->attribute(SketchPlugin_MultiRotation::ANGLE_ID()))->value();
+  if (fabs(myAngle - aValue) > tolerance)
+    myAdjusted = false;
+  // update angle value
+  myAngle = aValue;
+
+  // update center
+  DataPtr aData = myBaseConstraint->data();
+  AttributePoint2DPtr aCenterPointAttribute = GeomDataAPI_Point2D::getPoint2D(aData,
+                                           SketchPlugin_MultiRotation::CENTER_ID());
+  bool aCenterPointChanged = aCenterPointAttribute != myCenterPointAttribute;
+  if (aCenterPointChanged)
+    myCenterPointAttribute = aCenterPointAttribute;
+
+  AttributeStringPtr aMethodTypeAttr = aData->string(SketchPlugin_MultiRotation::ANGLE_TYPE());
+  bool aFullValue = aMethodTypeAttr->value() != "SingleAngle";
+  bool isMethodChanged = aFullValue != myIsFullValue;
+  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);
+
+    myAdjusted = false;
+  }
 }
 
 void SketchSolver_ConstraintMultiRotation::adjustConstraint()
 {
+  if (myAdjusted)
+    return;
+
   if (fabs(myAngle) < tolerance) {
     myStorage->setNeedToResolve(false);
     return;
   }
 
-  std::list<Slvs_Constraint> aCoincident = myStorage->getConstraintsByType(SLVS_C_POINTS_COINCIDENT);
-  std::list<Slvs_Constraint>::const_iterator aCoIt;
-
-  // Check overconstrained on rotation center (if it is coincident with other fixed point)
-  Slvs_hConstraint aFixedCenter;
-  if (myStorage->isPointFixed(myRotationCenter, aFixedCenter, false)) {
-    Slvs_hConstraint aFixed;
-    for (aCoIt = aCoincident.begin(); aCoIt != aCoincident.end(); ++aCoIt)
-      if ((aCoIt->ptA == myRotationCenter && myStorage->isPointFixed(aCoIt->ptB, aFixed, true)) ||
-          (aCoIt->ptB == myRotationCenter && myStorage->isPointFixed(aCoIt->ptA, aFixed, true))) {
-        // Un-fix the center
-        myStorage->removeConstraint(aFixedCenter);
-        std::vector<Slvs_hConstraint>::iterator aSCIt = mySlvsConstraints.begin();
-        for (; aSCIt != mySlvsConstraints.end(); ++aSCIt)
-          if (*aSCIt == aFixedCenter) {
-            mySlvsConstraints.erase(aSCIt);
-            break;
-          }
-      }
-  }
+  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
-  Slvs_Entity aRotCenter = myStorage->getEntity(myRotationCenter);
-  myCenterCoord[0] = myStorage->getParameter(aRotCenter.param[0]).val;
-  myCenterCoord[1] = myStorage->getParameter(aRotCenter.param[1]).val;
+  EntityWrapperPtr aRotCenter = myStorage->entity(
+      myBaseConstraint->attribute(SketchPlugin_MultiRotation::CENTER_ID()));
+  std::list<ParameterWrapperPtr> aParams = aRotCenter->parameters();
+  myCenterCoord[0] = aParams.front()->value();
+  myCenterCoord[1] = aParams.back()->value();
+
+  double anAngleValue = myAngle;
+  if (myIsFullValue && myNumberOfCopies > 0)
+    anAngleValue /= myNumberOfCopies;
 
-  myRotationVal[0] = sin(myAngle * PI / 180.0);
-  myRotationVal[1] = cos(myAngle * PI / 180.0);
+  myRotationVal[0] = sin(anAngleValue * PI / 180.0);
+  myRotationVal[1] = cos(anAngleValue * PI / 180.0);
 
   SketchSolver_ConstraintMulti::adjustConstraint();
 }
@@ -199,7 +176,7 @@ void SketchSolver_ConstraintMultiRotation::transformRelative(double& theX, doubl
   theX = aTemp;
 }
 
-const std::string& SketchSolver_ConstraintMultiRotation::nameNbCopies()
+const std::string& SketchSolver_ConstraintMultiRotation::nameNbObjects()
 {
-  return SketchPlugin_MultiRotation::NUMBER_OF_COPIES_ID();
+  return SketchPlugin_MultiRotation::NUMBER_OF_OBJECTS_ID();
 }