Salome HOME
SketchSolver library refactoring
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintCoincidence.cpp
1 #include <SketchSolver_ConstraintCoincidence.h>
2 #include <SketchSolver_Error.h>
3 #include <SketchSolver_Group.h>
4
5 #include <map>
6
7 bool SketchSolver_ConstraintCoincidence::hasConstraint(ConstraintPtr theConstraint) const
8 {
9   if (myBaseConstraint == theConstraint)
10     return true;
11   std::map<ConstraintPtr, Slvs_hConstraint>::const_iterator anIt = myExtraCoincidence.begin();
12   for (; anIt != myExtraCoincidence.end(); anIt++)
13     if (anIt->first == theConstraint)
14       return true;
15   return false;
16 }
17
18 std::list<ConstraintPtr> SketchSolver_ConstraintCoincidence::constraints() const
19 {
20   std::list<ConstraintPtr> aConstraints;
21   aConstraints.push_back(myBaseConstraint);
22   std::map<ConstraintPtr, Slvs_hConstraint>::const_iterator anIt = myExtraCoincidence.begin();
23   for (; anIt != myExtraCoincidence.end(); anIt++)
24     aConstraints.push_back(anIt->first);
25   return aConstraints;
26 }
27
28 bool SketchSolver_ConstraintCoincidence::isCoincide(
29     std::shared_ptr<SketchSolver_ConstraintCoincidence> theConstraint) const
30 {
31   std::map<FeaturePtr, Slvs_hEntity>::const_iterator aFeatIter = myFeatureMap.begin();
32   for (; aFeatIter != myFeatureMap.end(); aFeatIter++)
33     if (theConstraint->myFeatureMap.find(aFeatIter->first) != theConstraint->myFeatureMap.end())
34       return true;
35   std::map<AttributePtr, Slvs_hEntity>::const_iterator anAttrIter = myAttributeMap.begin();
36   for (; anAttrIter != myAttributeMap.end(); anAttrIter++)
37     if (theConstraint->myAttributeMap.find(anAttrIter->first) != theConstraint->myAttributeMap.end())
38       return true;
39   return false;
40 }
41
42 void SketchSolver_ConstraintCoincidence::attach(
43     std::shared_ptr<SketchSolver_ConstraintCoincidence> theConstraint)
44 {
45   cleanErrorMsg();
46   Slvs_Constraint aBaseCoincidence = myStorage->getConstraint(mySlvsConstraints.front());
47   // Remove constraints from theConstraint
48   std::vector<Slvs_hConstraint>::iterator aCIter = theConstraint->mySlvsConstraints.begin();
49   for (; aCIter != theConstraint->mySlvsConstraints.end(); aCIter++)
50     theConstraint->myStorage->removeConstraint(*aCIter);
51
52   if (myStorage == theConstraint->myStorage) {
53     // Clean removed items
54     std::set<Slvs_hParam> aRemParams;
55     std::set<Slvs_hEntity> aRemEnts;
56     std::set<Slvs_hConstraint> aRemConstr;
57     theConstraint->myStorage->getRemoved(aRemParams, aRemEnts, aRemConstr);
58   }
59
60   // Copy data.
61   addConstraint(theConstraint->myBaseConstraint);
62   std::map<ConstraintPtr, Slvs_hConstraint>::iterator aConstrIter =
63       theConstraint->myExtraCoincidence.begin();
64   for (; aConstrIter != theConstraint->myExtraCoincidence.end(); aConstrIter++)
65     addConstraint(aConstrIter->first);
66   // Clear the lists to not remove the entities on destruction
67   theConstraint->mySlvsConstraints.clear();
68   theConstraint->myFeatureMap.clear();
69   theConstraint->myAttributeMap.clear();
70 }
71
72 Slvs_hConstraint SketchSolver_ConstraintCoincidence::addConstraint(
73     Slvs_hEntity thePoint1, Slvs_hEntity thePoint2)
74 {
75   Slvs_Constraint aNewConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(),
76       SLVS_C_POINTS_COINCIDENT, myGroup->getWorkplaneId(), 0.0, thePoint1, thePoint2, 
77       SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
78   Slvs_hConstraint aNewID = myStorage->addConstraint(aNewConstraint);
79   mySlvsConstraints.push_back(aNewID);
80   return aNewID;
81 }
82
83 void SketchSolver_ConstraintCoincidence::addConstraint(ConstraintPtr theConstraint)
84 {
85   std::list<AttributePtr> anAttrList =
86       theConstraint->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
87   std::list<AttributePtr>::iterator anIter = anAttrList.begin();
88   std::vector<Slvs_hEntity> anEntities;
89   for (; anIter != anAttrList.end(); anIter++) {
90     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
91     if (!aRefAttr || aRefAttr->isObject() ||
92         myAttributeMap.find(aRefAttr->attr()) != myAttributeMap.end())
93       continue;
94     int aType;
95     Slvs_hEntity anEntityID = myGroup->getAttributeId(aRefAttr->attr());
96     if (anEntityID == SLVS_E_UNKNOWN)
97       anEntityID = changeEntity(aRefAttr->attr(), aType);
98     anEntities.push_back(anEntityID);
99   }
100
101   Slvs_Constraint aBaseCoincidence = myStorage->getConstraint(mySlvsConstraints.front());
102   Slvs_hConstraint aNewConstr = SLVS_E_UNKNOWN;
103   std::vector<Slvs_hEntity>::iterator anEntIter = anEntities.begin();
104   for (; anEntIter != anEntities.end(); anEntIter++)
105     aNewConstr = addConstraint(aBaseCoincidence.ptA, *anEntIter);
106   myExtraCoincidence[theConstraint] = aNewConstr;
107 }
108
109 bool SketchSolver_ConstraintCoincidence::remove(ConstraintPtr theConstraint)
110 {
111   cleanErrorMsg();
112   ConstraintPtr aConstraint = theConstraint ? theConstraint : myBaseConstraint;
113   int aPos = -1; // position of constraint in the list (-1 for base constraint)
114   if (aConstraint != myBaseConstraint) {
115     std::map<ConstraintPtr, Slvs_hConstraint>::const_iterator anIt = myExtraCoincidence.begin();
116     for (aPos = 0; anIt != myExtraCoincidence.end(); anIt++, aPos++)
117       if (anIt->first == aConstraint)
118         break;
119     if (aPos >= (int)myExtraCoincidence.size())
120       return false; // there is no constraint, which is specified to remove
121   }
122
123   bool isFullyRemoved = myStorage->removeConstraint(mySlvsConstraints[aPos+1]);
124   mySlvsConstraints.erase(mySlvsConstraints.begin() + (1+aPos));
125   cleanRemovedEntities();
126   if (aPos < 0 && !myExtraCoincidence.empty()) {
127     // Need to specify another base coincidence constraint
128     myBaseConstraint = myExtraCoincidence.begin()->first;
129     myExtraCoincidence.erase(myExtraCoincidence.begin());
130     std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
131     Slvs_Constraint aBase = myStorage->getConstraint(*aCIter);
132     for (++aCIter; aCIter != mySlvsConstraints.end(); aCIter++) {
133       Slvs_Constraint aConstr = myStorage->getConstraint(*aCIter);
134       aConstr.ptA = aBase.ptA;
135       myStorage->updateConstraint(aConstr);
136     }
137   }
138   return true;
139 }
140