]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/Test/TestCreateCircleByCenterAndPassed.py
Salome HOME
7e5ba93c0f82b6b63f2d4fc5a82be2743dbe8992
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateCircleByCenterAndPassed.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     TestCreateCircleByCenterAndPassed.py
22
23     Test creation methods of a circle built by a center and a passed point
24 """
25
26 #=========================================================================
27 # Initialization of the test
28 #=========================================================================
29 from GeomDataAPI import *
30 from GeomAPI import *
31 from ModelAPI import *
32 from SketchAPI import SketchAPI_Sketch
33 from salome.shaper import model
34 import math
35
36 __updated__ = "2023-06-28"
37
38
39 #=========================================================================
40 # Auxiliary functions
41 #=========================================================================
42 TOLERANCE = 1.e-7
43
44 def verifyLastCircle(theSketch, theX, theY, theR):
45     """
46     subroutine to verify position of last circle in the sketch
47     """
48     aLastCircle = model.lastSubFeature(theSketch, "SketchCircle")
49     model.assertCircle(aLastCircle, [theX, theY], theR)
50
51 def verifyPointOnLine(thePoint, theLine):
52     aDistance = model.distancePointLine(thePoint, theLine)
53     assert aDistance < TOLERANCE, "Point is not on Line, distance: {0}".format(aDistance)
54
55 def verifyTangentCircles(theCircle1, theCircle2):
56     aCenter1 = geomDataAPI_Point2D(theCircle1.attribute("circle_center"))
57     aCenter2 = geomDataAPI_Point2D(theCircle2.attribute("circle_center"))
58     aDistCC = model.distancePointPoint(aCenter1, aCenter2)
59     aRadius1 = theCircle1.real("circle_radius").value()
60     aRadius2 = theCircle2.real("circle_radius").value()
61     aRSum = aRadius1 + aRadius2
62     aRDiff = math.fabs(aRadius1 - aRadius2)
63     assert math.fabs(aRSum - aDistCC) < TOLERANCE or math.fabs(aRDiff - aDistCC) < TOLERANCE, "Circles do not tangent"
64
65 def verifyTangentCircleLine(theCircle, theLine):
66     aCenter = geomDataAPI_Point2D(theCircle.attribute("circle_center"))
67     aRadius = theCircle.real("circle_radius").value()
68     aDistCL = model.distancePointLine(aCenter, theLine)
69     assert math.fabs(aDistCL - aRadius) < TOLERANCE, "Circle and line are not tangent"
70
71 #=========================================================================
72 # Set old version of Circle for avoid create point.
73 #=========================================================================
74 def setVersionOfCircle(theCircle, theVersion):
75     aCircleVersion = theCircle.integer("version")
76     assert (type(aCircleVersion) == ModelAPI_AttributeInteger)
77     aCircleVersion.setValue(theVersion)
78
79 #=========================================================================
80 # Start of test
81 #=========================================================================
82
83 aSession = ModelAPI_Session.get()
84 aDocument = aSession.moduleDocument()
85 #=========================================================================
86 # Creation of a sketch
87 #=========================================================================
88 aSession.startOperation()
89 aSketchCommonFeature = aDocument.addFeature("Sketch")
90 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
91 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
92 origin.setValue(0, 0, 0)
93 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
94 dirx.setValue(1, 0, 0)
95 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
96 norm.setValue(0, 0, 1)
97 aSession.finishOperation()
98 aSketch = SketchAPI_Sketch(aSketchFeature)
99
100 #=========================================================================
101 # Test 1. Create a circle by center and radius
102 #=========================================================================
103 aSession.startOperation()
104 aCircle = aSketchFeature.addFeature("SketchCircle")
105 assert (aCircle.getKind() == "SketchCircle")
106 aCircleCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
107 assert (not aCircleCenter.isInitialized())
108 aCircleRadius = aCircle.real("circle_radius")
109 assert (type(aCircleRadius) == ModelAPI_AttributeDouble)
110 setVersionOfCircle(aCircle, 0)
111 # ModelAPI_AttributeDouble.typeId() is checked in ModelAPI_TestConstants
112 assert (aCircleRadius.attributeType() == ModelAPI_AttributeDouble.typeId())
113 aCircleCenter.setValue(-25., -25)
114 aCircleRadius.setValue(25.)
115 aSession.finishOperation()
116 verifyLastCircle(aSketchFeature, -25., -25., 25.)
117 #=========================================================================
118 # Edit the Circle
119 # 1. check that changing the center of a circle does not affect radius and vise versa
120 # 2. also check that int is acceptable as well as a real
121 #=========================================================================
122 aSession.startOperation()
123 aCircleCenter.setValue(10, 60)
124 aSession.finishOperation()
125 verifyLastCircle(aSketchFeature, 10., 60., 25.)
126 aSession.startOperation()
127 aCircleRadius.setValue(int(20))
128 aSession.finishOperation()
129 verifyLastCircle(aSketchFeature, 10., 60., 20.)
130
131 #=========================================================================
132 # Test 2. Create a circle as a macro-feature by center and passed point
133 #=========================================================================
134 aSession.startOperation()
135 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
136 assert (aCircle.getKind() == "SketchMacroCircle")
137 aCircleCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
138 assert (not aCircleCenter.isInitialized())
139 aCirclePassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
140 assert (not aCirclePassed.isInitialized())
141 aCircleType = aCircle.string("circle_type")
142 assert (not aCircleType.isInitialized())
143 aCircleType.setValue("circle_type_by_center_and_passed_points")
144 setVersionOfCircle(aCircle, 0)
145 aCircleCenter.setValue(-25., -25)
146 aCirclePassed.setValue(0., -25.)
147 aRadius = model.distancePointPoint(aCircleCenter, aCirclePassed)
148 aSession.finishOperation()
149 assert (aSketchFeature.numberOfSubs() == 2)
150 verifyLastCircle(aSketchFeature, -25., -25., aRadius)
151
152 #=========================================================================
153 # Test 3. Create a circle as a macro-feature by center and passed point coincident to other points
154 #=========================================================================
155 # get previous circle
156 aPrevCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
157 aPrevCenter = geomDataAPI_Point2D(aPrevCircle.attribute("circle_center"))
158 # create additional point
159 aPointCoordinates = [0., 0.]
160 aSession.startOperation()
161 aPoint = aSketchFeature.addFeature("SketchPoint")
162 aPointCoord = geomDataAPI_Point2D(aPoint.attribute("PointCoordinates"))
163 aPointCoord.setValue(aPointCoordinates[0], aPointCoordinates[1])
164 aSession.finishOperation()
165 # create new circle
166 aSession.startOperation()
167 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
168 aCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
169 aCenterRef = aCircle.refattr("center_point_ref")
170 assert (not aCenterRef.isInitialized())
171 aPassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
172 aPassedRef = aCircle.refattr("passed_point_ref")
173 assert (not aPassedRef.isInitialized())
174 aCircleType = aCircle.string("circle_type")
175 assert (not aCircleType.isInitialized())
176 # initialize attributes
177 aCircleType.setValue("circle_type_by_center_and_passed_points")
178 setVersionOfCircle(aCircle, 0)
179 aCenterRef.setObject(aPoint.lastResult())
180 aCenter.setValue(aPointCoord.pnt())
181 aPassedRef.setAttr(aPrevCenter)
182 aPassed.setValue(aPrevCenter.pnt())
183 aRadius = model.distancePointPoint(aPrevCenter, aPointCoord)
184 aSession.finishOperation()
185 assert (aSketchFeature.numberOfSubs() == 6)
186 model.assertPoint(aPointCoord, aPointCoordinates)
187 verifyLastCircle(aSketchFeature, aPointCoord.x(), aPointCoord.y(), aRadius)
188 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 2)
189
190 #=========================================================================
191 # Test 4. Create a circle as a macro-feature by center on a line and passed point on another circle
192 #=========================================================================
193 # get previous circle
194 aPrevCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
195 aPrevCenter = geomDataAPI_Point2D(aPrevCircle.attribute("circle_center"))
196 aPrevCenterXY = [aPrevCenter.x(), aPrevCenter.y()]
197 aPrevRadius = aPrevCircle.real("circle_radius").value()
198 # create additional line
199 aLineStart = [10., 0.]
200 aLineEnd = [10., 50.]
201 aSession.startOperation()
202 aLine = aSketchFeature.addFeature("SketchLine")
203 aStartPnt = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
204 aStartPnt.setValue(aLineStart[0], aLineStart[1])
205 aEndPnt = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
206 aEndPnt.setValue(aLineEnd[0], aLineEnd[1])
207 aSession.finishOperation()
208 # create new circle
209 aSession.startOperation()
210 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
211 aCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
212 aCenterRef = aCircle.refattr("center_point_ref")
213 aPassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
214 aPassedRef = aCircle.refattr("passed_point_ref")
215 aCircleType = aCircle.string("circle_type")
216 # initialize attributes
217 aCircleType.setValue("circle_type_by_center_and_passed_points")
218 setVersionOfCircle(aCircle, 0)
219 aCenterRef.setObject(aLine.lastResult())
220 anExpectedCenter = [(aLineStart[0] + aLineEnd[0]) * 0.5, (aLineStart[1] + aLineEnd[1]) * 0.5]
221 aCenter.setValue(anExpectedCenter[0], anExpectedCenter[1])
222 aPassedRef.setObject(aPrevCircle.lastResult())
223 aPassed.setValue(aPrevCenter.x() + aPrevRadius, aPrevCenter.y())
224 aRadius = model.distancePointPoint(aCenter, aPassed)
225 aSession.finishOperation()
226 assert (aSketchFeature.numberOfSubs() == 10)
227 # check connected features do not change their positions
228 model.assertPoint(aPrevCenter, aPrevCenterXY)
229 assert(aPrevCircle.real("circle_radius").value() == aPrevRadius)
230 model.assertLine(aLine, aLineStart, aLineEnd)
231 # verify newly created circle
232 aCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
233 aCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
234 model.assertPoint(aCenter, anExpectedCenter)
235 verifyPointOnLine(aCenter, aLine)
236 verifyTangentCircles(aCircle, aPrevCircle)
237 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 3)
238 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 1)
239
240 #=========================================================================
241 # Test 5. Create a circle as a macro-feature by center and passed point on line
242 #=========================================================================
243 # create new circle
244 aSession.startOperation()
245 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
246 aCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
247 aCenterRef = aCircle.refattr("center_point_ref")
248 aPassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
249 aPassedRef = aCircle.refattr("passed_point_ref")
250 aCircleType = aCircle.string("circle_type")
251 # initialize attributes
252 aCircleType.setValue("circle_type_by_center_and_passed_points")
253 setVersionOfCircle(aCircle, 0)
254 anExpectedCenter = [(aLineStart[0] + aLineEnd[0]) * 0.5 + 10., (aLineStart[1] + aLineEnd[1]) * 0.5]
255 aCenter.setValue(anExpectedCenter[0], anExpectedCenter[1])
256 aPassedRef.setObject(aLine.lastResult())
257 aPassed.setValue(aLineStart[0], aLineStart[1])
258 aSession.finishOperation()
259 assert (aSketchFeature.numberOfSubs() == 12)
260 # check connected features do not change their positions
261 model.assertLine(aLine, aLineStart, aLineEnd)
262 # verify newly created circle
263 aCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
264 aCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
265 model.assertPoint(aCenter, anExpectedCenter)
266 verifyTangentCircleLine(aCircle, aLine)
267 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 3)
268 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 2)
269
270 #=========================================================================
271 # Test 5. Create a circle as a macro-feature by center and passed point placed on the same line
272 #         Check the circle is not created
273 #=========================================================================
274 aSession.startOperation()
275 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
276 aCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
277 aCenterRef = aCircle.refattr("center_point_ref")
278 aPassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
279 aPassedRef = aCircle.refattr("passed_point_ref")
280 aCircleType = aCircle.string("circle_type")
281 # initialize attributes
282 aCircleType.setValue("circle_type_by_center_and_passed_points")
283 setVersionOfCircle(aCircle, 0)
284 aCenterRef.setObject(aLine.lastResult())
285 aCenter.setValue(anExpectedCenter[0], anExpectedCenter[1])
286 aPassedRef.setObject(aLine.lastResult())
287 aPassed.setValue(aLineStart[0], aLineStart[1])
288 aSession.finishOperation()
289 aLastFeature = aSketchFeature.subFeature(aSketchFeature.numberOfSubs() - 1)
290 assert aLastFeature.getKind() == "SketchMacroCircle", "ERROR: SketchMacroCircle has NOT expected to be valid"
291 aSession.startOperation()
292 aDocument.removeFeature(aCircle)
293 aSession.finishOperation()
294 assert (aSketchFeature.numberOfSubs() == 12)
295
296 #=========================================================================
297 # Test 6. Create a circle with point on circle line (addCircleWithPoint)
298 #=========================================================================
299 # create new circle
300 aSession.startOperation()
301 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
302 aCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
303 aCenterRef = aCircle.refattr("center_point_ref")
304 aPassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
305 aPassedRef = aCircle.refattr("passed_point_ref")
306 aCircleType = aCircle.string("circle_type")
307 aCircleAngle = aCircle.real("circle_angle")
308 # initialize attributes
309 aCircleType.setValue("circle_type_by_center_and_passed_points")
310 setVersionOfCircle(aCircle, 20232206)
311 aCenter.setValue(35., -35)
312 anExpectedCenter = [35., -35]
313 aPassed.setValue(45., -35)
314 aCircleAngle.setValue(90.)
315 anExpectedRot = [45., -35]
316 aSession.finishOperation()
317 assert (aSketchFeature.numberOfSubs() == 15)
318 # verify newly created circle
319 aCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
320 aCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
321 aRotPoint = geomDataAPI_Point2D(aCircle.attribute("circle_rotate"))
322 model.assertPoint(aCenter, anExpectedCenter)
323 model.assertPoint(aRotPoint, anExpectedRot)
324 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 3)
325 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 2)
326
327 #=========================================================================
328 # End of test
329 #=========================================================================
330
331 from salome.shaper import model
332 assert(model.checkPythonDump())