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();
99 myStorage->subscribeUpdates(this, PlaneGCSSolver_UpdateFeature::GROUP());
102 void SketchSolver_ConstraintDistance::adjustConstraint()
104 if (getType() == CONSTRAINT_PT_LINE_DISTANCE) {
105 bool isSigned = myBaseConstraint->boolean(SketchPlugin_ConstraintDistance::SIGNED())->value();
106 if (myIsSigned == isSigned) {
107 // adjust auxiliary point for sign-keeping
109 EntityWrapperPtr aDistPoint, aDistLine;
110 getPointAndLine(myBaseConstraint, myStorage, aDistPoint, aDistLine);
111 adjustOddPoint(aDistPoint, aDistLine, myOddPoint);
115 // Adjust point-line distance by setting/removing additional constraints
117 addConstraintsToKeepSign();
119 removeConstraintsKeepingSign();
121 myIsSigned = isSigned;
125 void SketchSolver_ConstraintDistance::update()
127 ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint);
128 myPrevValue = aConstraint->value();
130 SketchSolver_Constraint::update();
133 bool SketchSolver_ConstraintDistance::remove()
135 removeConstraintsKeepingSign();
136 return SketchSolver_Constraint::remove();
139 void SketchSolver_ConstraintDistance::addConstraintsToKeepSign()
141 std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
142 std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
144 ConstraintWrapperPtr aConstraint = aStorage->constraint(myBaseConstraint);
145 std::list<GCSConstraintPtr> aGCSConstraints = aConstraint->constraints();
147 // calculate projection of the point on the line and find a sign of a distance
148 EntityWrapperPtr aDistPoint, aDistLine;
149 getPointAndLine(myBaseConstraint, myStorage, aDistPoint, aDistLine);
151 std::shared_ptr<GeomAPI_Lin2d> aLine = PlaneGCSSolver_Tools::line(aDistLine);
152 std::shared_ptr<GeomAPI_Pnt2d> aPoint = PlaneGCSSolver_Tools::point(aDistPoint);
154 std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
155 std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
156 if (aLineVec->cross(aPtLineVec) >= 0.)
157 mySignValue = PI/2.0;
159 mySignValue = - PI/2.0;
161 // create auxiliary point on the line and set auxiliary constraints
162 myOddPoint = GCSPointPtr(new GCS::Point);
163 myOddPoint->x = aStorage->createParameter();
164 myOddPoint->y = aStorage->createParameter();
165 adjustOddPoint(aDistPoint, aDistLine, myOddPoint);
167 PointWrapperPtr aPointWr = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(aDistPoint);
168 EdgeWrapperPtr anEdgeWr = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(aDistLine);
169 std::shared_ptr<GCS::Line> aGCSLine = std::dynamic_pointer_cast<GCS::Line>(anEdgeWr->entity());
171 GCSConstraintPtr aNewConstraint(new GCS::ConstraintPointOnLine(*myOddPoint, *aGCSLine));
172 aGCSConstraints.push_back(aNewConstraint);
173 // angle which keep orientation
174 aNewConstraint = GCSConstraintPtr(new GCS::ConstraintL2LAngle(
175 aGCSLine->p1, aGCSLine->p2, *myOddPoint, *(aPointWr->point()), &mySignValue));
176 aGCSConstraints.push_back(aNewConstraint);
178 aConstraint->setConstraints(aGCSConstraints);
180 aStorage->removeConstraint(myBaseConstraint);
181 aStorage->addConstraint(myBaseConstraint, aConstraint);
184 void SketchSolver_ConstraintDistance::removeConstraintsKeepingSign()
187 return; // no sign kept => nothing to remove
189 std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
190 std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
192 ConstraintWrapperPtr aConstraint = aStorage->constraint(myBaseConstraint);
193 std::list<GCSConstraintPtr> aGCSConstraints = aConstraint->constraints();
195 aStorage->removeConstraint(myBaseConstraint);
197 // remove parameters related to auxiliary point
199 aParams.insert(myOddPoint->x);
200 aParams.insert(myOddPoint->y);
201 aStorage->removeParameters(aParams);
203 // remove constraints keeping sign of point-line distance,
204 // not more than 2 additional constraints is possible
205 if (!aGCSConstraints.empty())
206 aGCSConstraints.pop_back();
207 if (!aGCSConstraints.empty())
208 aGCSConstraints.pop_back();
209 aConstraint->setConstraints(aGCSConstraints);
210 aStorage->addConstraint(myBaseConstraint, aConstraint);
215 void SketchSolver_ConstraintDistance::notify(const FeaturePtr& theFeature,
216 PlaneGCSSolver_Update*)
218 if (getType() == CONSTRAINT_PT_LINE_DISTANCE && myIsSigned &&
219 theFeature->getKind() == SketchPlugin_Sketch::ID()) {
220 // the sketch plane was updated, recalculate auxiliary constraints
221 removeConstraintsKeepingSign();
222 addConstraintsToKeepSign();
223 myIsSigned = true; // reset it, due to changing by removeConstraintsKeepingSign()