Salome HOME
2b52b976bdfe50ba91e43a654ffb059dbf341c2c
[modules/shaper.git] / src / ParametersPlugin / Test / TestParameterChangeValue.py
1 # Copyright (C) 2014-2023  CEA, EDF
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 """
21     TestParameterCreation.py
22
23     class ParametersPlugin_Parameter
24     static const std::string MY_PARAMETER_ID("Parameter");
25     static const std::string MY_VARIABLE_ID("variable");
26     static const std::string MY_EXPRESSION_ID("expression");
27
28     data()->addAttribute(ParametersPlugin_Parameter::VARIABLE_ID(),
29                          ModelAPI_AttributeString::typeId());
30     data()->addAttribute(ParametersPlugin_Parameter::EXPRESSION_ID(),
31                          ModelAPI_AttributeString::typeId());
32
33     class ModelAPI_ResultParameter
34     static const std::string MY_VALUE_ID("Value");
35     static const std::string MY_VALUE_ID("State");
36 """
37
38 #=========================================================================
39 # Initialization of the test
40 #=========================================================================
41 from GeomDataAPI import *
42 from ModelAPI import *
43 import math
44 import unittest
45 from salome.shaper import model
46
47 __updated__ = "2015-04-27"
48
49 #=========================================================================
50 # Create several parameters and a feature.
51 # 1. Basic parameter definition:
52 #    x1 = 150.0, y1 = 200.0
53 # 2. Referencing between parameters:
54 #    x2 = x1 + y1 + 100.0
55 # 3. Referencing in feature SketchCircle
56 #    CircleCenter = (x1 + 10.0, x1 + 20.0), CircleRadius = x1
57 # 3. Change value
58 #    x1 = 200.0
59 #=========================================================================
60 class TestParameterRename(unittest.TestCase):
61     def setUp(self):
62         self.aSession = ModelAPI_Session.get()
63         self.aDocument = self.aSession.moduleDocument()
64         self.createParameters()
65         self.createFeature()
66
67     def tearDown(self):
68         assert(model.checkPythonDump())
69         self.aSession.closeAll()
70
71     def createParameters(self):
72         ltNames = ["x1", "y1", "x2"]
73         ltExpressions = ["150.", "200.", "x1 + y1 + 100.0"]
74         self.dtParams = {}
75         for name, expr in zip(ltNames, ltExpressions):
76             self.aSession.startOperation()
77             aParam = self.aDocument.addFeature("Parameter")
78             aParamName = aParam.string("variable")
79             aParamName.setValue(name)
80             aParamExpr = aParam.string("expression")
81             aParamExpr.setValue(expr)
82             self.dtParams[name] = aParam
83             self.aSession.finishOperation()
84         self.assertEqual(len(self.dtParams), len(ltNames))
85
86         aParam = self.dtParams["x2"]
87         aResultAttr = modelAPI_ResultParameter(aParam.firstResult())
88         self.assertEqual(aResultAttr.data().real("Value").value(), 450.)
89
90     def createFeature(self):
91         self.aSession.startOperation()
92         aSketchCommonFeature = self.aDocument.addFeature("Sketch")
93         aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
94         origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
95         origin.setValue(0, 0, 0)
96         dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
97         dirx.setValue(1, 0, 0)
98         norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
99         norm.setValue(0, 0, 1)
100         aSketchCircle = aSketchFeature.addFeature("SketchCircle")
101         anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("circle_center"))
102         aRadiusAttr = aSketchCircle.real("circle_radius")
103         anCircleCentr.setValue(10., 20.)
104         aRadiusAttr.setValue(10.)
105         self.aSession.finishOperation()
106
107         self.anCircleCentr = anCircleCentr
108         self.aRadiusAttr = aRadiusAttr
109
110         # constraints to fix circle position and radius
111         self.aSession.startOperation()
112         # fix X coordinate
113         aDistanceConstraint1 = aSketchFeature.addFeature("SketchConstraintDistance")
114         refattrA = aDistanceConstraint1.refattr("ConstraintEntityA")
115         refattrA.setAttr(anCircleCentr)
116         refattrB = aDistanceConstraint1.refattr("ConstraintEntityB")
117         anOY = aSketchFeature.addFeature("SketchLine")
118         aStartPoint = geomDataAPI_Point2D(anOY.attribute("StartPoint"))
119         anEndPoint = geomDataAPI_Point2D(anOY.attribute("EndPoint"))
120         aStartPoint.setValue(0., 0.)
121         anEndPoint.setValue(0., 100.)
122         anOYRes = modelAPI_Result(self.aDocument.objectByName("Construction", "OY"))
123         anOY.selection("External").setValue(anOYRes, anOYRes.shape())
124         anOY.execute()
125         refattrB.setObject(modelAPI_ResultConstruction(anOY.firstResult()))
126         value = aDistanceConstraint1.real("ConstraintValue")
127         value.setText("x1 + 10.0")
128         aDistanceConstraint1.execute();
129         # fix Y coordinate
130         aDistanceConstraint2 = aSketchFeature.addFeature("SketchConstraintDistance")
131         refattrA = aDistanceConstraint2.refattr("ConstraintEntityA")
132         refattrA.setAttr(anCircleCentr)
133         refattrB = aDistanceConstraint2.refattr("ConstraintEntityB")
134         anOX = aSketchFeature.addFeature("SketchLine")
135         aStartPoint = geomDataAPI_Point2D(anOX.attribute("StartPoint"))
136         anEndPoint = geomDataAPI_Point2D(anOX.attribute("EndPoint"))
137         aStartPoint.setValue(0., 0.)
138         anEndPoint.setValue(100., 0.)
139         anOXRes = modelAPI_Result(self.aDocument.objectByName("Construction", "OX"))
140         anOX.selection("External").setValue(anOXRes, anOXRes.shape())
141         anOX.execute()
142         refattrB.setObject(modelAPI_ResultConstruction(anOX.firstResult()))
143         value = aDistanceConstraint2.real("ConstraintValue")
144         value.setText("x1 + 20.0")
145         aDistanceConstraint2.execute();
146         # fix radius
147         aRadiusConstraint = aSketchFeature.addFeature("SketchConstraintRadius")
148         refattrA = aRadiusConstraint.refattr("ConstraintEntityA")
149         refattrA.setObject(modelAPI_ResultConstruction(aSketchCircle.lastResult()))
150         aRadiusConstrAttr = aRadiusConstraint.real("ConstraintValue")
151         aRadiusConstrAttr.setText("x1")
152         aRadiusConstraint.execute()
153         self.aSession.finishOperation()
154
155         self.assertEqual(self.anCircleCentr.x(), 160.)
156         self.assertEqual(self.anCircleCentr.y(), 170.)
157         self.assertEqual(aRadiusAttr.value(), 150.)
158
159     def test_change_value(self):
160         aParam = self.dtParams["x1"]
161         aResultAttr = modelAPI_ResultParameter(aParam.firstResult())
162         self.aSession.startOperation()
163         aParam.string("expression").setValue("200.0")
164         self.aSession.finishOperation()
165
166         # Check value of the parameter
167         self.assertEqual(aResultAttr.data().real("Value").value(), 200.)
168         # Check value of references
169         aParam = self.dtParams["x2"]
170         aResultAttr = modelAPI_ResultParameter(aParam.firstResult())
171         self.assertEqual(aResultAttr.data().real("Value").value(), 500.)
172         # Check value in the feature
173         self.assertEqual(self.anCircleCentr.x(), 210.)
174         self.assertEqual(self.anCircleCentr.y(), 220.)
175         self.assertEqual(self.aRadiusAttr.value(), 200.)
176
177 if __name__ == "__main__":
178     test_program = unittest.main(exit=False)
179     assert test_program.result.wasSuccessful(), "Test failed"
180 #=========================================================================
181 # End of test
182 #=========================================================================