Salome HOME
Fix the problem moving arc with radius constraint (issue #1375)
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintFixedArcRadius.cpp
1 #include <SketchSolver_ConstraintFixedArcRadius.h>
2 #include <SketchSolver_Error.h>
3 #include <SketchSolver_Manager.h>
4
5 #include <SketchPlugin_Arc.h>
6 #include <GeomAPI_Pnt2d.h>
7
8 SketchSolver_ConstraintFixedArcRadius::SketchSolver_ConstraintFixedArcRadius(FeaturePtr theFeature)
9   : SketchSolver_ConstraintFixed(theFeature)
10 {
11   myType = CONSTRAINT_RADIUS;
12   process();
13 }
14
15 void SketchSolver_ConstraintFixedArcRadius::process()
16 {
17   cleanErrorMsg();
18   if (!myBaseFeature || !myStorage || myGroupID == GID_UNKNOWN) {
19     // Not enough parameters are assigned
20     return;
21   }
22
23   if (myBaseFeature->getKind() != SketchPlugin_Arc::ID()) {
24     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
25     return;
26   }
27
28   ParameterWrapperPtr aValue;
29   std::vector<EntityWrapperPtr> anEntities;
30   getAttributes(aValue, anEntities);
31   if (!myErrorMsg.empty() || anEntities.empty())
32     return;
33   fixFeature(anEntities.front());
34 }
35
36 void SketchSolver_ConstraintFixedArcRadius::fixFeature(EntityWrapperPtr theFeature)
37 {
38   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
39
40   // Calculate radius of the arc
41   std::list<EntityWrapperPtr> aSubs = theFeature->subEntities();
42   std::list<EntityWrapperPtr>::const_iterator aSubIt = aSubs.begin();
43   std::shared_ptr<GeomAPI_Pnt2d> aCenter = aBuilder->point(*aSubIt++);
44   std::shared_ptr<GeomAPI_Pnt2d> aStart  = aBuilder->point(*aSubIt++);
45   double aRadius = aCenter->distance(aStart);
46
47   // Create constraint
48   std::list<ConstraintWrapperPtr> aConstrList = aBuilder->createConstraint(ConstraintPtr(),
49       myGroupID, mySketchID, myType, aRadius, EntityWrapperPtr(), EntityWrapperPtr(), theFeature);
50   myRadiusConstraint = aConstrList.front();
51   myStorage->addConstraint(ConstraintPtr(), myRadiusConstraint);
52 }
53
54 bool SketchSolver_ConstraintFixedArcRadius::remove()
55 {
56   bool isFullyRemoved = true;
57   if (myBaseFeature)
58     isFullyRemoved = myStorage->removeEntity(myBaseFeature) && isFullyRemoved;
59   if (myRadiusConstraint)
60     isFullyRemoved = myStorage->removeConstraint(ConstraintPtr()) &&
61                      myStorage->remove(myRadiusConstraint) && isFullyRemoved;
62   return isFullyRemoved;
63 }