#include <GeomDataAPI_Point2D.h>
#include <ModelAPI_AttributeDouble.h>
+#include <math.h>
+
SketchSolver_ConstraintRigid::SketchSolver_ConstraintRigid(FeaturePtr theFeature)
: SketchSolver_Constraint(),
myBaseFeature(theFeature)
double aValue;
std::vector<Slvs_hEntity> anEntities;
getAttributes(aValue, anEntities);
- if (!myErrorMsg.empty() || myFeatureMap.empty())
+ if (!myErrorMsg.empty() || (myFeatureMap.empty() && myAttributeMap.empty()))
return;
+ fixFeature();
+}
- Slvs_hEntity anEntID = myFeatureMap.begin()->second;
- if (myStorage->isEntityFixed(anEntID, true)) {
- myErrorMsg = SketchSolver_Error::ALREADY_FIXED();
- return;
- }
+void SketchSolver_ConstraintRigid::fixFeature()
+{
+ Slvs_hEntity anEntID;
+ if (!myFeatureMap.empty())
+ anEntID = myFeatureMap.begin()->second;
+ else
+ anEntID = myAttributeMap.begin()->second;
+ //if (myStorage->isEntityFixed(anEntID, true)) {
+ // myErrorMsg = SketchSolver_Error::ALREADY_FIXED();
+ // return;
+ //}
+
+ std::string aKind;
+ if (!myFeatureMap.empty())
+ aKind = myFeatureMap.begin()->first->getKind();
+ else
+ aKind = myAttributeMap.begin()->first->attributeType();
- if (myFeatureMap.begin()->first->getKind() == SketchPlugin_Line::ID()) {
+ if (aKind == SketchPlugin_Line::ID()) {
Slvs_Entity aLine = myStorage->getEntity(anEntID);
fixLine(aLine);
}
- else if (myFeatureMap.begin()->first->getKind() == SketchPlugin_Arc::ID()) {
+ else if (aKind == SketchPlugin_Arc::ID()) {
Slvs_Entity anArc = myStorage->getEntity(anEntID);
fixArc(anArc);
}
- else if (myFeatureMap.begin()->first->getKind() == SketchPlugin_Circle::ID()) {
+ else if (aKind == SketchPlugin_Circle::ID()) {
Slvs_Entity aCirc = myStorage->getEntity(anEntID);
fixCircle(aCirc);
}
- else if (myFeatureMap.begin()->first->getKind() == SketchPlugin_Point::ID()) {
+ else if (aKind == SketchPlugin_Point::ID() || aKind == GeomDataAPI_Point2D::typeId()) {
fixPoint(anEntID);
}
}
myFeatureMap.clear();
myAttributeMap.clear();
myValueMap.clear();
+ mySlvsConstraints.clear();
} else
cleanRemovedEntities();
return true;
void SketchSolver_ConstraintRigid::fixLine(const Slvs_Entity& theLine)
{
Slvs_Constraint anEqual;
- if (isUsedInEqual(theLine, anEqual)) {
+ if (myStorage->isAxisParallel(theLine.h)) {
+ // Fix one point and a line length
+ Slvs_hConstraint aFixed;
+ if (!myStorage->isPointFixed(theLine.point[0], aFixed, true) &&
+ !myStorage->isPointFixed(theLine.point[1], aFixed, true))
+ fixPoint(theLine.point[0]);
+ if (!myStorage->isUsedInEqual(theLine.h, anEqual)) {
+ // Check the distance is not set yet
+ std::list<Slvs_Constraint> aDistConstr = myStorage->getConstraintsByType(SLVS_C_PT_PT_DISTANCE);
+ std::list<Slvs_Constraint>::const_iterator aDIt = aDistConstr.begin();
+ for (; aDIt != aDistConstr.end(); aDIt++)
+ if ((aDIt->ptA == theLine.point[0] && aDIt->ptB == theLine.point[1]) ||
+ (aDIt->ptA == theLine.point[1] && aDIt->ptB == theLine.point[0]))
+ return;
+ // Calculate distance between points on the line
+ double aCoords[4];
+ for (int i = 0; i < 2; i++) {
+ Slvs_Entity aPnt = myStorage->getEntity(theLine.point[i]);
+ for (int j = 0; j < 2; j++) {
+ Slvs_Param aParam = myStorage->getParameter(aPnt.param[j]);
+ aCoords[2*i+j] = aParam.val;
+ }
+ }
+ double aLength = sqrt((aCoords[2] - aCoords[0]) * (aCoords[2] - aCoords[0]) +
+ (aCoords[3] - aCoords[1]) * (aCoords[3] - aCoords[1]));
+ // fix line length
+ Slvs_Constraint aDistance = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(),
+ SLVS_C_PT_PT_DISTANCE, myGroup->getWorkplaneId(), aLength,
+ theLine.point[0], theLine.point[1], SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
+ aDistance.h = myStorage->addConstraint(aDistance);
+ mySlvsConstraints.push_back(aDistance.h);
+ }
+ return;
+ }
+ else if (myStorage->isUsedInEqual(theLine.h, anEqual)) {
// Check another entity of Equal is already fixed
Slvs_hEntity anOtherEntID = anEqual.entityA == theLine.h ? anEqual.entityB : anEqual.entityA;
if (myStorage->isEntityFixed(anOtherEntID, true)) {
bool isFixRadius = true;
// Verify the arc is under Equal constraint
Slvs_Constraint anEqual;
- if (isUsedInEqual(theCircle, anEqual)) {
+ if (myStorage->isUsedInEqual(theCircle.h, anEqual)) {
// Check another entity of Equal is already fixed
Slvs_hEntity anOtherEntID = anEqual.entityA == theCircle.h ? anEqual.entityB : anEqual.entityA;
if (myStorage->isEntityFixed(anOtherEntID, true))
fixPoint(theCircle.point[0]);
if (isFixRadius) {
+ // Search the radius is already fixed
+ std::list<Slvs_Constraint> aDiamConstr = myStorage->getConstraintsByType(SLVS_C_DIAMETER);
+ std::list<Slvs_Constraint>::const_iterator aDiamIter = aDiamConstr.begin();
+ for (; aDiamIter != aDiamConstr.end(); aDiamIter++)
+ if (aDiamIter->entityA == theCircle.h)
+ return;
+
// Fix radius of a circle
AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
myFeatureMap.begin()->first->attribute(SketchPlugin_Circle::RADIUS_ID()));
// Verify the arc is under Equal constraint
Slvs_Constraint anEqual;
- if (isUsedInEqual(theArc, anEqual)) {
+ if (myStorage->isUsedInEqual(theArc.h, anEqual)) {
// Check another entity of Equal is already fixed
Slvs_hEntity anOtherEntID = anEqual.entityA == theArc.h ? anEqual.entityB : anEqual.entityA;
if (myStorage->isEntityFixed(anOtherEntID, true)) {
std::list<Slvs_Constraint> aDiamConstraints = myStorage->getConstraintsByType(SLVS_C_DIAMETER);
std::list<Slvs_Constraint>::iterator anIt = aDiamConstraints.begin();
for (; anIt != aDiamConstraints.end() && !isExists; anIt++)
- if (anIt->entityA == myFeatureMap.begin()->second)
+ if (anIt->entityA == theArc.h)
isExists = true;
if (!isExists) {
Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_DIAMETER,
}
}
}
-
-bool SketchSolver_ConstraintRigid::isUsedInEqual(
- const Slvs_Entity& theEntity, Slvs_Constraint& theEqual) const
-{
- // Check the entity is used in Equal constraint
- std::list<Slvs_Constraint> anEqualConstr = myStorage->getConstraintsByType(SLVS_C_EQUAL_LENGTH_LINES);
- std::list<Slvs_Constraint> anAddList = myStorage->getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN);
- anEqualConstr.insert(anEqualConstr.end(), anAddList.begin(), anAddList.end());
- anAddList = myStorage->getConstraintsByType(SLVS_C_EQUAL_RADIUS);
- anEqualConstr.insert(anEqualConstr.end(), anAddList.begin(), anAddList.end());
-
- std::list<Slvs_Constraint>::const_iterator anEqIter = anEqualConstr.begin();
- for (; anEqIter != anEqualConstr.end(); anEqIter++)
- if (anEqIter->entityA == theEntity.h || anEqIter->entityB == theEntity.h) {
- theEqual = *anEqIter;
- return true;
- }
- return false;
-}
-