1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <SketchSolver_Constraint.h>
22 #include <SketchSolver_Error.h>
24 #include <PlaneGCSSolver_AttributeBuilder.h>
25 #include <PlaneGCSSolver_Tools.h>
27 #include <SketchPlugin_Arc.h>
28 #include <SketchPlugin_Circle.h>
29 #include <SketchPlugin_Line.h>
30 #include <SketchPlugin_Point.h>
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>
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>
56 SketchSolver_Constraint::SketchSolver_Constraint(
57 ConstraintPtr theConstraint)
58 : myBaseConstraint(theConstraint),
59 myType(CONSTRAINT_UNKNOWN)
63 void SketchSolver_Constraint::process(StoragePtr theStorage, bool theEvensBlocked)
65 myStorage = theStorage;
66 blockEvents(theEvensBlocked);
67 // Process constraint according to its type
71 void SketchSolver_Constraint::blockEvents(bool isBlocked)
73 myBaseConstraint->data()->blockSendAttributeUpdated(isBlocked);
77 SketchSolver_ConstraintType SketchSolver_Constraint::TYPE(ConstraintPtr theConstraint)
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;
113 void SketchSolver_Constraint::process()
116 if (!myBaseConstraint || !myStorage) {
117 // Not enough parameters are assigned
121 SketchSolver_ConstraintType aConstrType = getType();
122 EntityWrapperPtr aValue;
123 std::vector<EntityWrapperPtr> anAttributes;
124 getAttributes(aValue, anAttributes);
125 if (!myErrorMsg.empty())
127 if (anAttributes.empty()) {
128 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
131 if (aConstrType == CONSTRAINT_UNKNOWN)
132 aConstrType = getType();
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();
141 myStorage->addConstraint(myBaseConstraint, aNewConstraint);
146 void SketchSolver_Constraint::update()
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));
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());
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())));
168 anAttributes.insert(myStorage->entity(anAttr));
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);
176 if (!anAttributes.empty()) {
182 AttributePtr aValueAttr = myBaseConstraint->attribute(SketchPlugin_Constraint::VALUE());
184 myStorage->update(aValueAttr);
189 bool SketchSolver_Constraint::remove()
192 myType = CONSTRAINT_UNKNOWN;
193 myStorage->unsubscribeUpdates(this);
194 return myStorage->removeConstraint(myBaseConstraint);
197 void SketchSolver_Constraint::getAttributes(
198 EntityWrapperPtr& theValue,
199 std::vector<EntityWrapperPtr>& theAttributes)
201 static const int anInitNbOfAttr = 4;
202 theAttributes.assign(anInitNbOfAttr, EntityWrapperPtr());
203 myAttributes.clear();
205 DataPtr aData = myBaseConstraint->data();
207 myType = TYPE(myBaseConstraint);
209 AttributePtr aValueAttr = aData->attribute(SketchPlugin_Constraint::VALUE());
211 PlaneGCSSolver_AttributeBuilder aValueBuilder;
212 theValue = aValueBuilder.createAttribute(aValueAttr);
213 myStorage->addEntity(aValueAttr, theValue);
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();
228 myStorage->update(*anIter, true);
229 EntityWrapperPtr anEntity = myStorage->entity(*anIter);
230 myAttributes.push_back(anEntity);
232 SketchSolver_EntityType aType = anEntity->type();
233 if (aType == ENTITY_UNKNOWN)
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;
241 theAttributes.push_back(anEntity);