Salome HOME
updated copyright message
[modules/shaper.git] / src / ParametersPlugin / Test / TestParameterCreation.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
45 __updated__ = "2015-04-27"
46
47 aSession = ModelAPI_Session.get()
48 aDocument = aSession.moduleDocument()
49 #=========================================================================
50 # Creation of a sketch
51 #=========================================================================
52 aSession.startOperation()
53 aSketchCommonFeature = aDocument.addFeature("Sketch")
54 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
55 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
56 origin.setValue(0, 0, 0)
57 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
58 dirx.setValue(1, 0, 0)
59 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
60 norm.setValue(0, 0, 1)
61 aSession.finishOperation()
62 #=========================================================================
63 # Create several parameters.
64 # 1. Basic parameter definition:
65 #    x1 = 150.0, y1 = 50.0, cr1 = 100.0, cl = 250.0;
66 # 2. Check referencing between parameters:
67 #    x2 = x1 + 100.0, y2 = y1/2.
68 # 3. Check math module
69 #    tm = 2 * pi
70 #=========================================================================
71 ltNames = ["x1", "y1", "cr1", "cl1"]
72 ltExpressions = ["150.", "50.", "100.", "250."]
73 dtParams = {}
74 aSession.startOperation()
75 for name, expr in zip(ltNames, ltExpressions):
76     aParam = aDocument.addFeature("Parameter")
77     aParamName = aParam.string("variable")
78     aParamName.setValue(name)
79     aParamExpr = aParam.string("expression")
80     aParamExpr.setValue(expr)
81     dtParams[name] = aParam
82 aSession.finishOperation()
83 assert (len(dtParams) == len(ltNames))
84 # Check results
85 for name, expr in zip(ltNames, ltExpressions):
86     aParam = dtParams[name]
87     aResultAttr = modelAPI_ResultParameter(aParam.firstResult())
88     assert(aResultAttr.data())
89     assert(aResultAttr.data().real("Value"))
90     aResultValue = aResultAttr.data().real("Value").value()
91     print(aResultValue, " == ", float(expr))
92     assert(aResultValue == float(expr))
93
94 # Check referencing between parameters
95 aSession.startOperation()
96 ltNames = ["x2", "y2"]
97 ltExpressions = ["x1 + 100.0", "y1/2."]
98 aSession.startOperation()
99 for name, expr in zip(ltNames, ltExpressions):
100     aParam = aDocument.addFeature("Parameter")
101     aParamName = aParam.string("variable")
102     aParamName.setValue(name)
103     aParamExpr = aParam.string("expression")
104     aParamExpr.setValue(expr)
105     dtParams[name] = aParam
106 aSession.finishOperation()
107
108 aParam = dtParams["x2"]
109 aResultAttr = modelAPI_ResultParameter(aParam.firstResult())
110 assert(aResultAttr.data())
111 assert(aResultAttr.data().real("Value"))
112 aX2Value = aResultAttr.data().real("Value").value()
113 assert (aX2Value == 250.)
114 aParam = dtParams["y2"]
115 aResultAttr = modelAPI_ResultParameter(aParam.firstResult())
116 assert(aResultAttr.data())
117 assert(aResultAttr.data().real("Value"))
118 aY2Value = aResultAttr.data().real("Value").value()
119 assert (aY2Value == 25.)
120 # check math
121 aSession.startOperation()
122 aParam = aDocument.addFeature("Parameter")
123 aParamName = aParam.string("variable")
124 aParamName.setValue("tm")
125 aParamExpr = aParam.string("expression")
126 aParamExpr.setValue("round(2 * pi, 6)")
127 aSession.finishOperation()
128 aResultAttr = modelAPI_ResultParameter(aParam.firstResult())
129 assert(aResultAttr.data())
130 assert(aResultAttr.data().real("Value"))
131 aTmValue = aResultAttr.data().real("Value").value()
132 assert (aTmValue == round(2 * math.pi, 6))
133 #=========================================================================
134 # Use parameters to set radius of a circle :
135 # 1. Create a circle (250., 250), r = 25.
136 # 2. Create a radius constraint and set 'cr1' as text value of constraint
137 #=========================================================================
138 aSession.startOperation()
139 aSketchCircle = aSketchFeature.addFeature("SketchCircle")
140 anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("circle_center"))
141 aRadiusAttr = aSketchCircle.real("circle_radius")
142 anCircleCentr.setValue(250., 250)
143 aRadiusAttr.setValue(25.)
144 aSession.finishOperation()
145 # Apply parameter
146 aSession.startOperation()
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("cr1")
152 aRadiusConstraint.execute()
153 aSession.finishOperation()
154 assert(aRadiusAttr.value() == 100.)
155 #=========================================================================
156 # Use parameters for a length constraint on a line:
157 # 1. Create a line A(10., 10.) - B(-10., -10.)
158 # 2. Create a length constraint, l = 100;
159 # 3. Set a 'cl1' as text value of length attribute
160 #=========================================================================
161 aSession.startOperation()
162 aSketchLine = aSketchFeature.addFeature("SketchLine")
163 aLineStartPoint = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
164 aLineEndPoint = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
165 aLineStartPoint.setValue(10., 10.)
166 aLineEndPoint.setValue(-10., -10.)
167 aSession.finishOperation()
168 # Length constraint
169 aSession.startOperation()
170 aLengthConstraint = aSketchFeature.addFeature("SketchConstraintLength")
171 refattrA = aLengthConstraint.refattr("ConstraintEntityA")
172 refattrA.setObject(modelAPI_ResultConstruction(aSketchLine.firstResult()))
173 aLengthConstraint.execute()
174 aSession.finishOperation()
175
176 # Apply parameter
177 aSession.startOperation()
178 aLengthAttr = aLengthConstraint.real("ConstraintValue")
179 aLengthAttr.setText("cl1")
180 aSession.finishOperation()
181 assert(aLengthAttr.value() == 250.)
182 #=========================================================================
183 # End of test
184 #=========================================================================
185
186 from salome.shaper import model
187 assert(model.checkPythonDump())