Salome HOME
updated copyright message
[modules/shaper.git] / src / SketchSolver / SketchSolver_Constraint.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
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_ConstraintCoincidenceInternal.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       aType == SketchPlugin_ConstraintCoincidenceInternal::ID())
82     return CONSTRAINT_COINCIDENCE;
83   else if (aType == SketchPlugin_ConstraintRigid::ID())
84     return CONSTRAINT_FIXED;
85   else if (aType == SketchPlugin_ConstraintHorizontal::ID())
86     return CONSTRAINT_HORIZONTAL;
87   else if (aType == SketchPlugin_ConstraintVertical::ID())
88     return CONSTRAINT_VERTICAL;
89   else if (aType == SketchPlugin_ConstraintAngle::ID())
90     return CONSTRAINT_ANGLE;
91   else if (aType == SketchPlugin_ConstraintDistance::ID())
92     return CONSTRAINT_DISTANCE;
93   else if (aType == SketchPlugin_ConstraintEqual::ID())
94     return CONSTRAINT_EQUAL;
95   else if (aType == SketchPlugin_ConstraintLength::ID())
96     return CONSTRAINT_PT_PT_DISTANCE;
97   else if (aType == SketchPlugin_ConstraintMirror::ID())
98     return CONSTRAINT_SYMMETRIC;
99   else if (aType == SketchPlugin_ConstraintParallel::ID())
100     return CONSTRAINT_PARALLEL;
101   else if (aType == SketchPlugin_ConstraintPerpendicular::ID())
102     return CONSTRAINT_PERPENDICULAR;
103   else if (aType == SketchPlugin_ConstraintRadius::ID())
104     return CONSTRAINT_RADIUS;
105   else if (aType == SketchPlugin_ConstraintTangent::ID())
106     return CONSTRAINT_TANGENT;
107   else if (aType == SketchPlugin_ConstraintCollinear::ID())
108     return CONSTRAINT_COLLINEAR;
109   else if (aType == SketchPlugin_ConstraintMiddle::ID())
110     return CONSTRAINT_MIDDLE_POINT;
111   return CONSTRAINT_UNKNOWN;
112 }
113
114 void SketchSolver_Constraint::process()
115 {
116   cleanErrorMsg();
117   if (!myBaseConstraint || !myStorage) {
118     // Not enough parameters are assigned
119     return;
120   }
121
122   SketchSolver_ConstraintType aConstrType = getType();
123   EntityWrapperPtr aValue;
124   std::vector<EntityWrapperPtr> anAttributes;
125   getAttributes(aValue, anAttributes);
126   if (!myErrorMsg.empty())
127     return;
128   if (anAttributes.empty()) {
129     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
130     return;
131   }
132   if (aConstrType == CONSTRAINT_UNKNOWN)
133     aConstrType = getType();
134
135   ConstraintWrapperPtr aNewConstraint = PlaneGCSSolver_Tools::createConstraint(
136       myBaseConstraint, aConstrType,
137       aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
138   if (!aNewConstraint) {
139     myErrorMsg = SketchSolver_Error::WRONG_CONSTRAINT_TYPE();
140     return;
141   }
142   myStorage->addConstraint(myBaseConstraint, aNewConstraint);
143
144   adjustConstraint();
145 }
146
147 void SketchSolver_Constraint::update()
148 {
149   cleanErrorMsg();
150
151   // Get list of attributes of the constraint and compare it with previously stored.
152   // If the lists are different, fully rebuild constraint
153   std::set<EntityWrapperPtr> anAttributes;
154   for (int anEntIndex = 0; anEntIndex < 4; ++anEntIndex) {
155     AttributePtr anAttr =
156         myBaseConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(anEntIndex));
157     if (!anAttr)
158       continue;
159
160     if (myBaseConstraint->getKind() == SketchPlugin_ConstraintLength::ID()) {
161       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
162       FeaturePtr aFeat = ModelAPI_Feature::feature(aRefAttr->object());
163       if (aFeat) {
164         // Workaround for the Length constraint: add points of line, not line itself
165         anAttributes.insert(myStorage->entity(aFeat->attribute(SketchPlugin_Line::START_ID())));
166         anAttributes.insert(myStorage->entity(aFeat->attribute(SketchPlugin_Line::END_ID())));
167       }
168     } else
169       anAttributes.insert(myStorage->entity(anAttr));
170   }
171
172   std::set<EntityWrapperPtr>::iterator aFound;
173   std::list<EntityWrapperPtr>::const_iterator anAttrIt = myAttributes.begin();
174   for (; anAttrIt != myAttributes.end() && !anAttributes.empty(); ++anAttrIt)
175     anAttributes.erase(*anAttrIt);
176
177   if (!anAttributes.empty()) {
178     remove();
179     process();
180     return;
181   }
182
183   AttributePtr aValueAttr = myBaseConstraint->attribute(SketchPlugin_Constraint::VALUE());
184   if (aValueAttr)
185     myStorage->update(aValueAttr);
186
187   adjustConstraint();
188 }
189
190 bool SketchSolver_Constraint::remove()
191 {
192   cleanErrorMsg();
193   myType = CONSTRAINT_UNKNOWN;
194   myStorage->unsubscribeUpdates(this);
195   return myStorage->removeConstraint(myBaseConstraint);
196 }
197
198 void SketchSolver_Constraint::getAttributes(
199     EntityWrapperPtr& theValue,
200     std::vector<EntityWrapperPtr>& theAttributes)
201 {
202   static const int anInitNbOfAttr = 4;
203   theAttributes.assign(anInitNbOfAttr, EntityWrapperPtr());
204   myAttributes.clear();
205
206   DataPtr aData = myBaseConstraint->data();
207
208   myType = TYPE(myBaseConstraint);
209
210   AttributePtr aValueAttr = aData->attribute(SketchPlugin_Constraint::VALUE());
211   if (aValueAttr) {
212     PlaneGCSSolver_AttributeBuilder aValueBuilder;
213     theValue = aValueBuilder.createAttribute(aValueAttr);
214     myStorage->addEntity(aValueAttr, theValue);
215   }
216
217   int aPtInd = 0; // index of first point in the list of attributes
218   int aEntInd = 2; // index of first entity in the list of attributes
219   std::list<AttributePtr> aConstrAttrs = aData->attributes(ModelAPI_AttributeRefAttr::typeId());
220   std::list<AttributePtr>::iterator anIter = aConstrAttrs.begin();
221   for (; anIter != aConstrAttrs.end(); anIter++) {
222     AttributeRefAttrPtr aRefAttr =
223         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
224     if (!aRefAttr || !aRefAttr->isInitialized()) {
225       myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
226       return;
227     }
228
229     myStorage->update(*anIter, true);
230     EntityWrapperPtr anEntity = myStorage->entity(*anIter);
231     myAttributes.push_back(anEntity);
232
233     SketchSolver_EntityType aType = anEntity->type();
234     if (aType == ENTITY_UNKNOWN)
235       continue;
236     else if (aType == ENTITY_POINT || aType == ENTITY_POINT_ARRAY)
237       theAttributes[aPtInd++] = anEntity; // the point is created
238     else { // another entity (not a point) is created
239       if (aEntInd < anInitNbOfAttr)
240         theAttributes[aEntInd] = anEntity;
241       else
242         theAttributes.push_back(anEntity);
243       aEntInd++;
244     }
245   }
246 }