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)
41 /// \brief Check if two connected arcs have centers
42 /// in same direction relatively to connection point
43 static bool isInternalTangency(EntityWrapperPtr theEntity1, EntityWrapperPtr theEntity2)
45 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
46 return aBuilder->isArcArcTangencyInternal(theEntity1, theEntity2);
50 void SketchSolver_ConstraintTangent::getAttributes(
52 std::vector<EntityWrapperPtr>& theAttributes)
54 SketchSolver_Constraint::getAttributes(theValue, theAttributes);
55 if (!myErrorMsg.empty() || !theAttributes[2] || !theAttributes[3]) {
56 theAttributes.clear();
60 // Check the quantity of entities of each type and their order (arcs first)
64 bool isSwap = false; // whether need to swap arguments (arc goes before line)
65 std::vector<EntityWrapperPtr>::iterator anEntIt = theAttributes.begin() + 2;
66 for (; anEntIt != theAttributes.end(); ++anEntIt) {
67 if ((*anEntIt)->type() == ENTITY_LINE)
69 else if ((*anEntIt)->type() == ENTITY_ARC) {
71 isSwap = aNbLines > 0;
73 else if ((*anEntIt)->type() == ENTITY_CIRCLE) {
75 isSwap = aNbLines > 0;
79 if (aNbArcs < 1 && aNbCircles < 1) {
80 myErrorMsg = SketchSolver_Error::INCORRECT_TANGENCY_ATTRIBUTE();
85 myType = CONSTRAINT_TANGENT_ARC_LINE;
86 else if (aNbCircles == 1)
87 myType = CONSTRAINT_TANGENT_CIRCLE_LINE;
89 else if (aNbArcs == 2) {
90 myType = CONSTRAINT_TANGENT_ARC_ARC;
91 isArcArcInternal = isInternalTangency(theAttributes[2], theAttributes[3]);
94 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
98 if (myType == CONSTRAINT_TANGENT_ARC_LINE &&
99 !hasSingleCoincidence(theAttributes[2], theAttributes[3]))
100 myErrorMsg = SketchSolver_Error::TANGENCY_FAILED();
103 EntityWrapperPtr aTemp = theAttributes[2];
104 theAttributes[2] = theAttributes[3];
105 theAttributes[3] = aTemp;
109 void SketchSolver_ConstraintTangent::adjustConstraint()
111 if (myType == CONSTRAINT_TANGENT_CIRCLE_LINE) {
112 ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint).front();
113 AttributePtr aCircleCenter = aConstraint->entities().front()->baseAttribute();
116 FeaturePtr aCircle = ModelAPI_Feature::feature(aCircleCenter->owner());
117 AttributeDoublePtr aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
118 aCircle->attribute(SketchPlugin_Circle::RADIUS_ID()));
120 if (fabs(aRadius->value()) == fabs(aConstraint->value()))
123 aConstraint->setValue(aRadius->value());
125 // Adjust the sign of constraint value
126 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
127 aBuilder->adjustConstraint(aConstraint);
128 myStorage->addConstraint(myBaseConstraint, aConstraint);
130 else if (myType == CONSTRAINT_TANGENT_ARC_ARC) {
131 ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint).front();
132 if (isArcArcInternal != isInternalTangency(
133 aConstraint->entities().front(), aConstraint->entities().back())) {
134 // fully rebuld constraint, because it is unable to access attributes of PlaneGCS constraint