]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintManager.cpp
Salome HOME
86cb8bacd457dd4feef778ce8148fa9b7b6698f3
[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_ConstraintDistance.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   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;
122
123   // Constraint type
124   int aConstrType = getConstraintType(theConstraint);
125   if (aConstrType == 0) return false;
126
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());
133
134   return true;
135 }
136
137 Slvs_hEntity SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addEntity(
138                 boost::shared_ptr<ModelAPI_Attribute> theEntity)
139 {
140   /// \todo Should be implemented
141   return 0;
142 }
143
144 bool SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::addWorkplane(
145                 std::list< boost::shared_ptr<ModelAPI_Attribute> >& theParams)
146 {
147   /// \todo Should be implemented
148   return false;
149 }
150
151 int SketchSolver_ConstraintManager::SketchSolver_ConstraintGroup::getConstraintType(
152                 const boost::shared_ptr<SketchPlugin_Constraint>& theConstraint) const
153 {
154   if (theConstraint->getKind() == SketchPlugin_ConstraintDistance().getKind())
155   {
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
163     {
164 //      if (aEntA.get()) // entityA is an attribute of the constraint
165 //      {
166 //        if (aEntA->feature()->getKind() == SketchPlugin_Line().getKind()) // entityA is a line
167 //        {
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;
172 //          else
173 //            return SLVS_C_PT_LINE_DISTANCE;
174 //        }
175 //        /// \todo Implement other point-entity distances
176 //      }
177 //      else 
178       if (aPtB.get()) // ptB is an attribute of the constrtaint => point-point distance
179       {
180         if (aDistance->value() == 0.0)
181           return SLVS_C_POINTS_COINCIDENT;
182         else 
183           return SLVS_C_PT_PT_DISTANCE;
184       }
185     }
186     else if (aEntA.get() && !aEntB.get() && !aPtB.get())
187       return SLVS_C_DIAMETER;
188     return SLVS_C_UNKNOWN;
189   }
190   /// \todo Implement other kind of constrtaints
191
192   return SLVS_C_UNKNOWN;
193 }