1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include <SketchSolver_ConstraintFixed.h>
4 #include <SketchSolver_Error.h>
5 #include <SketchSolver_Manager.h>
7 #include <SketchPlugin_Arc.h>
8 #include <SketchPlugin_Circle.h>
9 #include <SketchPlugin_ConstraintRigid.h>
10 #include <SketchPlugin_Line.h>
11 #include <SketchPlugin_Point.h>
13 #include <GeomAPI_Pnt2d.h>
14 #include <GeomAPI_XY.h>
15 #include <GeomDataAPI_Point2D.h>
16 #include <ModelAPI_AttributeDouble.h>
20 SketchSolver_ConstraintFixed::SketchSolver_ConstraintFixed(ConstraintPtr theConstraint)
21 : SketchSolver_Constraint()
23 myBaseConstraint = theConstraint;
24 myType = CONSTRAINT_FIXED;
25 AttributeRefAttrPtr anAttribute =
26 theConstraint->refattr(SketchPlugin_ConstraintRigid::ENTITY_A());
27 if (anAttribute->isObject())
28 myFixedFeature = ModelAPI_Feature::feature(anAttribute->object());
30 myFixedAttribute = anAttribute->attr();
33 SketchSolver_ConstraintFixed::SketchSolver_ConstraintFixed(FeaturePtr theFeature)
34 : SketchSolver_Constraint(),
35 myBaseFeature(theFeature)
37 myType = CONSTRAINT_FIXED;
41 void SketchSolver_ConstraintFixed::process()
44 if ((!myBaseConstraint && !myBaseFeature) || !myStorage || myGroupID == GID_UNKNOWN) {
45 // Not enough parameters are assigned
49 ParameterWrapperPtr aValue;
50 std::vector<EntityWrapperPtr> anEntities;
51 getAttributes(aValue, anEntities);
52 if (!myErrorMsg.empty() || anEntities.empty())
54 fixFeature(anEntities.front());
57 void SketchSolver_ConstraintFixed::fixFeature(EntityWrapperPtr theFeature)
59 // extract feature from the group
60 if (theFeature->baseAttribute())
61 myStorage->update(theFeature->baseAttribute(), GID_OUTOFGROUP);
62 else if (theFeature->baseFeature())
63 myStorage->update(theFeature->baseFeature(), GID_OUTOFGROUP);
66 myStorage->addConstraint(myBaseConstraint, std::list<ConstraintWrapperPtr>());
69 void SketchSolver_ConstraintFixed::getAttributes(
70 ParameterWrapperPtr& theValue,
71 std::vector<EntityWrapperPtr>& theAttributes)
73 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
75 EntityWrapperPtr aSolverEntity;
77 // The feature is fixed.
78 myStorage->update(myBaseFeature/*, myGroupID*/);
79 aSolverEntity = myStorage->entity(myBaseFeature);
80 } else if (myBaseConstraint) {
81 // Constraint Fixed is added by user.
82 // Get the attribute of constraint (it should be alone in the list of constraints).
83 AttributePtr anAttr = myBaseConstraint->attribute(SketchPlugin_ConstraintRigid::ENTITY_A());
84 AttributeRefAttrPtr aRefAttr =
85 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
86 if (!aRefAttr || !aRefAttr->isInitialized()) {
87 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
91 myStorage->update(anAttr, myGroupID);
92 aSolverEntity = myStorage->entity(anAttr);
94 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
97 theAttributes.push_back(aSolverEntity);
101 bool SketchSolver_ConstraintFixed::remove()
104 // Move fixed entities back to the current group
105 FeaturePtr aFeature = myBaseFeature;
106 if (myBaseConstraint) {
108 aFeature = myFixedFeature;
109 else if (myFixedAttribute)
110 myStorage->update(AttributePtr(myFixedAttribute), myGroupID);
113 myStorage->update(aFeature, myGroupID);
114 myStorage->setNeedToResolve(true);
116 // Remove constraint or base feature
117 if (myBaseConstraint) {
118 bool isRemoved = false;
120 isRemoved = myStorage->removeEntity(aFeature);
121 return SketchSolver_Constraint::remove() || isRemoved;
122 } else if (myBaseFeature)
123 myStorage->removeEntity(myBaseFeature);