Salome HOME
Revert "Merge branch 'Pre_2.8.0_development'"
[modules/shaper.git] / src / SketchSolver / SketchSolver_Constraint.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <SketchSolver_Constraint.h>
22 #include <SketchSolver_Error.h>
23
24 #include <PlaneGCSSolver_AttributeBuilder.h>
25 #include <PlaneGCSSolver_Tools.h>
26
27 #include <SketchPlugin_Arc.h>
28 #include <SketchPlugin_Circle.h>
29 #include <SketchPlugin_Line.h>
30 #include <SketchPlugin_Point.h>
31
32 #include <SketchPlugin_ConstraintAngle.h>
33 #include <SketchPlugin_ConstraintCoincidence.h>
34 #include <SketchPlugin_ConstraintCollinear.h>
35 #include <SketchPlugin_ConstraintDistance.h>
36 #include <SketchPlugin_ConstraintEqual.h>
37 #include <SketchPlugin_ConstraintHorizontal.h>
38 #include <SketchPlugin_ConstraintLength.h>
39 #include <SketchPlugin_ConstraintMiddle.h>
40 #include <SketchPlugin_ConstraintMirror.h>
41 #include <SketchPlugin_ConstraintParallel.h>
42 #include <SketchPlugin_ConstraintPerpendicular.h>
43 #include <SketchPlugin_ConstraintRadius.h>
44 #include <SketchPlugin_ConstraintRigid.h>
45 #include <SketchPlugin_ConstraintTangent.h>
46 #include <SketchPlugin_ConstraintVertical.h>
47
48 #include <GeomAPI_Dir2d.h>
49 #include <GeomDataAPI_Point.h>
50 #include <GeomDataAPI_Point2D.h>
51 #include <ModelAPI_AttributeDouble.h>
52 #include <ModelAPI_ResultConstruction.h>
53
54 #include <math.h>
55
56 SketchSolver_Constraint::SketchSolver_Constraint(
57     ConstraintPtr  theConstraint)
58   : myBaseConstraint(theConstraint),
59     myType(CONSTRAINT_UNKNOWN)
60 {
61 }
62
63 void SketchSolver_Constraint::process(StoragePtr theStorage, bool theEvensBlocked)
64 {
65   myStorage = theStorage;
66   blockEvents(theEvensBlocked);
67   // Process constraint according to its type
68   process();
69 }
70
71 void SketchSolver_Constraint::blockEvents(bool isBlocked)
72 {
73   myBaseConstraint->data()->blockSendAttributeUpdated(isBlocked);
74 }
75
76
77 SketchSolver_ConstraintType SketchSolver_Constraint::TYPE(ConstraintPtr theConstraint)
78 {
79   const std::string& aType = theConstraint->getKind();
80   if (aType == SketchPlugin_ConstraintCoincidence::ID())
81     return CONSTRAINT_COINCIDENCE;
82   else if (aType == SketchPlugin_ConstraintRigid::ID())
83     return CONSTRAINT_FIXED;
84   else if (aType == SketchPlugin_ConstraintHorizontal::ID())
85     return CONSTRAINT_HORIZONTAL;
86   else if (aType == SketchPlugin_ConstraintVertical::ID())
87     return CONSTRAINT_VERTICAL;
88   else if (aType == SketchPlugin_ConstraintAngle::ID())
89     return CONSTRAINT_ANGLE;
90   else if (aType == SketchPlugin_ConstraintDistance::ID())
91     return CONSTRAINT_DISTANCE;
92   else if (aType == SketchPlugin_ConstraintEqual::ID())
93     return CONSTRAINT_EQUAL;
94   else if (aType == SketchPlugin_ConstraintLength::ID())
95     return CONSTRAINT_PT_PT_DISTANCE;
96   else if (aType == SketchPlugin_ConstraintMirror::ID())
97     return CONSTRAINT_SYMMETRIC;
98   else if (aType == SketchPlugin_ConstraintParallel::ID())
99     return CONSTRAINT_PARALLEL;
100   else if (aType == SketchPlugin_ConstraintPerpendicular::ID())
101     return CONSTRAINT_PERPENDICULAR;
102   else if (aType == SketchPlugin_ConstraintRadius::ID())
103     return CONSTRAINT_RADIUS;
104   else if (aType == SketchPlugin_ConstraintTangent::ID())
105     return CONSTRAINT_TANGENT;
106   else if (aType == SketchPlugin_ConstraintCollinear::ID())
107     return CONSTRAINT_COLLINEAR;
108   else if (aType == SketchPlugin_ConstraintMiddle::ID())
109     return CONSTRAINT_MIDDLE_POINT;
110   return CONSTRAINT_UNKNOWN;
111 }
112
113 void SketchSolver_Constraint::process()
114 {
115   cleanErrorMsg();
116   if (!myBaseConstraint || !myStorage) {
117     // Not enough parameters are assigned
118     return;
119   }
120
121   SketchSolver_ConstraintType aConstrType = getType();
122   EntityWrapperPtr aValue;
123   std::vector<EntityWrapperPtr> anAttributes;
124   getAttributes(aValue, anAttributes);
125   if (!myErrorMsg.empty())
126     return;
127   if (anAttributes.empty()) {
128     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
129     return;
130   }
131   if (aConstrType == CONSTRAINT_UNKNOWN)
132     aConstrType = getType();
133
134   ConstraintWrapperPtr aNewConstraint = PlaneGCSSolver_Tools::createConstraint(
135       myBaseConstraint, aConstrType,
136       aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
137   myStorage->addConstraint(myBaseConstraint, aNewConstraint);
138
139   adjustConstraint();
140 }
141
142 void SketchSolver_Constraint::update()
143 {
144   cleanErrorMsg();
145
146   // Get list of attributes of the constraint and compare it with previously stored.
147   // If the lists are different, fully rebuild constraint
148   std::set<EntityWrapperPtr> anAttributes;
149   for (int anEntIndex = 0; anEntIndex < 4; ++anEntIndex) {
150     AttributePtr anAttr =
151         myBaseConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(anEntIndex));
152     if (!anAttr)
153       continue;
154
155     if (myBaseConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
156       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
157       FeaturePtr aFeat = ModelAPI_Feature::feature(aRefAttr->object());
158       if (aFeat) {
159         // Workaround for the Length constraint: add points of line, not line itself
160         anAttributes.insert(myStorage->entity(aFeat->attribute(SketchPlugin_Line::START_ID())));
161         anAttributes.insert(myStorage->entity(aFeat->attribute(SketchPlugin_Line::END_ID())));
162       }
163     } else
164       anAttributes.insert(myStorage->entity(anAttr));
165   }
166
167   std::set<EntityWrapperPtr>::iterator aFound;
168   std::list<EntityWrapperPtr>::const_iterator anAttrIt = myAttributes.begin();
169   for (; anAttrIt != myAttributes.end() && !anAttributes.empty(); ++anAttrIt)
170     anAttributes.erase(*anAttrIt);
171
172   if (!anAttributes.empty()) {
173     remove();
174     process();
175     return;
176   }
177
178   AttributePtr aValueAttr = myBaseConstraint->attribute(SketchPlugin_Constraint::VALUE());
179   if (aValueAttr)
180     myStorage->update(aValueAttr);
181
182   adjustConstraint();
183 }
184
185 bool SketchSolver_Constraint::remove()
186 {
187   cleanErrorMsg();
188   myType = CONSTRAINT_UNKNOWN;
189   myStorage->unsubscribeUpdates(this);
190   return myStorage->removeConstraint(myBaseConstraint);
191 }
192
193 void SketchSolver_Constraint::getAttributes(
194     EntityWrapperPtr& theValue,
195     std::vector<EntityWrapperPtr>& theAttributes)
196 {
197   static const int anInitNbOfAttr = 4;
198   theAttributes.assign(anInitNbOfAttr, EntityWrapperPtr());
199   myAttributes.clear();
200
201   DataPtr aData = myBaseConstraint->data();
202
203   myType = TYPE(myBaseConstraint);
204
205   AttributePtr aValueAttr = aData->attribute(SketchPlugin_Constraint::VALUE());
206   if (aValueAttr) {
207     PlaneGCSSolver_AttributeBuilder aValueBuilder;
208     theValue = aValueBuilder.createAttribute(aValueAttr);
209     myStorage->addEntity(aValueAttr, theValue);
210   }
211
212   int aPtInd = 0; // index of first point in the list of attributes
213   int aEntInd = 2; // index of first entity in the list of attributes
214   std::list<AttributePtr> aConstrAttrs = aData->attributes(ModelAPI_AttributeRefAttr::typeId());
215   std::list<AttributePtr>::iterator anIter = aConstrAttrs.begin();
216   for (; anIter != aConstrAttrs.end(); anIter++) {
217     AttributeRefAttrPtr aRefAttr =
218         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
219     if (!aRefAttr || !aRefAttr->isInitialized()) {
220       myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
221       return;
222     }
223
224     myStorage->update(*anIter, true);
225     EntityWrapperPtr anEntity = myStorage->entity(*anIter);
226     myAttributes.push_back(anEntity);
227
228     SketchSolver_EntityType aType = anEntity->type();
229     if (aType == ENTITY_UNKNOWN)
230       continue;
231     else if (aType == ENTITY_POINT)
232       theAttributes[aPtInd++] = anEntity; // the point is created
233     else { // another entity (not a point) is created
234       if (aEntInd < anInitNbOfAttr)
235         theAttributes[aEntInd] = anEntity;
236       else
237         theAttributes.push_back(anEntity);
238       aEntInd++;
239     }
240   }
241 }