Salome HOME
Merge remote-tracking branch 'remotes/origin/HigherLevelObjectsHistory'
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintCoincidence.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <SketchSolver_ConstraintCoincidence.h>
21 #include <SketchSolver_Error.h>
22 #include <PlaneGCSSolver_Tools.h>
23 #include <PlaneGCSSolver_UpdateCoincidence.h>
24
25 #include <SketchPlugin_Arc.h>
26 #include <SketchPlugin_Line.h>
27
28 static void getCoincidentFeatureExtremities(const ConstraintPtr& theConstraint,
29                                             const StoragePtr& theStorage,
30                                             EntityWrapperPtr theExtremities[2])
31 {
32   for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
33     AttributeRefAttrPtr aRefAttr = theConstraint->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
34     if (!aRefAttr || !aRefAttr->isObject())
35       continue;
36
37     FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
38     if (!aFeature)
39       continue;
40
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()));
47     }
48   }
49 }
50
51
52 void SketchSolver_ConstraintCoincidence::process()
53 {
54   cleanErrorMsg();
55   if (!myBaseConstraint || !myStorage) {
56     // Not enough parameters are assigned
57     return;
58   }
59
60   EntityWrapperPtr aValue;
61   std::vector<EntityWrapperPtr> anAttributes;
62   getAttributes(aValue, anAttributes);
63   if (!myErrorMsg.empty())
64     return;
65   if (anAttributes.empty()) {
66     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
67     return;
68   }
69
70   mySolverConstraint = PlaneGCSSolver_Tools::createConstraint(
71       myBaseConstraint, getType(),
72       aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
73
74   myStorage->subscribeUpdates(this, PlaneGCSSolver_UpdateCoincidence::GROUP());
75   myStorage->notify(myBaseConstraint);
76 }
77
78 bool SketchSolver_ConstraintCoincidence::remove()
79 {
80   myInSolver = false;
81   myFeatureExtremities[0] = EntityWrapperPtr();
82   myFeatureExtremities[1] = EntityWrapperPtr();
83   return SketchSolver_Constraint::remove();
84 }
85
86 void SketchSolver_ConstraintCoincidence::getAttributes(
87     EntityWrapperPtr& theValue,
88     std::vector<EntityWrapperPtr>& theAttributes)
89 {
90   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
91   if (!myErrorMsg.empty() || !theAttributes[0]) {
92     theAttributes.clear();
93     return;
94   }
95
96   if (theAttributes[1])
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;
105     else
106       myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
107
108     // obtain extremity points of the coincident feature for further checking of multi-coincidence
109     getCoincidentFeatureExtremities(myBaseConstraint, myStorage, myFeatureExtremities);
110   } else
111     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
112 }
113
114 void SketchSolver_ConstraintCoincidence::notify(const FeaturePtr&      theFeature,
115                                                 PlaneGCSSolver_Update* theUpdater)
116 {
117   PlaneGCSSolver_UpdateCoincidence* anUpdater =
118       static_cast<PlaneGCSSolver_UpdateCoincidence*>(theUpdater);
119   bool isAccepted = anUpdater->addCoincidence(myAttributes.front(), myAttributes.back());
120
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();
125
126     for (int i = 0; i < 2; ++i)
127       isAccepted = isAccepted && !anUpdater->isPointOnEntity(aPoint, myFeatureExtremities[i]);
128   }
129
130   if (isAccepted) {
131     if (!myInSolver) {
132       myInSolver = true;
133       myStorage->addConstraint(myBaseConstraint, mySolverConstraint);
134     }
135   } else {
136     if (myInSolver) {
137       myInSolver = false;
138       myStorage->removeConstraint(myBaseConstraint);
139     }
140   }
141 }