Salome HOME
Task 2.7. Horizontal and Vertical Distance constraint
[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_Error.h>
5
6 #include <PlaneGCSSolver_AttributeBuilder.h>
7 #include <PlaneGCSSolver_Tools.h>
8
9 #include <SketchPlugin_Arc.h>
10 #include <SketchPlugin_Circle.h>
11 #include <SketchPlugin_Line.h>
12 #include <SketchPlugin_Point.h>
13
14 #include <SketchPlugin_ConstraintAngle.h>
15 #include <SketchPlugin_ConstraintCoincidence.h>
16 #include <SketchPlugin_ConstraintCollinear.h>
17 #include <SketchPlugin_ConstraintDistance.h>
18 #include <SketchPlugin_ConstraintEqual.h>
19 #include <SketchPlugin_ConstraintHorizontal.h>
20 #include <SketchPlugin_ConstraintLength.h>
21 #include <SketchPlugin_ConstraintMiddle.h>
22 #include <SketchPlugin_ConstraintMirror.h>
23 #include <SketchPlugin_ConstraintParallel.h>
24 #include <SketchPlugin_ConstraintPerpendicular.h>
25 #include <SketchPlugin_ConstraintRadius.h>
26 #include <SketchPlugin_ConstraintRigid.h>
27 #include <SketchPlugin_ConstraintTangent.h>
28 #include <SketchPlugin_ConstraintVertical.h>
29
30 #include <GeomAPI_Dir2d.h>
31 #include <GeomDataAPI_Point.h>
32 #include <GeomDataAPI_Point2D.h>
33 #include <ModelAPI_AttributeDouble.h>
34 #include <ModelAPI_ResultConstruction.h>
35
36 #include <math.h>
37
38 SketchSolver_Constraint::SketchSolver_Constraint(
39     ConstraintPtr  theConstraint)
40   : myBaseConstraint(theConstraint),
41     myType(CONSTRAINT_UNKNOWN)
42 {
43 }
44
45 void SketchSolver_Constraint::process(StoragePtr theStorage, bool theEvensBlocked)
46 {
47   myStorage = theStorage;
48   blockEvents(theEvensBlocked);
49   // Process constraint according to its type
50   process();
51 }
52
53 void SketchSolver_Constraint::blockEvents(bool isBlocked)
54 {
55   myBaseConstraint->data()->blockSendAttributeUpdated(isBlocked);
56 }
57
58
59 SketchSolver_ConstraintType SketchSolver_Constraint::TYPE(ConstraintPtr theConstraint)
60 {
61   const std::string& aType = theConstraint->getKind();
62   if (aType == SketchPlugin_ConstraintCoincidence::ID())
63     return CONSTRAINT_COINCIDENCE;
64   else if (aType == SketchPlugin_ConstraintRigid::ID())
65     return CONSTRAINT_FIXED;
66   else if (aType == SketchPlugin_ConstraintHorizontal::ID())
67     return CONSTRAINT_HORIZONTAL;
68   else if (aType == SketchPlugin_ConstraintVertical::ID())
69     return CONSTRAINT_VERTICAL;
70   else if (aType == SketchPlugin_ConstraintAngle::ID())
71     return CONSTRAINT_ANGLE;
72   else if (aType == SketchPlugin_ConstraintDistance::ID())
73     return CONSTRAINT_DISTANCE;
74   else if (aType == SketchPlugin_ConstraintEqual::ID())
75     return CONSTRAINT_EQUAL;
76   else if (aType == SketchPlugin_ConstraintLength::ID())
77     return CONSTRAINT_PT_PT_DISTANCE;
78   else if (aType == SketchPlugin_ConstraintMirror::ID())
79     return CONSTRAINT_SYMMETRIC;
80   else if (aType == SketchPlugin_ConstraintParallel::ID())
81     return CONSTRAINT_PARALLEL;
82   else if (aType == SketchPlugin_ConstraintPerpendicular::ID())
83     return CONSTRAINT_PERPENDICULAR;
84   else if (aType == SketchPlugin_ConstraintRadius::ID())
85     return CONSTRAINT_RADIUS;
86   else if (aType == SketchPlugin_ConstraintTangent::ID())
87     return CONSTRAINT_TANGENT;
88   else if (aType == SketchPlugin_ConstraintCollinear::ID())
89     return CONSTRAINT_COLLINEAR;
90   else if (aType == SketchPlugin_ConstraintMiddle::ID())
91     return CONSTRAINT_MIDDLE_POINT;
92   return CONSTRAINT_UNKNOWN;
93 }
94
95 void SketchSolver_Constraint::process()
96 {
97   cleanErrorMsg();
98   if (!myBaseConstraint || !myStorage) {
99     // Not enough parameters are assigned
100     return;
101   }
102
103   SketchSolver_ConstraintType aConstrType = getType();
104   EntityWrapperPtr aValue;
105   std::vector<EntityWrapperPtr> anAttributes;
106   getAttributes(aValue, anAttributes);
107   if (!myErrorMsg.empty())
108     return;
109   if (anAttributes.empty()) {
110     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
111     return;
112   }
113   if (aConstrType == CONSTRAINT_UNKNOWN)
114     aConstrType = getType();
115
116   ConstraintWrapperPtr aNewConstraint = PlaneGCSSolver_Tools::createConstraint(
117       myBaseConstraint, aConstrType,
118       aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
119   if (!aNewConstraint) {
120     myErrorMsg = SketchSolver_Error::WRONG_CONSTRAINT_TYPE();
121     return;
122   }
123   myStorage->addConstraint(myBaseConstraint, aNewConstraint);
124
125   adjustConstraint();
126 }
127
128 void SketchSolver_Constraint::update()
129 {
130   cleanErrorMsg();
131
132   // Get list of attributes of the constraint and compare it with previously stored.
133   // If the lists are different, fully rebuild constraint
134   std::set<EntityWrapperPtr> anAttributes;
135   for (int anEntIndex = 0; anEntIndex < 4; ++anEntIndex) {
136     AttributePtr anAttr =
137         myBaseConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(anEntIndex));
138     if (!anAttr)
139       continue;
140
141     if (myBaseConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
142       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
143       FeaturePtr aFeat = ModelAPI_Feature::feature(aRefAttr->object());
144       if (aFeat) {
145         // Workaround for the Length constraint: add points of line, not line itself
146         anAttributes.insert(myStorage->entity(aFeat->attribute(SketchPlugin_Line::START_ID())));
147         anAttributes.insert(myStorage->entity(aFeat->attribute(SketchPlugin_Line::END_ID())));
148       }
149     } else
150       anAttributes.insert(myStorage->entity(anAttr));
151   }
152
153   std::set<EntityWrapperPtr>::iterator aFound;
154   std::list<EntityWrapperPtr>::const_iterator anAttrIt = myAttributes.begin();
155   for (; anAttrIt != myAttributes.end() && !anAttributes.empty(); ++anAttrIt)
156     anAttributes.erase(*anAttrIt);
157
158   if (!anAttributes.empty()) {
159     remove();
160     process();
161     return;
162   }
163
164   AttributePtr aValueAttr = myBaseConstraint->attribute(SketchPlugin_Constraint::VALUE());
165   if (aValueAttr)
166     myStorage->update(aValueAttr);
167
168   adjustConstraint();
169 }
170
171 bool SketchSolver_Constraint::remove()
172 {
173   cleanErrorMsg();
174   myType = CONSTRAINT_UNKNOWN;
175   myStorage->unsubscribeUpdates(this);
176   return myStorage->removeConstraint(myBaseConstraint);
177 }
178
179 void SketchSolver_Constraint::getAttributes(
180     EntityWrapperPtr& theValue,
181     std::vector<EntityWrapperPtr>& theAttributes)
182 {
183   static const int anInitNbOfAttr = 4;
184   theAttributes.assign(anInitNbOfAttr, EntityWrapperPtr());
185   myAttributes.clear();
186
187   DataPtr aData = myBaseConstraint->data();
188
189   myType = TYPE(myBaseConstraint);
190
191   AttributePtr aValueAttr = aData->attribute(SketchPlugin_Constraint::VALUE());
192   if (aValueAttr) {
193     PlaneGCSSolver_AttributeBuilder aValueBuilder;
194     theValue = aValueBuilder.createAttribute(aValueAttr);
195     myStorage->addEntity(aValueAttr, theValue);
196   }
197
198   int aPtInd = 0; // index of first point in the list of attributes
199   int aEntInd = 2; // index of first entity in the list of attributes
200   std::list<AttributePtr> aConstrAttrs = aData->attributes(ModelAPI_AttributeRefAttr::typeId());
201   std::list<AttributePtr>::iterator anIter = aConstrAttrs.begin();
202   for (; anIter != aConstrAttrs.end(); anIter++) {
203     AttributeRefAttrPtr aRefAttr =
204         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
205     if (!aRefAttr || !aRefAttr->isInitialized()) {
206       myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
207       return;
208     }
209
210     myStorage->update(*anIter, true);
211     EntityWrapperPtr anEntity = myStorage->entity(*anIter);
212     myAttributes.push_back(anEntity);
213
214     SketchSolver_EntityType aType = anEntity->type();
215     if (aType == ENTITY_UNKNOWN)
216       continue;
217     else if (aType == ENTITY_POINT)
218       theAttributes[aPtInd++] = anEntity; // the point is created
219     else { // another entity (not a point) is created
220       if (aEntInd < anInitNbOfAttr)
221         theAttributes[aEntInd] = anEntity;
222       else
223         theAttributes.push_back(anEntity);
224       aEntInd++;
225     }
226   }
227 }