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_ConstraintCoincidence.h>
21 #include <SketchSolver_Error.h>
22 #include <PlaneGCSSolver_Tools.h>
23 #include <PlaneGCSSolver_UpdateCoincidence.h>
25 #include <SketchPlugin_Arc.h>
26 #include <SketchPlugin_Line.h>
28 static void getCoincidentFeatureExtremities(const ConstraintPtr& theConstraint,
29 const StoragePtr& theStorage,
30 EntityWrapperPtr theExtremities[2])
32 for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
33 AttributeRefAttrPtr aRefAttr = theConstraint->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
34 if (!aRefAttr || !aRefAttr->isObject())
37 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
41 if (aFeature->getKind() == SketchPlugin_Line::ID()) {
42 theExtremities[0] = theStorage->entity(aFeature->attribute(SketchPlugin_Line::START_ID()));
43 theExtremities[1] = theStorage->entity(aFeature->attribute(SketchPlugin_Line::END_ID()));
44 } else if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
45 theExtremities[0] = theStorage->entity(aFeature->attribute(SketchPlugin_Arc::START_ID()));
46 theExtremities[1] = theStorage->entity(aFeature->attribute(SketchPlugin_Arc::END_ID()));
52 void SketchSolver_ConstraintCoincidence::process()
55 if (!myBaseConstraint || !myStorage) {
56 // Not enough parameters are assigned
60 EntityWrapperPtr aValue;
61 std::vector<EntityWrapperPtr> anAttributes;
62 getAttributes(aValue, anAttributes);
63 if (!myErrorMsg.empty())
65 if (anAttributes.empty()) {
66 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
70 mySolverConstraint = PlaneGCSSolver_Tools::createConstraint(
71 myBaseConstraint, getType(),
72 aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
74 myStorage->subscribeUpdates(this, PlaneGCSSolver_UpdateCoincidence::GROUP());
75 myStorage->notify(myBaseConstraint);
78 bool SketchSolver_ConstraintCoincidence::remove()
81 myFeatureExtremities[0] = EntityWrapperPtr();
82 myFeatureExtremities[1] = EntityWrapperPtr();
83 return SketchSolver_Constraint::remove();
86 void SketchSolver_ConstraintCoincidence::getAttributes(
87 EntityWrapperPtr& theValue,
88 std::vector<EntityWrapperPtr>& theAttributes)
90 SketchSolver_Constraint::getAttributes(theValue, theAttributes);
91 if (!myErrorMsg.empty() || !theAttributes[0]) {
92 theAttributes.clear();
97 myType = CONSTRAINT_PT_PT_COINCIDENT;
98 else if (theAttributes[2]) {
99 // check the type of entity (line or circle)
100 SketchSolver_EntityType anEntType = theAttributes[2]->type();
101 if (anEntType == ENTITY_LINE)
102 myType = CONSTRAINT_PT_ON_LINE;
103 else if (anEntType == ENTITY_CIRCLE || anEntType == ENTITY_ARC)
104 myType = CONSTRAINT_PT_ON_CIRCLE;
106 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
108 // obtain extremity points of the coincident feature for further checking of multi-coincidence
109 getCoincidentFeatureExtremities(myBaseConstraint, myStorage, myFeatureExtremities);
111 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
114 void SketchSolver_ConstraintCoincidence::notify(const FeaturePtr& theFeature,
115 PlaneGCSSolver_Update* theUpdater)
117 PlaneGCSSolver_UpdateCoincidence* anUpdater =
118 static_cast<PlaneGCSSolver_UpdateCoincidence*>(theUpdater);
119 bool isAccepted = anUpdater->addCoincidence(myAttributes.front(), myAttributes.back());
121 // additionally check the point is coincident to extremity of coincident feature
122 if (myFeatureExtremities[0] && myFeatureExtremities[1]) {
123 EntityWrapperPtr aPoint =
124 myAttributes.front()->type() == ENTITY_POINT ? myAttributes.front() : myAttributes.back();
126 for (int i = 0; i < 2; ++i)
127 isAccepted = isAccepted && !anUpdater->isPointOnEntity(aPoint, myFeatureExtremities[i]);
133 myStorage->addConstraint(myBaseConstraint, mySolverConstraint);
138 myStorage->removeConstraint(myBaseConstraint);