Salome HOME
Issue #1834: Fix length of lines
[modules/shaper.git] / src / SketchSolver / SketchSolver_Constraint.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include <SketchSolver_Constraint.h>
4 #include <SketchSolver_Group.h>
5 #include <SketchSolver_Error.h>
6 #include <SketchSolver_Manager.h>
7
8 #include <SketchPlugin_Arc.h>
9 #include <SketchPlugin_Circle.h>
10 #include <SketchPlugin_Line.h>
11 #include <SketchPlugin_Point.h>
12
13 #include <SketchPlugin_ConstraintAngle.h>
14 #include <SketchPlugin_ConstraintCoincidence.h>
15 #include <SketchPlugin_ConstraintCollinear.h>
16 #include <SketchPlugin_ConstraintDistance.h>
17 #include <SketchPlugin_ConstraintEqual.h>
18 #include <SketchPlugin_ConstraintHorizontal.h>
19 #include <SketchPlugin_ConstraintLength.h>
20 #include <SketchPlugin_ConstraintMiddle.h>
21 #include <SketchPlugin_ConstraintMirror.h>
22 #include <SketchPlugin_ConstraintParallel.h>
23 #include <SketchPlugin_ConstraintPerpendicular.h>
24 #include <SketchPlugin_ConstraintRadius.h>
25 #include <SketchPlugin_ConstraintRigid.h>
26 #include <SketchPlugin_ConstraintTangent.h>
27 #include <SketchPlugin_ConstraintVertical.h>
28
29 #include <GeomAPI_Dir2d.h>
30 #include <GeomDataAPI_Point.h>
31 #include <GeomDataAPI_Point2D.h>
32 #include <ModelAPI_AttributeDouble.h>
33 #include <ModelAPI_ResultConstruction.h>
34
35 #include <math.h>
36
37 SketchSolver_Constraint::SketchSolver_Constraint(
38     ConstraintPtr  theConstraint)
39   : myBaseConstraint(theConstraint),
40     myGroupID(GID_UNKNOWN),
41     myType(CONSTRAINT_UNKNOWN)
42 {
43 }
44
45 void SketchSolver_Constraint::process(StoragePtr theStorage,
46                                       const GroupID& theGroupID,
47                                       const EntityID& theSketchID)
48 {
49   myStorage = theStorage;
50   myGroupID = theGroupID;
51   mySketchID = theSketchID;
52   // Process constraint according to its type
53   process();
54 }
55
56
57 SketchSolver_ConstraintType SketchSolver_Constraint::TYPE(ConstraintPtr theConstraint)
58 {
59   const std::string& aType = theConstraint->getKind();
60   if (aType == SketchPlugin_ConstraintCoincidence::ID())
61     return CONSTRAINT_COINCIDENCE;
62   else if (aType == SketchPlugin_ConstraintRigid::ID())
63     return CONSTRAINT_FIXED;
64   else if (aType == SketchPlugin_ConstraintHorizontal::ID())
65     return CONSTRAINT_HORIZONTAL;
66   else if (aType == SketchPlugin_ConstraintVertical::ID())
67     return CONSTRAINT_VERTICAL;
68   else if (aType == SketchPlugin_ConstraintAngle::ID())
69     return CONSTRAINT_ANGLE;
70   else if (aType == SketchPlugin_ConstraintDistance::ID())
71     return CONSTRAINT_DISTANCE;
72   else if (aType == SketchPlugin_ConstraintEqual::ID())
73     return CONSTRAINT_EQUAL;
74   else if (aType == SketchPlugin_ConstraintLength::ID())
75     return CONSTRAINT_PT_PT_DISTANCE;
76   else if (aType == SketchPlugin_ConstraintMirror::ID())
77     return CONSTRAINT_SYMMETRIC;
78   else if (aType == SketchPlugin_ConstraintParallel::ID())
79     return CONSTRAINT_PARALLEL;
80   else if (aType == SketchPlugin_ConstraintPerpendicular::ID())
81     return CONSTRAINT_PERPENDICULAR;
82   else if (aType == SketchPlugin_ConstraintRadius::ID())
83     return CONSTRAINT_RADIUS;
84   else if (aType == SketchPlugin_ConstraintTangent::ID())
85     return CONSTRAINT_TANGENT;
86   else if (aType == SketchPlugin_ConstraintCollinear::ID())
87     return CONSTRAINT_COLLINEAR;
88   else if (aType == SketchPlugin_ConstraintMiddle::ID())
89     return CONSTRAINT_MIDDLE_POINT;
90   return CONSTRAINT_UNKNOWN;
91 }
92
93 void SketchSolver_Constraint::process()
94 {
95   cleanErrorMsg();
96   if (!myBaseConstraint || !myStorage || myGroupID == GID_UNKNOWN) {
97     // Not enough parameters are assigned
98     return;
99   }
100
101   SketchSolver_ConstraintType aConstrType = getType();
102   double aValue;
103   std::vector<EntityWrapperPtr> anAttributes;
104   getAttributes(aValue, anAttributes);
105   if (!myErrorMsg.empty())
106     return;
107   if (anAttributes.empty()) {
108     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
109     return;
110   }
111   if (aConstrType == CONSTRAINT_UNKNOWN)
112     aConstrType = getType();
113
114   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
115   std::list<ConstraintWrapperPtr> aNewConstraints = aBuilder->createConstraint(
116       myBaseConstraint, myGroupID, mySketchID, aConstrType,
117       aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
118   myStorage->addConstraint(myBaseConstraint, aNewConstraints);
119
120   adjustConstraint();
121 }
122
123 void SketchSolver_Constraint::update()
124 {
125   cleanErrorMsg();
126   std::list<ConstraintWrapperPtr> aWrapper = myStorage->constraint(myBaseConstraint);
127   std::list<ConstraintWrapperPtr>::iterator aWIt = aWrapper.begin();
128
129   // Check if attributes of constraint are changed, rebuild constraint
130   std::set<AttributePtr> anAttributes;
131   std::set<AttributePtr>::iterator aFoundAttr;
132   std::set<FeaturePtr> aFeatures;
133   std::set<FeaturePtr>::iterator aFoundFeat;
134   for (int anEntIndex = 0; anEntIndex < 4; ++anEntIndex) {
135     AttributePtr anAttr =
136         myBaseConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(anEntIndex));
137     if (!anAttr)
138       continue;
139
140     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
141     if (aRefAttr) {
142       if (aRefAttr->isObject()) {
143         FeaturePtr aFeat = ModelAPI_Feature::feature(aRefAttr->object());
144         if (myBaseConstraint->getKind() != SketchPlugin_ConstraintLength::ID())
145           aFeatures.insert(aFeat);
146         else {
147           // Workaround for the Length constraint: add points of line, not line itself
148           anAttributes.insert(aFeat->attribute(SketchPlugin_Line::START_ID()));
149           anAttributes.insert(aFeat->attribute(SketchPlugin_Line::END_ID()));
150         }
151       } else
152         anAttributes.insert(aRefAttr->attr());
153     } else
154       anAttributes.insert(anAttr);
155   }
156   bool hasNewAttr = !(anAttributes.empty() && aFeatures.empty());
157   for (; hasNewAttr && aWIt != aWrapper.end(); ++ aWIt) {
158     const std::list<EntityWrapperPtr>& aSubs = (*aWIt)->entities();
159     std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
160     for (; hasNewAttr && aSIt != aSubs.end(); ++aSIt) {
161       if ((*aSIt)->baseAttribute()) {
162         aFoundAttr = anAttributes.find((*aSIt)->baseAttribute());
163         if (aFoundAttr != anAttributes.end())
164           anAttributes.erase(aFoundAttr);
165       } else {
166         aFoundFeat = aFeatures.find((*aSIt)->baseFeature());
167         if (aFoundFeat != aFeatures.end())
168           aFeatures.erase(aFoundFeat);
169       }
170       hasNewAttr = !(anAttributes.empty() && aFeatures.empty());
171     }
172   }
173   if (hasNewAttr) {
174     remove();
175     process();
176     return;
177   }
178
179   AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
180       myBaseConstraint->attribute(SketchPlugin_Constraint::VALUE()));
181   if (aValueAttr) {
182     for (aWIt = aWrapper.begin(); aWIt != aWrapper.end(); ++aWIt)
183       if (fabs((*aWIt)->value() - aValueAttr->value()) > tolerance) {
184         (*aWIt)->setValue(aValueAttr->value());
185         myStorage->setNeedToResolve(true);
186       }
187   }
188   myStorage->addConstraint(myBaseConstraint, aWrapper);
189
190   adjustConstraint();
191 }
192
193 bool SketchSolver_Constraint::remove()
194 {
195   cleanErrorMsg();
196   myType = CONSTRAINT_UNKNOWN;
197   return myStorage->removeConstraint(myBaseConstraint);
198 }
199
200 void SketchSolver_Constraint::getAttributes(
201     double& theValue,
202     std::vector<EntityWrapperPtr>& theAttributes)
203 {
204   static const int anInitNbOfAttr = 4;
205   theAttributes.assign(anInitNbOfAttr, EntityWrapperPtr());
206   myAttributes.clear();
207
208   DataPtr aData = myBaseConstraint->data();
209   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
210
211   myType = TYPE(myBaseConstraint);
212
213   AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
214       aData->attribute(SketchPlugin_Constraint::VALUE()));
215   theValue = aValueAttr ? aValueAttr->value() : 0.0;
216
217   int aPtInd = 0; // index of first point in the list of attributes
218   int aEntInd = 2; // index of first entity in the list of attributes
219   std::list<AttributePtr> aConstrAttrs = aData->attributes(ModelAPI_AttributeRefAttr::typeId());
220   std::list<AttributePtr>::iterator anIter = aConstrAttrs.begin();
221   for (; anIter != aConstrAttrs.end(); anIter++) {
222     AttributeRefAttrPtr aRefAttr =
223         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
224     if (!aRefAttr || !aRefAttr->isInitialized()) {
225       myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
226       return;
227     }
228
229     myStorage->update(*anIter/*, myGroupID*/);
230     EntityWrapperPtr anEntity = myStorage->entity(*anIter);
231     if (!anEntity) {
232       // Force creation of an entity
233       myStorage->update(*anIter, GID_UNKNOWN, true);
234       anEntity = myStorage->entity(*anIter);
235     }
236     myAttributes.push_back(anEntity);
237
238     SketchSolver_EntityType aType = anEntity->type();
239     if (aType == ENTITY_UNKNOWN)
240       continue;
241     else if (aType == ENTITY_POINT)
242       theAttributes[aPtInd++] = anEntity; // the point is created
243     else { // another entity (not a point) is created
244       if (aEntInd < anInitNbOfAttr)
245         theAttributes[aEntInd] = anEntity;
246       else
247         theAttributes.push_back(anEntity);
248       aEntInd++;
249     }
250   }
251 }
252
253 bool SketchSolver_Constraint::isUsed(FeaturePtr theFeature) const
254 {
255   const std::list<ConstraintWrapperPtr>& aCList = myStorage->constraint(myBaseConstraint);
256   std::list<ConstraintWrapperPtr>::const_iterator aCIt = aCList.begin();
257   for (; aCIt != aCList.end(); ++aCIt)
258     if ((*aCIt)->isUsed(theFeature))
259       return true;
260
261   std::list<AttributePtr> anAttrList = 
262     theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
263   std::list<AttributePtr>::const_iterator anAttrIt = anAttrList.begin();
264   for (; anAttrIt != anAttrList.end(); ++ anAttrIt)
265     if (isUsed(*anAttrIt))
266       return true;
267
268   return false;
269 }
270
271 bool SketchSolver_Constraint::isUsed(AttributePtr theAttribute) const
272 {
273   AttributePtr anAttribute = theAttribute;
274   AttributeRefAttrPtr aRefAttr =
275       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
276   if (aRefAttr) {
277     if (aRefAttr->isObject())
278       return isUsed(ModelAPI_Feature::feature(aRefAttr->object()));
279     else
280       anAttribute = aRefAttr->attr();
281   }
282
283   const std::list<ConstraintWrapperPtr>& aCList = myStorage->constraint(myBaseConstraint);
284   std::list<ConstraintWrapperPtr>::const_iterator aCIt = aCList.begin();
285   for (; aCIt != aCList.end(); ++aCIt)
286     if ((*aCIt)->isUsed(theAttribute))
287       return true;
288   return false;
289 }