1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include <SketchSolver_ConstraintCoincidence.h>
4 #include <SketchSolver_Error.h>
5 #include <PlaneGCSSolver_Tools.h>
6 #include <PlaneGCSSolver_UpdateCoincidence.h>
8 #include <SketchPlugin_Arc.h>
9 #include <SketchPlugin_Line.h>
11 static void getCoincidentFeatureExtremities(const ConstraintPtr& theConstraint,
12 const StoragePtr& theStorage,
13 EntityWrapperPtr theExtremities[2])
15 for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
16 AttributeRefAttrPtr aRefAttr = theConstraint->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
17 if (!aRefAttr || !aRefAttr->isObject())
20 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
24 if (aFeature->getKind() == SketchPlugin_Line::ID()) {
25 theExtremities[0] = theStorage->entity(aFeature->attribute(SketchPlugin_Line::START_ID()));
26 theExtremities[1] = theStorage->entity(aFeature->attribute(SketchPlugin_Line::END_ID()));
27 } else if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
28 theExtremities[0] = theStorage->entity(aFeature->attribute(SketchPlugin_Arc::START_ID()));
29 theExtremities[1] = theStorage->entity(aFeature->attribute(SketchPlugin_Arc::END_ID()));
35 void SketchSolver_ConstraintCoincidence::process()
38 if (!myBaseConstraint || !myStorage) {
39 // Not enough parameters are assigned
43 EntityWrapperPtr aValue;
44 std::vector<EntityWrapperPtr> anAttributes;
45 getAttributes(aValue, anAttributes);
46 if (!myErrorMsg.empty())
48 if (anAttributes.empty()) {
49 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
53 mySolverConstraint = PlaneGCSSolver_Tools::createConstraint(
54 myBaseConstraint, getType(),
55 aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
57 myStorage->subscribeUpdates(this, PlaneGCSSolver_UpdateCoincidence::GROUP());
58 myStorage->notify(myBaseConstraint);
61 bool SketchSolver_ConstraintCoincidence::remove()
64 myFeatureExtremities[0] = EntityWrapperPtr();
65 myFeatureExtremities[1] = EntityWrapperPtr();
66 return SketchSolver_Constraint::remove();
69 void SketchSolver_ConstraintCoincidence::getAttributes(
70 EntityWrapperPtr& theValue,
71 std::vector<EntityWrapperPtr>& theAttributes)
73 SketchSolver_Constraint::getAttributes(theValue, theAttributes);
74 if (!myErrorMsg.empty() || !theAttributes[0]) {
75 theAttributes.clear();
80 myType = CONSTRAINT_PT_PT_COINCIDENT;
81 else if (theAttributes[2]) {
82 // check the type of entity (line or circle)
83 SketchSolver_EntityType anEntType = theAttributes[2]->type();
84 if (anEntType == ENTITY_LINE)
85 myType = CONSTRAINT_PT_ON_LINE;
86 else if (anEntType == ENTITY_CIRCLE || anEntType == ENTITY_ARC)
87 myType = CONSTRAINT_PT_ON_CIRCLE;
89 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
91 // obtain extremity points of the coincident feature for further checking of multi-coincidence
92 getCoincidentFeatureExtremities(myBaseConstraint, myStorage, myFeatureExtremities);
94 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
97 void SketchSolver_ConstraintCoincidence::notify(const FeaturePtr& theFeature,
98 PlaneGCSSolver_Update* theUpdater)
100 PlaneGCSSolver_UpdateCoincidence* anUpdater =
101 static_cast<PlaneGCSSolver_UpdateCoincidence*>(theUpdater);
102 bool isAccepted = anUpdater->addCoincidence(myAttributes.front(), myAttributes.back());
104 // additionally check the point is coincident to extremity of coincident feature
105 if (myFeatureExtremities[0] && myFeatureExtremities[1]) {
106 EntityWrapperPtr aPoint =
107 myAttributes.front()->type() == ENTITY_POINT ? myAttributes.front() : myAttributes.back();
109 for (int i = 0; i < 2; ++i)
110 isAccepted = isAccepted && !anUpdater->isPointOnEntity(aPoint, myFeatureExtremities[i]);
116 myStorage->addConstraint(myBaseConstraint, mySolverConstraint);
121 myStorage->removeConstraint(myBaseConstraint);