#include <SketchSolver_ConstraintMovement.h>
#include <SketchSolver_Error.h>
-#include <SketchSolver_Group.h>
+#include <SketchSolver_Manager.h>
+
+#include <SketchPlugin_Arc.h>
+#include <SketchPlugin_Circle.h>
+#include <SketchPlugin_Line.h>
+#include <SketchPlugin_Point.h>
#include <GeomDataAPI_Point2D.h>
-SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(FeaturePtr theFeature)
- : SketchSolver_ConstraintRigid(theFeature)
-{
- process();
-}
void SketchSolver_ConstraintMovement::process()
{
cleanErrorMsg();
- if (!myBaseFeature || !myStorage || myGroup == 0) {
- /// TODO: Put error message here
+ if (!myBaseFeature || !myStorage || myGroupID == GID_UNKNOWN) {
+ // 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()))
+ ParameterWrapperPtr aValue;
+ getAttributes(aValue, myMovedEntities);
+ if (!myErrorMsg.empty() || myMovedEntities.empty()) {
+ // Nothing to move, clear the feature to avoid changing its group
+ // after removing the Movement constraint.
+ myBaseFeature = FeaturePtr();
return;
+ }
- if (isFullyMoved)
- fixFeature();
- else {
- std::vector<Slvs_hEntity>::iterator anEntIt = anEntities.begin();
- for (; anEntIt != anEntities.end(); ++anEntIt)
- fixPoint(*anEntIt);
+ std::vector<EntityWrapperPtr>::iterator anEntIt = myMovedEntities.begin();
+ for (; anEntIt != myMovedEntities.end(); ++anEntIt)
+ fixFeature(*anEntIt);
+}
+
+
+static std::list<EntityWrapperPtr> movedEntities(
+ EntityWrapperPtr theOld, StoragePtr theOldStorage,
+ EntityWrapperPtr theNew, StoragePtr theNewStorage)
+{
+ bool isFullyMoved = true;
+ std::list<EntityWrapperPtr> aMoved;
+ if (theOld->isEqual(theNew))
+ return aMoved;
+
+ std::list<EntityWrapperPtr> anOldSubs = theOld->subEntities();
+ std::list<EntityWrapperPtr> aNewSubs = theNew->subEntities();
+ std::list<EntityWrapperPtr>::const_iterator anOldIt = anOldSubs.begin();
+ std::list<EntityWrapperPtr>::const_iterator aNewIt = aNewSubs.begin();
+ for (; anOldIt != anOldSubs.end() && aNewIt != aNewSubs.end(); ++anOldIt, ++aNewIt) {
+ std::list<EntityWrapperPtr> aMovedSubs = movedEntities(
+ *anOldIt, theOldStorage, *aNewIt, theNewStorage);
+ if ((*anOldIt)->type() == ENTITY_POINT && // check only the points to be moved (because arcs in PlaneGCS have scalar subs too)
+ (aMovedSubs.size() != 1 || aMovedSubs.front() != *anOldIt))
+ isFullyMoved = false;
+ aMoved.insert(aMoved.end(), aMovedSubs.begin(), aMovedSubs.end());
+ }
+ if (isFullyMoved) {
+ aMoved.clear();
+ aMoved.push_back(theOld);
}
+ return aMoved;
}
void SketchSolver_ConstraintMovement::getAttributes(
- double& theValue,
- std::vector<Slvs_hEntity>& theAttributes,
- bool& theIsFullyMoved)
+ ParameterWrapperPtr& theValue,
+ std::vector<EntityWrapperPtr>& theAttributes)
{
- 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;
- }
+ // There will be build entities, according to the fixed feature, in the separate storage
+ // to check whether any point is moved
+ BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
+ StoragePtr anOtherStorage = aBuilder->createStorage(myGroupID);
+ anOtherStorage->setSketch(myStorage->sketch());
+ if (!anOtherStorage->update(myBaseFeature, myGroupID))
+ return;
+ EntityWrapperPtr aNewEntity = anOtherStorage->entity(myBaseFeature);
+ EntityWrapperPtr anOldEntity = myStorage->entity(myBaseFeature);
- // Check the entity is complex
- Slvs_Entity anEntity = myStorage->getEntity(anEntityID);
- if (anEntity.point[0] != SLVS_E_UNKNOWN) {
- for (int i = 0; i < 4 && anEntity.point[i]; i++)
- theAttributes.push_back(anEntity.point[i]);
- } else // simple entity
- theAttributes.push_back(anEntityID);
- }
+ std::list<EntityWrapperPtr> aMoved;
+ if (aNewEntity && anOldEntity)
+ aMoved = movedEntities(anOldEntity, myStorage, aNewEntity, anOtherStorage);
else {
- myFeatureMap[myBaseFeature] = anEntityID;
-
- std::list<AttributePtr> aPoints =
+ // get attributes moved
+ std::list<AttributePtr> anAttrList =
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
- Slvs_Entity anAttrEnt = myStorage->getEntity(anAttr);
- double aDeltaX = myStorage->getParameter(anAttrEnt.param[0]).val;
- double aDeltaY = myStorage->getParameter(anAttrEnt.param[1]).val;
- std::shared_ptr<GeomDataAPI_Point2D> aPt =
- std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
- aDeltaX -= aPt->x();
- aDeltaY -= aPt->y();
- if (aDeltaX * aDeltaX + aDeltaY * aDeltaY >= tolerance * tolerance) {
- theAttributes.push_back(anAttr);
- // update point coordinates
- 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;
- }
+ std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
+ for (; anIt != anAttrList.end(); ++anIt) {
+ aNewEntity = anOtherStorage->entity(*anIt);
+ anOldEntity = myStorage->entity(*anIt);
+ if (!aNewEntity || !anOldEntity)
+ continue;
+ std::list<EntityWrapperPtr> aMovedAttr = movedEntities(
+ anOldEntity, myStorage, aNewEntity, anOtherStorage);
+ aMoved.insert(aMoved.end(), aMovedAttr.begin(), aMovedAttr.end());
+ }
}
+ theAttributes.clear();
+ theAttributes.insert(theAttributes.begin(), aMoved.begin(), aMoved.end());
}
+bool SketchSolver_ConstraintMovement::remove()
+{
+ cleanErrorMsg();
+ // Move fixed entities back to the current group
+ std::vector<EntityWrapperPtr>::iterator aMoveIt = myMovedEntities.begin();
+ for (; aMoveIt != myMovedEntities.end(); ++aMoveIt) {
+ if ((*aMoveIt)->baseAttribute())
+ myStorage->update((*aMoveIt)->baseAttribute(), myGroupID);
+ else if ((*aMoveIt)->baseFeature())
+ myStorage->update((*aMoveIt)->baseFeature(), myGroupID);
+ }
+
+ // Remove base feature
+ if (myBaseFeature)
+ myStorage->removeEntity(myBaseFeature);
+ return true;
+}