Salome HOME
a6af9c5055f4b6258ef2999612e66668bbe6e157
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateCircleByCenterAndPassed.py
1 # Copyright (C) 2014-2023  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 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__ = "2017-03-22"
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 #=========================================================================
73 # Start of test
74 #=========================================================================
75
76 aSession = ModelAPI_Session.get()
77 aDocument = aSession.moduleDocument()
78 #=========================================================================
79 # Creation of a sketch
80 #=========================================================================
81 aSession.startOperation()
82 aSketchCommonFeature = aDocument.addFeature("Sketch")
83 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
84 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
85 origin.setValue(0, 0, 0)
86 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
87 dirx.setValue(1, 0, 0)
88 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
89 norm.setValue(0, 0, 1)
90 aSession.finishOperation()
91 aSketch = SketchAPI_Sketch(aSketchFeature)
92
93 #=========================================================================
94 # Test 1. Create a circle by center and radius
95 #=========================================================================
96 aSession.startOperation()
97 aCircle = aSketchFeature.addFeature("SketchCircle")
98 assert (aCircle.getKind() == "SketchCircle")
99 aCircleCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
100 assert (not aCircleCenter.isInitialized())
101 aCircleRadius = aCircle.real("circle_radius")
102 assert (type(aCircleRadius) == ModelAPI_AttributeDouble)
103 # ModelAPI_AttributeDouble.typeId() is checked in ModelAPI_TestConstants
104 assert (aCircleRadius.attributeType() == ModelAPI_AttributeDouble.typeId())
105 aCircleCenter.setValue(-25., -25)
106 aCircleRadius.setValue(25.)
107 aSession.finishOperation()
108 verifyLastCircle(aSketchFeature, -25., -25., 25.)
109 #=========================================================================
110 # Edit the Circle
111 # 1. check that changing the center of a circle does not affect radius and vise versa
112 # 2. also check that int is acceptable as well as a real
113 #=========================================================================
114 aSession.startOperation()
115 aCircleCenter.setValue(10, 60)
116 aSession.finishOperation()
117 verifyLastCircle(aSketchFeature, 10., 60., 25.)
118 aSession.startOperation()
119 aCircleRadius.setValue(int(20))
120 aSession.finishOperation()
121 verifyLastCircle(aSketchFeature, 10., 60., 20.)
122
123 #=========================================================================
124 # Test 2. Create a circle as a macro-feature by center and passed point
125 #=========================================================================
126 aSession.startOperation()
127 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
128 assert (aCircle.getKind() == "SketchMacroCircle")
129 aCircleCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
130 assert (not aCircleCenter.isInitialized())
131 aCirclePassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
132 assert (not aCirclePassed.isInitialized())
133 aCircleType = aCircle.string("circle_type")
134 assert (not aCircleType.isInitialized())
135 aCircleType.setValue("circle_type_by_center_and_passed_points")
136 aCircleCenter.setValue(-25., -25)
137 aCirclePassed.setValue(0., -25.)
138 aRadius = model.distancePointPoint(aCircleCenter, aCirclePassed)
139 aSession.finishOperation()
140 assert (aSketchFeature.numberOfSubs() == 2)
141 verifyLastCircle(aSketchFeature, -25., -25., aRadius)
142
143 #=========================================================================
144 # Test 3. Create a circle as a macro-feature by center and passed point coincident to other points
145 #=========================================================================
146 # get previous circle
147 aPrevCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
148 aPrevCenter = geomDataAPI_Point2D(aPrevCircle.attribute("circle_center"))
149 # create additional point
150 aPointCoordinates = [0., 0.]
151 aSession.startOperation()
152 aPoint = aSketchFeature.addFeature("SketchPoint")
153 aPointCoord = geomDataAPI_Point2D(aPoint.attribute("PointCoordinates"))
154 aPointCoord.setValue(aPointCoordinates[0], aPointCoordinates[1])
155 aSession.finishOperation()
156 # create new circle
157 aSession.startOperation()
158 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
159 aCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
160 aCenterRef = aCircle.refattr("center_point_ref")
161 assert (not aCenterRef.isInitialized())
162 aPassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
163 aPassedRef = aCircle.refattr("passed_point_ref")
164 assert (not aPassedRef.isInitialized())
165 aCircleType = aCircle.string("circle_type")
166 assert (not aCircleType.isInitialized())
167 # initialize attributes
168 aCircleType.setValue("circle_type_by_center_and_passed_points")
169 aCenterRef.setObject(aPoint.lastResult())
170 aCenter.setValue(aPointCoord.pnt())
171 aPassedRef.setAttr(aPrevCenter)
172 aPassed.setValue(aPrevCenter.pnt())
173 aRadius = model.distancePointPoint(aPrevCenter, aPointCoord)
174 aSession.finishOperation()
175 assert (aSketchFeature.numberOfSubs() == 6)
176 model.assertPoint(aPointCoord, aPointCoordinates)
177 verifyLastCircle(aSketchFeature, aPointCoord.x(), aPointCoord.y(), aRadius)
178 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 2)
179
180 #=========================================================================
181 # Test 4. Create a circle as a macro-feature by center on a line and passed point on another circle
182 #=========================================================================
183 # get previous circle
184 aPrevCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
185 aPrevCenter = geomDataAPI_Point2D(aPrevCircle.attribute("circle_center"))
186 aPrevCenterXY = [aPrevCenter.x(), aPrevCenter.y()]
187 aPrevRadius = aPrevCircle.real("circle_radius").value()
188 # create additional line
189 aLineStart = [10., 0.]
190 aLineEnd = [10., 50.]
191 aSession.startOperation()
192 aLine = aSketchFeature.addFeature("SketchLine")
193 aStartPnt = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
194 aStartPnt.setValue(aLineStart[0], aLineStart[1])
195 aEndPnt = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
196 aEndPnt.setValue(aLineEnd[0], aLineEnd[1])
197 aSession.finishOperation()
198 # create new circle
199 aSession.startOperation()
200 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
201 aCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
202 aCenterRef = aCircle.refattr("center_point_ref")
203 aPassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
204 aPassedRef = aCircle.refattr("passed_point_ref")
205 aCircleType = aCircle.string("circle_type")
206 # initialize attributes
207 aCircleType.setValue("circle_type_by_center_and_passed_points")
208 aCenterRef.setObject(aLine.lastResult())
209 anExpectedCenter = [(aLineStart[0] + aLineEnd[0]) * 0.5, (aLineStart[1] + aLineEnd[1]) * 0.5]
210 aCenter.setValue(anExpectedCenter[0], anExpectedCenter[1])
211 aPassedRef.setObject(aPrevCircle.lastResult())
212 aPassed.setValue(aPrevCenter.x() + aPrevRadius, aPrevCenter.y())
213 aRadius = model.distancePointPoint(aCenter, aPassed)
214 aSession.finishOperation()
215 assert (aSketchFeature.numberOfSubs() == 10)
216 # check connected features do not change their positions
217 model.assertPoint(aPrevCenter, aPrevCenterXY)
218 assert(aPrevCircle.real("circle_radius").value() == aPrevRadius)
219 model.assertLine(aLine, aLineStart, aLineEnd)
220 # verify newly created circle
221 aCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
222 aCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
223 model.assertPoint(aCenter, anExpectedCenter)
224 verifyPointOnLine(aCenter, aLine)
225 verifyTangentCircles(aCircle, aPrevCircle)
226 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 3)
227 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 1)
228
229 #=========================================================================
230 # Test 5. Create a circle as a macro-feature by center and passed point on line
231 #=========================================================================
232 # create new circle
233 aSession.startOperation()
234 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
235 aCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
236 aCenterRef = aCircle.refattr("center_point_ref")
237 aPassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
238 aPassedRef = aCircle.refattr("passed_point_ref")
239 aCircleType = aCircle.string("circle_type")
240 # initialize attributes
241 aCircleType.setValue("circle_type_by_center_and_passed_points")
242 anExpectedCenter = [(aLineStart[0] + aLineEnd[0]) * 0.5 + 10., (aLineStart[1] + aLineEnd[1]) * 0.5]
243 aCenter.setValue(anExpectedCenter[0], anExpectedCenter[1])
244 aPassedRef.setObject(aLine.lastResult())
245 aPassed.setValue(aLineStart[0], aLineStart[1])
246 aSession.finishOperation()
247 assert (aSketchFeature.numberOfSubs() == 12)
248 # check connected features do not change their positions
249 model.assertLine(aLine, aLineStart, aLineEnd)
250 # verify newly created circle
251 aCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
252 aCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
253 model.assertPoint(aCenter, anExpectedCenter)
254 verifyTangentCircleLine(aCircle, aLine)
255 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 3)
256 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 2)
257
258 #=========================================================================
259 # Test 5. Create a circle as a macro-feature by center and passed point placed on the same line
260 #         Check the circle is not created
261 #=========================================================================
262 aSession.startOperation()
263 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
264 aCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
265 aCenterRef = aCircle.refattr("center_point_ref")
266 aPassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
267 aPassedRef = aCircle.refattr("passed_point_ref")
268 aCircleType = aCircle.string("circle_type")
269 # initialize attributes
270 aCircleType.setValue("circle_type_by_center_and_passed_points")
271 aCenterRef.setObject(aLine.lastResult())
272 aCenter.setValue(anExpectedCenter[0], anExpectedCenter[1])
273 aPassedRef.setObject(aLine.lastResult())
274 aPassed.setValue(aLineStart[0], aLineStart[1])
275 aSession.finishOperation()
276 aLastFeature = aSketchFeature.subFeature(aSketchFeature.numberOfSubs() - 1)
277 assert aLastFeature.getKind() == "SketchMacroCircle", "ERROR: SketchMacroCircle has NOT expected to be valid"
278 aSession.startOperation()
279 aDocument.removeFeature(aCircle)
280 aSession.finishOperation()
281 assert (aSketchFeature.numberOfSubs() == 12)
282
283 #=========================================================================
284 # End of test
285 #=========================================================================
286
287 from salome.shaper import model
288 assert(model.checkPythonDump())