Salome HOME
Fix for the issue #2753 : error when dump/load script
[modules/shaper.git] / src / ParametersPlugin / Test / TestParameterRename.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. Check renaming
59 #    x1 -> a1
60 # 4. Check renaming to not unique name
61 #    x1 -> y1
62 #=========================================================================
63 class TestParameterRename(unittest.TestCase):
64     def setUp(self):
65         self.aSession = ModelAPI_Session.get()
66         self.aDocument = self.aSession.moduleDocument()
67         self.createParameters()
68         self.createFeature()
69
70     def tearDown(self):
71         #assert(model.checkPythonDump())
72         #self.aSession.closeAll()
73         pass
74
75     def createParameters(self):
76         ltNames = ["x1", "y1", "x2"]
77         ltExpressions = ["150.", "200.", "x1 + y1 + 100.0"]
78         self.dtParams = {}
79         for name, expr in zip(ltNames, ltExpressions):
80             self.aSession.startOperation()
81             aParam = self.aDocument.addFeature("Parameter")
82             aParamName = aParam.string("variable")
83             aParamName.setValue(name)
84             aParamExpr = aParam.string("expression")
85             aParamExpr.setValue(expr)
86             self.dtParams[name] = aParam
87             self.aSession.finishOperation()
88         self.assertEqual(len(self.dtParams), len(ltNames))
89
90         aParam = self.dtParams["x2"]
91         aResultAttr = modelAPI_ResultParameter(aParam.firstResult())
92         self.assertEqual(aResultAttr.data().real("Value").value(), 450.)
93
94     def createFeature(self):
95         self.aSession.startOperation()
96         aSketchCommonFeature = self.aDocument.addFeature("Sketch")
97         aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
98         origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
99         origin.setValue(0, 0, 0)
100         dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
101         dirx.setValue(1, 0, 0)
102         norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
103         norm.setValue(0, 0, 1)
104         aSketchCircle = aSketchFeature.addFeature("SketchCircle")
105         anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("circle_center"))
106         aRadiusAttr = aSketchCircle.real("circle_radius")
107         anCircleCentr.setValue(10., 20.)
108         aRadiusAttr.setValue(10.)
109         self.aSession.finishOperation()
110
111         self.anCircleCentr = anCircleCentr
112         self.aRadiusAttr = aRadiusAttr
113
114         # constraints to fix circle position and radius
115         self.aSession.startOperation()
116         # fix X coordinate
117         aDistanceConstraint1 = aSketchFeature.addFeature("SketchConstraintDistance")
118         refattrA = aDistanceConstraint1.refattr("ConstraintEntityA")
119         refattrA.setAttr(anCircleCentr)
120         refattrB = aDistanceConstraint1.refattr("ConstraintEntityB")
121         anOY = aSketchFeature.addFeature("SketchLine")
122         aStartPoint = geomDataAPI_Point2D(anOY.attribute("StartPoint"))
123         anEndPoint = geomDataAPI_Point2D(anOY.attribute("EndPoint"))
124         aStartPoint.setValue(0., 0.)
125         anEndPoint.setValue(0., 100.)
126         anOYRes = modelAPI_Result(self.aDocument.objectByName("Construction", "OY"))
127         anOY.selection("External").setValue(anOYRes, anOYRes.shape())
128         anOY.execute()
129         refattrB.setObject(modelAPI_ResultConstruction(anOY.firstResult()))
130         valueX = aDistanceConstraint1.real("ConstraintValue")
131         valueX.setText("x1 + 10.0")
132         aDistanceConstraint1.execute();
133         # fix Y coordinate
134         aDistanceConstraint2 = aSketchFeature.addFeature("SketchConstraintDistance")
135         refattrA = aDistanceConstraint2.refattr("ConstraintEntityA")
136         refattrA.setAttr(anCircleCentr)
137         refattrB = aDistanceConstraint2.refattr("ConstraintEntityB")
138         anOX = aSketchFeature.addFeature("SketchLine")
139         aStartPoint = geomDataAPI_Point2D(anOX.attribute("StartPoint"))
140         anEndPoint = geomDataAPI_Point2D(anOX.attribute("EndPoint"))
141         aStartPoint.setValue(0., 0.)
142         anEndPoint.setValue(100., 0.)
143         anOXRes = modelAPI_Result(self.aDocument.objectByName("Construction", "OX"))
144         anOX.selection("External").setValue(anOXRes, anOXRes.shape())
145         anOX.execute()
146         refattrB.setObject(modelAPI_ResultConstruction(anOX.firstResult()))
147         valueY = aDistanceConstraint2.real("ConstraintValue")
148         valueY.setText("x1 + 20.0")
149         aDistanceConstraint2.execute();
150         # fix radius
151         aRadiusConstraint = aSketchFeature.addFeature("SketchConstraintRadius")
152         refattrA = aRadiusConstraint.refattr("ConstraintEntityA")
153         refattrA.setObject(modelAPI_ResultConstruction(aSketchCircle.lastResult()))
154         aRadiusConstrAttr = aRadiusConstraint.real("ConstraintValue")
155         aRadiusConstrAttr.setText("x1")
156         aRadiusConstraint.execute()
157         self.aSession.finishOperation()
158
159         self.aCircleCenterX = valueX
160         self.aCircleCenterY = valueY
161         self.aCircleRadius = aRadiusConstrAttr
162
163         self.assertEqual(self.anCircleCentr.x(), 160.)
164         self.assertEqual(self.anCircleCentr.y(), 170.)
165         self.assertEqual(aRadiusAttr.value(), 150.)
166
167     def test_rename(self):
168         # Rename
169         aParam = self.dtParams["x1"]
170         aResultAttr = modelAPI_ResultParameter(aParam.firstResult())
171         self.aSession.startOperation()
172         aResultAttr.data().setName("a1")
173         self.aSession.finishOperation()
174
175         # Check rename in the parameter
176         self.assertEqual(aParam.name(), "a1")
177         self.assertEqual(aParam.string("variable").value(), "a1")
178         self.assertEqual(aResultAttr.data().name(), "a1")
179         # Check rename in references
180         aParam = self.dtParams["x2"]
181         self.assertEqual(aParam.string("expression").value(), "a1 + y1 + 100.0")
182         # Check rename in the feature
183         self.assertEqual(self.aCircleCenterX.text(), "a1 + 10.0")
184         self.assertEqual(self.aCircleCenterY.text(), "a1 + 20.0")
185         self.assertEqual(self.aCircleRadius.text(), "a1")
186         # Check values
187         self.assertEqual(self.anCircleCentr.x(), 160.)
188         self.assertEqual(self.anCircleCentr.y(), 170.)
189         self.assertEqual(self.aRadiusAttr.value(), 150.)
190
191     def test_rename_not_unique(self):
192         # Rename to not unique name
193         aParam = self.dtParams["x1"]
194         aParamX1 = aParam
195         aResultAttr = modelAPI_ResultParameter(aParam.firstResult())
196         self.aSession.startOperation() # don't finish operation until feature is valid (real case)
197         aResultAttr.data().setName("y1")
198         # Check rename in the parameter (Expected: renamed, but invalid)
199         self.assertEqual(aParam.name(), "y1")
200         validators = ModelAPI_Session.get().validators()
201         self.assertEqual(validators.validate(aParamX1), False)
202         # Check rename in references (Expected: not renamed)
203         aParam = self.dtParams["x2"]
204         self.assertEqual(aParam.string("expression").value(), "x1 + y1 + 100.0")
205         # Check rename in the feature (Expected: not renamed)
206         self.assertEqual(self.aCircleCenterX.text(), "x1 + 10.0")
207         self.assertEqual(self.aCircleCenterY.text(), "x1 + 20.0")
208         self.assertEqual(self.aCircleRadius.text(), "x1")
209         # Check values
210         self.assertEqual(self.anCircleCentr.x(), 160.)
211         self.assertEqual(self.anCircleCentr.y(), 170.)
212         self.assertEqual(self.aRadiusAttr.value(), 150.)
213
214         # rename to the correct one, but new
215         aResultAttr.data().setName("xx1")
216         self.aSession.finishOperation() # feature becomes valid
217         # Check rename in the parameter (Expected: renamed)
218         self.assertEqual(validators.validate(aParamX1), True)
219         self.assertEqual(aParamX1.name(), "xx1")
220         self.assertEqual(aParamX1.error(), "")
221         # Check rename in references (Expected: renamed)
222         aParam = self.dtParams["x2"]
223         self.assertEqual(aParam.string("expression").value(), "xx1 + y1 + 100.0")
224         # Check rename in the feature (Expected: renamed)
225         self.assertEqual(self.aCircleCenterX.text(), "xx1 + 10.0")
226         self.assertEqual(self.aCircleCenterY.text(), "xx1 + 20.0")
227         self.assertEqual(self.aCircleRadius.text(), "xx1")
228         # Check values
229         self.assertEqual(self.anCircleCentr.x(), 160.)
230         self.assertEqual(self.anCircleCentr.y(), 170.)
231         self.assertEqual(self.aRadiusAttr.value(), 150.)
232
233 if __name__ == "__main__":
234     test_program = unittest.main(exit=False)
235     assert test_program.result.wasSuccessful(), "Test failed"
236 #=========================================================================
237 # End of test
238 #=========================================================================