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 void SketchSolver_ConstraintMultiRotation::getAttributes(
20 Slvs_hEntity& theCenter, double& theAngle,
21 std::vector< std::vector<Slvs_hEntity> >& thePoints,
22 std::vector< std::vector<Slvs_hEntity> >& theEntities)
24 DataPtr aData = myBaseConstraint->data();
25 theAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
26 aData->attribute(SketchPlugin_MultiRotation::ANGLE_ID()))->value();
28 AttributePtr aCenterAttr = aData->attribute(SketchPlugin_MultiRotation::CENTER_ID());
29 if (!aCenterAttr || !aCenterAttr->isInitialized()) {
30 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
33 int aType = SLVS_E_UNKNOWN; // type of created entity
34 Slvs_hEntity anEntityID = myGroup->getAttributeId(aCenterAttr);
35 if (anEntityID == SLVS_E_UNKNOWN)
36 anEntityID = changeEntity(aCenterAttr, aType);
37 theCenter = anEntityID;
39 // Lists of objects and number of copies
40 AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
41 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
42 myNumberOfObjects = anInitialRefList->size();
43 myNumberOfCopies = (size_t) aData->integer(SketchPlugin_MultiRotation::NUMBER_OF_COPIES_ID())->value();
44 AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
45 myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
47 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
51 // Obtain all points of initial features and store them into separate lists
52 // containing their translated copies.
53 // Also all circles and arc collected too, because they will be constrained by equal radii.
55 ResultConstructionPtr aRC;
56 static const size_t MAX_POINTS = 3;
57 std::vector<Slvs_hEntity> aPoints[MAX_POINTS]; // lists of points of features
58 std::vector<Slvs_hEntity> anEntities;
59 std::list<ObjectPtr> anObjectList = aRefList->list();
60 std::list<ObjectPtr>::iterator anObjectIter = anObjectList.begin();
61 while (anObjectIter != anObjectList.end()) {
62 for (size_t i = 0; i < MAX_POINTS; ++i)
66 for (size_t i = 0; i <= myNumberOfCopies && anObjectIter != anObjectList.end(); i++, anObjectIter++) {
67 aFeature = ModelAPI_Feature::feature(*anObjectIter);
70 anEntityID = changeEntity(aFeature, aType);
71 anEntities.push_back(anEntityID);
72 Slvs_Entity anEntity = myStorage->getEntity(anEntityID);
74 case SLVS_E_POINT_IN_2D:
75 case SLVS_E_POINT_IN_3D:
76 aPoints[0].push_back(anEntityID);
78 case SLVS_E_LINE_SEGMENT:
79 aPoints[0].push_back(anEntity.point[0]); // start point of line
80 aPoints[1].push_back(anEntity.point[1]); // end point of line
83 aPoints[0].push_back(anEntity.point[0]); // center of circle
85 case SLVS_E_ARC_OF_CIRCLE:
86 aPoints[0].push_back(anEntity.point[0]); // center of arc
87 aPoints[1].push_back(anEntity.point[1]); // start point of arc
88 aPoints[2].push_back(anEntity.point[2]); // end point of arc
91 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
96 for (size_t i = 0; i < MAX_POINTS; ++i)
97 if (!aPoints[i].empty())
98 thePoints.push_back(aPoints[i]);
99 if (!anEntities.empty())
100 theEntities.push_back(anEntities);
104 void SketchSolver_ConstraintMultiRotation::process()
107 if (!myBaseConstraint || !myStorage || myGroup == 0) {
108 /// TODO: Put error message here
111 if (!mySlvsConstraints.empty()) // some data is changed, update constraint
112 update(myBaseConstraint);
114 std::vector<std::vector<Slvs_hEntity> > anEntitiesAndCopies;
115 getAttributes(myRotationCenter, myAngle, myPointsAndCopies, anEntitiesAndCopies);
116 if (!myErrorMsg.empty())
119 // Set the rotation center unchanged during constraint recalculation
120 Slvs_Constraint aConstraint;
121 if (!myStorage->isPointFixed(myRotationCenter, aConstraint.h, true)) {
122 aConstraint = Slvs_MakeConstraint(
123 SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_WHERE_DRAGGED, myGroup->getWorkplaneId(), 0.0,
124 myRotationCenter, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
125 aConstraint.h = myStorage->addConstraint(aConstraint);
126 mySlvsConstraints.push_back(aConstraint.h);
130 processEntities(anEntitiesAndCopies);
134 void SketchSolver_ConstraintMultiRotation::updateLocal()
136 double aValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
137 myBaseConstraint->attribute(SketchPlugin_MultiRotation::ANGLE_ID()))->value();
138 if (fabs(myAngle - aValue) > tolerance)
140 // update angle value
144 void SketchSolver_ConstraintMultiRotation::adjustConstraint()
146 if (fabs(myAngle) < tolerance) {
147 myStorage->setNeedToResolve(false);
153 std::list<Slvs_Constraint> aCoincident = myStorage->getConstraintsByType(SLVS_C_POINTS_COINCIDENT);
154 std::list<Slvs_Constraint>::const_iterator aCoIt;
156 // Check overconstrained on rotation center (if it is coincident with other fixed point)
157 Slvs_hConstraint aFixedCenter;
158 if (myStorage->isPointFixed(myRotationCenter, aFixedCenter, false)) {
159 Slvs_hConstraint aFixed;
160 for (aCoIt = aCoincident.begin(); aCoIt != aCoincident.end(); ++aCoIt)
161 if ((aCoIt->ptA == myRotationCenter && myStorage->isPointFixed(aCoIt->ptB, aFixed, true)) ||
162 (aCoIt->ptB == myRotationCenter && myStorage->isPointFixed(aCoIt->ptA, aFixed, true))) {
164 myStorage->removeConstraint(aFixedCenter);
165 std::vector<Slvs_hConstraint>::iterator aSCIt = mySlvsConstraints.begin();
166 for (; aSCIt != mySlvsConstraints.end(); ++aSCIt)
167 if (*aSCIt == aFixedCenter) {
168 mySlvsConstraints.erase(aSCIt);
174 // Obtain coordinates of rotation center
175 Slvs_Entity aRotCenter = myStorage->getEntity(myRotationCenter);
176 myCenterCoord[0] = myStorage->getParameter(aRotCenter.param[0]).val;
177 myCenterCoord[1] = myStorage->getParameter(aRotCenter.param[1]).val;
179 myRotationVal[0] = sin(myAngle * PI / 180.0);
180 myRotationVal[1] = cos(myAngle * PI / 180.0);
182 SketchSolver_ConstraintMulti::adjustConstraint();
185 void SketchSolver_ConstraintMultiRotation::getRelative(
186 double theAbsX, double theAbsY, double& theRelX, double& theRelY)
188 theRelX = theAbsX - myCenterCoord[0];
189 theRelY = theAbsY - myCenterCoord[1];
192 void SketchSolver_ConstraintMultiRotation::getAbsolute(
193 double theRelX, double theRelY, double& theAbsX, double& theAbsY)
195 theAbsX = theRelX + myCenterCoord[0];
196 theAbsY = theRelY + myCenterCoord[1];
199 void SketchSolver_ConstraintMultiRotation::transformRelative(double& theX, double& theY)
202 // myRotationVal[0] = sinA, myRotationVal[1] = cosA
203 double aTemp = theX * myRotationVal[1] - theY * myRotationVal[0];
204 theY = theX * myRotationVal[0] + theY * myRotationVal[1];
208 const std::string& SketchSolver_ConstraintMultiRotation::nameNbCopies()
210 return SketchPlugin_MultiRotation::NUMBER_OF_COPIES_ID();