1 #include <SketchSolver_ConstraintMultiRotation.h>
2 #include <SketchSolver_Error.h>
3 #include <SketchSolver_Manager.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 EntityWrapperPtr& theCenter, double& theAngle,
21 std::list< std::list<EntityWrapperPtr> >& theEntities)
23 DataPtr aData = myBaseConstraint->data();
24 theAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
25 aData->attribute(SketchPlugin_MultiRotation::ANGLE_ID()))->value();
27 AttributePtr aCenterAttr = aData->attribute(SketchPlugin_MultiRotation::CENTER_ID());
28 if (!aCenterAttr || !aCenterAttr->isInitialized()) {
29 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
33 myType = CONSTRAINT_MULTI_ROTATION;
35 myStorage->update(aCenterAttr, GID_OUTOFGROUP);
36 theCenter = myStorage->entity(aCenterAttr);
38 getEntitiesAndCopies(theEntities);
40 //// // Lists of objects and number of copies
41 //// AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
42 //// aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
43 //// myNumberOfObjects = anInitialRefList->size();
44 //// myNumberOfCopies = (size_t) aData->integer(SketchPlugin_MultiRotation::NUMBER_OF_OBJECTS_ID())->value() - 1;
45 //// if (myNumberOfCopies <= 0)
47 //// AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
48 //// myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
50 //// myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
54 //// // Obtain all points of initial features and store them into separate lists
55 //// // containing their translated copies.
56 //// // Also all circles and arc collected too, because they will be constrained by equal radii.
57 //// FeaturePtr aFeature;
58 //// ResultConstructionPtr aRC;
59 //// static const size_t MAX_POINTS = 3;
60 //// std::vector<Slvs_hEntity> aPoints[MAX_POINTS]; // lists of points of features
61 //// std::vector<Slvs_hEntity> anEntities;
62 //// std::list<ObjectPtr> anObjectList = aRefList->list();
63 //// std::list<ObjectPtr>::iterator anObjectIter = anObjectList.begin();
64 //// while (anObjectIter != anObjectList.end()) {
65 //// for (size_t i = 0; i < MAX_POINTS; ++i)
66 //// aPoints[i].clear();
67 //// anEntities.clear();
69 //// for (size_t i = 0; i <= myNumberOfCopies && anObjectIter != anObjectList.end(); i++, anObjectIter++) {
70 //// aFeature = ModelAPI_Feature::feature(*anObjectIter);
73 //// anEntityID = changeEntity(aFeature, aType);
74 //// anEntities.push_back(anEntityID);
75 //// Slvs_Entity anEntity = myStorage->getEntity(anEntityID);
77 //// case SLVS_E_POINT_IN_2D:
78 //// case SLVS_E_POINT_IN_3D:
79 //// aPoints[0].push_back(anEntityID);
81 //// case SLVS_E_LINE_SEGMENT:
82 //// aPoints[0].push_back(anEntity.point[0]); // start point of line
83 //// aPoints[1].push_back(anEntity.point[1]); // end point of line
85 //// case SLVS_E_CIRCLE:
86 //// aPoints[0].push_back(anEntity.point[0]); // center of circle
88 //// case SLVS_E_ARC_OF_CIRCLE:
89 //// aPoints[0].push_back(anEntity.point[0]); // center of arc
90 //// aPoints[1].push_back(anEntity.point[1]); // start point of arc
91 //// aPoints[2].push_back(anEntity.point[2]); // end point of arc
94 //// myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
99 //// for (size_t i = 0; i < MAX_POINTS; ++i)
100 //// if (!aPoints[i].empty())
101 //// thePoints.push_back(aPoints[i]);
102 //// if (!anEntities.empty())
103 //// theEntities.push_back(anEntities);
107 void SketchSolver_ConstraintMultiRotation::process()
110 if (!myBaseConstraint || !myStorage || myGroupID == GID_UNKNOWN) {
111 /// TODO: Put error message here
114 //// if (!mySlvsConstraints.empty()) // some data is changed, update constraint
115 //// update(myBaseConstraint);
117 EntityWrapperPtr aRotationCenter;
118 std::list<std::list<EntityWrapperPtr> > anEntitiesAndCopies;
119 getAttributes(aRotationCenter, myAngle, anEntitiesAndCopies);
120 if (!myErrorMsg.empty())
123 //// // Set the rotation center unchanged during constraint recalculation
124 //// Slvs_Constraint aConstraint;
125 //// if (!myStorage->isPointFixed(myRotationCenter, aConstraint.h, true)) {
126 //// aConstraint = Slvs_MakeConstraint(
127 //// SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_WHERE_DRAGGED, myGroup->getWorkplaneId(), 0.0,
128 //// myRotationCenter, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
129 //// aConstraint.h = myStorage->addConstraint(aConstraint);
130 //// mySlvsConstraints.push_back(aConstraint.h);
133 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
134 std::list<ConstraintWrapperPtr> aRotConstraints;
136 std::list< std::list<EntityWrapperPtr> >::iterator anEntIt = anEntitiesAndCopies.begin();
137 for (; anEntIt != anEntitiesAndCopies.end(); ++anEntIt) {
138 std::list<ConstraintWrapperPtr> aNewConstraints =
139 aBuilder->createConstraint(myBaseConstraint, myGroupID, mySketchID, myType,
140 myAngle, aRotationCenter, EntityWrapperPtr(), *anEntIt);
141 aRotConstraints.insert(aRotConstraints.end(), aNewConstraints.begin(), aNewConstraints.end());
143 myStorage->addConstraint(myBaseConstraint, aRotConstraints);
146 //// processEntities(anEntitiesAndCopies);
150 void SketchSolver_ConstraintMultiRotation::updateLocal()
152 double aValue = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
153 myBaseConstraint->attribute(SketchPlugin_MultiRotation::ANGLE_ID()))->value();
154 if (fabs(myAngle - aValue) > tolerance)
156 // update angle value
160 AttributePtr aCenterAttr = myBaseConstraint->attribute(SketchPlugin_MultiRotation::CENTER_ID());
161 if (myStorage->update(aCenterAttr, myGroupID)) {
162 myStorage->update(aCenterAttr, GID_UNKNOWN);
167 void SketchSolver_ConstraintMultiRotation::adjustConstraint()
169 if (fabs(myAngle) < tolerance) {
170 myStorage->setNeedToResolve(false);
176 //// std::list<Slvs_Constraint> aCoincident = myStorage->getConstraintsByType(SLVS_C_POINTS_COINCIDENT);
177 //// std::list<Slvs_Constraint>::const_iterator aCoIt;
179 //// // Check overconstrained on rotation center (if it is coincident with other fixed point)
180 //// Slvs_hConstraint aFixedCenter;
181 //// if (myStorage->isPointFixed(myRotationCenter, aFixedCenter, false)) {
182 //// Slvs_hConstraint aFixed;
183 //// for (aCoIt = aCoincident.begin(); aCoIt != aCoincident.end(); ++aCoIt)
184 //// if ((aCoIt->ptA == myRotationCenter && myStorage->isPointFixed(aCoIt->ptB, aFixed, true)) ||
185 //// (aCoIt->ptB == myRotationCenter && myStorage->isPointFixed(aCoIt->ptA, aFixed, true))) {
186 //// // Un-fix the center
187 //// myStorage->removeConstraint(aFixedCenter);
188 //// std::vector<Slvs_hConstraint>::iterator aSCIt = mySlvsConstraints.begin();
189 //// for (; aSCIt != mySlvsConstraints.end(); ++aSCIt)
190 //// if (*aSCIt == aFixedCenter) {
191 //// mySlvsConstraints.erase(aSCIt);
197 //// // Obtain coordinates of rotation center
198 //// Slvs_Entity aRotCenter = myStorage->getEntity(myRotationCenter);
199 //// myCenterCoord[0] = myStorage->getParameter(aRotCenter.param[0]).val;
200 //// myCenterCoord[1] = myStorage->getParameter(aRotCenter.param[1]).val;
202 //// myRotationVal[0] = sin(myAngle * PI / 180.0);
203 //// myRotationVal[1] = cos(myAngle * PI / 180.0);
206 const std::list<ConstraintWrapperPtr>& aConstraints = myStorage->constraint(myBaseConstraint);
207 std::list<ConstraintWrapperPtr>::const_iterator aCIt = aConstraints.begin();
208 for (; aCIt != aConstraints.end(); ++aCIt)
209 (*aCIt)->setValue(myAngle);
211 SketchSolver_ConstraintMulti::adjustConstraint();
214 ////void SketchSolver_ConstraintMultiRotation::getRelative(
215 //// double theAbsX, double theAbsY, double& theRelX, double& theRelY)
217 //// theRelX = theAbsX - myCenterCoord[0];
218 //// theRelY = theAbsY - myCenterCoord[1];
221 ////void SketchSolver_ConstraintMultiRotation::getAbsolute(
222 //// double theRelX, double theRelY, double& theAbsX, double& theAbsY)
224 //// theAbsX = theRelX + myCenterCoord[0];
225 //// theAbsY = theRelY + myCenterCoord[1];
228 ////void SketchSolver_ConstraintMultiRotation::transformRelative(double& theX, double& theY)
230 //// // rotate direction
231 //// // myRotationVal[0] = sinA, myRotationVal[1] = cosA
232 //// double aTemp = theX * myRotationVal[1] - theY * myRotationVal[0];
233 //// theY = theX * myRotationVal[0] + theY * myRotationVal[1];
237 const std::string& SketchSolver_ConstraintMultiRotation::nameNbObjects()
239 return SketchPlugin_MultiRotation::NUMBER_OF_OBJECTS_ID();