1 // Copyright (C) 2014-2019 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
20 #include <SketchSolver_ConstraintDistance.h>
21 #include <SketchSolver_Error.h>
22 #include <SketchSolver_Manager.h>
24 #include <PlaneGCSSolver_EdgeWrapper.h>
25 #include <PlaneGCSSolver_PointWrapper.h>
26 #include <PlaneGCSSolver_Storage.h>
27 #include <PlaneGCSSolver_Tools.h>
29 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
30 #include <SketchPlugin_ConstraintDistanceVertical.h>
32 #include <GeomAPI_Dir2d.h>
33 #include <GeomAPI_Lin2d.h>
34 #include <GeomAPI_Pnt2d.h>
35 #include <GeomAPI_XY.h>
40 static void getPointAndLine(const ConstraintPtr& theConstraint, const StoragePtr& theStorage,
41 EntityWrapperPtr& thePoint, EntityWrapperPtr& theLine)
43 for (int i = 0; i < 2; ++i) {
44 AttributePtr anAttr = theConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(i));
45 EntityWrapperPtr anEntity = theStorage->entity(anAttr);
46 if (anEntity->type() == ENTITY_POINT)
48 else if (anEntity->type() == ENTITY_LINE)
53 static void adjustOddPoint(const EntityWrapperPtr& theDistPoint,
54 const EntityWrapperPtr& theDistLine,
55 GCSPointPtr& theOddPoint)
60 std::shared_ptr<GeomAPI_Lin2d> aLine = PlaneGCSSolver_Tools::line(theDistLine);
61 std::shared_ptr<GeomAPI_Pnt2d> aPoint = PlaneGCSSolver_Tools::point(theDistPoint);
63 std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
64 std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
66 double aDot = aPtLineVec->dot(aLineVec);
67 std::shared_ptr<GeomAPI_XY> aProjectedPnt =
68 aLine->location()->xy()->added(aLineVec->multiplied(aDot));
70 *(theOddPoint->x) = aProjectedPnt->x();
71 *(theOddPoint->y) = aProjectedPnt->y();
75 void SketchSolver_ConstraintDistance::getAttributes(
76 EntityWrapperPtr& theValue,
77 std::vector<EntityWrapperPtr>& theAttributes)
79 SketchSolver_Constraint::getAttributes(theValue, theAttributes);
80 if (!myErrorMsg.empty() || !theAttributes[0]) {
81 theAttributes.clear();
85 if (theAttributes[1]) {
86 if (myBaseConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID())
87 myType = CONSTRAINT_HORIZONTAL_DISTANCE;
88 else if (myBaseConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
89 myType = CONSTRAINT_VERTICAL_DISTANCE;
91 myType = CONSTRAINT_PT_PT_DISTANCE;
92 } else if (theAttributes[2] && theAttributes[2]->type() == ENTITY_LINE)
93 myType = CONSTRAINT_PT_LINE_DISTANCE;
95 theAttributes.clear();
100 void SketchSolver_ConstraintDistance::adjustConstraint()
102 if (getType() == CONSTRAINT_PT_LINE_DISTANCE) {
103 bool isSigned = myBaseConstraint->boolean(SketchPlugin_ConstraintDistance::SIGNED())->value();
104 if (myIsSigned == isSigned) {
105 // adjust auxiliary point for sign-keeping
107 EntityWrapperPtr aDistPoint, aDistLine;
108 getPointAndLine(myBaseConstraint, myStorage, aDistPoint, aDistLine);
109 adjustOddPoint(aDistPoint, aDistLine, myOddPoint);
113 // Adjust point-line distance by setting/removing additional constraints
115 addConstraintsToKeepSign();
117 removeConstraintsKeepingSign();
119 myIsSigned = isSigned;
123 void SketchSolver_ConstraintDistance::update()
125 ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint);
126 myPrevValue = aConstraint->value();
128 SketchSolver_Constraint::update();
131 bool SketchSolver_ConstraintDistance::remove()
133 removeConstraintsKeepingSign();
134 return SketchSolver_Constraint::remove();
137 void SketchSolver_ConstraintDistance::addConstraintsToKeepSign()
139 std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
140 std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
142 ConstraintWrapperPtr aConstraint = aStorage->constraint(myBaseConstraint);
143 std::list<GCSConstraintPtr> aGCSConstraints = aConstraint->constraints();
145 // calculate projection of the point on the line and find a sign of a distance
146 EntityWrapperPtr aDistPoint, aDistLine;
147 getPointAndLine(myBaseConstraint, myStorage, aDistPoint, aDistLine);
149 std::shared_ptr<GeomAPI_Lin2d> aLine = PlaneGCSSolver_Tools::line(aDistLine);
150 std::shared_ptr<GeomAPI_Pnt2d> aPoint = PlaneGCSSolver_Tools::point(aDistPoint);
152 std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
153 std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
154 if (aLineVec->cross(aPtLineVec) >= 0.)
155 mySignValue = PI/2.0;
157 mySignValue = - PI/2.0;
159 // create auxiliary point on the line and set auxiliary constraints
160 myOddPoint = GCSPointPtr(new GCS::Point);
161 myOddPoint->x = aStorage->createParameter();
162 myOddPoint->y = aStorage->createParameter();
163 adjustOddPoint(aDistPoint, aDistLine, myOddPoint);
165 PointWrapperPtr aPointWr = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(aDistPoint);
166 EdgeWrapperPtr anEdgeWr = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(aDistLine);
167 std::shared_ptr<GCS::Line> aGCSLine = std::dynamic_pointer_cast<GCS::Line>(anEdgeWr->entity());
169 GCSConstraintPtr aNewConstraint(new GCS::ConstraintPointOnLine(*myOddPoint, *aGCSLine));
170 aGCSConstraints.push_back(aNewConstraint);
171 // angle which keep orientation
172 aNewConstraint = GCSConstraintPtr(new GCS::ConstraintL2LAngle(
173 aGCSLine->p1, aGCSLine->p2, *myOddPoint, *(aPointWr->point()), &mySignValue));
174 aGCSConstraints.push_back(aNewConstraint);
176 aConstraint->setConstraints(aGCSConstraints);
178 aStorage->removeConstraint(myBaseConstraint);
179 aStorage->addConstraint(myBaseConstraint, aConstraint);
182 void SketchSolver_ConstraintDistance::removeConstraintsKeepingSign()
185 return; // no sign kept => nothing to remove
187 std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
188 std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
190 ConstraintWrapperPtr aConstraint = aStorage->constraint(myBaseConstraint);
191 std::list<GCSConstraintPtr> aGCSConstraints = aConstraint->constraints();
193 aStorage->removeConstraint(myBaseConstraint);
195 // remove parameters related to auxiliary point
197 aParams.insert(myOddPoint->x);
198 aParams.insert(myOddPoint->y);
199 aStorage->removeParameters(aParams);
201 // remove constraints keeping sign of point-line distance,
202 // not more than 2 additional constraints is possible
203 if (!aGCSConstraints.empty())
204 aGCSConstraints.pop_back();
205 if (!aGCSConstraints.empty())
206 aGCSConstraints.pop_back();
207 aConstraint->setConstraints(aGCSConstraints);
208 aStorage->addConstraint(myBaseConstraint, aConstraint);