Salome HOME
The class for the points coincidence was renamed
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintManager.cpp
1 // File:    SketchSolver_ConstraintManager.cpp
2 // Created: 08 May 2014
3 // Author:  Artem ZHIDKOV
4
5 #include "SketchSolver_ConstraintManager.h"
6
7 #include <ModelAPI_AttributeDouble.h>
8 #include <ModelAPI_Data.h>
9 #include <SketchPlugin_Constraint.h>
10 #include <SketchPlugin_ConstraintCoincidence.h>
11 #include <SketchPlugin_Line.h>
12
13
14 /// This value is used to give unique index to the groups
15 static Slvs_hGroup myGroupIndexer = 0;
16
17 // ========================================================
18 // ========= SketchSolver_ConstraintManager ===============
19 // ========================================================
20 SketchSolver_ConstraintManager::SketchSolver_ConstraintManager()
21 {
22   myGroups.clear();
23 }
24
25 SketchSolver_ConstraintManager::~SketchSolver_ConstraintManager()
26 {
27   myGroups.clear();
28 }
29
30 void SketchSolver_ConstraintManager::findGroups(
31               boost::shared_ptr<SketchPlugin_Constraint> theConstraint, 
32               std::vector<Slvs_hGroup>&                  theGroupIDs) const
33 {
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());
38 }
39
40
41 // ========================================================
42 // =========  SketchSolver_ConstraintGroup  ===============
43 // ========================================================
44
45 SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::SketchSolver_ConstraintGroup()
46   : myID(++myGroupIndexer),
47     myParamMaxID(0),
48     myEntityMaxID(0),
49     myConstrMaxID(0),
50     myConstraintMap()
51 {
52   myParams.clear();
53   myEntities.clear();
54   myConstraints.clear();
55
56   // The workplane will be initialized on first constraint, so its handle is NULL meanwhile
57   myWorkplane.h = 0;
58
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;
64 }
65
66 SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::~SketchSolver_ConstraintGroup()
67 {
68   myParams.clear();
69   myEntities.clear();
70   myConstraints.clear();
71   myConstraintMap.clear();
72
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;
81 }
82
83 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::isInteract(
84                 boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
85 {
86   /// \todo Should be implemented
87   return false;
88 }
89
90 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addConstraint(
91                 boost::shared_ptr<SketchPlugin_Constraint> theConstraint)
92 {
93   if (myWorkplane.h == 0)
94   {
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))
99 //      return false;
100   }
101
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));
106   if (aDistAttr.get())
107     aDistance = aDistAttr->value();
108
109   /// \todo Specify the entities
110   Slvs_hEntity aPtA, aPtB, aEntityA, aEntityB; // parameters of the constraint
111   boost::shared_ptr<ModelAPI_Attribute> aEntAttr = theConstraint->data()->attribute(CONSTRAINT_ATTR_ENTITY_A);
112   aPtA = addEntity(aEntAttr);
113   if (aPtA == 0) return false;
114   aEntAttr = theConstraint->data()->attribute(CONSTRAINT_ATTR_ENTITY_B);
115   aPtB = addEntity(aEntAttr);
116   if (aPtB == 0) return false;
117   aEntAttr = theConstraint->data()->attribute(CONSTRAINT_ATTR_ENTITY_C);
118   aEntityA = addEntity(aEntAttr);
119   if (aEntityA == 0) return false;
120   aEntAttr = theConstraint->data()->attribute(CONSTRAINT_ATTR_ENTITY_D);
121   aEntityB = addEntity(aEntAttr);
122   if (aEntityB == 0) return false;
123
124   // Constraint type
125   int aConstrType = getConstraintType(theConstraint);
126   if (aConstrType == 0) return false;
127
128   // Create SolveSpace constraint structure
129   Slvs_Constraint aConstraint = 
130     Slvs_MakeConstraint(++myConstrMaxID, myID, aConstrType, myWorkplane.h, 
131                         aDistance, aPtA, aPtB, aEntityA, aEntityB);
132   myConstraints.push_back(aConstraint);
133   myConstraintMap[theConstraint] = *(myConstraints.rbegin());
134
135   return true;
136 }
137
138 Slvs_hEntity SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addEntity(
139                 boost::shared_ptr<ModelAPI_Attribute> theEntity)
140 {
141   /// \todo Should be implemented
142   return 0;
143 }
144
145 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addWorkplane(
146                 std::list< boost::shared_ptr<ModelAPI_Attribute> >& theParams)
147 {
148   /// \todo Should be implemented
149   return false;
150 }
151
152 int SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::getConstraintType(
153                 const boost::shared_ptr<SketchPlugin_Constraint>& theConstraint) const
154 {
155 //  if (theConstraint->getKind() == SketchPlugin_ConstraintDistance().getKind())
156 //  {
157 //    boost::shared_ptr<ModelAPI_Attribute> aPtA  = theConstraint->data()->attribute(CONSTRAINT_ATTR_POINT_A);
158 //    boost::shared_ptr<ModelAPI_Attribute> aPtB  = theConstraint->data()->attribute(CONSTRAINT_ATTR_POINT_B);
159 //    boost::shared_ptr<ModelAPI_Attribute> aEntA = theConstraint->data()->attribute(CONSTRAINT_ATTR_ENTITY_A);
160 //    boost::shared_ptr<ModelAPI_Attribute> aEntB = theConstraint->data()->attribute(CONSTRAINT_ATTR_ENTITY_B);
161 //    boost::shared_ptr<ModelAPI_AttributeDouble> aDistance = 
162 //      boost::shared_dynamic_cast<ModelAPI_AttributeDouble>(theConstraint->data()->attribute(CONSTRAINT_ATTR_VALUE));
163 //    if (aPtA.get()) // ptA is an attribute of the constraint
164 //    {
165 ////      if (aEntA.get()) // entityA is an attribute of the constraint
166 ////      {
167 ////        if (aEntA->feature()->getKind() == SketchPlugin_Line().getKind()) // entityA is a line
168 ////        {
169 ////          if (aEntB.get() && aEntB->feature()->getKind() == SketchPlugin_Line().getKind()) // entityB is a line too
170 ////            return SLVS_C_ANGLE;
171 ////          else if (aPtB.get()) // ptB is also an attribute of the constraint
172 ////            return SLVS_C_PROJ_PT_DISTANCE;
173 ////          else
174 ////            return SLVS_C_PT_LINE_DISTANCE;
175 ////        }
176 ////        /// \todo Implement other point-entity distances
177 ////      }
178 ////      else 
179 //      if (aPtB.get()) // ptB is an attribute of the constrtaint => point-point distance
180 //      {
181 //        if (aDistance->value() == 0.0)
182 //          return SLVS_C_POINTS_COINCIDENT;
183 //        else 
184 //          return SLVS_C_PT_PT_DISTANCE;
185 //      }
186 //    }
187 //    else if (aEntA.get() && !aEntB.get() && !aPtB.get())
188 //      return SLVS_C_DIAMETER;
189 //    return SLVS_C_UNKNOWN;
190 //  }
191   /// \todo Implement other kind of constrtaints
192
193   return SLVS_C_UNKNOWN;
194 }