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