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