X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FSketchSolver%2FSketchSolver_ConstraintMovement.cpp;h=916b8d5515d988d4eeca5cd5fc8d713ae96b74af;hb=4f565b2204d3fba046aa8c851abada2a5a17bf6c;hp=b97d1a754fde25fbc659b8bfa8e0e771e1502c48;hpb=6b82f436079f958ed743771a9ae91a7fd99b632e;p=modules%2Fshaper.git diff --git a/src/SketchSolver/SketchSolver_ConstraintMovement.cpp b/src/SketchSolver/SketchSolver_ConstraintMovement.cpp index b97d1a754..916b8d551 100644 --- a/src/SketchSolver/SketchSolver_ConstraintMovement.cpp +++ b/src/SketchSolver/SketchSolver_ConstraintMovement.cpp @@ -1,176 +1,120 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D + #include #include -#include +#include #include #include #include #include -SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(FeaturePtr theFeature) - : SketchSolver_ConstraintRigid(theFeature) -{ - process(); -} +#include + 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 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::iterator anEntIt = anEntities.begin(); - for (; anEntIt != anEntities.end(); ++anEntIt) - fixPoint(*anEntIt); } + + std::vector::iterator anEntIt = myMovedEntities.begin(); + for (; anEntIt != myMovedEntities.end(); ++anEntIt) + fixFeature(*anEntIt); } -void SketchSolver_ConstraintMovement::getAttributes( - double& theValue, - std::vector& theAttributes, - bool& theIsFullyMoved) +static std::list movedEntities( + EntityWrapperPtr theOld, StoragePtr theOldStorage, + EntityWrapperPtr theNew, StoragePtr theNewStorage) { - bool isComplexFeature = false; - theValue = 0.0; - theIsFullyMoved = true; - int aType = SLVS_E_UNKNOWN; // type of created entity - Slvs_hEntity anEntityID = SLVS_E_UNKNOWN; - Slvs_hEntity anEntMaxID = myStorage->entityMaxID(); - 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); - } - else { - myFeatureMap[myBaseFeature] = anEntityID; - isComplexFeature = true; + bool isFullyMoved = true; + std::list aMoved; + if (theOld->isEqual(theNew)) + return aMoved; + + std::list anOldSubs = theOld->subEntities(); + std::list aNewSubs = theNew->subEntities(); + std::list::const_iterator anOldIt = anOldSubs.begin(); + std::list::const_iterator aNewIt = aNewSubs.begin(); + for (; anOldIt != anOldSubs.end() && aNewIt != aNewSubs.end(); ++anOldIt, ++aNewIt) { + std::list aMovedSubs = movedEntities( + *anOldIt, theOldStorage, *aNewIt, theNewStorage); + // check only the points to be moved (because arcs in PlaneGCS have scalar subs too) + if ((*anOldIt)->type() == ENTITY_POINT && + (aMovedSubs.size() != 1 || aMovedSubs.front() != *anOldIt)) + isFullyMoved = false; + aMoved.insert(aMoved.end(), aMovedSubs.begin(), aMovedSubs.end()); } - - int aNbOutOfGroup = 0; - if (isComplexFeature) { - std::list aPoints = - myBaseFeature->data()->attributes(GeomDataAPI_Point2D::typeId()); - std::list::iterator anIt = aPoints.begin(); - for (; anIt != aPoints.end(); ++anIt) { - std::map::const_iterator aFound = myAttributeMap.find(*anIt); - Slvs_hEntity anAttr = aFound != myAttributeMap.end() ? - aFound->second : myGroup->getAttributeId(*anIt); - Slvs_Entity anAttrEnt = myStorage->getEntity(anAttr); - - // Check the attribute changes coordinates - std::shared_ptr aPt = - std::dynamic_pointer_cast(*anIt); - // Check the entity is not lying in the current group or it is not moved - if (anAttr == SLVS_E_UNKNOWN || anAttrEnt.group != myGroup->getId() || - (anAttr <= anEntMaxID && !isMoved(aPt, anAttrEnt))) { - if (anAttrEnt.group == SLVS_G_OUTOFGROUP) - ++aNbOutOfGroup; - theIsFullyMoved = false; - } - else { - 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); - } - } - } + if (isFullyMoved) { + aMoved.clear(); + aMoved.push_back(theOld); } + return aMoved; +} - // Additional checking, which leads to fix whole feature, if it has fixed points - if (!theIsFullyMoved) { - Slvs_Entity aFeature = myStorage->getEntity(anEntityID); - int aNbPoints = 4; - while (aNbPoints > 0 && aFeature.point[aNbPoints-1] == SLVS_E_UNKNOWN) - --aNbPoints; - if (aNbPoints == aNbOutOfGroup + (int)theAttributes.size()) { - theIsFullyMoved = true; - return; - } - } - // Leave only points which are used in constraints - if (myStorage->isUsedByConstraints(anEntityID)) +void SketchSolver_ConstraintMovement::getAttributes( + ParameterWrapperPtr& theValue, + std::vector& theAttributes) +{ + // 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; - std::vector::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; + EntityWrapperPtr aNewEntity = anOtherStorage->entity(myBaseFeature); + EntityWrapperPtr anOldEntity = myStorage->entity(myBaseFeature); + + std::list aMoved; + if (aNewEntity && anOldEntity) + aMoved = movedEntities(anOldEntity, myStorage, aNewEntity, anOtherStorage); + else { + // get attributes moved + std::list anAttrList = + myBaseFeature->data()->attributes(GeomDataAPI_Point2D::typeId()); + std::list::const_iterator anIt = anAttrList.begin(); + for (; anIt != anAttrList.end(); ++anIt) { + aNewEntity = anOtherStorage->entity(*anIt); + anOldEntity = myStorage->entity(*anIt); + if (!aNewEntity || !anOldEntity) + continue; + std::list 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::isMoved( - std::shared_ptr thePoint, const Slvs_Entity& theEntity) -{ - double aDeltaX = myStorage->getParameter(theEntity.param[0]).val; - double aDeltaY = myStorage->getParameter(theEntity.param[1]).val; - aDeltaX -= thePoint->x(); - aDeltaY -= thePoint->y(); - return aDeltaX * aDeltaX + aDeltaY * aDeltaY >= tolerance * tolerance; -} - -void SketchSolver_ConstraintMovement::fixFeature() +bool SketchSolver_ConstraintMovement::remove() { - Slvs_hEntity anEntID = fixedEntity(); - - std::string aKind; - std::map::const_iterator aFIt = myFeatureMap.begin(); - for (; aFIt != myFeatureMap.end() && aKind.empty(); ++aFIt) - if (aFIt->second == anEntID) - aKind = aFIt->first->getKind(); - std::map::const_iterator anAtIt = myAttributeMap.begin(); - for (; anAtIt != myAttributeMap.end() && aKind.empty(); ++anAtIt) - if (anAtIt->second == anEntID) - aKind = anAtIt->first->attributeType(); - - if (aKind == SketchPlugin_Line::ID()) { - Slvs_Entity aLine = myStorage->getEntity(anEntID); - fixLine(aLine); - } - else if (aKind == SketchPlugin_Arc::ID()) { - Slvs_Entity anArc = myStorage->getEntity(anEntID); - fixArc(anArc); - } - else if (aKind == SketchPlugin_Circle::ID()) { - Slvs_Entity aCirc = myStorage->getEntity(anEntID); - fixCircle(aCirc); - } - else if (aKind == SketchPlugin_Point::ID() || aKind == GeomDataAPI_Point2D::typeId()) { - fixPoint(anEntID); + cleanErrorMsg(); + // Move fixed entities back to the current group + std::vector::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; +}