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