1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PlaneGCSSolver_Storage.cpp
4 // Created: 14 Dec 2015
5 // Author: Artem ZHIDKOV
7 #include <PlaneGCSSolver_Storage.h>
8 #include <PlaneGCSSolver_Builder.h>
9 #include <PlaneGCSSolver_Solver.h>
10 #include <PlaneGCSSolver_ConstraintWrapper.h>
11 #include <PlaneGCSSolver_EntityWrapper.h>
12 #include <PlaneGCSSolver_PointWrapper.h>
13 #include <PlaneGCSSolver_ScalarWrapper.h>
14 #include <PlaneGCSSolver_ParameterWrapper.h>
16 #include <GeomAPI_Edge.h>
17 #include <GeomAPI_Dir2d.h>
18 #include <GeomAPI_Pnt2d.h>
19 #include <GeomAPI_XY.h>
20 #include <GeomDataAPI_Point2D.h>
21 #include <SketchPlugin_Arc.h>
26 PlaneGCSSolver_Storage::PlaneGCSSolver_Storage(const GroupID& theGroup)
27 : SketchSolver_Storage(theGroup),
28 myEntityLastID(EID_SKETCH),
29 myConstraintLastID(CID_UNKNOWN)
33 void PlaneGCSSolver_Storage::addConstraint(
34 ConstraintPtr theConstraint,
35 std::list<ConstraintWrapperPtr> theSolverConstraints)
37 SketchSolver_Storage::addConstraint(theConstraint, theSolverConstraints);
39 // update point-point coincidence
40 if (!theSolverConstraints.empty() &&
41 theSolverConstraints.front()->type() == CONSTRAINT_PT_PT_COINCIDENT) {
42 std::list<ConstraintWrapperPtr>::iterator aCIt = theSolverConstraints.begin();
43 for (; aCIt != theSolverConstraints.end(); ++aCIt)
49 bool PlaneGCSSolver_Storage::update(ConstraintWrapperPtr theConstraint)
51 bool isUpdated = false;
52 std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aConstraint =
53 std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(theConstraint);
55 // point-Line distance should be positive
56 if (aConstraint->type() == CONSTRAINT_PT_LINE_DISTANCE && aConstraint->value() < 0.0)
57 aConstraint->setValue(-aConstraint->value());
59 // make value of constraint unchangeable
60 ParameterWrapperPtr aValue = aConstraint->valueParameter();
62 isUpdated = update(aValue) || isUpdated;
64 // update constrained entities
65 std::list<EntityWrapperPtr> anEntities = theConstraint->entities();
66 std::list<EntityWrapperPtr>::iterator anIt = anEntities.begin();
67 for (; anIt != anEntities.end(); ++anIt)
68 isUpdated = update(*anIt) || isUpdated;
70 if (aConstraint->id() == CID_UNKNOWN) {
71 const std::list<EntityWrapperPtr>& aSubs = aConstraint->entities();
72 // check middle-point constraint conflicts with point-on-line
73 if (aConstraint->type() == CONSTRAINT_MIDDLE_POINT) {
74 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
75 anIt = myConstraintMap.begin();
76 for (; anIt != myConstraintMap.end(); ++anIt) {
77 EntityWrapperPtr aPoint, aLine;
78 ConstraintWrapperPtr aCurrentConstr = anIt->second.front();
79 if (aCurrentConstr->type() != CONSTRAINT_PT_ON_LINE)
81 const std::list<EntityWrapperPtr>& aCurSubs = aCurrentConstr->entities();
82 std::list<EntityWrapperPtr>::const_iterator aSIt1, aSIt2;
83 for (aSIt1 = aSubs.begin(); aSIt1 != aSubs.end(); ++aSIt1) {
84 if ((*aSIt1)->type() == ENTITY_POINT)
86 else if((*aSIt1)->type() == ENTITY_LINE)
90 for (aSIt2 = aCurSubs.begin(); aSIt2 != aCurSubs.end(); ++aSIt2)
91 if ((*aSIt1)->id() == (*aSIt2)->id())
93 if (aSIt2 == aCurSubs.end())
96 // point-on-line found, change it to bisector
97 if (aSIt1 == aSubs.end()) {
98 std::list<GCSConstraintPtr> aConstrList = aConstraint->constraints();
99 aConstrList.pop_front();
100 aConstraint->setConstraints(aConstrList);
106 // Change ID of constraints
107 aConstraint->setId(++myConstraintLastID);
113 /// \brief Update coordinates of the point or scalar using its base attribute
114 static bool updateValues(EntityWrapperPtr& theEntity)
116 bool isUpdated = false;
117 AttributePtr anAttr = theEntity->baseAttribute();
118 const std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
122 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
123 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
125 aCoord[0] = aPoint2D->x();
126 aCoord[1] = aPoint2D->y();
128 AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anAttr);
130 aCoord[0] = aScalar->value();
133 std::list<ParameterWrapperPtr>::const_iterator anIt = aParams.begin();
134 for (int i = 0; anIt != aParams.end(); ++anIt, ++i)
135 if (fabs((*anIt)->value() - aCoord[i]) > tolerance) {
136 (*anIt)->setValue(aCoord[i]);
142 bool PlaneGCSSolver_Storage::update(EntityWrapperPtr theEntity)
144 if (theEntity->type() == ENTITY_SKETCH)
145 return true; // sketch is not necessary for PlaneGCS, so it is always says true
147 bool isUpdated = false;
149 if (theEntity->baseAttribute()) {
150 isUpdated = updateValues(theEntity);
152 setNeedToResolve(true);
156 std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
157 std::list<ParameterWrapperPtr>::iterator aPIt = aParams.begin();
158 for (; aPIt != aParams.end(); ++aPIt)
159 isUpdated = update(*aPIt) || isUpdated;
161 // update sub-entities
162 std::list<EntityWrapperPtr> aSubEntities = theEntity->subEntities();
163 std::list<EntityWrapperPtr>::iterator aSIt = aSubEntities.begin();
164 for (; aSIt != aSubEntities.end(); ++aSIt)
165 isUpdated = update(*aSIt) || isUpdated;
167 // additional constraints for the arc processing
168 if (theEntity->type() == ENTITY_ARC)
169 processArc(theEntity);
171 // Change entity's ID, if necessary
172 if (theEntity->id() == EID_UNKNOWN) {
173 if (theEntity->type() == ENTITY_POINT) {
174 std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint =
175 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theEntity);
177 aPoint = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(
178 theEntity->subEntities().front());
180 aPoint->setId(++myEntityLastID);
181 } else if (theEntity->type() == ENTITY_SCALAR) {
182 std::shared_ptr<PlaneGCSSolver_ScalarWrapper> aScalar =
183 std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(theEntity);
184 aScalar->setId(++myEntityLastID);
186 std::shared_ptr<PlaneGCSSolver_EntityWrapper> aGCSEnt =
187 std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theEntity);
188 aGCSEnt->setId(++myEntityLastID);
194 bool PlaneGCSSolver_Storage::update(ParameterWrapperPtr theParameter)
196 std::shared_ptr<PlaneGCSSolver_ParameterWrapper> aParam =
197 std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(theParameter);
198 if (aParam->isProcessed())
200 if (theParameter->group() != myGroupID || theParameter->isParametric())
201 myConst.push_back(aParam->parameter());
203 myParameters.push_back(aParam->parameter());
204 aParam->setProcessed(true);
209 bool PlaneGCSSolver_Storage::remove(ConstraintWrapperPtr theConstraint)
211 std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aConstraint =
212 std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(theConstraint);
214 bool isFullyRemoved = true;
215 const std::list<EntityWrapperPtr>& aSubs = aConstraint->entities();
216 std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
217 for (; aSIt != aSubs.end(); ++ aSIt)
218 isFullyRemoved = remove(*aSIt) && isFullyRemoved;
220 if (aConstraint->valueParameter())
221 isFullyRemoved = remove(aConstraint->valueParameter()) && isFullyRemoved;
222 if (!isFullyRemoved && aConstraint->baseConstraint() &&
223 (!aConstraint->baseConstraint()->data() || !aConstraint->baseConstraint()->data()->isValid()))
224 isFullyRemoved = true;
225 setNeedToResolve(true);
226 myRemovedConstraints.insert(myRemovedConstraints.end(),
227 aConstraint->constraints().begin(), aConstraint->constraints().end());
229 if (isFullyRemoved && theConstraint->id() == myConstraintLastID)
230 --myConstraintLastID;
232 return isFullyRemoved;
235 bool PlaneGCSSolver_Storage::remove(EntityWrapperPtr theEntity)
237 if ((theEntity->baseAttribute() && isUsed(theEntity->baseAttribute())) ||
238 (theEntity->baseFeature() && isUsed(theEntity->baseFeature())))
241 bool isFullyRemoved = SketchSolver_Storage::remove(theEntity);
242 if (isFullyRemoved && theEntity->id() == myEntityLastID)
244 return isFullyRemoved;
247 bool PlaneGCSSolver_Storage::remove(ParameterWrapperPtr theParameter)
249 std::shared_ptr<PlaneGCSSolver_ParameterWrapper> aParam =
250 std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(theParameter);
251 if (aParam->isProcessed()) {
252 double* aValPtr = aParam->parameter();
253 GCS::VEC_pD::iterator anIt = myParameters.begin();
254 for (; anIt != myParameters.end(); ++anIt)
255 if (*anIt == aValPtr)
257 if (anIt != myParameters.end()) {
258 myParameters.erase(anIt);
259 setNeedToResolve(true);
262 for (anIt = myConst.begin(); anIt != myConst.end(); ++anIt)
263 if (*anIt == aValPtr)
265 if (anIt != myConst.end()) {
267 setNeedToResolve(true);
271 aParam->setProcessed(false);
276 void PlaneGCSSolver_Storage::addCoincidentPoints(
277 EntityWrapperPtr theMaster, EntityWrapperPtr theSlave)
279 if (theMaster->type() != ENTITY_POINT || theSlave->type() != ENTITY_POINT)
282 std::shared_ptr<PlaneGCSSolver_PointWrapper> aMaster =
283 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theMaster);
285 aMaster = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(
286 std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theMaster)->subEntities().front());
287 std::shared_ptr<PlaneGCSSolver_PointWrapper> aSlave =
288 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theSlave);
290 aSlave = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(
291 std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theSlave)->subEntities().front());
293 // Search available coincidence
294 CoincidentPointsMap::iterator aMasterFound = myCoincidentPoints.find(aMaster);
295 CoincidentPointsMap::iterator aSlaveFound = myCoincidentPoints.find(aSlave);
296 if (aMasterFound == myCoincidentPoints.end() && aSlaveFound == myCoincidentPoints.end()) {
297 // try to find master and slave points in the lists of slaves of already existent coincidences
298 CoincidentPointsMap::iterator anIt = myCoincidentPoints.begin();
299 for (; anIt != myCoincidentPoints.end(); ++anIt) {
300 if (anIt->second.find(aMaster) != anIt->second.end())
302 else if (anIt->second.find(aSlave) != anIt->second.end())
305 if (aMasterFound != myCoincidentPoints.end() && aSlaveFound != myCoincidentPoints.end())
310 if (aMasterFound == myCoincidentPoints.end()) {
312 myCoincidentPoints[aMaster] = std::set<EntityWrapperPtr>();
313 aMasterFound = myCoincidentPoints.find(aMaster);
314 } else if (aMasterFound == aSlaveFound)
315 return; // already coincident
317 if (aSlaveFound != myCoincidentPoints.end()) {
318 // A slave has been found, we need to attach all points coincident with it to the new master
319 std::set<EntityWrapperPtr> aNewSlaves = aSlaveFound->second;
320 aNewSlaves.insert(aSlaveFound->first);
321 myCoincidentPoints.erase(aSlaveFound);
323 std::set<EntityWrapperPtr>::const_iterator aSlIt = aNewSlaves.begin();
324 for (; aSlIt != aNewSlaves.end(); ++aSlIt)
325 addCoincidentPoints(aMaster, *aSlIt);
327 //std::list<ParameterWrapperPtr> aSlaveParams = aSlave->parameters();
328 //aSlave->setParameters(aMaster->parameters());
330 //// Remove slave's parameters
331 //std::list<ParameterWrapperPtr>::iterator aParIt = aSlaveParams.begin();
332 //for (; aParIt != aSlaveParams.end(); ++aParIt)
335 aMasterFound->second.insert(aSlave);
340 void PlaneGCSSolver_Storage::changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup)
342 theEntity->setGroup(theGroup);
343 if (theGroup == myGroupID)
344 makeVariable(theEntity);
346 if (theEntity->type() == ENTITY_POINT)
348 makeConstant(theEntity);
352 void PlaneGCSSolver_Storage::changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup)
357 void PlaneGCSSolver_Storage::verifyFixed()
362 void PlaneGCSSolver_Storage::processArc(const EntityWrapperPtr& theArc)
364 // Calculate additional parameters necessary for PlaneGCS
365 const std::list<EntityWrapperPtr>& aSubs = theArc->subEntities();
366 std::list<EntityWrapperPtr>::const_iterator aSubIt = aSubs.begin();
367 while ((*aSubIt)->type() == ENTITY_POINT) // search scalar entities
369 double* aStartAngle = std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(*aSubIt++)->scalar();
370 double* aEndAngle = std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(*aSubIt++)->scalar();
371 double* aRadius = std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(*aSubIt)->scalar();
373 FeaturePtr anArcFeature = theArc->baseFeature();
374 std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
375 anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
376 std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
377 anArcFeature->attribute(SketchPlugin_Arc::START_ID()));
378 std::shared_ptr<GeomDataAPI_Point2D> aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
379 anArcFeature->attribute(SketchPlugin_Arc::END_ID()));
380 if (!aCenterAttr || !aStartAttr || !aEndAttr)
382 std::shared_ptr<GeomAPI_Pnt2d> aCenterPnt = aCenterAttr->pnt();
383 std::shared_ptr<GeomAPI_Pnt2d> aStartPnt = aStartAttr->pnt();
384 std::shared_ptr<GeomAPI_Pnt2d> aEndPnt = aEndAttr->pnt();
386 *aRadius = aCenterPnt->distance(aStartPnt);
387 if (!anArcFeature->lastResult())
389 std::shared_ptr<GeomAPI_Edge> anArcEdge =
390 std::dynamic_pointer_cast<GeomAPI_Edge>(anArcFeature->lastResult()->shape());
393 anArcEdge->getRange(*aStartAngle, *aEndAngle);
395 // No need to add constraints if they are already exist
396 std::map<EntityWrapperPtr, std::vector<GCSConstraintPtr> >::const_iterator
397 aFound = myArcConstraintMap.find(theArc);
398 if (aFound != myArcConstraintMap.end())
401 // Prepare additional constraints to produce the arc
402 std::vector<GCSConstraintPtr> anArcConstraints;
403 std::shared_ptr<PlaneGCSSolver_EntityWrapper> anArcEnt =
404 std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theArc);
405 std::shared_ptr<GCS::Arc> anArc = std::dynamic_pointer_cast<GCS::Arc>(anArcEnt->entity());
406 // Distances from center till start and end points are equal to radius
407 anArcConstraints.push_back(GCSConstraintPtr(new GCS::ConstraintP2PDistance(
408 anArc->center, anArc->start, anArc->rad)));
409 anArcConstraints.push_back(GCSConstraintPtr(new GCS::ConstraintP2PDistance(
410 anArc->center, anArc->end, anArc->rad)));
411 // Angles of start and end points should be equal to given angles
412 anArcConstraints.push_back(GCSConstraintPtr(new GCS::ConstraintP2PAngle(
413 anArc->center, anArc->start, anArc->startAngle)));
414 anArcConstraints.push_back(GCSConstraintPtr(new GCS::ConstraintP2PAngle(
415 anArc->center, anArc->end, anArc->endAngle)));
417 myArcConstraintMap[theArc] = anArcConstraints;
421 void PlaneGCSSolver_Storage::makeConstant(const EntityWrapperPtr& theEntity)
423 toggleEntity(theEntity, myParameters, myConst);
424 if (theEntity->type() == ENTITY_POINT)
425 updateCoincident(theEntity);
428 void PlaneGCSSolver_Storage::makeVariable(const EntityWrapperPtr& theEntity)
430 toggleEntity(theEntity, myConst, myParameters);
433 static void getParametersToMove(const EntityWrapperPtr& theEntity, std::set<double*>& theParamList)
435 const std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
436 std::list<ParameterWrapperPtr>::const_iterator aPIt = aParams.begin();
437 for (; aPIt != aParams.end(); ++aPIt)
439 std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(*aPIt)->parameter());
441 const std::list<EntityWrapperPtr> aSubs = theEntity->subEntities();
442 std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
444 if (theEntity->type() == ENTITY_ARC) {
445 // workaround for the arc processing, because the arc is fixed by a set of constraints,
446 // which will conflict with all parameters fixed:
448 getParametersToMove(*aSIt++, theParamList);
449 // 2. take start point
450 getParametersToMove(*aSIt++, theParamList);
451 // 3. skip end point, radius and start angle, but take end angle parameter
452 getParametersToMove(*(++aSIt), theParamList);
454 for (; aSIt != aSubs.end(); ++aSIt)
455 getParametersToMove(*aSIt, theParamList);
459 void PlaneGCSSolver_Storage::toggleEntity(
460 const EntityWrapperPtr& theEntity, GCS::VEC_pD& theFrom, GCS::VEC_pD& theTo)
462 std::set<double*> aParamsToMove;
463 getParametersToMove(theEntity, aParamsToMove);
465 GCS::VEC_pD::iterator anIt = theFrom.begin();
466 while (anIt != theFrom.end()) {
467 if (aParamsToMove.find(*anIt) == aParamsToMove.end()) {
472 theTo.push_back(*anIt);
473 int aShift = anIt - theFrom.begin();
475 anIt = theFrom.begin() + aShift;
479 void PlaneGCSSolver_Storage::updateCoincident(const EntityWrapperPtr& thePoint)
481 CoincidentPointsMap::iterator anIt = myCoincidentPoints.begin();
482 for (; anIt != myCoincidentPoints.end(); ++anIt) {
483 if (anIt->first == thePoint || anIt->second.find(thePoint) != anIt->second.end()) {
484 std::set<EntityWrapperPtr> aCoincident = anIt->second;
485 aCoincident.insert(anIt->first);
487 const std::list<ParameterWrapperPtr>& aBaseParams = thePoint->parameters();
488 std::list<ParameterWrapperPtr> aParams;
489 std::list<ParameterWrapperPtr>::const_iterator aBaseIt, anUpdIt;
491 std::set<EntityWrapperPtr>::const_iterator aCoincIt = aCoincident.begin();
492 for (; aCoincIt != aCoincident.end(); ++aCoincIt)
493 if (*aCoincIt != thePoint && (*aCoincIt)->group() != GID_OUTOFGROUP) {
494 aParams = (*aCoincIt)->parameters();
495 aBaseIt = aBaseParams.begin();
496 for (anUpdIt = aParams.begin(); anUpdIt != aParams.end(); ++anUpdIt, ++aBaseIt)
497 (*anUpdIt)->setValue((*aBaseIt)->value());
506 bool PlaneGCSSolver_Storage::isRedundant(
507 GCSConstraintPtr theCheckedConstraint,
508 ConstraintWrapperPtr theParentConstraint) const
510 if (theParentConstraint->type() == CONSTRAINT_SYMMETRIC) {
511 if (theCheckedConstraint->getTypeId() == GCS::Perpendicular) {
512 BuilderPtr aBuilder = PlaneGCSSolver_Builder::getInstance();
513 // check the initial point is placed on the mirror line
514 std::list<EntityWrapperPtr> aSubs = theParentConstraint->entities();
515 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aBuilder->point(aSubs.front());
516 std::shared_ptr<GeomAPI_Lin2d> aLine = aBuilder->line(aSubs.back());
517 return aLine->distance(aPoint) < tolerance;
524 void PlaneGCSSolver_Storage::initializeSolver(SolverPtr theSolver)
526 std::shared_ptr<PlaneGCSSolver_Solver> aSolver =
527 std::dynamic_pointer_cast<PlaneGCSSolver_Solver>(theSolver);
534 // initialize constraints
535 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
536 aCIt = myConstraintMap.begin();
537 for (; aCIt != myConstraintMap.end(); ++aCIt) {
538 std::list<ConstraintWrapperPtr>::const_iterator aCWIt = aCIt->second.begin();
539 for (; aCWIt != aCIt->second.end(); ++ aCWIt) {
540 std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aGCS =
541 std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(*aCWIt);
542 std::list<GCSConstraintPtr>::const_iterator anIt = aGCS->constraints().begin();
543 for (; anIt != aGCS->constraints().end(); ++anIt)
544 if (!isRedundant(*anIt, aGCS))
545 aSolver->addConstraint(*anIt);
548 // additional constraints for arcs
549 std::map<EntityWrapperPtr, std::vector<GCSConstraintPtr> >::const_iterator
550 anArcIt = myArcConstraintMap.begin();
551 for (; anArcIt != myArcConstraintMap.end(); ++anArcIt) {
552 std::vector<GCSConstraintPtr>::const_iterator anIt = anArcIt->second.begin();
553 for (; anIt != anArcIt->second.end(); ++anIt)
554 aSolver->addConstraint(*anIt);
556 // removed waste constraints
557 std::list<GCSConstraintPtr>::const_iterator aRemIt = myRemovedConstraints.begin();
558 for (; aRemIt != myRemovedConstraints.end(); ++aRemIt)
559 aSolver->removeConstraint(*aRemIt);
560 myRemovedConstraints.clear();
561 // initialize unknowns
562 aSolver->setParameters(myParameters);
565 void PlaneGCSSolver_Storage::refresh(bool theFixedOnly) const
569 std::map<AttributePtr, EntityWrapperPtr>::const_iterator anIt = myAttributeMap.begin();
570 std::list<ParameterWrapperPtr> aParams;
571 std::list<ParameterWrapperPtr>::const_iterator aParIt;
572 for (; anIt != myAttributeMap.end(); ++anIt) {
573 // the external feature always should keep the up to date values, so,
574 // refresh from the solver is never needed
575 bool isExternal = false;
576 if (anIt->first.get()) {
577 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
578 std::dynamic_pointer_cast<SketchPlugin_Feature>(anIt->first->owner());
579 if (aSketchFeature.get() && aSketchFeature->isExternal())
583 // update parameter wrappers and obtain values of attributes
584 aParams = anIt->second->parameters();
586 bool isUpd[3] = {false};
588 for (aParIt = aParams.begin(); i < 3 && aParIt != aParams.end(); ++aParIt, ++i) {
589 if (!theFixedOnly || isExternal ||
590 (*aParIt)->group() == GID_OUTOFGROUP || (*aParIt)->isParametric()) {
591 aCoords[i] = (*aParIt)->value();
595 if (!isUpd[0] && !isUpd[1] && !isUpd[2])
596 continue; // nothing is updated
598 std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
599 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anIt->first);
601 if ((isUpd[0] && fabs(aPoint2D->x() - aCoords[0]) > tolerance) ||
602 (isUpd[1] && fabs(aPoint2D->y() - aCoords[1]) > tolerance) || isExternal) {
603 if (!isUpd[0] || isExternal) aCoords[0] = aPoint2D->x();
604 if (!isUpd[1] || isExternal) aCoords[1] = aPoint2D->y();
605 aPoint2D->setValue(aCoords[0], aCoords[1]);
606 // Find points coincident with this one (probably not in GID_OUTOFGROUP)
607 CoincidentPointsMap::const_iterator aCoincIt = myCoincidentPoints.begin();
608 for (; aCoincIt != myCoincidentPoints.end(); ++aCoincIt)
609 if (aCoincIt->first == anIt->second ||
610 aCoincIt->second.find(anIt->second) != aCoincIt->second.end())
612 if (aCoincIt != myCoincidentPoints.end()) {
613 aPoint2D = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
614 aCoincIt->first->baseAttribute());
616 aPoint2D->setValue(aCoords[0], aCoords[1]);
617 std::set<EntityWrapperPtr>::const_iterator aSlaveIt = aCoincIt->second.begin();
618 for (; aSlaveIt != aCoincIt->second.end(); ++aSlaveIt) {
619 aPoint2D = std::dynamic_pointer_cast<GeomDataAPI_Point2D>((*aSlaveIt)->baseAttribute());
621 aPoint2D->setValue(aCoords[0], aCoords[1]);
627 AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anIt->first);
628 if (aScalar && !isExternal) {
629 if (isUpd[0] && fabs(aScalar->value() - aCoords[0]) > tolerance)
630 aScalar->setValue(aCoords[0]);
635 //blockEvents(false);
638 EntityWrapperPtr PlaneGCSSolver_Storage::calculateMiddlePoint(
639 EntityWrapperPtr theBase, double theCoeff)
641 std::shared_ptr<PlaneGCSSolver_Builder> aBuilder =
642 std::dynamic_pointer_cast<PlaneGCSSolver_Builder>(PlaneGCSSolver_Builder::getInstance());
644 std::shared_ptr<GeomAPI_XY> aMidPoint;
645 if (theBase->type() == ENTITY_LINE) {
646 std::shared_ptr<GeomAPI_Pnt2d> aPoints[2];
647 const std::list<EntityWrapperPtr>& aSubs = theBase->subEntities();
648 std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
649 for (int i = 0; i < 2; ++i, ++anIt)
650 aPoints[i] = aBuilder->point(*anIt);
651 aMidPoint = aPoints[0]->xy()->multiplied(1.0 - theCoeff)->added(
652 aPoints[1]->xy()->multiplied(theCoeff));
654 else if (theBase->type() == ENTITY_ARC) {
656 double anArcPoint[3][2];
657 const std::list<EntityWrapperPtr>& aSubs = theBase->subEntities();
658 std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
659 for (int i = 0; i < 3; ++i, ++anIt) {
660 std::shared_ptr<GeomAPI_Pnt2d> aPoint = aBuilder->point(*anIt);
661 anArcPoint[i][0] = aPoint->x();
662 anArcPoint[i][1] = aPoint->y();
664 // project last point of arc on the arc
665 double x = anArcPoint[1][0] - anArcPoint[0][0];
666 double y = anArcPoint[1][1] - anArcPoint[0][1];
667 double aRad = sqrt(x*x + y*y);
668 x = anArcPoint[2][0] - anArcPoint[0][0];
669 y = anArcPoint[2][1] - anArcPoint[0][1];
670 double aNorm = sqrt(x*x + y*y);
671 if (aNorm >= tolerance) {
672 anArcPoint[2][0] = x * aRad / aNorm;
673 anArcPoint[2][1] = y * aRad / aNorm;
675 anArcPoint[1][0] -= anArcPoint[0][0];
676 anArcPoint[1][1] -= anArcPoint[0][1];
677 if (theCoeff < tolerance) {
678 theX = anArcPoint[0][0] + anArcPoint[1][0];
679 theY = anArcPoint[0][1] + anArcPoint[1][1];
680 } else if (1 - theCoeff < tolerance) {
681 theX = anArcPoint[0][0] + anArcPoint[2][0];
682 theY = anArcPoint[0][1] + anArcPoint[2][1];
684 std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(anArcPoint[1][0], anArcPoint[1][1]));
685 std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(anArcPoint[2][0], anArcPoint[2][1]));
686 double anAngle = aStartDir->angle(aEndDir);
690 double aCos = cos(anAngle);
691 double aSin = sin(anAngle);
692 theX = anArcPoint[0][0] + anArcPoint[1][0] * aCos - anArcPoint[1][1] * aSin;
693 theY = anArcPoint[0][1] + anArcPoint[1][0] * aSin + anArcPoint[1][1] * aCos;
695 aMidPoint = std::shared_ptr<GeomAPI_XY>(new GeomAPI_XY(theX, theY));
699 return EntityWrapperPtr();
701 std::list<ParameterWrapperPtr> aParameters;
702 aParameters.push_back(aBuilder->createParameter(myGroupID, aMidPoint->x()));
703 aParameters.push_back(aBuilder->createParameter(myGroupID, aMidPoint->y()));
704 // Create entity (parameters are not filled)
705 GCSPointPtr aPnt(new GCS::Point);
706 aPnt->x = std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(aParameters.front())->parameter();
707 aPnt->y = std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(aParameters.back())->parameter();
709 EntityWrapperPtr aResult(new PlaneGCSSolver_PointWrapper(AttributePtr(), aPnt));
710 aResult->setGroup(myGroupID);
711 aResult->setParameters(aParameters);