Salome HOME
First phase of SketchSolver refactoring
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintCoincidence.cpp
1 #include <SketchSolver_ConstraintCoincidence.h>
2 #include <SketchSolver_Error.h>
3 #include <SketchSolver_Manager.h>
4
5 void SketchSolver_ConstraintCoincidence::getAttributes(
6     double& theValue,
7     std::vector<EntityWrapperPtr>& theAttributes)
8 {
9   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
10   if (!myErrorMsg.empty() || !theAttributes[0]) {
11     theAttributes.clear();
12     return;
13   }
14
15   if (theAttributes[1]) {
16     myType = CONSTRAINT_PT_PT_COINCIDENT;
17     // Set the slave (second) point the same as master (first) point.
18     // This will allow to skip adding point-point coincidence to the set of constraints
19     // and give us speed-up in solving the set of equations
20     myStorage->addCoincidentPoints(theAttributes[0], theAttributes[1]);
21   }
22   else if (theAttributes[2]) {
23     // check the type of entity (line or circle)
24     SketchSolver_EntityType anEntType = theAttributes[2]->type();
25     if (anEntType == ENTITY_LINE)
26       myType = CONSTRAINT_PT_ON_LINE;
27     else if (anEntType == ENTITY_CIRCLE || anEntType == ENTITY_ARC)
28       myType = CONSTRAINT_PT_ON_CIRCLE;
29     else
30       myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
31   } else
32     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
33 }
34
35
36 static bool isBase(const std::list<ConstraintWrapperPtr>& theConstraints, AttributePtr theAttribute)
37 {
38   AttributePtr anAttribute = theAttribute;
39   FeaturePtr aFeature;
40   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
41   if (aRefAttr) {
42     if (aRefAttr->isObject())
43       aFeature = ModelAPI_Feature::feature(aRefAttr->object());
44     else
45       anAttribute = aRefAttr->attr();
46   }
47
48   std::list<ConstraintWrapperPtr>::const_iterator aCIt = theConstraints.begin();
49   for (; aCIt != theConstraints.end(); ++aCIt) {
50     std::list<EntityWrapperPtr> aSubs = (*aCIt)->entities();
51     std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
52     for (; aSIt != aSubs.end(); ++aSIt)
53       if ((aFeature && (*aSIt)->isBase(aFeature)) ||
54          (!aFeature && (*aSIt)->isBase(anAttribute)))
55         return true;
56   }
57   return false;
58 }