1 // Copyright (C) 2014-2023 CEA, EDF
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_ConstraintCoincidence.h>
21 #include <SketchSolver_Error.h>
22 #include <PlaneGCSSolver_PointArrayWrapper.h>
23 #include <PlaneGCSSolver_Storage.h>
24 #include <PlaneGCSSolver_Tools.h>
25 #include <PlaneGCSSolver_UpdateCoincidence.h>
27 #include <GeomAPI_BSpline2d.h>
28 #include <GeomAPI_Pnt2d.h>
30 #include <GeomDataAPI_Point2D.h>
32 #include <ModelAPI_AttributeInteger.h>
34 #include <SketchPlugin_Arc.h>
35 #include <SketchPlugin_ConstraintCoincidenceInternal.h>
36 #include <SketchPlugin_Ellipse.h>
37 #include <SketchPlugin_EllipticArc.h>
38 #include <SketchPlugin_Line.h>
39 #include <SketchPlugin_Point.h>
41 static void getCoincidentFeatureExtremities(const ConstraintPtr& theConstraint,
42 const StoragePtr& theStorage,
43 EntityWrapperPtr theExtremities[2])
45 for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
46 AttributeRefAttrPtr aRefAttr = theConstraint->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
47 if (!aRefAttr || !aRefAttr->isObject())
50 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
54 if (aFeature->getKind() == SketchPlugin_Line::ID()) {
55 theExtremities[0] = theStorage->entity(aFeature->attribute(SketchPlugin_Line::START_ID()));
56 theExtremities[1] = theStorage->entity(aFeature->attribute(SketchPlugin_Line::END_ID()));
57 } else if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
58 theExtremities[0] = theStorage->entity(aFeature->attribute(SketchPlugin_Arc::START_ID()));
59 theExtremities[1] = theStorage->entity(aFeature->attribute(SketchPlugin_Arc::END_ID()));
60 } else if (aFeature->getKind() == SketchPlugin_EllipticArc::ID()) {
61 theExtremities[0] = theStorage->entity(
62 aFeature->attribute(SketchPlugin_EllipticArc::START_POINT_ID()));
63 theExtremities[1] = theStorage->entity(
64 aFeature->attribute(SketchPlugin_EllipticArc::END_POINT_ID()));
69 static void getPointOwnerAndParent(const AttributeRefAttrPtr theRefAttr,
70 AttributePoint2DPtr& thePoint,
72 FeaturePtr& theParent)
74 AttributePtr anAttr = theRefAttr->attr();
75 if (theRefAttr->isObject()) {
76 FeaturePtr anOwner = ModelAPI_Feature::feature(theRefAttr->object());
77 if (anOwner && anOwner->getKind() == SketchPlugin_Point::ID())
78 anAttr = anOwner->attribute(SketchPlugin_Point::COORD_ID());
82 thePoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
84 theOwner = ModelAPI_Feature::feature(thePoint->owner());
86 AttributeReferencePtr aParentRef =
87 theOwner->reference(SketchPlugin_SketchEntity::PARENT_ID());
88 theParent = aParentRef ? ModelAPI_Feature::feature(aParentRef->value()) : FeaturePtr();
93 static void ellipseDiameters(FeaturePtr theEllipse,
94 std::pair<std::string, std::string>& theMajorAxis,
95 std::pair<std::string, std::string>& theMinorAxis)
97 if (theEllipse->getKind() == SketchPlugin_Ellipse::ID()) {
98 theMajorAxis.first = SketchPlugin_Ellipse::MAJOR_AXIS_START_ID();
99 theMajorAxis.second = SketchPlugin_Ellipse::MAJOR_AXIS_END_ID();
100 theMinorAxis.first = SketchPlugin_Ellipse::MINOR_AXIS_START_ID();
101 theMinorAxis.second = SketchPlugin_Ellipse::MINOR_AXIS_END_ID();
102 } else if (theEllipse->getKind() == SketchPlugin_EllipticArc::ID()) {
103 theMajorAxis.first = SketchPlugin_EllipticArc::MAJOR_AXIS_START_ID();
104 theMajorAxis.second = SketchPlugin_EllipticArc::MAJOR_AXIS_END_ID();
105 theMinorAxis.first = SketchPlugin_EllipticArc::MINOR_AXIS_START_ID();
106 theMinorAxis.second = SketchPlugin_EllipticArc::MINOR_AXIS_END_ID();
110 static void findDiameterOnEllipse(FeaturePtr theConstruction,
111 FeaturePtr theEllipse,
112 AttributePtr& theStart,
113 AttributePtr& theEnd)
115 AttributePtr anEllipseAttr;
116 const std::set<AttributePtr>& aRefs = theConstruction->data()->refsToMe();
117 for (std::set<AttributePtr>::const_iterator aRefIt = aRefs.begin();
118 aRefIt != aRefs.end(); ++aRefIt) {
119 FeaturePtr anOwner = ModelAPI_Feature::feature((*aRefIt)->owner());
120 if (anOwner && anOwner->getKind() == SketchPlugin_ConstraintCoincidenceInternal::ID()) {
121 AttributeRefAttrPtr aRefAttr;
122 if ((*aRefIt)->id() == SketchPlugin_Constraint::ENTITY_A())
123 aRefAttr = anOwner->refattr(SketchPlugin_Constraint::ENTITY_B());
125 aRefAttr = anOwner->refattr(SketchPlugin_Constraint::ENTITY_A());
126 anEllipseAttr = aRefAttr->attr();
133 std::pair<std::string, std::string> aMajorAxis, aMinorAxis;
134 ellipseDiameters(theEllipse, aMajorAxis, aMinorAxis);
135 if (anEllipseAttr->id() == aMajorAxis.first) {
136 theStart = anEllipseAttr;
137 theEnd = theEllipse->attribute(aMajorAxis.second);
139 else if (anEllipseAttr->id() == aMajorAxis.second) {
140 theStart = theEllipse->attribute(aMajorAxis.first);
141 theEnd = anEllipseAttr;
143 else if (anEllipseAttr->id() == aMinorAxis.first) {
144 theStart = anEllipseAttr;
145 theEnd = theEllipse->attribute(aMinorAxis.second);
147 else if (anEllipseAttr->id() == aMinorAxis.second) {
148 theStart = theEllipse->attribute(aMinorAxis.first);
149 theEnd = anEllipseAttr;
153 static void processEllipticArcExtremities(SketchSolver_ConstraintType& theType,
154 const ConstraintPtr& theConstraint,
155 const StoragePtr& theStorage,
156 std::vector<EntityWrapperPtr>& theAttributes,
157 EntityWrapperPtr theExtremities[2])
159 AttributePoint2DPtr aPointA, aPointB;
160 FeaturePtr anOwnerA, anOwnerB;
161 FeaturePtr aParentA, aParentB;
162 getPointOwnerAndParent(theConstraint->refattr(SketchPlugin_Constraint::ENTITY_A()),
163 aPointA, anOwnerA, aParentA);
164 getPointOwnerAndParent(theConstraint->refattr(SketchPlugin_Constraint::ENTITY_B()),
165 aPointB, anOwnerB, aParentB);
167 AttributePtr anAxisStart, anAxisEnd, aPoint;
168 FeaturePtr aConstruction, anEllipticArc;
169 if (aParentA && aParentA == anOwnerB) {
171 aConstruction = anOwnerA;
172 anEllipticArc = anOwnerB;
174 else if (aParentB && aParentB == anOwnerA) {
176 aConstruction = anOwnerB;
177 anEllipticArc = anOwnerA;
180 if (!anEllipticArc || anEllipticArc->getKind() != SketchPlugin_EllipticArc::ID() ||
181 (aPoint->id() != SketchPlugin_EllipticArc::START_POINT_ID() &&
182 aPoint->id() != SketchPlugin_EllipticArc::END_POINT_ID()))
185 findDiameterOnEllipse(aConstruction, anEllipticArc, anAxisStart, anAxisEnd);
187 if (anAxisStart && anAxisEnd) {
188 theAttributes[0] = theStorage->entity(aPoint);
189 theAttributes[1] = theStorage->entity(anAxisStart);
190 theAttributes[2] = theStorage->entity(anAxisEnd);
191 theType = CONSTRAINT_PT_ON_CURVE;
192 getCoincidentFeatureExtremities(theConstraint, theStorage, theExtremities);
196 static void getPointFromArray(EntityWrapperPtr& theArray,
197 const ConstraintPtr& theConstraint,
198 const std::string& theIndexAttrId)
200 if (theArray && theArray->type() == ENTITY_POINT_ARRAY) {
201 AttributeIntegerPtr anIndexAttr = theConstraint->integer(theIndexAttrId);
203 PointArrayWrapperPtr aPointsArray =
204 std::dynamic_pointer_cast<PlaneGCSSolver_PointArrayWrapper>(theArray);
205 theArray = aPointsArray->value(anIndexAttr->value());
211 void SketchSolver_ConstraintCoincidence::process()
214 if (!myBaseConstraint || !myStorage) {
215 // Not enough parameters are assigned
219 EntityWrapperPtr aValue;
220 std::vector<EntityWrapperPtr> anAttributes;
221 getAttributes(aValue, anAttributes);
222 if (!myErrorMsg.empty())
224 if (anAttributes.empty()) {
225 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
229 mySolverConstraint = PlaneGCSSolver_Tools::createConstraint(
230 myBaseConstraint, getType(),
231 myAuxValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
233 myStorage->subscribeUpdates(this, PlaneGCSSolver_UpdateCoincidence::GROUP());
234 myStorage->notify(myBaseConstraint);
237 bool SketchSolver_ConstraintCoincidence::remove()
240 myFeatureExtremities[0] = EntityWrapperPtr();
241 myFeatureExtremities[1] = EntityWrapperPtr();
243 std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
244 std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
246 aParams.insert(myAuxValue->scalar());
247 aStorage->removeParameters(aParams);
249 return SketchSolver_Constraint::remove();
252 void SketchSolver_ConstraintCoincidence::getAttributes(
253 EntityWrapperPtr& theValue,
254 std::vector<EntityWrapperPtr>& theAttributes)
256 SketchSolver_Constraint::getAttributes(theValue, theAttributes);
257 if (!myErrorMsg.empty() || !theAttributes[0]) {
258 theAttributes.clear();
262 if (theAttributes[1]) {
263 myType = CONSTRAINT_PT_PT_COINCIDENT;
264 // if elliptic arc boundary point is connected with one of ellipse characteristics,
265 // it should be changed from point-point coincidence to coincidence between point
266 // and axis of the ellipse to decrease only 1 DoF instead of 2 DoF and avoid overconstraint.
267 processEllipticArcExtremities(myType, myBaseConstraint, myStorage,
268 theAttributes, myFeatureExtremities);
269 } else if (theAttributes[2]) {
270 myType = CONSTRAINT_PT_ON_CURVE;
271 // point-on-bspline requires additional parameter
272 if (theAttributes[2]->type() == ENTITY_BSPLINE) {
273 std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
274 std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
275 myAuxValue.reset(new PlaneGCSSolver_ScalarWrapper(aStorage->createParameter()));
276 // calculate the parameter of the point on B-spline nearest to the constrained point.
277 GeomPnt2dPtr aPoint = PlaneGCSSolver_Tools::point(theAttributes[0]);
278 std::shared_ptr<GeomAPI_BSpline2d> aSpline = PlaneGCSSolver_Tools::bspline(theAttributes[2]);
279 if (aPoint && aSpline)
280 aSpline->parameter(aPoint, 1.e100, *myAuxValue->scalar());
283 // obtain extremity points of the coincident feature for further checking of multi-coincidence
284 getCoincidentFeatureExtremities(myBaseConstraint, myStorage, myFeatureExtremities);
287 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
289 // process internal coincidence with a point in the array of points
290 getPointFromArray(theAttributes[0], myBaseConstraint,
291 SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_A());
292 getPointFromArray(theAttributes[1], myBaseConstraint,
293 SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_B());
296 void SketchSolver_ConstraintCoincidence::notify(const FeaturePtr& theFeature,
297 PlaneGCSSolver_Update* theUpdater)
299 PlaneGCSSolver_UpdateCoincidence* anUpdater =
300 static_cast<PlaneGCSSolver_UpdateCoincidence*>(theUpdater);
301 bool isAccepted = anUpdater->addCoincidence(myAttributes.front(), myAttributes.back());
302 // additionally process internal coincidence, set point coincident with ellipse/elliptic arc
303 // for correct processing further coincidences set by the user
305 myBaseConstraint->getKind() == SketchPlugin_ConstraintCoincidenceInternal::ID()) {
306 AttributeRefAttrPtr aRefAttrA = myBaseConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
307 AttributeRefAttrPtr aRefAttrB = myBaseConstraint->refattr(SketchPlugin_Constraint::ENTITY_B());
308 if (aRefAttrA && aRefAttrB) {
309 AttributePoint2DPtr anAttrA, anAttrB;
310 FeaturePtr anOwnerA, anOwnerB;
311 FeaturePtr aParentA, aParentB;
312 getPointOwnerAndParent(aRefAttrA, anAttrA, anOwnerA, aParentA);
313 getPointOwnerAndParent(aRefAttrB, anAttrB, anOwnerB, aParentB);
315 EntityWrapperPtr aPoint, anEntity;
316 if (aParentA == anOwnerB) {
317 aPoint = myStorage->entity(anAttrA);
318 anEntity = myStorage->entity(anOwnerB);
320 else if (aParentB == anOwnerA) {
321 aPoint = myStorage->entity(anAttrB);
322 anEntity = myStorage->entity(anOwnerA);
324 if (aPoint && anEntity)
325 anUpdater->addCoincidence(aPoint, anEntity);
329 // additionally check the point is coincident to extremity of coincident feature
330 if (myFeatureExtremities[0] && myFeatureExtremities[1]) {
331 EntityWrapperPtr aPoint =
332 myAttributes.front()->type() == ENTITY_POINT ? myAttributes.front() : myAttributes.back();
334 for (int i = 0; i < 2; ++i)
335 isAccepted = isAccepted && !anUpdater->isPointOnEntity(aPoint, myFeatureExtremities[i]);
341 myStorage->addConstraint(myBaseConstraint, mySolverConstraint);
346 myStorage->removeConstraint(myBaseConstraint);
351 void SketchSolver_ConstraintCoincidence::adjustConstraint()
353 if (myBaseConstraint->getKind() == SketchPlugin_ConstraintCoincidenceInternal::ID()) {
354 AttributeIntegerPtr anIndexA = myBaseConstraint->integer(
355 SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_A());
356 AttributeIntegerPtr anIndexB = myBaseConstraint->integer(
357 SketchPlugin_ConstraintCoincidenceInternal::INDEX_ENTITY_B());
358 if ((anIndexA && anIndexA->isInitialized()) ||
359 (anIndexB && anIndexB->isInitialized())) {