]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_Constraint.cpp
Salome HOME
Merge branch 'Dev_GroupsRevision'
[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   if (!aNewConstraint) {
138     myErrorMsg = SketchSolver_Error::WRONG_CONSTRAINT_TYPE();
139     return;
140   }
141   myStorage->addConstraint(myBaseConstraint, aNewConstraint);
142
143   adjustConstraint();
144 }
145
146 void SketchSolver_Constraint::update()
147 {
148   cleanErrorMsg();
149
150   // Get list of attributes of the constraint and compare it with previously stored.
151   // If the lists are different, fully rebuild constraint
152   std::set<EntityWrapperPtr> anAttributes;
153   for (int anEntIndex = 0; anEntIndex < 4; ++anEntIndex) {
154     AttributePtr anAttr =
155         myBaseConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(anEntIndex));
156     if (!anAttr)
157       continue;
158
159     if (myBaseConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
160       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
161       FeaturePtr aFeat = ModelAPI_Feature::feature(aRefAttr->object());
162       if (aFeat) {
163         // Workaround for the Length constraint: add points of line, not line itself
164         anAttributes.insert(myStorage->entity(aFeat->attribute(SketchPlugin_Line::START_ID())));
165         anAttributes.insert(myStorage->entity(aFeat->attribute(SketchPlugin_Line::END_ID())));
166       }
167     } else
168       anAttributes.insert(myStorage->entity(anAttr));
169   }
170
171   std::set<EntityWrapperPtr>::iterator aFound;
172   std::list<EntityWrapperPtr>::const_iterator anAttrIt = myAttributes.begin();
173   for (; anAttrIt != myAttributes.end() && !anAttributes.empty(); ++anAttrIt)
174     anAttributes.erase(*anAttrIt);
175
176   if (!anAttributes.empty()) {
177     remove();
178     process();
179     return;
180   }
181
182   AttributePtr aValueAttr = myBaseConstraint->attribute(SketchPlugin_Constraint::VALUE());
183   if (aValueAttr)
184     myStorage->update(aValueAttr);
185
186   adjustConstraint();
187 }
188
189 bool SketchSolver_Constraint::remove()
190 {
191   cleanErrorMsg();
192   myType = CONSTRAINT_UNKNOWN;
193   myStorage->unsubscribeUpdates(this);
194   return myStorage->removeConstraint(myBaseConstraint);
195 }
196
197 void SketchSolver_Constraint::getAttributes(
198     EntityWrapperPtr& theValue,
199     std::vector<EntityWrapperPtr>& theAttributes)
200 {
201   static const int anInitNbOfAttr = 4;
202   theAttributes.assign(anInitNbOfAttr, EntityWrapperPtr());
203   myAttributes.clear();
204
205   DataPtr aData = myBaseConstraint->data();
206
207   myType = TYPE(myBaseConstraint);
208
209   AttributePtr aValueAttr = aData->attribute(SketchPlugin_Constraint::VALUE());
210   if (aValueAttr) {
211     PlaneGCSSolver_AttributeBuilder aValueBuilder;
212     theValue = aValueBuilder.createAttribute(aValueAttr);
213     myStorage->addEntity(aValueAttr, theValue);
214   }
215
216   int aPtInd = 0; // index of first point in the list of attributes
217   int aEntInd = 2; // index of first entity in the list of attributes
218   std::list<AttributePtr> aConstrAttrs = aData->attributes(ModelAPI_AttributeRefAttr::typeId());
219   std::list<AttributePtr>::iterator anIter = aConstrAttrs.begin();
220   for (; anIter != aConstrAttrs.end(); anIter++) {
221     AttributeRefAttrPtr aRefAttr =
222         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
223     if (!aRefAttr || !aRefAttr->isInitialized()) {
224       myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
225       return;
226     }
227
228     myStorage->update(*anIter, true);
229     EntityWrapperPtr anEntity = myStorage->entity(*anIter);
230     myAttributes.push_back(anEntity);
231
232     SketchSolver_EntityType aType = anEntity->type();
233     if (aType == ENTITY_UNKNOWN)
234       continue;
235     else if (aType == ENTITY_POINT)
236       theAttributes[aPtInd++] = anEntity; // the point is created
237     else { // another entity (not a point) is created
238       if (aEntInd < anInitNbOfAttr)
239         theAttributes[aEntInd] = anEntity;
240       else
241         theAttributes.push_back(anEntity);
242       aEntInd++;
243     }
244   }
245 }