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