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