]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintTangent.cpp
Salome HOME
Update constraint Tangent to process non-connected features. Improve corresponding...
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintTangent.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include <SketchSolver_ConstraintTangent.h>
4 #include <SketchSolver_Error.h>
5
6 #include <PlaneGCSSolver_Tools.h>
7
8 #include <GeomAPI_Pnt2d.h>
9 #include <SketchPlugin_Circle.h>
10 #include <SketchPlugin_ConstraintCoincidence.h>
11
12 #include <cmath>
13
14
15 /// \brief Check whether the entities has only one shared point or less
16 static bool hasSingleCoincidence(FeaturePtr theFeature1, FeaturePtr theFeature2)
17 {
18   const std::set<AttributePtr>& aRefs1 = theFeature1->data()->refsToMe();
19   const std::set<AttributePtr>& aRefs2 = theFeature2->data()->refsToMe();
20
21   // collect all shared coincidendes
22   std::set<FeaturePtr> aCoincidences;
23   std::set<AttributePtr>::const_iterator anIt;
24   for (anIt = aRefs1.begin(); anIt != aRefs1.end(); ++anIt) {
25     FeaturePtr aRef = ModelAPI_Feature::feature((*anIt)->owner());
26     if (aRef && aRef->getKind() == SketchPlugin_ConstraintCoincidence::ID())
27       aCoincidences.insert(aRef);
28   }
29   for (anIt = aRefs2.begin(); anIt != aRefs2.end(); ++anIt) {
30     FeaturePtr aRef = ModelAPI_Feature::feature((*anIt)->owner());
31     if (aRef)
32       aCoincidences.erase(aRef);
33   }
34
35   return aCoincidences.size() <= 1;
36 }
37
38 void SketchSolver_ConstraintTangent::getAttributes(
39     EntityWrapperPtr& theValue,
40     std::vector<EntityWrapperPtr>& theAttributes)
41 {
42   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
43   if (!myErrorMsg.empty() || !theAttributes[2] || !theAttributes[3]) {
44     theAttributes.clear();
45     return;
46   }
47
48   // Check the quantity of entities of each type and their order (arcs first)
49   int aNbLines = 0;
50   int aNbCircles = 0;
51   bool isSwap = false; // whether need to swap arguments (arc goes before line)
52   std::vector<EntityWrapperPtr>::iterator anEntIt = theAttributes.begin() + 2;
53   for (; anEntIt != theAttributes.end(); ++anEntIt) {
54     if ((*anEntIt)->type() == ENTITY_LINE)
55       ++aNbLines;
56     else if ((*anEntIt)->type() == ENTITY_ARC || (*anEntIt)->type() == ENTITY_CIRCLE) {
57       ++aNbCircles;
58       isSwap = aNbLines > 0;
59     }
60   }
61
62   if (aNbCircles < 1) {
63     myErrorMsg = SketchSolver_Error::INCORRECT_TANGENCY_ATTRIBUTE();
64     return;
65   }
66   if (aNbLines == 1 && aNbCircles == 1) {
67     myType = CONSTRAINT_TANGENT_CIRCLE_LINE;
68   }
69   else if (aNbCircles == 2) {
70     myType = CONSTRAINT_TANGENT_CIRCLE_CIRCLE;
71     isArcArcInternal =
72         PlaneGCSSolver_Tools::isArcArcTangencyInternal(theAttributes[2], theAttributes[3]);
73   }
74   else {
75     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
76     return;
77   }
78
79   if (myType == CONSTRAINT_TANGENT_CIRCLE_LINE) {
80     AttributeRefAttrPtr aRefAttr = myBaseConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
81     FeaturePtr aFeature1 = ModelAPI_Feature::feature(aRefAttr->object());
82     aRefAttr = myBaseConstraint->refattr(SketchPlugin_Constraint::ENTITY_B());
83     FeaturePtr aFeature2 = ModelAPI_Feature::feature(aRefAttr->object());
84
85     if (!hasSingleCoincidence(aFeature1, aFeature2))
86       myErrorMsg = SketchSolver_Error::TANGENCY_FAILED();
87   }
88
89   if (isSwap) {
90     EntityWrapperPtr aTemp = theAttributes[2];
91     theAttributes[2] = theAttributes[3];
92     theAttributes[3] = aTemp;
93   }
94 }
95
96 void SketchSolver_ConstraintTangent::adjustConstraint()
97 {
98   if (myType == CONSTRAINT_TANGENT_CIRCLE_CIRCLE) {
99     EntityWrapperPtr anEntity1 =
100         myStorage->entity(myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
101     EntityWrapperPtr anEntity2 =
102         myStorage->entity(myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
103
104     if (isArcArcInternal != PlaneGCSSolver_Tools::isArcArcTangencyInternal(anEntity1, anEntity2)) {
105       // fully rebuld constraint, because it is unable to access attributes of PlaneGCS constraint
106       remove();
107       process();
108     }
109   }
110 }