Salome HOME
Copyright update 2022
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintEqual.cpp
1 // Copyright (C) 2014-2022  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_ConstraintEqual.h>
21 #include <SketchSolver_Error.h>
22 #include <PlaneGCSSolver_AttributeBuilder.h>
23
24 #include <ModelAPI_AttributeRefAttr.h>
25 #include <SketchPlugin_Line.h>
26
27 void SketchSolver_ConstraintEqual::getAttributes(
28     EntityWrapperPtr& theValue,
29     std::vector<EntityWrapperPtr>& theAttributes)
30 {
31   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
32   if (!myErrorMsg.empty() || !theAttributes[2] || !theAttributes[3]) {
33     theAttributes.clear();
34     return;
35   }
36
37   // Check the quantity of entities of each type
38   int aNbLines = 0;
39   int aNbArcs = 0;
40   int aNbCircs = 0;
41   int aNbEllipses = 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     else if (aType == ENTITY_ELLIPSE || aType == ENTITY_ELLIPTIC_ARC)
55       ++aNbEllipses;
56   }
57
58   if (aNbLines + aNbArcs + aNbCircs + aNbEllipses != 2 ||
59      (aNbArcs == 1 && aNbEllipses != 0) || aNbEllipses == 1) {
60     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
61     return;
62   }
63
64   switch (aNbLines) {
65   case 0:
66     if (aNbEllipses == 2) {
67       myType = CONSTRAINT_EQUAL_ELLIPSES;
68       std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
69           std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
70       myAuxValue = ScalarWrapperPtr(new PlaneGCSSolver_ScalarWrapper(aStorage->createParameter()));
71       theValue = myAuxValue;
72     }
73     else
74       myType = CONSTRAINT_EQUAL_RADIUS;
75     break;
76   case 1:
77     myType = CONSTRAINT_EQUAL_LINE_ARC;
78     if (isArcFirst) { // change the order of arc and line
79       EntityWrapperPtr aTmp = theAttributes[2];
80       theAttributes[2] = theAttributes[3];
81       theAttributes[3] = aTmp;
82     }
83     break;
84   default:
85     myType = CONSTRAINT_EQUAL_LINES;
86
87     AttributeRefAttrPtr aRefLine1 =
88         myBaseConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
89     FeaturePtr aLine1 = ModelAPI_Feature::feature(aRefLine1->object());
90     if (aLine1) {
91       PlaneGCSSolver_AttributeBuilder aBuilder(myStorage);
92       // store length of first line as a value for constraint
93       // (will be used to make equal lengths of lines)
94       theValue = aBuilder.createAttribute(aLine1->attribute(SketchPlugin_Line::LENGTH_ID()));
95     }
96     break;
97   }
98 }
99
100 bool SketchSolver_ConstraintEqual::remove()
101 {
102   if (myAuxValue) {
103     std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
104         std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
105     GCS::SET_pD aParams;
106     aParams.insert(myAuxValue->scalar());
107     aStorage->removeParameters(aParams);
108   }
109   return SketchSolver_Constraint::remove();
110 }