1 // File: SketchSolver_ConstraintManager.cpp
2 // Created: 08 May 2014
3 // Author: Artem ZHIDKOV
5 #include "SketchSolver_ConstraintManager.h"
7 #include <ModelAPI_AttributeDouble.h>
8 #include <ModelAPI_Data.h>
9 #include <SketchPlugin_Constraint.h>
10 #include <SketchPlugin_ConstraintPointsCoincident.h>
11 #include <SketchPlugin_Line.h>
14 /// This value is used to give unique index to the groups
15 static Slvs_hGroup myGroupIndexer = 0;
17 // ========================================================
18 // ========= SketchSolver_ConstraintManager ===============
19 // ========================================================
20 SketchSolver_ConstraintManager::SketchSolver_ConstraintManager()
25 SketchSolver_ConstraintManager::~SketchSolver_ConstraintManager()
30 void SketchSolver_ConstraintManager::findGroups(
31 boost::shared_ptr<SketchPlugin_Constraint> theConstraint,
32 std::vector<Slvs_hGroup>& theGroupIDs) const
34 std::vector<SketchSolver_ConstraintGroup>::const_iterator aGroupIter;
35 for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
36 if (aGroupIter->isInteract(theConstraint))
37 theGroupIDs.push_back(aGroupIter->getId());
41 // ========================================================
42 // ========= SketchSolver_ConstraintGroup ===============
43 // ========================================================
45 SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::SketchSolver_ConstraintGroup()
46 : myID(++myGroupIndexer),
54 myConstraints.clear();
56 // The workplane will be initialized on first constraint, so its handle is NULL meanwhile
59 // Nullify all elements of the set of equations
60 myConstrSet.param = 0;
61 myConstrSet.entity = 0;
62 myConstrSet.constraint = 0;
63 myConstrSet.failed = 0;
66 SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::~SketchSolver_ConstraintGroup()
70 myConstraints.clear();
71 myConstraintMap.clear();
73 if (myConstrSet.param)
74 delete [] myConstrSet.param;
75 if (myConstrSet.entity)
76 delete [] myConstrSet.entity;
77 if (myConstrSet.constraint)
78 delete [] myConstrSet.constraint;
79 if (myConstrSet.failed)
80 delete [] myConstrSet.failed;
83 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::isInteract(
84 boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
86 /// \todo Should be implemented
90 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addConstraint(
91 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
93 if (myWorkplane.h == 0)
95 // // Create workplane when first constraint is added
96 // std::list< boost::shared_ptr<ModelAPI_Attribute> > aWPAttr;
97 // theConstraint->getSketchParameters(aWPAttr);
98 // if (!addWorkplane(aWPAttr))
102 // Create constraint parameters
103 double aDistance = 0.0; // scalar value of the constraint
104 boost::shared_ptr<ModelAPI_AttributeDouble> aDistAttr =
105 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theConstraint->data()->attribute(CONSTRAINT_ATTR_VALUE));
107 aDistance = aDistAttr->value();
109 Slvs_hEntity aPtA, aPtB, aEntityA, aEntityB; // parameters of the constraint
110 boost::shared_ptr<ModelAPI_Attribute> aEntAttr = theConstraint->data()->attribute(CONSTRAINT_ATTR_POINT_A);
111 aPtA = addEntity(aEntAttr);
112 if (aPtA == 0) return false;
113 aEntAttr = theConstraint->data()->attribute(CONSTRAINT_ATTR_POINT_B);
114 aPtB = addEntity(aEntAttr);
115 if (aPtB == 0) return false;
116 aEntAttr = theConstraint->data()->attribute(CONSTRAINT_ATTR_ENTITY_A);
117 aEntityA = addEntity(aEntAttr);
118 if (aEntityA == 0) return false;
119 aEntAttr = theConstraint->data()->attribute(CONSTRAINT_ATTR_ENTITY_B);
120 aEntityB = addEntity(aEntAttr);
121 if (aEntityB == 0) return false;
124 int aConstrType = getConstraintType(theConstraint);
125 if (aConstrType == 0) return false;
127 // Create SolveSpace constraint structure
128 Slvs_Constraint aConstraint =
129 Slvs_MakeConstraint(++myConstrMaxID, myID, aConstrType, myWorkplane.h,
130 aDistance, aPtA, aPtB, aEntityA, aEntityB);
131 myConstraints.push_back(aConstraint);
132 myConstraintMap[theConstraint] = *(myConstraints.rbegin());
137 Slvs_hEntity SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addEntity(
138 boost::shared_ptr<ModelAPI_Attribute> theEntity)
140 /// \todo Should be implemented
144 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addWorkplane(
145 std::list< boost::shared_ptr<ModelAPI_Attribute> >& theParams)
147 /// \todo Should be implemented
151 int SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::getConstraintType(
152 const boost::shared_ptr<SketchPlugin_Constraint>& theConstraint) const
154 // if (theConstraint->getKind() == SketchPlugin_ConstraintDistance().getKind())
156 // boost::shared_ptr<ModelAPI_Attribute> aPtA = theConstraint->data()->attribute(CONSTRAINT_ATTR_POINT_A);
157 // boost::shared_ptr<ModelAPI_Attribute> aPtB = theConstraint->data()->attribute(CONSTRAINT_ATTR_POINT_B);
158 // boost::shared_ptr<ModelAPI_Attribute> aEntA = theConstraint->data()->attribute(CONSTRAINT_ATTR_ENTITY_A);
159 // boost::shared_ptr<ModelAPI_Attribute> aEntB = theConstraint->data()->attribute(CONSTRAINT_ATTR_ENTITY_B);
160 // boost::shared_ptr<ModelAPI_AttributeDouble> aDistance =
161 // boost::shared_dynamic_cast<ModelAPI_AttributeDouble>(theConstraint->data()->attribute(CONSTRAINT_ATTR_VALUE));
162 // if (aPtA.get()) // ptA is an attribute of the constraint
164 //// if (aEntA.get()) // entityA is an attribute of the constraint
166 //// if (aEntA->feature()->getKind() == SketchPlugin_Line().getKind()) // entityA is a line
168 //// if (aEntB.get() && aEntB->feature()->getKind() == SketchPlugin_Line().getKind()) // entityB is a line too
169 //// return SLVS_C_ANGLE;
170 //// else if (aPtB.get()) // ptB is also an attribute of the constraint
171 //// return SLVS_C_PROJ_PT_DISTANCE;
173 //// return SLVS_C_PT_LINE_DISTANCE;
175 //// /// \todo Implement other point-entity distances
178 // if (aPtB.get()) // ptB is an attribute of the constrtaint => point-point distance
180 // if (aDistance->value() == 0.0)
181 // return SLVS_C_POINTS_COINCIDENT;
183 // return SLVS_C_PT_PT_DISTANCE;
186 // else if (aEntA.get() && !aEntB.get() && !aPtB.get())
187 // return SLVS_C_DIAMETER;
188 // return SLVS_C_UNKNOWN;
190 /// \todo Implement other kind of constrtaints
192 return SLVS_C_UNKNOWN;