1 #include <SketchSolver_ConstraintRigid.h>
2 #include <SketchSolver_Error.h>
3 #include <SketchSolver_Group.h>
5 #include <SketchPlugin_Arc.h>
6 #include <SketchPlugin_Circle.h>
7 #include <SketchPlugin_ConstraintRigid.h>
8 #include <SketchPlugin_Line.h>
9 #include <SketchPlugin_Point.h>
11 #include <GeomAPI_Pnt2d.h>
12 #include <GeomAPI_XY.h>
13 #include <GeomDataAPI_Point2D.h>
14 #include <ModelAPI_AttributeDouble.h>
18 SketchSolver_ConstraintRigid::SketchSolver_ConstraintRigid(FeaturePtr theFeature)
19 : SketchSolver_Constraint(),
20 myBaseFeature(theFeature)
25 void SketchSolver_ConstraintRigid::process()
28 if ((!myBaseConstraint && !myBaseFeature) || !myStorage || myGroup == 0) {
29 /// TODO: Put error message here
32 if (!mySlvsConstraints.empty()) // some data is changed, update constraint
33 update(myBaseConstraint);
36 std::vector<Slvs_hEntity> anEntities;
37 getAttributes(aValue, anEntities);
38 if (!myErrorMsg.empty() || myFeatureMap.empty())
41 Slvs_hEntity anEntID = myFeatureMap.begin()->second;
42 if (myStorage->isEntityFixed(anEntID, true)) {
43 myErrorMsg = SketchSolver_Error::ALREADY_FIXED();
47 if (myFeatureMap.begin()->first->getKind() == SketchPlugin_Line::ID()) {
48 Slvs_Entity aLine = myStorage->getEntity(anEntID);
51 else if (myFeatureMap.begin()->first->getKind() == SketchPlugin_Arc::ID()) {
52 Slvs_Entity anArc = myStorage->getEntity(anEntID);
55 else if (myFeatureMap.begin()->first->getKind() == SketchPlugin_Circle::ID()) {
56 Slvs_Entity aCirc = myStorage->getEntity(anEntID);
59 else if (myFeatureMap.begin()->first->getKind() == SketchPlugin_Point::ID()) {
65 void SketchSolver_ConstraintRigid::getAttributes(
67 std::vector<Slvs_hEntity>& theAttributes)
70 int aType = SLVS_E_UNKNOWN; // type of created entity
71 Slvs_hEntity anEntityID = SLVS_E_UNKNOWN;
72 if (myBaseConstraint) {
73 // Get the attribute of constraint
74 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
75 myBaseConstraint->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
76 if (!aRefAttr || !aRefAttr->isInitialized()) {
77 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
80 anEntityID = myGroup->getAttributeId(aRefAttr);
81 if (anEntityID == SLVS_E_UNKNOWN)
82 anEntityID = changeEntity(aRefAttr, aType);
84 anEntityID = myGroup->getFeatureId(myBaseFeature);
85 if (anEntityID == SLVS_E_UNKNOWN)
86 anEntityID = changeEntity(myBaseFeature, aType);
88 myFeatureMap[myBaseFeature] = anEntityID;
91 if (anEntityID == SLVS_E_UNKNOWN) {
92 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
96 // Check the entity is complex
97 Slvs_Entity anEntity = myStorage->getEntity(anEntityID);
98 if (anEntity.point[0] != SLVS_E_UNKNOWN) {
99 for (int i = 0; i < 4 && anEntity.point[i]; i++)
100 theAttributes.push_back(anEntity.point[i]);
101 } else // simple entity
102 theAttributes.push_back(anEntityID);
105 void SketchSolver_ConstraintRigid::adjustConstraint()
107 if (myFeatureMap.empty() || (
108 myFeatureMap.begin()->first->getKind() != SketchPlugin_Arc::ID() &&
109 myFeatureMap.begin()->first->getKind() != SketchPlugin_Circle::ID()))
111 FeaturePtr aFeature = myFeatureMap.begin()->first;
113 // Search radius constraints and update them
114 Slvs_Constraint aConstraint;
115 std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
116 for (; aCIter != mySlvsConstraints.end(); aCIter++) {
117 aConstraint = myStorage->getConstraint(*aCIter);
118 if (aConstraint.type != SLVS_C_DIAMETER)
120 double aRadius = 0.0;
121 if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
122 std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
123 aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
124 std::shared_ptr<GeomAPI_Pnt2d> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
125 aFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
126 aRadius = aCenter->distance(aStart);
128 aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
129 aFeature->attribute(SketchPlugin_Circle::RADIUS_ID()))->value();
132 aConstraint.valA = aRadius * 2.0;
133 *aCIter = myStorage->updateConstraint(aConstraint);
138 bool SketchSolver_ConstraintRigid::remove(ConstraintPtr theConstraint)
141 if (theConstraint && theConstraint != myBaseConstraint)
143 bool isFullyRemoved = true;
144 std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
145 for (; aCIter != mySlvsConstraints.end(); aCIter++)
146 isFullyRemoved = myStorage->removeConstraint(*aCIter) && isFullyRemoved;
148 if (isFullyRemoved) {
149 myFeatureMap.clear();
150 myAttributeMap.clear();
153 cleanRemovedEntities();
157 void SketchSolver_ConstraintRigid::fixPoint(const Slvs_hEntity& thePointID)
159 if (thePointID == SLVS_E_UNKNOWN)
162 Slvs_Constraint aConstraint;
163 Slvs_hConstraint aConstrID = SLVS_E_UNKNOWN;
164 bool isFixed = myStorage->isPointFixed(thePointID, aConstrID, true);
165 bool isForceUpdate = (isFixed && !myBaseConstraint &&
166 myStorage->isTemporary(aConstrID));
167 if (!isForceUpdate) { // create new constraint
169 aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), getType(), myGroup->getWorkplaneId(),
170 0.0, thePointID, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
171 aConstraint.h = myStorage->addConstraint(aConstraint);
172 mySlvsConstraints.push_back(aConstraint.h);
173 if (!myBaseConstraint)
174 myStorage->addConstraintWhereDragged(aConstraint.h);
175 } else { // update already existent constraint
176 if (!isFixed || aConstrID == SLVS_E_UNKNOWN || myBaseConstraint)
178 aConstraint = myStorage->getConstraint(aConstrID);
179 aConstraint.ptA = thePointID;
180 myStorage->addConstraint(aConstraint);
181 if (!myBaseConstraint)
182 myStorage->addConstraintWhereDragged(aConstraint.h);
186 void SketchSolver_ConstraintRigid::fixLine(const Slvs_Entity& theLine)
188 Slvs_Constraint anEqual;
189 if (isAxisParallel(theLine)) {
190 // Fix one point and a line length
191 Slvs_hConstraint aFixed;
192 if (!myStorage->isPointFixed(theLine.point[0], aFixed, true) &&
193 !myStorage->isPointFixed(theLine.point[1], aFixed, true))
194 fixPoint(theLine.point[0]);
195 if (!isUsedInEqual(theLine, anEqual)) {
196 // Calculate distance between points on the line
198 for (int i = 0; i < 2; i++) {
199 Slvs_Entity aPnt = myStorage->getEntity(theLine.point[i]);
200 for (int j = 0; j < 2; j++) {
201 Slvs_Param aParam = myStorage->getParameter(aPnt.param[j]);
202 aCoords[2*i+j] = aParam.val;
205 double aLength = sqrt((aCoords[2] - aCoords[0]) * (aCoords[2] - aCoords[0]) +
206 (aCoords[3] - aCoords[1]) * (aCoords[3] - aCoords[1]));
208 Slvs_Constraint aDistance = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(),
209 SLVS_C_PT_PT_DISTANCE, myGroup->getWorkplaneId(), aLength,
210 theLine.point[0], theLine.point[1], SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
211 aDistance.h = myStorage->addConstraint(aDistance);
212 mySlvsConstraints.push_back(aDistance.h);
216 else if (isUsedInEqual(theLine, anEqual)) {
217 // Check another entity of Equal is already fixed
218 Slvs_hEntity anOtherEntID = anEqual.entityA == theLine.h ? anEqual.entityB : anEqual.entityA;
219 if (myStorage->isEntityFixed(anOtherEntID, true)) {
220 // Fix start point of the line (if end point is not fixed yet) ...
221 Slvs_hConstraint anEndFixedID = SLVS_E_UNKNOWN;
222 bool isFixed = myStorage->isPointFixed(theLine.point[1], anEndFixedID, true);
223 if (isFixed == SLVS_E_UNKNOWN)
224 fixPoint(theLine.point[0]);
225 // ... and create fixed point lying on this line
226 Slvs_hEntity aPointToCopy = anEndFixedID == SLVS_E_UNKNOWN ? theLine.point[1] : theLine.point[0];
227 // Firstly, search already fixed point on line
228 bool isPonLineFixed = false;
229 Slvs_hEntity aFixedPoint;
230 std::list<Slvs_Constraint> aPonLineList = myStorage->getConstraintsByType(SLVS_C_PT_ON_LINE);
231 std::list<Slvs_Constraint>::const_iterator aPLIter = aPonLineList.begin();
232 for (; aPLIter != aPonLineList.end() && !isPonLineFixed; aPLIter++)
233 if (aPLIter->entityA == theLine.h) {
234 isPonLineFixed = myStorage->isPointFixed(aPLIter->ptA, anEndFixedID);
235 aFixedPoint = aPLIter->ptA;
238 if (isPonLineFixed) { // update existent constraint
239 myStorage->copyEntity(aPointToCopy, aFixedPoint);
240 } else { // create new constraint
241 Slvs_hEntity aCopied = myStorage->copyEntity(aPointToCopy);
242 Slvs_Constraint aPonLine = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_PT_ON_LINE,
243 myGroup->getWorkplaneId(), 0.0, aCopied, SLVS_E_UNKNOWN, theLine.h, SLVS_E_UNKNOWN);
244 aPonLine.h = myStorage->addConstraint(aPonLine);
245 mySlvsConstraints.push_back(aPonLine.h);
252 for (int i = 0; i < 2; i++)
253 fixPoint(theLine.point[i]);
256 void SketchSolver_ConstraintRigid::fixCircle(const Slvs_Entity& theCircle)
258 bool isFixRadius = true;
259 // Verify the arc is under Equal constraint
260 Slvs_Constraint anEqual;
261 if (isUsedInEqual(theCircle, anEqual)) {
262 // Check another entity of Equal is already fixed
263 Slvs_hEntity anOtherEntID = anEqual.entityA == theCircle.h ? anEqual.entityB : anEqual.entityA;
264 if (myStorage->isEntityFixed(anOtherEntID, true))
268 fixPoint(theCircle.point[0]);
271 // Search the radius is already fixed
272 std::list<Slvs_Constraint> aDiamConstr = myStorage->getConstraintsByType(SLVS_C_DIAMETER);
273 std::list<Slvs_Constraint>::const_iterator aDiamIter = aDiamConstr.begin();
274 for (; aDiamIter != aDiamConstr.end(); aDiamIter++)
275 if (aDiamIter->entityA == theCircle.h)
278 // Fix radius of a circle
279 AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
280 myFeatureMap.begin()->first->attribute(SketchPlugin_Circle::RADIUS_ID()));
281 Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_DIAMETER,
282 myGroup->getWorkplaneId(), aRadiusAttr->value() * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN,
283 myFeatureMap.begin()->second, SLVS_E_UNKNOWN);
284 aFixedR.h = myStorage->addConstraint(aFixedR);
285 mySlvsConstraints.push_back(aFixedR.h);
289 void SketchSolver_ConstraintRigid::fixArc(const Slvs_Entity& theArc)
291 bool isFixRadius = true;
292 std::list<Slvs_hEntity> aPointsToFix;
293 aPointsToFix.push_back(theArc.point[1]);
294 aPointsToFix.push_back(theArc.point[2]);
296 // Verify the arc is under Equal constraint
297 Slvs_Constraint anEqual;
298 if (isUsedInEqual(theArc, anEqual)) {
299 // Check another entity of Equal is already fixed
300 Slvs_hEntity anOtherEntID = anEqual.entityA == theArc.h ? anEqual.entityB : anEqual.entityA;
301 if (myStorage->isEntityFixed(anOtherEntID, true)) {
303 Slvs_Entity anOtherEntity = myStorage->getEntity(anOtherEntID);
304 if (anOtherEntity.type == SLVS_E_LINE_SEGMENT) {
305 aPointsToFix.pop_back();
306 aPointsToFix.push_back(theArc.point[0]);
311 Slvs_hConstraint aConstrID;
312 int aNbPointsToFix = 2; // number of fixed points for the arc
313 if (myStorage->isPointFixed(theArc.point[0], aConstrID, true))
317 FeaturePtr aFeature = myFeatureMap.begin()->first;
318 std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
319 aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
320 std::shared_ptr<GeomAPI_Pnt2d> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
321 aFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
322 double aRadius = aCenter->distance(aStart);
324 // Update end point of the arc to be on a curve
325 std::shared_ptr<GeomAPI_Pnt2d> anEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
326 aFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
327 double aDistance = anEnd->distance(aCenter);
328 std::shared_ptr<GeomAPI_XY> aDir = anEnd->xy()->decreased(aCenter->xy());
329 if (aDistance < tolerance)
330 aDir = aStart->xy()->decreased(aCenter->xy())->multiplied(-1.0);
332 aDir = aDir->multiplied(aRadius / aDistance);
333 double xy[2] = {aCenter->x() + aDir->x(), aCenter->y() + aDir->y()};
334 Slvs_Entity aEndPoint = myStorage->getEntity(theArc.point[2]);
335 for (int i = 0; i < 2; i++) {
336 Slvs_Param aParam = myStorage->getParameter(aEndPoint.param[i]);
338 myStorage->updateParameter(aParam);
341 std::list<Slvs_hEntity>::iterator aPtIt = aPointsToFix.begin();
342 for (; aNbPointsToFix > 0; aPtIt++, aNbPointsToFix--)
346 // Fix radius of the arc
347 bool isExists = false;
348 std::list<Slvs_Constraint> aDiamConstraints = myStorage->getConstraintsByType(SLVS_C_DIAMETER);
349 std::list<Slvs_Constraint>::iterator anIt = aDiamConstraints.begin();
350 for (; anIt != aDiamConstraints.end() && !isExists; anIt++)
351 if (anIt->entityA == theArc.h)
354 Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_DIAMETER,
355 myGroup->getWorkplaneId(), aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN,
356 myFeatureMap.begin()->second, SLVS_E_UNKNOWN);
357 aFixedR.h = myStorage->addConstraint(aFixedR);
358 mySlvsConstraints.push_back(aFixedR.h);
359 if (!myBaseConstraint)
360 myStorage->addConstraintWhereDragged(aFixedR.h);
365 bool SketchSolver_ConstraintRigid::isUsedInEqual(
366 const Slvs_Entity& theEntity, Slvs_Constraint& theEqual) const
368 // Check the entity is used in Equal constraint
369 std::list<Slvs_Constraint> anEqualConstr = myStorage->getConstraintsByType(SLVS_C_EQUAL_LENGTH_LINES);
370 std::list<Slvs_Constraint> anAddList = myStorage->getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN);
371 anEqualConstr.insert(anEqualConstr.end(), anAddList.begin(), anAddList.end());
372 anAddList = myStorage->getConstraintsByType(SLVS_C_EQUAL_RADIUS);
373 anEqualConstr.insert(anEqualConstr.end(), anAddList.begin(), anAddList.end());
375 std::list<Slvs_Constraint>::const_iterator anEqIter = anEqualConstr.begin();
376 for (; anEqIter != anEqualConstr.end(); anEqIter++)
377 if (anEqIter->entityA == theEntity.h || anEqIter->entityB == theEntity.h) {
378 theEqual = *anEqIter;
384 bool SketchSolver_ConstraintRigid::isAxisParallel(const Slvs_Entity& theEntity) const
386 std::list<Slvs_Constraint> aConstr = myStorage->getConstraintsByType(SLVS_C_HORIZONTAL);
387 std::list<Slvs_Constraint> aVert = myStorage->getConstraintsByType(SLVS_C_VERTICAL);
388 aConstr.insert(aConstr.end(), aVert.begin(), aVert.end());
390 std::list<Slvs_Constraint>::const_iterator anIter = aConstr.begin();
391 for (; anIter != aConstr.end(); anIter++)
392 if (anIter->entityA == theEntity.h)