Salome HOME
Issue #1649: Added options to create plane by coincident point;
[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     if (!myStorage->update(aFeature)) // the entity is not created, so it is a copy in "multi" constraint, force its creation
47       myStorage->update(aFeature, myGroupID, true);
48     theEntities.push_back(myStorage->entity(aFeature));
49     myFeatures.insert(aFeature);
50     for (int i = 0; i < myNumberOfCopies && anObjIt != anObjectList.end(); ++i, ++anObjIt) {
51       // just add copied features into the list of objects
52       aFeature = ModelAPI_Feature::feature(*anObjIt);
53       if (aFeature)
54         myFeatures.insert(aFeature);
55     }
56   }
57 }
58
59 bool SketchSolver_ConstraintMulti::remove()
60 {
61   myFeatures.clear();
62   return SketchSolver_Constraint::remove();
63 }
64
65 void SketchSolver_ConstraintMulti::update()
66 {
67   update(false);
68 }
69
70 void SketchSolver_ConstraintMulti::update(bool isForce)
71 {
72   cleanErrorMsg();
73   AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
74       myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
75   AttributeIntegerPtr aNbObjects = myBaseConstraint->integer(nameNbObjects());
76   bool isUpdated= anInitialRefList->size() != myNumberOfObjects || aNbObjects->value()-1 != myNumberOfCopies;
77   if (!isUpdated) {
78     // additional check that the features and their copies are changed
79     AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
80         myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
81     if (aRefList && aRefList->size() != 0) {
82       FeaturePtr aFeature;
83       std::list<ObjectPtr> anObjectList = aRefList->list();
84       std::list<ObjectPtr>::iterator anObjIt = anObjectList.begin();
85       for (; anObjIt != anObjectList.end(); ++anObjIt) {
86         aFeature = ModelAPI_Feature::feature(*anObjIt);
87         if (aFeature && myFeatures.find(aFeature) == myFeatures.end()) {
88           isUpdated = true;
89           break;
90         }
91       }
92     } else
93       isUpdated = true;
94   }
95   if (isUpdated) {
96     remove();
97     process();
98     return;
99   }
100
101   // update derivative object
102   updateLocal();
103   if (isForce)
104     myAdjusted = false;
105   adjustConstraint();
106 }
107
108 void SketchSolver_ConstraintMulti::adjustConstraint()
109 {
110   AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
111       myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
112   if (!aRefList || aRefList->size() == 0) {
113     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
114     return;
115   }
116
117   FeaturePtr anOriginal, aFeature;
118   std::list<ObjectPtr> anObjectList = aRefList->list();
119   std::list<ObjectPtr>::iterator anObjIt = anObjectList.begin();
120   while (anObjIt != anObjectList.end()) {
121     anOriginal = ModelAPI_Feature::feature(*anObjIt++);
122     if (!anOriginal)
123       continue;
124
125     // Fill lists of coordinates of points composing a feature
126     std::list<double> aX, aY;
127     std::list<double>::iterator aXIt, aYIt;
128     double aXCoord, aYCoord;
129     EntityWrapperPtr anEntity = myStorage->entity(anOriginal);
130     std::list<EntityWrapperPtr> aSubs = anEntity->subEntities();
131     std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
132     for (; aSIt != aSubs.end(); ++aSIt) {
133       if ((*aSIt)->type() != ENTITY_POINT)
134         continue;
135       std::list<ParameterWrapperPtr> aParameters = (*aSIt)->parameters();
136       aXCoord = aParameters.front()->value();
137       aYCoord = aParameters.back()->value();
138       getRelative(aXCoord, aYCoord, aXCoord, aYCoord);
139       aX.push_back(aXCoord);
140       aY.push_back(aYCoord);
141     }
142
143     // Calculate positions of copied features
144     for (int i = 0; i < myNumberOfCopies && anObjIt != anObjectList.end(); ++i, ++anObjIt) {
145       aFeature = ModelAPI_Feature::feature(*anObjIt);
146       if (!aFeature)
147         continue;
148       anEntity = myStorage->entity(aFeature);
149
150       if (!anEntity || !myStorage->isEventsBlocked())
151         aFeature->data()->blockSendAttributeUpdated(true);
152
153       std::list<AttributePtr> aPoints;
154       if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
155         aPoints.push_back(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
156         aPoints.push_back(aFeature->attribute(SketchPlugin_Arc::START_ID()));
157         aPoints.push_back(aFeature->attribute(SketchPlugin_Arc::END_ID()));
158       } else if (aFeature->getKind() == SketchPlugin_Line::ID()) {
159         aPoints.push_back(aFeature->attribute(SketchPlugin_Line::START_ID()));
160         aPoints.push_back(aFeature->attribute(SketchPlugin_Line::END_ID()));
161       } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
162         aPoints.push_back(aFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
163         // update circle's radius
164         aFeature->real(SketchPlugin_Circle::RADIUS_ID())->setValue(
165             anOriginal->real(SketchPlugin_Circle::RADIUS_ID())->value());
166       } else if (aFeature->getKind() == SketchPlugin_Point::ID() ||
167                aFeature->getKind() == SketchPlugin_IntersectionPoint::ID())
168         aPoints.push_back(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
169
170       std::list<AttributePtr>::iterator aPtIt = aPoints.begin();
171       for (aXIt = aX.begin(), aYIt = aY.begin(); aPtIt != aPoints.end(); ++aXIt, ++aYIt, ++aPtIt) {
172         transformRelative(*aXIt, *aYIt);
173         getAbsolute(*aXIt, *aYIt, aXCoord, aYCoord);
174
175         std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
176             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aPtIt);
177         aPoint2D->setValue(aXCoord, aYCoord);
178       }
179
180       // update feature in the storage if it is used by another constraints
181       if (anEntity)
182         myStorage->update(aFeature);
183       else { // update attributes, if they exist in the storage
184         for (aPtIt = aPoints.begin(); aPtIt != aPoints.end(); ++aPtIt) {
185           EntityWrapperPtr aPntEnt = myStorage->entity(*aPtIt);
186           if (aPntEnt)
187             myStorage->update(*aPtIt);
188         }
189       }
190
191       if (!anEntity || !myStorage->isEventsBlocked())
192         aFeature->data()->blockSendAttributeUpdated(false);
193     }
194   }
195
196   myAdjusted = true;
197 }
198
199 bool SketchSolver_ConstraintMulti::isUsed(FeaturePtr theFeature) const
200 {
201   return theFeature && (myFeatures.find(theFeature) != myFeatures.end() ||
202          SketchSolver_Constraint::isUsed(theFeature));
203 }
204
205 bool SketchSolver_ConstraintMulti::isUsed(AttributePtr theAttribute) const
206 {
207   AttributePtr anAttribute = theAttribute;
208   AttributeRefAttrPtr aRefAttr =
209       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
210   if (aRefAttr) {
211     if (aRefAttr->isObject())
212       return isUsed(ModelAPI_Feature::feature(aRefAttr->object()));
213     else
214       anAttribute = aRefAttr->attr();
215   }
216   if (!anAttribute)
217     return false;
218
219   FeaturePtr anOwner = ModelAPI_Feature::feature(anAttribute->owner());
220   return myFeatures.find(anOwner) != myFeatures.end();
221 }