1 #include <SketchSolver_ConstraintMulti.h>
2 #include <SketchSolver_Error.h>
3 #include <SketchSolver_Manager.h>
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>
15 void SketchSolver_ConstraintMulti::getEntities(std::list<EntityWrapperPtr>& theEntities)
18 DataPtr aData = myBaseConstraint->data();
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)
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();
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;
41 while (anObjIt != anObjectList.end()) {
42 aFeature = ModelAPI_Feature::feature(*anObjIt++);
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);
54 myFeatures.insert(aFeature);
59 bool SketchSolver_ConstraintMulti::remove()
62 return SketchSolver_Constraint::remove();
65 void SketchSolver_ConstraintMulti::update()
70 void SketchSolver_ConstraintMulti::update(bool isForce)
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;
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) {
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()) {
101 // update derivative object
108 void SketchSolver_ConstraintMulti::adjustConstraint()
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();
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++);
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)
135 AttributePoint2DPtr aPoint =
136 std::dynamic_pointer_cast<GeomDataAPI_Point2D>((*aSIt)->baseAttribute());
138 aXCoord = aPoint->x();
139 aYCoord = aPoint->y();
141 std::list<ParameterWrapperPtr> aParameters = (*aSIt)->parameters();
142 aXCoord = aParameters.front()->value();
143 aYCoord = aParameters.back()->value();
145 getRelative(aXCoord, aYCoord, aXCoord, aYCoord);
146 aX.push_back(aXCoord);
147 aY.push_back(aYCoord);
150 // Calculate positions of copied features
151 for (int i = 0; i < myNumberOfCopies && anObjIt != anObjectList.end(); ++i, ++anObjIt) {
152 aFeature = ModelAPI_Feature::feature(*anObjIt);
155 anEntity = myStorage->entity(aFeature);
157 if (!anEntity || !myStorage->isEventsBlocked())
158 aFeature->data()->blockSendAttributeUpdated(true);
160 std::list<AttributePtr> aPoints;
161 if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
162 aPoints.push_back(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
163 aPoints.push_back(aFeature->attribute(SketchPlugin_Arc::START_ID()));
164 aPoints.push_back(aFeature->attribute(SketchPlugin_Arc::END_ID()));
165 } else if (aFeature->getKind() == SketchPlugin_Line::ID()) {
166 aPoints.push_back(aFeature->attribute(SketchPlugin_Line::START_ID()));
167 aPoints.push_back(aFeature->attribute(SketchPlugin_Line::END_ID()));
168 } else if (aFeature->getKind() == SketchPlugin_Circle::ID()) {
169 aPoints.push_back(aFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
170 // update circle's radius
171 aFeature->real(SketchPlugin_Circle::RADIUS_ID())->setValue(
172 anOriginal->real(SketchPlugin_Circle::RADIUS_ID())->value());
173 } else if (aFeature->getKind() == SketchPlugin_Point::ID() ||
174 aFeature->getKind() == SketchPlugin_IntersectionPoint::ID())
175 aPoints.push_back(aFeature->attribute(SketchPlugin_Point::COORD_ID()));
177 std::list<AttributePtr>::iterator aPtIt = aPoints.begin();
178 for (aXIt = aX.begin(), aYIt = aY.begin(); aPtIt != aPoints.end(); ++aXIt, ++aYIt, ++aPtIt) {
179 transformRelative(*aXIt, *aYIt);
180 getAbsolute(*aXIt, *aYIt, aXCoord, aYCoord);
182 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
183 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aPtIt);
184 aPoint2D->setValue(aXCoord, aYCoord);
187 // update feature in the storage if it is used by another constraints
189 myStorage->update(aFeature);
190 else { // update attributes, if they exist in the storage
191 for (aPtIt = aPoints.begin(); aPtIt != aPoints.end(); ++aPtIt) {
192 EntityWrapperPtr aPntEnt = myStorage->entity(*aPtIt);
194 myStorage->update(*aPtIt);
198 if (!anEntity || !myStorage->isEventsBlocked())
199 aFeature->data()->blockSendAttributeUpdated(false);
206 bool SketchSolver_ConstraintMulti::isUsed(FeaturePtr theFeature) const
208 return theFeature && (myFeatures.find(theFeature) != myFeatures.end() ||
209 SketchSolver_Constraint::isUsed(theFeature));
212 bool SketchSolver_ConstraintMulti::isUsed(AttributePtr theAttribute) const
214 AttributePtr anAttribute = theAttribute;
215 AttributeRefAttrPtr aRefAttr =
216 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
218 if (aRefAttr->isObject())
219 return isUsed(ModelAPI_Feature::feature(aRefAttr->object()));
221 anAttribute = aRefAttr->attr();
226 FeaturePtr anOwner = ModelAPI_Feature::feature(anAttribute->owner());
227 return myFeatures.find(anOwner) != myFeatures.end();