1 #include <SketchSolver_ConstraintTangent.h>
2 #include <SketchSolver_Error.h>
3 #include <SketchSolver_Manager.h>
5 #include <GeomAPI_Pnt2d.h>
6 #include <SketchPlugin_Circle.h>
11 /// \brief Check whether the entities has only one shared point
12 static bool hasSingleCoincidence(EntityWrapperPtr theEntity1, EntityWrapperPtr theEntity2)
14 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
16 const std::list<EntityWrapperPtr>& aPoints1 = theEntity1->subEntities();
17 const std::list<EntityWrapperPtr>& aPoints2 = theEntity2->subEntities();
19 std::list<EntityWrapperPtr>::const_iterator aStartIt1 = aPoints1.begin();
20 if (theEntity1->type() == ENTITY_ARC) ++aStartIt1; // skip center of arc
21 std::list<EntityWrapperPtr>::const_iterator aStartIt2 = aPoints2.begin();
22 if (theEntity2->type() == ENTITY_ARC) ++aStartIt2; // skip center of arc
25 std::list<EntityWrapperPtr>::const_iterator anIt1, anIt2;
26 for (anIt1 = aStartIt1; anIt1 != aPoints1.end(); ++anIt1) {
27 if ((*anIt1)->type() != ENTITY_POINT)
29 std::shared_ptr<GeomAPI_Pnt2d> aPt1 = aBuilder->point(*anIt1);
30 for (anIt2 = aStartIt2; anIt2 != aPoints2.end(); ++anIt2) {
31 if ((*anIt2)->type() != ENTITY_POINT)
33 std::shared_ptr<GeomAPI_Pnt2d> aPt2 = aBuilder->point(*anIt2);
34 if (aPt1->distance(aPt2) < tolerance)
42 void SketchSolver_ConstraintTangent::getAttributes(
44 std::vector<EntityWrapperPtr>& theAttributes)
46 SketchSolver_Constraint::getAttributes(theValue, theAttributes);
47 if (!myErrorMsg.empty() || !theAttributes[2] || !theAttributes[3]) {
48 theAttributes.clear();
52 // Check the quantity of entities of each type and their order (arcs first)
56 bool isSwap = false; // whether need to swap arguments (arc goes before line)
57 std::vector<EntityWrapperPtr>::iterator anEntIt = theAttributes.begin() + 2;
58 for (; anEntIt != theAttributes.end(); ++anEntIt) {
59 if ((*anEntIt)->type() == ENTITY_LINE)
61 else if ((*anEntIt)->type() == ENTITY_ARC) {
63 isSwap = aNbLines > 0;
65 else if ((*anEntIt)->type() == ENTITY_CIRCLE) {
67 isSwap = aNbLines > 0;
71 if (aNbArcs < 1 && aNbCircles < 1) {
72 myErrorMsg = SketchSolver_Error::INCORRECT_TANGENCY_ATTRIBUTE();
77 myType = CONSTRAINT_TANGENT_ARC_LINE;
78 else if (aNbCircles == 1)
79 myType = CONSTRAINT_TANGENT_CIRCLE_LINE;
81 else if (aNbArcs == 2)
82 myType = CONSTRAINT_TANGENT_ARC_ARC;
84 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
88 if (myType == CONSTRAINT_TANGENT_ARC_LINE &&
89 !hasSingleCoincidence(theAttributes[2], theAttributes[3]))
90 myErrorMsg = SketchSolver_Error::TANGENCY_FAILED();
93 EntityWrapperPtr aTemp = theAttributes[2];
94 theAttributes[2] = theAttributes[3];
95 theAttributes[3] = aTemp;
99 void SketchSolver_ConstraintTangent::adjustConstraint()
101 if (myType != CONSTRAINT_TANGENT_CIRCLE_LINE)
104 ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint).front();
105 AttributePtr aCircleCenter = aConstraint->entities().front()->baseAttribute();
108 FeaturePtr aCircle = ModelAPI_Feature::feature(aCircleCenter->owner());
109 AttributeDoublePtr aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
110 aCircle->attribute(SketchPlugin_Circle::RADIUS_ID()));
112 if (fabs(aRadius->value()) == fabs(aConstraint->value()))
115 aConstraint->setValue(aRadius->value());
117 // Adjust the sign of constraint value
118 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
119 aBuilder->adjustConstraint(aConstraint);
120 myStorage->addConstraint(myBaseConstraint, aConstraint);