Salome HOME
6f05e9a53ad43150f4543638dc83e808fbf35264
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMulti.cpp
1 #include <SketchSolver_ConstraintMulti.h>
2 #include <SketchSolver_Error.h>
3 #include <SketchSolver_Manager.h>
4
5 #include <GeomDataAPI_Point2D.h>
6 #include <ModelAPI_AttributeInteger.h>
7 #include <ModelAPI_AttributeRefAttr.h>
8 #include <ModelAPI_AttributeRefList.h>
9 #include <SketchPlugin_Arc.h>
10 #include <SketchPlugin_Circle.h>
11 #include <SketchPlugin_Line.h>
12 #include <SketchPlugin_Point.h>
13 #include <SketchPlugin_IntersectionPoint.h>
14
15 void SketchSolver_ConstraintMulti::getEntities(std::list<EntityWrapperPtr>& theEntities)
16 {
17   myAdjusted = false;
18   DataPtr aData = myBaseConstraint->data();
19
20   // Lists of objects and number of copies
21   AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
22       aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
23   myNumberOfObjects = anInitialRefList->size();
24   myNumberOfCopies = aData->integer(nameNbObjects())->value() - 1;
25   if (myNumberOfCopies <= 0)
26     return;
27
28   AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
29       aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
30   if (!aRefList || aRefList->size() == 0) {
31     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
32     return;
33   }
34
35   FeaturePtr aFeature;
36   std::list<ObjectPtr> anObjectList = aRefList->list();
37   std::list<ObjectPtr>::iterator anObjIt = anObjectList.begin();
38   if ((myNumberOfCopies + 1) * myNumberOfObjects != aRefList->size()) // execute for the feature is not called yet
39     myNumberOfCopies = aRefList->size() / myNumberOfObjects - 1;
40
41   while (anObjIt != anObjectList.end()) {
42     aFeature = ModelAPI_Feature::feature(*anObjIt++);
43     if (!aFeature)
44       continue;
45
46     myStorage->update(aFeature);
47     theEntities.push_back(myStorage->entity(aFeature));
48     myFeatures.insert(aFeature);
49     for (int i = 0; i < myNumberOfCopies && anObjIt != anObjectList.end(); ++i, ++anObjIt) {
50       // just add copied features into the list of objects
51       aFeature = ModelAPI_Feature::feature(*anObjIt);
52       if (aFeature)
53         myFeatures.insert(aFeature);
54     }
55   }
56 }
57
58 bool SketchSolver_ConstraintMulti::remove()
59 {
60   myFeatures.clear();
61   return SketchSolver_Constraint::remove();
62 }
63
64 void SketchSolver_ConstraintMulti::update()
65 {
66   update(false);
67 }
68
69 void SketchSolver_ConstraintMulti::update(bool isForce)
70 {
71   cleanErrorMsg();
72   AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
73       myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
74   AttributeIntegerPtr aNbObjects = myBaseConstraint->integer(nameNbObjects());
75   bool isUpdated= anInitialRefList->size() != myNumberOfObjects || aNbObjects->value()-1 != myNumberOfCopies;
76   if (!isUpdated) {
77     // additional check that the features and their copies are changed
78     AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
79         myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
80     if (aRefList && aRefList->size() != 0) {
81       FeaturePtr aFeature;
82       std::list<ObjectPtr> anObjectList = aRefList->list();
83       std::list<ObjectPtr>::iterator anObjIt = anObjectList.begin();
84       for (; anObjIt != anObjectList.end(); ++anObjIt) {
85         aFeature = ModelAPI_Feature::feature(*anObjIt);
86         if (aFeature && myFeatures.find(aFeature) == myFeatures.end()) {
87           isUpdated = true;
88           break;
89         }
90       }
91     } else
92       isUpdated = true;
93   }
94   if (isUpdated) {
95     remove();
96     process();
97     return;
98   }
99
100   // update derivative object
101   updateLocal();
102   if (isForce)
103     myAdjusted = false;
104   adjustConstraint();
105 }
106
107 void SketchSolver_ConstraintMulti::adjustConstraint()
108 {
109   AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
110       myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
111   if (!aRefList || aRefList->size() == 0) {
112     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
113     return;
114   }
115
116   FeaturePtr aFeature;
117   std::list<ObjectPtr> anObjectList = aRefList->list();
118   std::list<ObjectPtr>::iterator anObjIt = anObjectList.begin();
119   while (anObjIt != anObjectList.end()) {
120     aFeature = ModelAPI_Feature::feature(*anObjIt++);
121     if (!aFeature)
122       continue;
123
124     // Fill lists of coordinates of points composing a feature
125     std::list<double> aX, aY;
126     std::list<double>::iterator aXIt, aYIt;
127     double aXCoord, aYCoord;
128     EntityWrapperPtr anEntity = myStorage->entity(aFeature);
129     std::list<EntityWrapperPtr> aSubs = anEntity->subEntities();
130     std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
131     for (; aSIt != aSubs.end(); ++aSIt) {
132       if ((*aSIt)->type() != ENTITY_POINT)
133         continue;
134       std::list<ParameterWrapperPtr> aParameters = (*aSIt)->parameters();
135       aXCoord = aParameters.front()->value();
136       aYCoord = aParameters.back()->value();
137       getRelative(aXCoord, aYCoord, aXCoord, aYCoord);
138       aX.push_back(aXCoord);
139       aY.push_back(aYCoord);
140     }
141
142     // Calculate positions of copied features
143     for (int i = 0; i < myNumberOfCopies && anObjIt != anObjectList.end(); ++i, ++anObjIt) {
144       aFeature = ModelAPI_Feature::feature(*anObjIt);
145       if (!aFeature)
146         continue;
147       anEntity = myStorage->entity(aFeature);
148
149       if (!anEntity || !myStorage->isEventsBlocked())
150         aFeature->data()->blockSendAttributeUpdated(true);
151
152       std::list<AttributePtr> aPoints;
153       if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
154         aPoints.push_back(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
155         aPoints.push_back(aFeature->attribute(SketchPlugin_Arc::START_ID()));
156         aPoints.push_back(aFeature->attribute(SketchPlugin_Arc::END_ID()));
157       } else if (aFeature->getKind() == SketchPlugin_Line::ID()) {
158         aPoints.push_back(aFeature->attribute(SketchPlugin_Line::START_ID()));
159         aPoints.push_back(aFeature->attribute(SketchPlugin_Line::END_ID()));
160       } else if (aFeature->getKind() == SketchPlugin_Circle::ID())
161         aPoints.push_back(aFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
162       else if (aFeature->getKind() == SketchPlugin_Point::ID() ||
163                aFeature->getKind() == SketchPlugin_IntersectionPoint::ID())
164         aPoints.push_back(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
165
166       std::list<AttributePtr>::iterator aPtIt = aPoints.begin();
167       for (aXIt = aX.begin(), aYIt = aY.begin(); aPtIt != aPoints.end(); ++aXIt, ++aYIt, ++aPtIt) {
168         transformRelative(*aXIt, *aYIt);
169         getAbsolute(*aXIt, *aYIt, aXCoord, aYCoord);
170
171         std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
172             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aPtIt);
173         aPoint2D->setValue(aXCoord, aYCoord);
174       }
175
176       // update feature in the storage if it is used by another constraints
177       if (anEntity)
178         myStorage->update(aFeature);
179
180       if (!anEntity || !myStorage->isEventsBlocked())
181         aFeature->data()->blockSendAttributeUpdated(false);
182     }
183   }
184
185   myAdjusted = true;
186 }
187
188 bool SketchSolver_ConstraintMulti::isUsed(FeaturePtr theFeature) const
189 {
190   return myFeatures.find(theFeature) != myFeatures.end() ||
191          SketchSolver_Constraint::isUsed(theFeature);
192 }