Salome HOME
Incorrect attribute when create arc (issue #1355)
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintTangent.cpp
1 #include <SketchSolver_ConstraintTangent.h>
2 #include <SketchSolver_Error.h>
3 #include <SketchSolver_Manager.h>
4
5 #include <GeomAPI_Pnt2d.h>
6 #include <SketchPlugin_Circle.h>
7
8 #include <cmath>
9
10
11 /// \brief Check whether the entities has only one shared point
12 static bool hasSingleCoincidence(EntityWrapperPtr theEntity1, EntityWrapperPtr theEntity2)
13 {
14   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
15
16   const std::list<EntityWrapperPtr>& aPoints1 = theEntity1->subEntities();
17   const std::list<EntityWrapperPtr>& aPoints2 = theEntity2->subEntities();
18
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
23
24   int aNbCoinc = 0;
25   std::list<EntityWrapperPtr>::const_iterator anIt1, anIt2;
26   for (anIt1 = aStartIt1; anIt1 != aPoints1.end(); ++anIt1) {
27     if ((*anIt1)->type() != ENTITY_POINT)
28       continue;
29     std::shared_ptr<GeomAPI_Pnt2d> aPt1 = aBuilder->point(*anIt1);
30     for (anIt2 = aStartIt2; anIt2 != aPoints2.end(); ++anIt2) {
31       if ((*anIt2)->type() != ENTITY_POINT)
32         continue;
33       std::shared_ptr<GeomAPI_Pnt2d> aPt2 = aBuilder->point(*anIt2);
34       if (aPt1->distance(aPt2) < tolerance)
35         ++aNbCoinc;
36     }
37   }
38   return aNbCoinc == 1;
39 }
40
41
42 void SketchSolver_ConstraintTangent::getAttributes(
43     double& theValue,
44     std::vector<EntityWrapperPtr>& theAttributes)
45 {
46   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
47   if (!myErrorMsg.empty() || !theAttributes[2] || !theAttributes[3]) {
48     theAttributes.clear();
49     return;
50   }
51
52   // Check the quantity of entities of each type and their order (arcs first)
53   int aNbLines = 0;
54   int aNbArcs = 0;
55   int aNbCircles = 0;
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)
60       ++aNbLines;
61     else if ((*anEntIt)->type() == ENTITY_ARC) {
62       ++aNbArcs;
63       isSwap = aNbLines > 0;
64     }
65     else if ((*anEntIt)->type() == ENTITY_CIRCLE) {
66       ++aNbCircles;
67       isSwap = aNbLines > 0;
68     }
69   }
70
71   if (aNbArcs < 1 && aNbCircles < 1) {
72     myErrorMsg = SketchSolver_Error::INCORRECT_TANGENCY_ATTRIBUTE();
73     return;
74   }
75   if (aNbLines == 1) {
76     if (aNbArcs == 1)
77       myType = CONSTRAINT_TANGENT_ARC_LINE;
78     else if (aNbCircles == 1)
79       myType = CONSTRAINT_TANGENT_CIRCLE_LINE;
80   }
81   else if (aNbArcs == 2)
82     myType = CONSTRAINT_TANGENT_ARC_ARC;
83   else {
84     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
85     return;
86   }
87
88   if (myType == CONSTRAINT_TANGENT_ARC_LINE && 
89       !hasSingleCoincidence(theAttributes[2], theAttributes[3]))
90     myErrorMsg = SketchSolver_Error::TANGENCY_FAILED();
91
92   if (isSwap) {
93     EntityWrapperPtr aTemp = theAttributes[2];
94     theAttributes[2] = theAttributes[3];
95     theAttributes[3] = aTemp;
96   }
97 }
98
99 void SketchSolver_ConstraintTangent::adjustConstraint()
100 {
101   if (myType != CONSTRAINT_TANGENT_CIRCLE_LINE)
102     return;
103
104   ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint).front();
105   AttributePtr aCircleCenter = aConstraint->entities().front()->baseAttribute();
106   if (!aCircleCenter)
107     return;
108   FeaturePtr aCircle = ModelAPI_Feature::feature(aCircleCenter->owner());
109   AttributeDoublePtr aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
110       aCircle->attribute(SketchPlugin_Circle::RADIUS_ID()));
111
112   if (fabs(aRadius->value()) == fabs(aConstraint->value()))
113     return;
114
115   aConstraint->setValue(aRadius->value());
116
117   // Adjust the sign of constraint value
118   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
119   aBuilder->adjustConstraint(aConstraint);
120   myStorage->addConstraint(myBaseConstraint, aConstraint);
121 }