1 #include <SketchSolver_ConstraintMultiRotation.h>
2 #include <SketchSolver_Group.h>
3 #include <SketchSolver_Error.h>
5 #include <SketchPlugin_Arc.h>
6 #include <SketchPlugin_MultiRotation.h>
8 #include <ModelAPI_AttributeDouble.h>
9 #include <ModelAPI_AttributeInteger.h>
10 #include <ModelAPI_AttributeRefAttr.h>
11 #include <ModelAPI_AttributeRefList.h>
12 #include <ModelAPI_ResultConstruction.h>
14 #include <GeomAPI_Dir2d.h>
15 #include <GeomAPI_XY.h>
19 static double squareDistance(
20 StoragePtr theStorage, const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2)
22 Slvs_Entity aPoint1 = theStorage->getEntity(thePoint1);
23 Slvs_Entity aPoint2 = theStorage->getEntity(thePoint2);
24 double x1 = theStorage->getParameter(aPoint1.param[0]).val;
25 double y1 = theStorage->getParameter(aPoint1.param[1]).val;
26 double x2 = theStorage->getParameter(aPoint2.param[0]).val;
27 double y2 = theStorage->getParameter(aPoint2.param[1]).val;
28 return (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2);
31 void SketchSolver_ConstraintMultiRotation::getAttributes(
32 Slvs_hEntity& theCenter, double& theAngle,
33 std::vector< std::vector<Slvs_hEntity> >& thePoints,
34 std::vector< std::vector<Slvs_hEntity> >& theEntities)
36 DataPtr aData = myBaseConstraint->data();
37 theAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
38 aData->attribute(SketchPlugin_MultiRotation::ANGLE_ID()))->value();
40 AttributePtr aCenterAttr = aData->attribute(SketchPlugin_MultiRotation::CENTER_ID());
41 if (!aCenterAttr || !aCenterAttr->isInitialized()) {
42 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
45 int aType = SLVS_E_UNKNOWN; // type of created entity
46 Slvs_hEntity anEntityID = myGroup->getAttributeId(aCenterAttr);
47 if (anEntityID == SLVS_E_UNKNOWN)
48 anEntityID = changeEntity(aCenterAttr, aType);
49 theCenter = anEntityID;
51 // Lists of objects and number of copies
52 AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
53 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
54 myNumberOfObjects = anInitialRefList->size();
55 myNumberOfCopies = (size_t) aData->integer(SketchPlugin_MultiRotation::NUMBER_OF_COPIES_ID())->value();
56 AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
57 myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
59 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
63 // Obtain all points of initial features and store them into separate lists
64 // containing their translated copies.
65 // Also all circles and arc collected too, because they will be constrained by equal radii.
67 ResultConstructionPtr aRC;
68 static const size_t MAX_POINTS = 3;
69 std::vector<Slvs_hEntity> aPoints[MAX_POINTS]; // lists of points of features
70 std::vector<Slvs_hEntity> anEntities;
71 std::list<ObjectPtr> anObjectList = aRefList->list();
72 std::list<ObjectPtr>::iterator anObjectIter = anObjectList.begin();
73 while (anObjectIter != anObjectList.end()) {
74 for (size_t i = 0; i < MAX_POINTS; ++i)
78 for (size_t i = 0; i <= myNumberOfCopies && anObjectIter != anObjectList.end(); i++, anObjectIter++) {
79 aFeature = ModelAPI_Feature::feature(*anObjectIter);
82 anEntityID = changeEntity(aFeature, aType);
83 anEntities.push_back(anEntityID);
84 Slvs_Entity anEntity = myStorage->getEntity(anEntityID);
86 case SLVS_E_POINT_IN_2D:
87 case SLVS_E_POINT_IN_3D:
88 aPoints[0].push_back(anEntityID);
90 case SLVS_E_LINE_SEGMENT:
91 aPoints[0].push_back(anEntity.point[0]); // start point of line
92 aPoints[1].push_back(anEntity.point[1]); // end point of line
95 aPoints[0].push_back(anEntity.point[0]); // center of circle
97 case SLVS_E_ARC_OF_CIRCLE:
98 aPoints[0].push_back(anEntity.point[0]); // center of arc
99 aPoints[1].push_back(anEntity.point[1]); // start point of arc
100 aPoints[2].push_back(anEntity.point[2]); // end point of arc
103 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
108 for (size_t i = 0; i < MAX_POINTS; ++i)
109 if (!aPoints[i].empty())
110 thePoints.push_back(aPoints[i]);
111 if (!anEntities.empty())
112 theEntities.push_back(anEntities);
116 void SketchSolver_ConstraintMultiRotation::process()
119 if (!myBaseConstraint || !myStorage || myGroup == 0) {
120 /// TODO: Put error message here
123 if (!mySlvsConstraints.empty()) // some data is changed, update constraint
124 update(myBaseConstraint);
126 std::vector<std::vector<Slvs_hEntity> > anEntitiesAndCopies;
127 getAttributes(myRotationCenter, myAngle, myPointsAndCopies, anEntitiesAndCopies);
128 if (!myErrorMsg.empty())
131 // Set the rotation center unchanged during constraint recalculation
132 Slvs_Constraint aConstraint;
133 if (!myStorage->isPointFixed(myRotationCenter, aConstraint.h, true)) {
134 aConstraint = Slvs_MakeConstraint(
135 SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_WHERE_DRAGGED, myGroup->getWorkplaneId(), 0.0,
136 myRotationCenter, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
137 aConstraint.h = myStorage->addConstraint(aConstraint);
138 mySlvsConstraints.push_back(aConstraint.h);
141 // Keep all objects unchanged (only initial object may be changed by user)
142 myCircsAndCopies.clear();
143 std::vector<std::vector<Slvs_hEntity> >::const_iterator anEntIt = anEntitiesAndCopies.begin();
144 std::vector<Slvs_hEntity>::const_iterator aCpIt;
145 for (; anEntIt != anEntitiesAndCopies.end(); ++anEntIt) {
146 std::vector<Slvs_hEntity> aCircs;
147 aCpIt = anEntIt->begin();
148 // Obtain initial points
149 Slvs_Entity anInitial = myStorage->getEntity(*aCpIt);
150 if (anInitial.type == SLVS_E_POINT_IN_2D || anInitial.type == SLVS_E_POINT_IN_3D)
151 myInitialPoints.insert(anInitial.h);
153 for (int i = 0; i < 4 && anInitial.point[i] != SLVS_E_UNKNOWN; i++)
154 myInitialPoints.insert(anInitial.point[i]);
158 for (++aCpIt; aCpIt != anEntIt->end(); ++aCpIt) {
159 const Slvs_Entity& anEntity = myStorage->getEntity(*aCpIt);
160 std::vector<Slvs_hConstraint> aNewConstr;
161 if (anEntity.type == SLVS_E_CIRCLE) {
162 aCircs.push_back(anEntity.distance);
163 // for circles we fix only center
164 aNewConstr = myStorage->fixEntity(anEntity.point[0]);
166 aNewConstr = myStorage->fixEntity(*aCpIt);
167 mySlvsConstraints.insert(mySlvsConstraints.end(), aNewConstr.begin(), aNewConstr.end());
171 myCircsAndCopies.push_back(aCircs);
177 void SketchSolver_ConstraintMultiRotation::update(ConstraintPtr theConstraint)
180 if (!theConstraint || theConstraint == myBaseConstraint) {
181 AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
182 myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
183 AttributeIntegerPtr aNbCopies = myBaseConstraint->integer(SketchPlugin_MultiRotation::NUMBER_OF_COPIES_ID());
184 if (anInitialRefList->size() != myNumberOfObjects || aNbCopies->value() != myNumberOfCopies) {
185 remove(myBaseConstraint);
191 // update angle value
192 myAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
193 myBaseConstraint->attribute(SketchPlugin_MultiRotation::ANGLE_ID()))->value();
195 SketchSolver_Constraint::update();
198 bool SketchSolver_ConstraintMultiRotation::remove(ConstraintPtr theConstraint)
201 if (theConstraint && theConstraint != myBaseConstraint)
203 bool isFullyRemoved = true;
204 std::vector<Slvs_hEntity>::iterator aCIter = mySlvsConstraints.begin();
205 for (; aCIter != mySlvsConstraints.end(); aCIter++)
206 isFullyRemoved = myStorage->removeConstraint(*aCIter) && isFullyRemoved;
207 mySlvsConstraints.clear();
209 std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIt = myFeatureMap.begin();
210 for (; aFeatIt != myFeatureMap.end(); aFeatIt++)
211 myStorage->removeEntity(aFeatIt->second);
212 myStorage->removeUnusedEntities();
214 std::map<FeaturePtr, Slvs_hEntity> aFeatureMapCopy = myFeatureMap;
216 if (isFullyRemoved) {
217 myFeatureMap.clear();
218 myAttributeMap.clear();
221 cleanRemovedEntities();
223 // Restore initial features
224 std::map<FeaturePtr, Slvs_hEntity>::iterator aFIter = aFeatureMapCopy.begin();
225 for (; aFIter != aFeatureMapCopy.end(); ++aFIter)
227 if (myFeatureMap.find(aFIter->first) != myFeatureMap.end())
228 continue; // the feature was not removed
229 Slvs_hEntity anEntity = myGroup->getFeatureId(aFIter->first);
230 if (anEntity != SLVS_E_UNKNOWN)
231 myFeatureMap[aFIter->first] = anEntity;
234 // Clear list of rotated points
235 myPointsAndCopies.clear();
236 myInitialPoints.clear();
241 void SketchSolver_ConstraintMultiRotation::addFeature(FeaturePtr theFeature)
243 SketchSolver_Constraint::addFeature(theFeature);
245 std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIt = myFeatureMap.find(theFeature);
246 if (aFeatIt == myFeatureMap.end())
249 // store list of points of the feature
250 const Slvs_Entity& theEntity = myStorage->getEntity(aFeatIt->second);
251 for (int i = 0; i < 4; i++)
252 if (theEntity.point[i] != SLVS_E_UNKNOWN)
253 myPointsJustUpdated.insert(theEntity.point[i]);
256 void SketchSolver_ConstraintMultiRotation::adjustConstraint()
258 if (abs(myAngle) < tolerance) {
259 myStorage->setNeedToResolve(false);
263 std::list<Slvs_Constraint> aCoincident = myStorage->getConstraintsByType(SLVS_C_POINTS_COINCIDENT);
264 std::list<Slvs_Constraint>::const_iterator aCoIt;
266 // Check overconstrained on rotation center (if it is coincident with other fixed point)
267 Slvs_hConstraint aFixedCenter;
268 if (myStorage->isPointFixed(myRotationCenter, aFixedCenter, false)) {
269 Slvs_hConstraint aFixed;
270 for (aCoIt = aCoincident.begin(); aCoIt != aCoincident.end(); ++aCoIt)
271 if ((aCoIt->ptA == myRotationCenter && myStorage->isPointFixed(aCoIt->ptB, aFixed, true)) ||
272 (aCoIt->ptB == myRotationCenter && myStorage->isPointFixed(aCoIt->ptA, aFixed, true))) {
274 myStorage->removeConstraint(aFixedCenter);
275 std::vector<Slvs_hConstraint>::iterator aSCIt = mySlvsConstraints.begin();
276 for (; aSCIt != mySlvsConstraints.end(); ++aSCIt)
277 if (*aSCIt == aFixedCenter) {
278 mySlvsConstraints.erase(aSCIt);
284 // Obtain coordinates of rotation center
285 Slvs_Entity aRotCenter = myStorage->getEntity(myRotationCenter);
287 for (int i = 0; i < 2; i++)
288 aCenterXY[i] = myStorage->getParameter(aRotCenter.param[i]).val;
290 double cosA = cos(myAngle * PI / 180.0);
291 double sinA = sin(myAngle * PI / 180.0);
293 double aVec[2]; // coordinates of vector defining a direction from rotation center to a point
295 // Update positions of all points to satisfy angles
296 std::vector< std::vector<Slvs_hEntity> >::const_iterator aPointsIter = myPointsAndCopies.begin();
297 std::vector<Slvs_hEntity>::const_iterator aCopyIter;
298 for (; aPointsIter != myPointsAndCopies.end(); ++aPointsIter) {
299 aCopyIter = aPointsIter->begin();
300 const Slvs_Entity& anInitial = myStorage->getEntity(*aCopyIter);
301 for (int i = 0; i < 2; i++)
302 aVec[i] = myStorage->getParameter(anInitial.param[i]).val - aCenterXY[i];
304 // if the point is coincident with another one which is temporary fixed (moved by user),
305 // we will update its position correspondingly
306 Slvs_hConstraint aFixed;
307 for (aCoIt = aCoincident.begin(); aCoIt != aCoincident.end(); ++aCoIt) {
308 if ((aCoIt->ptA == anInitial.h && myInitialPoints.find(aCoIt->ptB) != myInitialPoints.end()) ||
309 (aCoIt->ptB == anInitial.h && myInitialPoints.find(aCoIt->ptA) != myInitialPoints.end())) {
310 Slvs_hEntity anOtherId = aCoIt->ptA == anInitial.h ? aCoIt->ptB : aCoIt->ptA;
311 if (!myStorage->isTemporary(aFixed) &&
312 myPointsJustUpdated.find(anOtherId) == myPointsJustUpdated.end())
313 continue; // nothing to change
315 const Slvs_Entity& anOtherPnt = myStorage->getEntity(anOtherId);
316 for (int i = 0; i < 2; i++) {
317 Slvs_Param anInitParam = myStorage->getParameter(anInitial.param[i]);
318 const Slvs_Param& anOtherParam = myStorage->getParameter(anOtherPnt.param[i]);
319 anInitParam.val = anOtherParam.val;
320 myStorage->updateParameter(anInitParam);
321 aVec[i] = anOtherParam.val - aCenterXY[i];
326 // update copied points
327 aCopyIter = aPointsIter->begin();
328 for (++aCopyIter; aCopyIter != aPointsIter->end(); ++aCopyIter) {
330 double aTemp = aVec[0] * cosA - aVec[1] * sinA;
331 aVec[1] = aVec[0] * sinA + aVec[1] * cosA;
334 const Slvs_Entity& aTarget = myStorage->getEntity(*aCopyIter);
335 for (int i = 0; i < 2; i++) {
336 Slvs_Param aParam = myStorage->getParameter(aTarget.param[i]);
337 aParam.val = aCenterXY[i] + aVec[i];
338 myStorage->updateParameter(aParam);
343 for (aPointsIter = myCircsAndCopies.begin(); aPointsIter != myCircsAndCopies.end(); ++aPointsIter) {
344 aCopyIter = aPointsIter->begin();
345 const Slvs_Entity& anInitial = myStorage->getEntity(*aCopyIter);
346 const Slvs_Param& anInitRad = myStorage->getParameter(anInitial.param[0]);
347 for (++aCopyIter; aCopyIter != aPointsIter->end(); ++aCopyIter) {
348 const Slvs_Entity& aCopy = myStorage->getEntity(*aCopyIter);
349 Slvs_Param aCopyRad = myStorage->getParameter(aCopy.param[0]);
350 aCopyRad.val = anInitRad.val;
351 myStorage->updateParameter(aCopyRad);
355 myPointsJustUpdated.clear();