1 #include <SketchSolver_Constraint.h>
2 #include <SketchSolver_Group.h>
3 #include <SketchSolver_Error.h>
5 #include <SketchPlugin_Arc.h>
6 #include <SketchPlugin_Circle.h>
7 #include <SketchPlugin_Line.h>
8 #include <SketchPlugin_Point.h>
10 #include <GeomDataAPI_Point.h>
11 #include <GeomDataAPI_Point2D.h>
12 #include <ModelAPI_AttributeDouble.h>
13 #include <ModelAPI_ResultConstruction.h>
17 SketchSolver_Constraint::SketchSolver_Constraint(
18 ConstraintPtr theConstraint)
19 : myBaseConstraint(theConstraint),
24 SketchSolver_Constraint::~SketchSolver_Constraint()
26 std::map<AttributePtr, Slvs_hParam>::const_iterator anIt1 = myValueMap.begin();
27 for (; anIt1 != myValueMap.end(); anIt1++)
28 myStorage->removeParameter(anIt1->second);
31 std::map<AttributePtr, Slvs_hEntity>::const_iterator anIt2 = myAttributeMap.begin();
32 for (; anIt2 != myAttributeMap.end(); anIt2++)
33 myStorage->removeEntity(anIt2->second);
34 myAttributeMap.clear();
36 std::map<FeaturePtr, Slvs_hEntity>::const_iterator anIt3 = myFeatureMap.begin();
37 for (; anIt3 != myFeatureMap.end(); anIt3++)
38 myStorage->removeEntity(anIt3->second);
41 std::vector<Slvs_hConstraint>::const_iterator anIt4 = mySlvsConstraints.begin();
42 for (; anIt4 != mySlvsConstraints.end(); anIt4++)
43 myStorage->removeConstraint(*anIt4);
44 mySlvsConstraints.clear();
47 void SketchSolver_Constraint::setStorage(StoragePtr theStorage)
49 myStorage = theStorage;
53 void SketchSolver_Constraint::setGroup(SketchSolver_Group* theGroup)
60 void SketchSolver_Constraint::process()
63 if (!myBaseConstraint || !myStorage || myGroup == 0) {
64 /// TODO: Put error message here
67 if (!mySlvsConstraints.empty()) // some data is changed, update constraint
68 update(myBaseConstraint);
70 int aConstrType = getType();
72 std::vector<Slvs_hEntity> anAttributes;
73 getAttributes(aValue, anAttributes);
74 if (!myErrorMsg.empty())
77 Slvs_hGroup aGroupID = myGroup->getId();
78 Slvs_hEntity aWorkplaneID = myGroup->getWorkplaneId();
79 Slvs_Constraint aConstraint;
80 if (mySlvsConstraints.empty())
81 aConstraint = Slvs_MakeConstraint(SLVS_C_UNKNOWN, aGroupID, aConstrType, aWorkplaneID,
82 aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
84 aConstraint = myStorage->getConstraint(mySlvsConstraints[0]);
85 aConstraint.valA = aValue;
86 static const int aNbAttrs = 6;
87 Slvs_hEntity* aConstrAttrs[aNbAttrs] = {
88 &aConstraint.ptA, &aConstraint.ptB,
89 &aConstraint.entityA, &aConstraint.entityB,
90 &aConstraint.entityC, &aConstraint.entityD};
91 std::vector<Slvs_hEntity>::const_iterator anIter = anAttributes.begin();
92 for (int i = 0; i < aNbAttrs && anIter != anAttributes.end(); i++, anIter++)
93 *(aConstrAttrs[i]) = *anIter;
96 Slvs_hConstraint anID = myStorage->addConstraint(aConstraint);
97 if (mySlvsConstraints.empty())
98 mySlvsConstraints.push_back(anID);
100 mySlvsConstraints[0] = anID;
104 void SketchSolver_Constraint::update(ConstraintPtr theConstraint)
107 if (theConstraint && theConstraint != myBaseConstraint) {
108 if (theConstraint->getKind() != myBaseConstraint->getKind())
110 remove(myBaseConstraint);
111 myBaseConstraint = theConstraint;
115 // Update all attributes
117 std::map<Slvs_hEntity, Slvs_hEntity> aRelocationMap;
118 std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIter = myFeatureMap.begin();
119 for (; aFeatIter != myFeatureMap.end(); aFeatIter++) {
120 Slvs_hEntity aPrevID = aFeatIter->second;
121 aFeatIter->second = changeEntity(aFeatIter->first, aType);
122 if (aFeatIter->second != aPrevID)
123 aRelocationMap[aPrevID] = aFeatIter->second;
125 std::map<AttributePtr, Slvs_hEntity>::iterator anAttrIter = myAttributeMap.begin();
126 for (; anAttrIter != myAttributeMap.end(); anAttrIter++) {
127 Slvs_hEntity aPrevID = anAttrIter->second;
128 anAttrIter->second = changeEntity(anAttrIter->first, aType);
129 if (anAttrIter->second != aPrevID)
130 aRelocationMap[aPrevID] = anAttrIter->second;
134 DataPtr aData = myBaseConstraint->data();
136 AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
137 myBaseConstraint->attribute(SketchPlugin_Constraint::VALUE()));
138 double aValue = aValueAttr ? aValueAttr->value() : 0.0;
141 std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
142 for (; aCIter != mySlvsConstraints.end(); aCIter++) {
143 Slvs_Constraint aConstraint = myStorage->getConstraint(*aCIter);
144 aConstraint.valA = aValue;
145 Slvs_hEntity* aCoeffs[6] = {
146 &aConstraint.ptA, &aConstraint.ptB,
147 &aConstraint.entityA, &aConstraint.entityB,
148 &aConstraint.entityC, &aConstraint.entityD};
149 for (int i = 0; i < 6; i++) {
150 if (*(aCoeffs[i]) == SLVS_E_UNKNOWN)
152 std::map<Slvs_hEntity, Slvs_hEntity>::iterator aFound = aRelocationMap.find(*(aCoeffs[i]));
153 if (aFound != aRelocationMap.end())
154 *(aCoeffs[i]) = aFound->second;
156 *aCIter = myStorage->addConstraint(aConstraint);
161 bool SketchSolver_Constraint::remove(ConstraintPtr theConstraint)
164 if (theConstraint && theConstraint != myBaseConstraint)
166 if (mySlvsConstraints.empty())
168 bool isFullyRemoved = myStorage->removeConstraint(mySlvsConstraints.front());
169 if (isFullyRemoved) {
170 myFeatureMap.clear();
171 myAttributeMap.clear();
174 cleanRemovedEntities();
175 mySlvsConstraints.clear();
179 void SketchSolver_Constraint::cleanRemovedEntities()
181 std::set<Slvs_hParam> aRemovedParams;
182 std::set<Slvs_hEntity> aRemovedEntities;
183 std::set<Slvs_hConstraint> aRemovedConstraints;
184 myStorage->getRemoved(aRemovedParams, aRemovedEntities, aRemovedConstraints);
185 std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIt = myFeatureMap.begin();
186 while (aFeatIt != myFeatureMap.end()) {
187 if (aRemovedEntities.find(aFeatIt->second) == aRemovedEntities.end()) {
191 std::map<FeaturePtr, Slvs_hEntity>::iterator aTmpIter = aFeatIt++;
192 myFeatureMap.erase(aTmpIter);
194 std::map<AttributePtr, Slvs_hEntity>::iterator anAttrIt = myAttributeMap.begin();
195 while (anAttrIt != myAttributeMap.end()) {
196 if (aRemovedEntities.find(anAttrIt->second) == aRemovedEntities.end()) {
200 std::map<AttributePtr, Slvs_hEntity>::iterator aTmpIter = anAttrIt++;
201 myAttributeMap.erase(aTmpIter);
203 std::map<AttributePtr, Slvs_hParam>::iterator aValIt = myValueMap.begin();
204 while (aValIt != myValueMap.end()) {
205 if (aRemovedParams.find(aValIt->second) == aRemovedParams.end()) {
209 std::map<AttributePtr, Slvs_hParam>::iterator aTmpIter = aValIt++;
210 myValueMap.erase(aTmpIter);
214 void SketchSolver_Constraint::getAttributes(
216 std::vector<Slvs_hEntity>& theAttributes)
218 static const int anInitNbOfAttr = 4;
219 theAttributes.assign(anInitNbOfAttr, SLVS_E_UNKNOWN);
221 DataPtr aData = myBaseConstraint->data();
223 AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
224 aData->attribute(SketchPlugin_Constraint::VALUE()));
225 theValue = aValueAttr ? aValueAttr->value() : 0.0;
227 int aPtInd = 0; // index of first point in the list of attributes
228 int aEntInd = 2; // index of first antity in the list of attributes
229 std::list<AttributePtr> aConstrAttrs = aData->attributes(ModelAPI_AttributeRefAttr::typeId());
230 std::list<AttributePtr>::iterator anIter = aConstrAttrs.begin();
231 for (; anIter != aConstrAttrs.end(); anIter++) {
232 AttributeRefAttrPtr aRefAttr =
233 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
234 if (!aRefAttr || !aRefAttr->isInitialized()) {
235 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
239 int aType = SLVS_E_UNKNOWN; // type of created entity
240 Slvs_hEntity anEntity = myGroup->getAttributeId(aRefAttr);
241 if (anEntity == SLVS_E_UNKNOWN)
242 anEntity = changeEntity(aRefAttr, aType);
244 Slvs_Entity anEnt = myStorage->getEntity(anEntity);
248 if (aType == SLVS_E_UNKNOWN)
250 else if (aType == SLVS_E_POINT_IN_2D || aType == SLVS_E_POINT_IN_3D)
251 theAttributes[aPtInd++] = anEntity; // the point is created
252 else { // another entity (not a point) is created
253 if (aEntInd < anInitNbOfAttr)
254 theAttributes[aEntInd] = anEntity;
256 theAttributes.push_back(anEntity);
262 Slvs_hEntity SketchSolver_Constraint::changeEntity(AttributeRefAttrPtr theAttribute, int& theType)
264 // Convert the object of the attribute to the feature
266 if (theAttribute->isObject() && theAttribute->object()) {
267 ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(
268 theAttribute->object());
270 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
271 return SLVS_E_UNKNOWN;
273 std::shared_ptr<ModelAPI_Document> aDoc = aRC->document();
274 aFeature = aDoc->feature(aRC);
276 return changeEntity(aFeature, theType);
279 return changeEntity(theAttribute->attr(), theType);
282 Slvs_hEntity SketchSolver_Constraint::changeEntity(AttributePtr theEntity, int& theType)
284 Slvs_hEntity aResult = SLVS_E_UNKNOWN;
285 if (!theEntity || !isInitialized(theEntity))
286 return SLVS_E_UNKNOWN;
288 // If the entity is already in the group, try to find it
289 std::map<std::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator anEntIter =
290 myAttributeMap.find(theEntity);
291 Slvs_Entity aCurrentEntity;
292 aCurrentEntity.h = SLVS_E_UNKNOWN;
293 if (anEntIter != myAttributeMap.end())
294 aCurrentEntity = myStorage->getEntity(anEntIter->second);
296 aResult = myGroup->getAttributeId(theEntity);
297 if (aResult != SLVS_E_UNKNOWN) {
298 Slvs_Entity anEnt = myStorage->getEntity(aResult);
299 theType = anEnt.type;
300 myAttributeMap[theEntity] = aResult;
305 Slvs_hGroup aGroupID = myGroup->getId();
307 std::shared_ptr<GeomDataAPI_Point> aPoint =
308 std::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
310 double aXYZ[3] = {aPoint->x(), aPoint->y(), aPoint->z()};
311 Slvs_hParam aParams[3];
312 for (int i = 0; i < 3; i++) {
313 Slvs_Param aPar = aCurrentEntity.h != SLVS_E_UNKNOWN ?
314 myStorage->getParameter(aCurrentEntity.param[i]) :
315 Slvs_MakeParam(SLVS_E_UNKNOWN, aGroupID, 0.0);
317 aParams[i] = myStorage->addParameter(aPar);
320 if (aCurrentEntity.h == SLVS_E_UNKNOWN) // New entity
321 aCurrentEntity = Slvs_MakePoint3d(SLVS_E_UNKNOWN, aGroupID, aParams[0], aParams[1], aParams[2]);
322 else { // update entity data
323 for (int i = 0; i < 3; i++)
324 aCurrentEntity.param[i] = aParams[i];
326 aResult = myStorage->addEntity(aCurrentEntity);
328 // All entities except 3D points are created on workplane. So, if there is no workplane yet, then error
329 Slvs_hEntity aWorkplaneID = myGroup->getWorkplaneId();
330 if (aWorkplaneID == SLVS_E_UNKNOWN)
331 return SLVS_E_UNKNOWN;
334 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
335 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
337 double aXY[2] = {aPoint2D->x(), aPoint2D->y()};
338 Slvs_hParam aParams[2];
339 for (int i = 0; i < 2; i++) {
340 Slvs_Param aPar = aCurrentEntity.h != SLVS_E_UNKNOWN ?
341 myStorage->getParameter(aCurrentEntity.param[i]) :
342 Slvs_MakeParam(SLVS_E_UNKNOWN, aGroupID, 0.0);
344 aParams[i] = myStorage->addParameter(aPar);
347 if (aCurrentEntity.h == SLVS_E_UNKNOWN) // New entity
348 aCurrentEntity = Slvs_MakePoint2d(SLVS_E_UNKNOWN, aGroupID, aWorkplaneID, aParams[0], aParams[1]);
349 else { // update entity data
350 for (int i = 0; i < 2; i++)
351 aCurrentEntity.param[i] = aParams[i];
353 aResult = myStorage->addEntity(aCurrentEntity);
355 // Scalar value (used for the distance entities)
356 AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theEntity);
358 Slvs_Param aParam = aCurrentEntity.h != SLVS_E_UNKNOWN ?
359 myStorage->getParameter(aCurrentEntity.param[0]) :
360 Slvs_MakeParam(SLVS_E_UNKNOWN, aGroupID, 0.0);
361 aParam.val = aScalar->value();
362 Slvs_hParam aValue = myStorage->addParameter(aParam);
364 if (aCurrentEntity.h == SLVS_E_UNKNOWN) // New entity
365 aCurrentEntity = Slvs_MakeDistance(SLVS_E_UNKNOWN, aGroupID, aWorkplaneID, aValue);
367 aCurrentEntity.param[0] = aValue;
368 aResult = myStorage->addEntity(aCurrentEntity);
373 myAttributeMap[theEntity] = aResult;
374 theType = aCurrentEntity.type;
378 Slvs_hEntity SketchSolver_Constraint::changeEntity(FeaturePtr theEntity, int& theType)
380 Slvs_hEntity aResult = SLVS_E_UNKNOWN;
381 if (!theEntity || !theEntity->data() || !theEntity->data()->isValid())
382 return SLVS_E_UNKNOWN;
383 // If the entity is already in the group, try to find it
384 std::map<FeaturePtr, Slvs_hEntity>::const_iterator anEntIter = myFeatureMap.find(theEntity);
385 Slvs_Entity aCurrentEntity;
386 aCurrentEntity.h = SLVS_E_UNKNOWN;
387 if (anEntIter != myFeatureMap.end())
388 aCurrentEntity = myStorage->getEntity(anEntIter->second);
390 aResult = myGroup->getFeatureId(theEntity);
391 if (aResult != SLVS_E_UNKNOWN) {
392 Slvs_Entity anEnt = myStorage->getEntity(aResult);
393 theType = anEnt.type;
394 myFeatureMap[theEntity] = aResult;
399 Slvs_hGroup aGroupID = myGroup->getId();
400 Slvs_hEntity aWorkplaneID = myGroup->getWorkplaneId();
401 DataPtr aData = theEntity->data();
403 // SketchPlugin features
404 const std::string& aFeatureKind = theEntity->getKind();
405 AttributePtr anAttribute;
408 if (aFeatureKind == SketchPlugin_Line::ID()) {
409 anAttribute = aData->attribute(SketchPlugin_Line::START_ID());
410 if (!isInitialized(anAttribute)) return SLVS_E_UNKNOWN;
411 Slvs_hEntity aStart = changeEntity(anAttribute, anAttrType);
413 anAttribute = aData->attribute(SketchPlugin_Line::END_ID());
414 if (!isInitialized(anAttribute)) return SLVS_E_UNKNOWN;
415 Slvs_hEntity aEnd = changeEntity(anAttribute, anAttrType);
417 if (aCurrentEntity.h == SLVS_E_UNKNOWN) // New entity
418 aCurrentEntity = Slvs_MakeLineSegment(SLVS_E_UNKNOWN, aGroupID, aWorkplaneID, aStart, aEnd);
420 aCurrentEntity.point[0] = aStart;
421 aCurrentEntity.point[1] = aEnd;
423 aResult = myStorage->addEntity(aCurrentEntity);
426 else if (aFeatureKind == SketchPlugin_Circle::ID()) {
427 anAttribute = aData->attribute(SketchPlugin_Circle::CENTER_ID());
428 if (!isInitialized(anAttribute)) return SLVS_E_UNKNOWN;
429 Slvs_hEntity aCenter = changeEntity(anAttribute, anAttrType);
431 anAttribute = aData->attribute(SketchPlugin_Circle::RADIUS_ID());
432 if (!isInitialized(anAttribute)) return SLVS_E_UNKNOWN;
433 Slvs_hEntity aRadius = changeEntity(anAttribute, anAttrType);
435 if (aCurrentEntity.h == SLVS_E_UNKNOWN) { // New entity
436 Slvs_Entity aWorkplane = myStorage->getEntity(aWorkplaneID);
437 aCurrentEntity = Slvs_MakeCircle(SLVS_E_UNKNOWN, aGroupID, aWorkplaneID,
438 aCenter, aWorkplane.normal, aRadius);
440 aCurrentEntity.point[0] = aCenter;
441 aCurrentEntity.distance = aRadius;
443 aResult = myStorage->addEntity(aCurrentEntity);
446 else if (aFeatureKind == SketchPlugin_Arc::ID()) {
447 anAttribute = aData->attribute(SketchPlugin_Arc::CENTER_ID());
448 if (!isInitialized(anAttribute)) return SLVS_E_UNKNOWN;
449 Slvs_hEntity aCenter = changeEntity(anAttribute, anAttrType);
451 anAttribute = aData->attribute(SketchPlugin_Arc::START_ID());
452 if (!isInitialized(anAttribute)) return SLVS_E_UNKNOWN;
453 Slvs_hEntity aStart = changeEntity(anAttribute, anAttrType);
455 anAttribute = aData->attribute(SketchPlugin_Arc::END_ID());
456 if (!isInitialized(anAttribute)) return SLVS_E_UNKNOWN;
457 Slvs_hEntity aEnd = changeEntity(anAttribute, anAttrType);
459 if (aCurrentEntity.h == SLVS_E_UNKNOWN) { // New entity
460 Slvs_Entity aWorkplane = myStorage->getEntity(aWorkplaneID);
461 aCurrentEntity = Slvs_MakeArcOfCircle(SLVS_E_UNKNOWN, aGroupID, aWorkplaneID,
462 aWorkplane.normal, aCenter, aStart, aEnd);
464 aCurrentEntity.point[0] = aCenter;
465 aCurrentEntity.point[1] = aStart;
466 aCurrentEntity.point[2] = aEnd;
468 aResult = myStorage->addEntity(aCurrentEntity);
470 // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
471 else if (aFeatureKind == SketchPlugin_Point::ID()) {
472 anAttribute = aData->attribute(SketchPlugin_Point::COORD_ID());
473 if (!isInitialized(anAttribute)) return SLVS_E_UNKNOWN;
474 // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier
475 aResult = changeEntity(anAttribute, anAttrType);
476 aCurrentEntity.type = SLVS_E_POINT_IN_3D;
479 if (aResult != SLVS_E_UNKNOWN) {
480 myFeatureMap[theEntity] = aResult;
481 theType = aCurrentEntity.type;
486 std::list<ConstraintPtr> SketchSolver_Constraint::constraints() const
488 std::list<ConstraintPtr> aConstraints;
489 aConstraints.push_back(myBaseConstraint);
493 void SketchSolver_Constraint::refresh()
496 std::map<AttributePtr, Slvs_hEntity>::iterator anAttrIter = myAttributeMap.begin();
497 for (; anAttrIter != myAttributeMap.end(); anAttrIter++) {
498 std::shared_ptr<GeomDataAPI_Point> aPoint =
499 std::dynamic_pointer_cast<GeomDataAPI_Point>(anAttrIter->first);
501 Slvs_Entity anEntity = myStorage->getEntity(anAttrIter->second);
503 for (int i = 0; i < 3; i++) {
504 Slvs_Param aPar = myStorage->getParameter(anEntity.param[i]);
507 if (fabs(aPoint->x() - aXYZ[0]) > tolerance ||
508 fabs(aPoint->y() - aXYZ[1]) > tolerance ||
509 fabs(aPoint->z() - aXYZ[2]) > tolerance)
510 aPoint->setValue(aXYZ[0], aXYZ[1], aXYZ[2]);
513 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
514 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrIter->first);
516 Slvs_Entity anEntity = myStorage->getEntity(anAttrIter->second);
518 for (int i = 0; i < 2; i++) {
519 Slvs_Param aPar = myStorage->getParameter(anEntity.param[i]);
522 if (fabs(aPoint2D->x() - aXY[0]) > tolerance ||
523 fabs(aPoint2D->y() - aXY[1]) > tolerance)
524 aPoint2D->setValue(aXY[0], aXY[1]);
526 // Scalar value (used for the distance entities)
527 AttributeDoublePtr aScalar =
528 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anAttrIter->first);
530 Slvs_Entity anEntity = myStorage->getEntity(anAttrIter->second);
531 Slvs_Param aPar = myStorage->getParameter(anEntity.param[0]);
532 if (fabs(aScalar->value() - aPar.val) > tolerance)
533 aScalar->setValue(aPar.val);
539 std::map<AttributePtr, Slvs_hParam>::iterator aValIter = myValueMap.begin();
540 for (; aValIter != myValueMap.end(); aValIter++) {
541 AttributeDoublePtr aScalar =
542 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anAttrIter->first);
544 Slvs_Param aPar = myStorage->getParameter(anAttrIter->second);
545 aScalar->setValue(aPar.val);
550 Slvs_hEntity SketchSolver_Constraint::getId(FeaturePtr theFeature) const
552 std::map<FeaturePtr, Slvs_hEntity>::const_iterator aFIter = myFeatureMap.find(theFeature);
553 if (aFIter == myFeatureMap.end())
554 return SLVS_E_UNKNOWN;
555 return aFIter->second;
558 Slvs_hEntity SketchSolver_Constraint::getId(AttributePtr theAttribute) const
560 std::map<AttributePtr, Slvs_hEntity>::const_iterator anAttrIter = myAttributeMap.find(theAttribute);
561 if (anAttrIter == myAttributeMap.end())
562 return SLVS_E_UNKNOWN;
563 return anAttrIter->second;
566 bool SketchSolver_Constraint::isInitialized(AttributePtr theAttribute)
568 if (theAttribute->isInitialized())
570 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
575 void SketchSolver_Constraint::calculateMiddlePoint(
576 const Slvs_Entity& theEntity, double theCoeff, double& theX, double& theY) const
578 if (theEntity.type == SLVS_E_LINE_SEGMENT) {
579 double aStartEndXY[2][2];
581 for (int i = 0; i < 2; i++) {
582 aPoint = myStorage->getEntity(theEntity.point[i]);
583 for (int j = 0; j < 2; j++)
584 aStartEndXY[i][j] = myStorage->getParameter(aPoint.param[j]).val;
586 theX = (1.0 - theCoeff) * aStartEndXY[0][0] + theCoeff * aStartEndXY[1][0];
587 theY = (1.0 - theCoeff) * aStartEndXY[0][1] + theCoeff * aStartEndXY[1][1];
588 } else if (theEntity.type == SLVS_E_ARC_OF_CIRCLE) {
589 double anArcPoint[3][2];
591 for (int i = 0; i < 3; i++) {
592 aPoint = myStorage->getEntity(theEntity.point[i]);
593 for (int j = 0; j < 2; j++)
594 anArcPoint[i][j] = myStorage->getParameter(aPoint.param[j]).val;
596 // project last point of arc on the arc
597 double x = anArcPoint[1][0] - anArcPoint[0][0];
598 double y = anArcPoint[1][1] - anArcPoint[0][1];
599 double aRad = sqrt(x*x + y*y);
600 x = anArcPoint[2][0] - anArcPoint[0][0];
601 y = anArcPoint[2][1] - anArcPoint[0][1];
602 double aNorm = sqrt(x*x + y*y);
603 if (aNorm >= tolerance) {
604 anArcPoint[2][0] = x * aRad / aNorm;
605 anArcPoint[2][1] = y * aRad / aNorm;
607 anArcPoint[1][0] -= anArcPoint[0][0];
608 anArcPoint[1][1] -= anArcPoint[0][1];
609 if (theCoeff < tolerance) {
610 theX = anArcPoint[0][0] + anArcPoint[1][0];
611 theY = anArcPoint[0][1] + anArcPoint[1][1];
613 } else if (1 - theCoeff < tolerance) {
614 theX = anArcPoint[0][0] + anArcPoint[2][0];
615 theY = anArcPoint[0][1] + anArcPoint[2][1];
619 double xStart = anArcPoint[1][0] / aRad, xEnd = anArcPoint[2][0] / aRad;
620 double yStart = anArcPoint[1][1] / aRad, yEnd = anArcPoint[2][1] / aRad;
621 if (anArcPoint[1][0] * anArcPoint[2][0] < 0.0) {
622 if (anArcPoint[1][0] > 0.0)
625 yStart = -2.0 - yStart;
628 yStart = 2.0 - yStart;
631 yStart = -2.0 - yStart;
635 if (anArcPoint[1][1] * anArcPoint[2][1] < 0.0) {
636 if (anArcPoint[1][1] > 0.0)
639 xStart = -2.0 - xStart;
642 xStart = 2.0 - xStart;
645 xStart = -2.0 - xStart;
649 x = (1.0 - theCoeff) * xStart + theCoeff * xEnd;
650 y = (1.0 - theCoeff) * yStart + theCoeff * yEnd;
651 if (x > 1.0) x = 2.0 - x;
652 if (x < -1.0) x = -2.0 - x;
653 if (y > 1.0) y = 2.0 - y;
654 if (y < -1.0) y = -2.0 - y;
656 aNorm = sqrt(x*x + y*y);
657 if (aNorm >= tolerance) {
661 x = -0.5 * (anArcPoint[2][1] + anArcPoint[1][1]);
662 y = -0.5 * (anArcPoint[2][0] + anArcPoint[1][0]);
664 theX = anArcPoint[0][0] + x;
665 theY = anArcPoint[0][1] + y;