Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintEqual.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_ConstraintEqual.h>
22 #include <SketchSolver_Error.h>
23 #include <PlaneGCSSolver_AttributeBuilder.h>
24
25 #include <ModelAPI_AttributeRefAttr.h>
26 #include <SketchPlugin_Line.h>
27
28 void SketchSolver_ConstraintEqual::getAttributes(
29     EntityWrapperPtr& theValue,
30     std::vector<EntityWrapperPtr>& theAttributes)
31 {
32   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
33   if (!myErrorMsg.empty() || !theAttributes[2] || !theAttributes[3]) {
34     theAttributes.clear();
35     return;
36   }
37
38   // Check the quantity of entities of each type
39   int aNbLines = 0;
40   int aNbArcs = 0;
41   int aNbCircs = 0;
42   bool isArcFirst = false; // in line-arc equivalence, the line should be first
43   std::vector<EntityWrapperPtr>::iterator anAttrIt = theAttributes.begin() + 2;
44   for (; anAttrIt != theAttributes.end(); ++anAttrIt) {
45     SketchSolver_EntityType aType = (*anAttrIt)->type();
46     if (aType == ENTITY_LINE)
47       ++aNbLines;
48     else if (aType == ENTITY_CIRCLE)
49       ++aNbCircs;
50     else if (aType == ENTITY_ARC) {
51       ++aNbArcs;
52       isArcFirst = (aNbLines == 0);
53     }
54   }
55
56   if (aNbLines + aNbArcs + aNbCircs != 2 ||
57      (aNbLines == aNbCircs && aNbArcs == 0)) {
58     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
59     return;
60   }
61
62   switch (aNbLines) {
63   case 0:
64     myType = CONSTRAINT_EQUAL_RADIUS;
65     break;
66   case 1:
67     myType = CONSTRAINT_EQUAL_LINE_ARC;
68     if (isArcFirst) { // change the order of arc and line
69       EntityWrapperPtr aTmp = theAttributes[2];
70       theAttributes[2] = theAttributes[3];
71       theAttributes[3] = aTmp;
72     }
73     break;
74   default:
75     myType = CONSTRAINT_EQUAL_LINES;
76
77     AttributeRefAttrPtr aRefLine1 =
78         myBaseConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
79     FeaturePtr aLine1 = ModelAPI_Feature::feature(aRefLine1->object());
80     if (aLine1) {
81       PlaneGCSSolver_AttributeBuilder aBuilder(myStorage);
82       // store length of first line as a value for constraint
83       // (will be used to make equal lengths of lines)
84       theValue = aBuilder.createAttribute(aLine1->attribute(SketchPlugin_Line::LENGTH_ID()));
85     }
86     break;
87   }
88 }