1 #include <SketchSolver_ConstraintDistance.h>
2 #include <SketchSolver_Error.h>
3 #include <SketchSolver_Manager.h>
5 #include <GeomAPI_Dir2d.h>
6 #include <GeomAPI_Lin2d.h>
7 #include <GeomAPI_Pnt2d.h>
8 #include <GeomAPI_XY.h>
13 void SketchSolver_ConstraintDistance::getAttributes(
15 std::vector<EntityWrapperPtr>& theAttributes)
17 SketchSolver_Constraint::getAttributes(theValue, theAttributes);
18 if (!myErrorMsg.empty() || !theAttributes[0]) {
19 theAttributes.clear();
24 myType = CONSTRAINT_PT_PT_DISTANCE;
25 else if (theAttributes[2] && theAttributes[2]->type() == ENTITY_LINE)
26 myType = CONSTRAINT_PT_LINE_DISTANCE;
28 theAttributes.clear();
33 void SketchSolver_ConstraintDistance::adjustConstraint()
35 ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint).front();
37 // Adjust point-point distance if the points are equal
38 if (getType() == CONSTRAINT_PT_PT_DISTANCE) {
39 const std::list<EntityWrapperPtr>& aSubs = aConstraint->entities();
40 if (aSubs.front()->isEqual(aSubs.back())) {
41 // Change X coordinate of second point to eliminate coincidence
42 ParameterWrapperPtr aX = aSubs.back()->parameters().front();
43 aX->setValue(aX->value() + 1.0);
44 myStorage->update(aX);
49 // Adjust point-line distance
50 if (fabs(myPrevValue) == fabs(aConstraint->value())) {
51 // sign of distance is not changed
52 aConstraint->setValue(myPrevValue);
53 myStorage->addConstraint(myBaseConstraint, aConstraint);
57 // Get constraint parameters and check the sign of constraint value
58 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
59 std::shared_ptr<GeomAPI_Pnt2d> aPoint;
60 std::shared_ptr<GeomAPI_Lin2d> aLine;
61 std::list<EntityWrapperPtr> aSubs = aConstraint->entities();
62 std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
63 for (; aSIt != aSubs.end(); ++aSIt) {
64 if ((*aSIt)->type() == ENTITY_POINT)
65 aPoint = aBuilder->point(*aSIt);
66 else if ((*aSIt)->type() == ENTITY_LINE)
67 aLine = aBuilder->line(*aSIt);
70 std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
71 std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
72 if (aPtLineVec->cross(aLineVec) * aConstraint->value() < 0.0 || myIsNegative) {
73 aConstraint->setValue(aConstraint->value() * (-1.0));
74 myStorage->addConstraint(myBaseConstraint, aConstraint);
79 void SketchSolver_ConstraintDistance::update()
81 ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint).front();
82 myPrevValue = aConstraint->value();
84 SketchSolver_Constraint::update();