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