#include <PlaneGCSSolver_ConstraintWrapper.h>
#include <PlaneGCSSolver_EdgeWrapper.h>
+#include <PlaneGCSSolver_EntityDestroyer.h>
+#include <PlaneGCSSolver_FeatureBuilder.h>
#include <PlaneGCSSolver_PointWrapper.h>
#include <GeomDataAPI_Point2D.h>
#include <cmath>
-// Find parameters of the feature that have been updated during the movement
-static EntityWrapperPtr getChangedEntity(const FeaturePtr& theFeature,
- const StoragePtr& theStorage);
+// Verify the entities are equal
+static bool isEqual(const EntityWrapperPtr& theEntity1, const EntityWrapperPtr& theEntity2);
+// Convert entity to the list of parameters
+static GCS::VEC_pD toParameters(const EntityWrapperPtr& theEntity);
SketchSolver_ConstraintFixed::SketchSolver_ConstraintFixed(ConstraintPtr theConstraint)
return;
}
- EntityWrapperPtr aValue;
- std::vector<EntityWrapperPtr> anEntities;
- getAttributes(aValue, anEntities);
- if (anEntities.empty())
+ EntityWrapperPtr aBaseEntity;
+ getAttributes(aBaseEntity, myFixedEntity);
+ if (!aBaseEntity) {
+ moveFeature(); // remove myFixed entity
myErrorMsg = SketchSolver_Error::ALREADY_FIXED();
+ }
if (!myErrorMsg.empty())
return;
- fixFeature(anEntities.front());
+
+ fixFeature(aBaseEntity);
}
void SketchSolver_ConstraintFixed::fixFeature(EntityWrapperPtr theFeature)
{
- std::shared_ptr<PlaneGCSSolver_EdgeWrapper> anEntity =
- std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theFeature);
-
- GCS::VEC_pD aParameters; // parameters of entity to be fixed
-
- // Collect parameters for each type of entity
- switch (theFeature->type()) {
- case ENTITY_POINT: {
- std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint =
- std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theFeature);
- aParameters.push_back(aPoint->point()->x);
- aParameters.push_back(aPoint->point()->y);
- break;
- }
- case ENTITY_LINE: {
- std::shared_ptr<GCS::Line> aLine = std::dynamic_pointer_cast<GCS::Line>(anEntity->entity());
- aParameters.push_back(aLine->p1.x);
- aParameters.push_back(aLine->p1.y);
- aParameters.push_back(aLine->p2.x);
- aParameters.push_back(aLine->p2.y);
- break;
- }
- case ENTITY_CIRCLE: {
- std::shared_ptr<GCS::Circle> aCircle =
- std::dynamic_pointer_cast<GCS::Circle>(anEntity->entity());
- aParameters.push_back(aCircle->center.x);
- aParameters.push_back(aCircle->center.y);
- aParameters.push_back(aCircle->rad);
- break;
- }
- case ENTITY_ARC: {
- myFixedValues.reserve(4);
- std::shared_ptr<GCS::Arc> anArc = std::dynamic_pointer_cast<GCS::Arc>(anEntity->entity());
- aParameters.push_back(anArc->center.x);
- aParameters.push_back(anArc->center.y);
- aParameters.push_back(anArc->rad);
- aParameters.push_back(anArc->startAngle);
- aParameters.push_back(anArc->endAngle);
- break;
- }
- default:
- break;
- }
+ GCS::VEC_pD aParameters = toParameters(theFeature);
// Fix given list of parameters
std::list<GCSConstraintPtr> aConstraints;
for (int i = 0; anIt != aParameters.end(); ++anIt, ++i) {
myFixedValues.push_back(**anIt);
aConstraints.push_back(
- GCSConstraintPtr(new GCS::ConstraintEqual(*anIt, &myFixedValues[i])));
+ GCSConstraintPtr(new GCS::ConstraintEqual(&myFixedValues[i], *anIt)));
}
myConstraint = ConstraintWrapperPtr(
myStorage->addTemporaryConstraint(myConstraint);
}
-void SketchSolver_ConstraintFixed::getAttributes(
- EntityWrapperPtr& theValue,
- std::vector<EntityWrapperPtr>& theAttributes)
+void SketchSolver_ConstraintFixed::getAttributes(EntityWrapperPtr& theBaseEntity,
+ EntityWrapperPtr& theFixedEntity)
{
if (myBaseFeature) {
// if the feature is copy, do not move it
}
// The feature is fixed.
- EntityWrapperPtr aSolverEntity = getChangedEntity(myBaseFeature, myStorage);
- myStorage->update(myBaseFeature);
- if (aSolverEntity)
- theAttributes.push_back(aSolverEntity);
+ PlaneGCSSolver_FeatureBuilder aBuilder;
+ std::list<AttributePtr> aBaseAttr = myBaseFeature->data()->attributes(std::string());
+ std::list<AttributePtr>::const_iterator anIt = aBaseAttr.begin();
+ for (; anIt != aBaseAttr.end(); ++anIt)
+ aBuilder.createAttribute(*anIt);
+ theFixedEntity = aBuilder.createFeature(myBaseFeature);
+
+ theBaseEntity = myStorage->entity(myBaseFeature);
+ if (isEqual(theBaseEntity, theFixedEntity))
+ theBaseEntity = EntityWrapperPtr(); // do not want to fix already fixed entity
+
} else if (myBaseConstraint) {
// Constraint Fixed is added by user.
// Get the attribute of constraint (it should be alone in the list of constraints).
+ EntityWrapperPtr aValue;
std::vector<EntityWrapperPtr> anAttributes;
- SketchSolver_Constraint::getAttributes(theValue, anAttributes);
+ SketchSolver_Constraint::getAttributes(aValue, anAttributes);
std::vector<EntityWrapperPtr>::const_iterator anIt = anAttributes.begin();
for (; anIt != anAttributes.end(); ++anIt)
if (*anIt)
- theAttributes.push_back(*anIt);
+ theBaseEntity = *anIt;
} else
myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
}
+void SketchSolver_ConstraintFixed::moveFeature()
+{
+ if (!myFixedEntity)
+ return;
+ GCS::VEC_pD aFixedParams = toParameters(myFixedEntity);
+ for (int i = 0; i < aFixedParams.size() && i < myFixedValues.size(); ++i)
+ myFixedValues[i] = *(aFixedParams[i]);
+ // Remove fixed entity due to its parameters already copied into the constraint
+ PlaneGCSSolver_EntityDestroyer aDestroyer;
+ aDestroyer.remove(myFixedEntity);
+ std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage)->removeParameters(
+ aDestroyer.parametersToRemove());
-// ==================== Auxiliary functions ===============================
-static bool isSameCoordinates(const AttributePoint2DPtr& thePointAttr,
- const PointWrapperPtr& thePointWrapper)
-{
- GCSPointPtr aGCSPoint = thePointWrapper->point();
- return fabs(*aGCSPoint->x - thePointAttr->x()) < tolerance &&
- fabs(*aGCSPoint->y - thePointAttr->y()) < tolerance;
+ myFixedEntity = EntityWrapperPtr();
}
-EntityWrapperPtr getChangedEntity(const FeaturePtr& theFeature,
- const StoragePtr& theStorage)
+
+
+
+// ==================== Auxiliary functions ===============================
+GCS::VEC_pD toParameters(const EntityWrapperPtr& theEntity)
{
- std::list<AttributePtr> aPoints = theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
- std::list<EntityWrapperPtr> aChangedPoints;
-
- std::list<AttributePtr>::const_iterator aPIt = aPoints.begin();
- for (; aPIt != aPoints.end(); ++aPIt) {
- AttributePoint2DPtr aPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aPIt);
- EntityWrapperPtr anEnt = theStorage->entity(*aPIt);
- if (anEnt) {
- PointWrapperPtr aPW = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(anEnt);
- if (!isSameCoordinates(aPnt, aPW))
- aChangedPoints.push_back(anEnt);
- } else {
- theStorage->update(*aPIt);
- aChangedPoints.push_back(theStorage->entity(*aPIt));
+ GCS::VEC_pD aParameters;
+ if (!theEntity)
+ return aParameters;
+
+ std::shared_ptr<PlaneGCSSolver_EdgeWrapper> anEntity =
+ std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theEntity);
+
+ // Collect parameters for each type of entity
+ switch (theEntity->type()) {
+ case ENTITY_POINT: {
+ std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint =
+ std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theEntity);
+ aParameters.push_back(aPoint->point()->x);
+ aParameters.push_back(aPoint->point()->y);
+ break;
}
+ case ENTITY_LINE: {
+ std::shared_ptr<GCS::Line> aLine = std::dynamic_pointer_cast<GCS::Line>(anEntity->entity());
+ aParameters.push_back(aLine->p1.x);
+ aParameters.push_back(aLine->p1.y);
+ aParameters.push_back(aLine->p2.x);
+ aParameters.push_back(aLine->p2.y);
+ break;
+ }
+ case ENTITY_CIRCLE: {
+ std::shared_ptr<GCS::Circle> aCircle =
+ std::dynamic_pointer_cast<GCS::Circle>(anEntity->entity());
+ aParameters.push_back(aCircle->center.x);
+ aParameters.push_back(aCircle->center.y);
+ aParameters.push_back(aCircle->rad);
+ break;
+ }
+ case ENTITY_ARC: {
+ std::shared_ptr<GCS::Arc> anArc = std::dynamic_pointer_cast<GCS::Arc>(anEntity->entity());
+ aParameters.push_back(anArc->center.x);
+ aParameters.push_back(anArc->center.y);
+ aParameters.push_back(anArc->rad);
+ aParameters.push_back(anArc->startAngle);
+ aParameters.push_back(anArc->endAngle);
+ break;
+ }
+ default:
+ break;
}
- EntityWrapperPtr aChanged;
- if (aChangedPoints.size() == 1)
- aChanged = aChangedPoints.front();
- else if (!aChangedPoints.empty()) // update whole feature
- aChanged = theStorage->entity(theFeature);
- return aChanged;
+ return aParameters;
+}
+
+bool isEqual(const EntityWrapperPtr& theEntity1, const EntityWrapperPtr& theEntity2)
+{
+ GCS::VEC_pD aParamList1 = toParameters(theEntity1);
+ GCS::VEC_pD aParamList2 = toParameters(theEntity2);
+
+ GCS::VEC_pD::const_iterator anIt1 = aParamList1.begin();
+ GCS::VEC_pD::const_iterator anIt2 = aParamList2.begin();
+ for (; anIt1 != aParamList1.end() && anIt2 != aParamList2.end(); ++anIt1, ++anIt2)
+ if (fabs((**anIt1) - (**anIt2)) > tolerance)
+ return false;
+
+ return anIt1 == aParamList1.end() && anIt2 == aParamList2.end();
}