+// Copyright (C) 2017 CEA/DEN, EDF R&D
+//
+// 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<mailto:webmaster.salome@opencascade.com>
+//
+
#include <SketchSolver_ConstraintMovement.h>
#include <SketchSolver_Error.h>
-#include <SketchSolver_Group.h>
+#include <SketchSolver_Manager.h>
+
+#include <PlaneGCSSolver_EdgeWrapper.h>
+#include <PlaneGCSSolver_PointWrapper.h>
+
+#include <SketchPlugin_Arc.h>
+#include <SketchPlugin_Circle.h>
+#include <SketchPlugin_Line.h>
+#include <SketchPlugin_Point.h>
+
+#include <GeomDataAPI_Point2D.h>
+
+#include <GeomAPI_Pnt2d.h>
+
SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(FeaturePtr theFeature)
- : SketchSolver_ConstraintRigid(theFeature)
+ : SketchSolver_ConstraintFixed(ConstraintPtr()),
+ myMovedFeature(theFeature),
+ mySimpleMove(true)
+{
+}
+
+SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(AttributePtr thePoint)
+ : SketchSolver_ConstraintFixed(ConstraintPtr()),
+ myDraggedPoint(thePoint),
+ mySimpleMove(true)
+{
+ myMovedFeature = ModelAPI_Feature::feature(thePoint->owner());
+}
+
+void SketchSolver_ConstraintMovement::blockEvents(bool isBlocked)
{
- process();
+ if (myMovedFeature)
+ myMovedFeature->data()->blockSendAttributeUpdated(isBlocked);
}
void SketchSolver_ConstraintMovement::process()
{
cleanErrorMsg();
- if (!myBaseFeature || !myStorage || myGroup == 0) {
- /// TODO: Put error message here
+ if (!myMovedFeature || !myStorage) {
+ // Not enough parameters are initialized
return;
}
- if (!mySlvsConstraints.empty()) // some data is changed, update constraint
- update(myBaseConstraint);
-
- double aValue;
- std::vector<Slvs_hEntity> anEntities;
- bool isFullyMoved;
- getAttributes(aValue, anEntities, isFullyMoved);
- if (!myErrorMsg.empty() || (myFeatureMap.empty() && myAttributeMap.empty()))
- return;
- if (isFullyMoved)
- fixFeature();
- else {
- std::vector<Slvs_hEntity>::iterator anEntIt = anEntities.begin();
- for (; anEntIt != anEntities.end(); ++anEntIt)
- fixPoint(*anEntIt);
+ mySolverConstraint = initMovement();
+ if (!myErrorMsg.empty() || !mySolverConstraint) {
+ // Nothing to move, clear the feature to avoid changing its group
+ // after removing the Movement constraint.
+ myMovedFeature = FeaturePtr();
+ return;
}
+ myStorage->addMovementConstraint(mySolverConstraint);
}
-void SketchSolver_ConstraintMovement::getAttributes(
- double& theValue,
- std::vector<Slvs_hEntity>& theAttributes,
- bool& theIsFullyMoved)
+static bool isSimpleMove(FeaturePtr theMovedFeature, AttributePtr theDraggedPoint)
{
- bool isComplexFeature = false;
- theValue = 0.0;
- theIsFullyMoved = true;
- int aType = SLVS_E_UNKNOWN; // type of created entity
- Slvs_hEntity anEntityID = SLVS_E_UNKNOWN;
- anEntityID = myGroup->getFeatureId(myBaseFeature);
- if (anEntityID == SLVS_E_UNKNOWN) {
- anEntityID = changeEntity(myBaseFeature, aType);
- if (anEntityID == SLVS_E_UNKNOWN) {
- myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
- return;
- }
-
- // Check the entity is complex
- Slvs_Entity anEntity = myStorage->getEntity(anEntityID);
- if (anEntity.point[0] != SLVS_E_UNKNOWN)
- isComplexFeature = true;
- else // simple entity
- theAttributes.push_back(anEntityID);
+ bool isSimple = true;
+#ifdef CHANGE_RADIUS_WHILE_MOVE
+ if (theMovedFeature->getKind() == SketchPlugin_Circle::ID())
+ isSimple = (theDraggedPoint.get() != 0);
+ else if (theMovedFeature->getKind() == SketchPlugin_Arc::ID()) {
+ isSimple = (theDraggedPoint.get() != 0 &&
+ theDraggedPoint->id() == SketchPlugin_Arc::CENTER_ID());
}
+#endif
+ return isSimple;
+}
+
+ConstraintWrapperPtr SketchSolver_ConstraintMovement::initMovement()
+{
+ ConstraintWrapperPtr aConstraint;
+
+ // if the feature is copy, do not move it
+ std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
+ std::dynamic_pointer_cast<SketchPlugin_Feature>(myMovedFeature);
+ if (!aSketchFeature || aSketchFeature->isCopy()) {
+ myStorage->setNeedToResolve(true);
+ return aConstraint;
+ }
+
+ EntityWrapperPtr anEntity =
+ myDraggedPoint ? myStorage->entity(myDraggedPoint) : myStorage->entity(myMovedFeature);
+ if (!anEntity) {
+ myStorage->update(myMovedFeature, true);
+ anEntity =
+ myDraggedPoint ? myStorage->entity(myDraggedPoint) : myStorage->entity(myMovedFeature);
+ if (!anEntity)
+ return aConstraint;
+ }
+
+ mySimpleMove = isSimpleMove(myMovedFeature, myDraggedPoint);
+
+ if (mySimpleMove)
+ aConstraint = fixFeature(anEntity);
else {
- myFeatureMap[myBaseFeature] = anEntityID;
- isComplexFeature = true;
+ if (myDraggedPoint) // start or end point of arc has been moved
+ aConstraint = fixArcExtremity(anEntity);
+ else // arc or circle has been moved
+ aConstraint = fixPointOnCircle(anEntity);
}
- if (isComplexFeature) {
- std::list<AttributePtr> aPoints =
- myBaseFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
- std::list<AttributePtr>::iterator anIt = aPoints.begin();
- for (; anIt != aPoints.end(); ++anIt) {
- Slvs_hEntity anAttr = myGroup->getAttributeId(*anIt);
-
- // Check the attribute changes coordinates
- std::shared_ptr<GeomDataAPI_Point2D> aPt =
- std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
- if (isMoved(aPt, anAttr)) {
- theAttributes.push_back(anAttr);
- // update point coordinates
- Slvs_Entity anAttrEnt = myStorage->getEntity(anAttr);
- double aNewPos[2] = {aPt->x(), aPt->y()};
- for (int i = 0; i < 2; i++) {
- Slvs_Param aParam = myStorage->getParameter(anAttrEnt.param[i]);
- aParam.val = aNewPos[i];
- myStorage->updateParameter(aParam);
- }
- }
- else
- theIsFullyMoved = false;
- }
+ return aConstraint;
+}
+
+ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixArcExtremity(
+ const EntityWrapperPtr& theArcExtremity)
+{
+ static const int nbParams = 4;
+ myFixedValues.reserve(nbParams); // moved point and center of arc
+
+ EdgeWrapperPtr aCircularEntity = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(
+ myStorage->entity(myMovedFeature));
+ std::shared_ptr<GCS::Arc> anArc =
+ std::dynamic_pointer_cast<GCS::Arc>(aCircularEntity->entity());
+
+ PointWrapperPtr aPoint =
+ std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theArcExtremity);
+
+ double* aParams[nbParams] = { aPoint->point()->x, aPoint->point()->y,
+ anArc->center.x, anArc->center.y };
+
+ std::list<GCSConstraintPtr> aConstraints;
+ for (int i = 0; i < nbParams; ++i) {
+ myFixedValues.push_back(*aParams[i]);
+ GCSConstraintPtr aNewConstraint(new GCS::ConstraintEqual(&myFixedValues[i], aParams[i]));
+ aNewConstraint->rescale(0.01);
+ aConstraints.push_back(aNewConstraint);
}
- // Leave only points which are used in constraints
- if (myStorage->isUsedByConstraints(anEntityID))
- return;
- std::vector<Slvs_hEntity>::iterator anIt = theAttributes.begin();
- while (anIt != theAttributes.end()) {
- if (myStorage->isUsedByConstraints(*anIt))
- ++anIt;
- else {
- int aShift = anIt - theAttributes.begin();
- theAttributes.erase(anIt);
- anIt = theAttributes.begin() + aShift;
- }
+ return ConstraintWrapperPtr(
+ new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
+}
+
+ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixPointOnCircle(
+ const EntityWrapperPtr& theCircular)
+{
+ static const double scale = 0.01;
+ static const int nbParams = 4;
+ myFixedValues.reserve(nbParams); // moved point and center of arc/circle
+
+ EdgeWrapperPtr aCircularEntity =
+ std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theCircular);
+ std::shared_ptr<GCS::Circle> aCircular =
+ std::dynamic_pointer_cast<GCS::Circle>(aCircularEntity->entity());
+
+ // initialize fixed values
+ myFixedValues.push_back(*aCircular->center.x + *aCircular->rad);
+ myFixedValues.push_back(*aCircular->center.y);
+ myFixedValues.push_back(*aCircular->center.x);
+ myFixedValues.push_back(*aCircular->center.y);
+
+ // create a moved point
+ GCS::Point aPointOnCircle;
+ aPointOnCircle.x = &myFixedValues[0];
+ aPointOnCircle.y = &myFixedValues[1];
+
+ std::list<GCSConstraintPtr> aConstraints;
+ // point-on-circle
+ GCSConstraintPtr aNewConstraint(
+ new GCS::ConstraintP2PDistance(aPointOnCircle, aCircular->center, aCircular->rad));
+ aNewConstraint->rescale(scale);
+ aConstraints.push_back(aNewConstraint);
+ // fixed center (x)
+ aNewConstraint = GCSConstraintPtr(
+ new GCS::ConstraintEqual(&myFixedValues[2], aCircular->center.x));
+ aNewConstraint->rescale(scale);
+ aConstraints.push_back(aNewConstraint);
+ // fixed center (y)
+ aNewConstraint = GCSConstraintPtr(
+ new GCS::ConstraintEqual(&myFixedValues[3], aCircular->center.y));
+ aNewConstraint->rescale(scale);
+ aConstraints.push_back(aNewConstraint);
+
+ return ConstraintWrapperPtr(
+ new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
+}
+
+
+void SketchSolver_ConstraintMovement::startPoint(
+ const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint)
+{
+ myStartPoint = theStartPoint;
+ if (!mySimpleMove) {
+ myFixedValues[0] = myStartPoint->x();
+ myFixedValues[1] = myStartPoint->y();
}
}
-bool SketchSolver_ConstraintMovement::isMoved(
- std::shared_ptr<GeomDataAPI_Point2D> thePoint, Slvs_hEntity theEntity)
+void SketchSolver_ConstraintMovement::moveTo(
+ const std::shared_ptr<GeomAPI_Pnt2d>& theDestinationPoint)
{
- Slvs_Entity anAttrEnt = myStorage->getEntity(theEntity);
- double aDeltaX = myStorage->getParameter(anAttrEnt.param[0]).val;
- double aDeltaY = myStorage->getParameter(anAttrEnt.param[1]).val;
- aDeltaX -= thePoint->x();
- aDeltaY -= thePoint->y();
- return aDeltaX * aDeltaX + aDeltaY * aDeltaY >= tolerance * tolerance;
+ if (!myMovedFeature)
+ return; // nothing to move
+
+ double aDelta[2] = { theDestinationPoint->x() - myStartPoint->x(),
+ theDestinationPoint->y() - myStartPoint->y() };
+
+#ifdef CHANGE_RADIUS_WHILE_MOVE
+ int aMaxSize = mySimpleMove ? (int)myFixedValues.size() : 2;
+#else
+ int aMaxSize = myMovedFeature->getKind() == SketchPlugin_Line::ID() && !myDraggedPoint ? 4 : 2;
+#endif
+ for (int i = 0; i < aMaxSize; ++i)
+ myFixedValues[i] += aDelta[i % 2];
}