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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <SketchSolver_ConstraintCoincidence.h>
22 #include <SketchSolver_Error.h>
23 #include <PlaneGCSSolver_Tools.h>
24 #include <PlaneGCSSolver_UpdateCoincidence.h>
26 #include <SketchPlugin_Arc.h>
27 #include <SketchPlugin_Line.h>
29 static void getCoincidentFeatureExtremities(const ConstraintPtr& theConstraint,
30 const StoragePtr& theStorage,
31 EntityWrapperPtr theExtremities[2])
33 for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
34 AttributeRefAttrPtr aRefAttr = theConstraint->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
35 if (!aRefAttr || !aRefAttr->isObject())
38 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
42 if (aFeature->getKind() == SketchPlugin_Line::ID()) {
43 theExtremities[0] = theStorage->entity(aFeature->attribute(SketchPlugin_Line::START_ID()));
44 theExtremities[1] = theStorage->entity(aFeature->attribute(SketchPlugin_Line::END_ID()));
45 } else if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
46 theExtremities[0] = theStorage->entity(aFeature->attribute(SketchPlugin_Arc::START_ID()));
47 theExtremities[1] = theStorage->entity(aFeature->attribute(SketchPlugin_Arc::END_ID()));
53 void SketchSolver_ConstraintCoincidence::process()
56 if (!myBaseConstraint || !myStorage) {
57 // Not enough parameters are assigned
61 EntityWrapperPtr aValue;
62 std::vector<EntityWrapperPtr> anAttributes;
63 getAttributes(aValue, anAttributes);
64 if (!myErrorMsg.empty())
66 if (anAttributes.empty()) {
67 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
71 mySolverConstraint = PlaneGCSSolver_Tools::createConstraint(
72 myBaseConstraint, getType(),
73 aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
75 myStorage->subscribeUpdates(this, PlaneGCSSolver_UpdateCoincidence::GROUP());
76 myStorage->notify(myBaseConstraint);
79 bool SketchSolver_ConstraintCoincidence::remove()
82 myFeatureExtremities[0] = EntityWrapperPtr();
83 myFeatureExtremities[1] = EntityWrapperPtr();
84 return SketchSolver_Constraint::remove();
87 void SketchSolver_ConstraintCoincidence::getAttributes(
88 EntityWrapperPtr& theValue,
89 std::vector<EntityWrapperPtr>& theAttributes)
91 SketchSolver_Constraint::getAttributes(theValue, theAttributes);
92 if (!myErrorMsg.empty() || !theAttributes[0]) {
93 theAttributes.clear();
98 myType = CONSTRAINT_PT_PT_COINCIDENT;
99 else if (theAttributes[2]) {
100 // check the type of entity (line or circle)
101 SketchSolver_EntityType anEntType = theAttributes[2]->type();
102 if (anEntType == ENTITY_LINE)
103 myType = CONSTRAINT_PT_ON_LINE;
104 else if (anEntType == ENTITY_CIRCLE || anEntType == ENTITY_ARC)
105 myType = CONSTRAINT_PT_ON_CIRCLE;
107 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
109 // obtain extremity points of the coincident feature for further checking of multi-coincidence
110 getCoincidentFeatureExtremities(myBaseConstraint, myStorage, myFeatureExtremities);
112 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
115 void SketchSolver_ConstraintCoincidence::notify(const FeaturePtr& theFeature,
116 PlaneGCSSolver_Update* theUpdater)
118 PlaneGCSSolver_UpdateCoincidence* anUpdater =
119 static_cast<PlaneGCSSolver_UpdateCoincidence*>(theUpdater);
120 bool isAccepted = anUpdater->addCoincidence(myAttributes.front(), myAttributes.back());
122 // additionally check the point is coincident to extremity of coincident feature
123 if (myFeatureExtremities[0] && myFeatureExtremities[1]) {
124 EntityWrapperPtr aPoint =
125 myAttributes.front()->type() == ENTITY_POINT ? myAttributes.front() : myAttributes.back();
127 for (int i = 0; i < 2; ++i)
128 isAccepted = isAccepted && !anUpdater->isPointOnEntity(aPoint, myFeatureExtremities[i]);
134 myStorage->addConstraint(myBaseConstraint, mySolverConstraint);
139 myStorage->removeConstraint(myBaseConstraint);