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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include <SketchSolver_ConstraintFixed.h>
21 #include <SketchSolver_Error.h>
23 #include <PlaneGCSSolver_ConstraintWrapper.h>
24 #include <PlaneGCSSolver_EdgeWrapper.h>
25 #include <PlaneGCSSolver_EntityDestroyer.h>
26 #include <PlaneGCSSolver_FeatureBuilder.h>
27 #include <PlaneGCSSolver_PointWrapper.h>
29 #include <GeomDataAPI_Point2D.h>
30 #include <SketchPlugin_Feature.h>
34 // Verify the points are equal
35 static bool isEqual(const GCS::Point& thePoint1, const GCS::Point& thePoint2);
36 // Verify the entities are equal
37 static bool isEqual(const EntityWrapperPtr& theEntity1, const EntityWrapperPtr& theEntity2);
38 // Convert entity to the list of parameters
39 static GCS::VEC_pD toParameters(const EntityWrapperPtr& theEntity);
42 SketchSolver_ConstraintFixed::SketchSolver_ConstraintFixed(ConstraintPtr theConstraint)
43 : SketchSolver_Constraint(theConstraint)
45 myType = CONSTRAINT_FIXED;
48 SketchSolver_ConstraintFixed::SketchSolver_ConstraintFixed(FeaturePtr theFeature)
49 : SketchSolver_Constraint(),
50 myBaseFeature(theFeature)
52 myType = CONSTRAINT_FIXED;
55 void SketchSolver_ConstraintFixed::blockEvents(bool isBlocked)
58 myBaseFeature->data()->blockSendAttributeUpdated(isBlocked);
60 SketchSolver_Constraint::blockEvents(isBlocked);
63 void SketchSolver_ConstraintFixed::process()
66 if ((!myBaseConstraint && !myBaseFeature) || !myStorage) {
67 // Not enough parameters are assigned
71 EntityWrapperPtr aBaseEntity;
72 getAttributes(aBaseEntity, myFixedEntity);
74 moveFeature(); // remove myFixed entity
75 myErrorMsg = SketchSolver_Error::ALREADY_FIXED();
77 if (!myErrorMsg.empty())
80 fixFeature(aBaseEntity);
83 void SketchSolver_ConstraintFixed::fixFeature(EntityWrapperPtr theFeature)
85 GCS::VEC_pD aParameters = toParameters(theFeature);
87 // Fix given list of parameters
88 std::list<GCSConstraintPtr> aConstraints;
89 myFixedValues.reserve(aParameters.size());
90 GCS::VEC_pD::const_iterator anIt = aParameters.begin();
91 for (int i = 0; anIt != aParameters.end(); ++anIt, ++i) {
92 myFixedValues.push_back(**anIt);
93 aConstraints.push_back(
94 GCSConstraintPtr(new GCS::ConstraintEqual(&myFixedValues[i], *anIt)));
97 myConstraint = ConstraintWrapperPtr(
98 new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
100 if (myBaseConstraint)
101 myStorage->addConstraint(myBaseConstraint, myConstraint);
103 myStorage->addTemporaryConstraint(myConstraint);
106 void SketchSolver_ConstraintFixed::getAttributes(EntityWrapperPtr& theBaseEntity,
107 EntityWrapperPtr& theFixedEntity)
110 // if the feature is copy, do not move it
111 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
112 std::dynamic_pointer_cast<SketchPlugin_Feature>(myBaseFeature);
113 if (aSketchFeature && aSketchFeature->isCopy()) {
114 myStorage->setNeedToResolve(true);
118 bool fixFullFeature = false;
119 theBaseEntity = EntityWrapperPtr();
120 theFixedEntity = EntityWrapperPtr();
122 // The feature is fixed.
123 PlaneGCSSolver_FeatureBuilder aBuilder;
124 std::list<AttributePtr> aBaseAttr = myBaseFeature->data()->attributes(std::string());
125 std::list<AttributePtr>::const_iterator anIt = aBaseAttr.begin();
126 for (; anIt != aBaseAttr.end(); ++anIt) {
127 EntityWrapperPtr anEntity = aBuilder.createAttribute(*anIt);
128 EntityWrapperPtr aBaseEntity = myStorage->entity(*anIt);
129 if (!anEntity || !aBaseEntity)
132 if (!fixFullFeature && anEntity->type() == ENTITY_POINT &&
133 !isEqual(anEntity, aBaseEntity)) {
135 fixFullFeature = true;
136 theFixedEntity = anEntity;
137 theBaseEntity = aBaseEntity;
141 if (fixFullFeature) {
142 theFixedEntity = aBuilder.createFeature(myBaseFeature);
143 theBaseEntity = myStorage->entity(myBaseFeature);
144 if (isEqual(theBaseEntity, theFixedEntity))
145 theBaseEntity = EntityWrapperPtr(); // do not want to fix already fixed entity
148 } else if (myBaseConstraint) {
149 // Constraint Fixed is added by user.
150 // Get the attribute of constraint (it should be alone in the list of constraints).
151 EntityWrapperPtr aValue;
152 std::vector<EntityWrapperPtr> anAttributes;
153 SketchSolver_Constraint::getAttributes(aValue, anAttributes);
154 std::vector<EntityWrapperPtr>::const_iterator anIt = anAttributes.begin();
155 for (; anIt != anAttributes.end(); ++anIt)
157 theBaseEntity = *anIt;
159 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
162 void SketchSolver_ConstraintFixed::moveFeature()
167 GCS::VEC_pD aFixedParams = toParameters(myFixedEntity);
168 for (int i = 0; i < aFixedParams.size() && i < myFixedValues.size(); ++i)
169 myFixedValues[i] = *(aFixedParams[i]);
171 // Remove fixed entity due to its parameters already copied into the constraint
172 PlaneGCSSolver_EntityDestroyer aDestroyer;
173 aDestroyer.remove(myFixedEntity);
174 std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage)->removeParameters(
175 aDestroyer.parametersToRemove());
177 myFixedEntity = EntityWrapperPtr();
183 // ==================== Auxiliary functions ===============================
184 GCS::VEC_pD toParameters(const EntityWrapperPtr& theEntity)
186 GCS::VEC_pD aParameters;
190 std::shared_ptr<PlaneGCSSolver_EdgeWrapper> anEntity =
191 std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theEntity);
193 // Collect parameters for each type of entity
194 switch (theEntity->type()) {
196 std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint =
197 std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theEntity);
198 aParameters.push_back(aPoint->point()->x);
199 aParameters.push_back(aPoint->point()->y);
203 std::shared_ptr<GCS::Line> aLine = std::dynamic_pointer_cast<GCS::Line>(anEntity->entity());
204 aParameters.push_back(aLine->p1.x);
205 aParameters.push_back(aLine->p1.y);
206 aParameters.push_back(aLine->p2.x);
207 aParameters.push_back(aLine->p2.y);
210 case ENTITY_CIRCLE: {
211 std::shared_ptr<GCS::Circle> aCircle =
212 std::dynamic_pointer_cast<GCS::Circle>(anEntity->entity());
213 aParameters.push_back(aCircle->center.x);
214 aParameters.push_back(aCircle->center.y);
215 aParameters.push_back(aCircle->rad);
219 std::shared_ptr<GCS::Arc> anArc = std::dynamic_pointer_cast<GCS::Arc>(anEntity->entity());
220 aParameters.push_back(anArc->center.x);
221 aParameters.push_back(anArc->center.y);
222 aParameters.push_back(anArc->rad);
223 aParameters.push_back(anArc->startAngle);
224 aParameters.push_back(anArc->endAngle);
234 bool isEqual(const GCS::Point& thePoint1, const GCS::Point& thePoint2)
236 return fabs((*thePoint1.x) - (*thePoint2.x)) <= tolerance &&
237 fabs((*thePoint1.y) - (*thePoint2.y)) <= tolerance;
240 bool isEqual(const EntityWrapperPtr& theEntity1, const EntityWrapperPtr& theEntity2)
242 GCS::VEC_pD aParamList1 = toParameters(theEntity1);
243 GCS::VEC_pD aParamList2 = toParameters(theEntity2);
245 GCS::VEC_pD::const_iterator anIt1 = aParamList1.begin();
246 GCS::VEC_pD::const_iterator anIt2 = aParamList2.begin();
247 for (; anIt1 != aParamList1.end() && anIt2 != aParamList2.end(); ++anIt1, ++anIt2)
248 if (fabs((**anIt1) - (**anIt2)) > tolerance)
251 return anIt1 == aParamList1.end() && anIt2 == aParamList2.end();