]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/Test/TestCreateArcByThreePoints.py
Salome HOME
Simplification and refactoring of unit tests for SketchPlugin
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateArcByThreePoints.py
1 """
2     TestCreateArcByThreePoints.py
3
4     Test creation methods of arc built by three points
5 """
6
7 #=========================================================================
8 # Initialization of the test
9 #=========================================================================
10 from GeomDataAPI import *
11 from GeomAPI import *
12 from ModelAPI import *
13 from SketchAPI import SketchAPI_Sketch
14 from salome.shaper import model
15 import math
16
17 __updated__ = "2017-03-22"
18
19
20 #=========================================================================
21 # Auxiliary functions
22 #=========================================================================
23 TOLERANCE = 1.e-7
24
25 def verifyLastArc(theSketch, theX, theY, theR):
26     """
27     subroutine to verify position of last arc in the sketch
28     """
29     aLastArc = model.lastSubFeature(theSketch, "SketchArc")
30     aCenter = geomDataAPI_Point2D(aLastArc.attribute("center_point"))
31     model.assertPoint(aCenter, [theX, theY])
32     aRadius = aLastArc.real("radius")
33     assert aRadius.value() == theR, "Wrong radius {0}, expected {1}".format(aRadius.value(), theR)
34
35 def verifyPointOnArc(thePoint, theArc):
36     aCenter = geomDataAPI_Point2D(theArc.attribute("center_point"))
37     aDistCP = model.distancePointPoint(aCenter, thePoint)
38     aRadius = theArc.real("radius").value()
39     assert math.fabs(aDistCP - aRadius) < TOLERANCE, "Point is not on arc, distance: {0}, radius of arc: {1}".format(aDistCP, aRadius)
40
41 def verifyTangentCircleArc(theCircle, theArc):
42     aCircleCenter = geomDataAPI_Point2D(theCircle.attribute("circle_center"))
43     anArcCenter = geomDataAPI_Point2D(theArc.attribute("center_point"))
44     anArcStart = geomDataAPI_Point2D(theArc.attribute("start_point"))
45     aDistCC = model.distancePointPoint(aCircleCenter, anArcCenter)
46     aCircleRadius = theCircle.real("circle_radius").value()
47     anArcRadius = model.distancePointPoint(anArcCenter, anArcStart)
48     verifyTangentCircular(aDistCC, aCircleRadius, anArcRadius)
49
50 def verifyTangentCircular(theDistBetweenCenters, theRadius1, theRadius2):
51     aRSum = theRadius1 + theRadius2
52     aRDiff = math.fabs(theRadius1 - theRadius2)
53     assert math.fabs(aRSum - theDistBetweenCenters) < TOLERANCE or math.fabs(aRDiff - theDistBetweenCenters) < TOLERANCE, "Two circulars are not tangent"
54
55 def verifyTangentArcLine(theArc, theLine):
56     aCenter = geomDataAPI_Point2D(theArc.attribute("center_point"))
57     aRadius = theArc.real("radius").value()
58     aDistCL = model.distancePointLine(aCenter, theLine)
59     assert math.fabs(aDistCL - aRadius) < TOLERANCE, "Circle and line are not tangent"
60
61
62 #=========================================================================
63 # Start of test
64 #=========================================================================
65
66 aSession = ModelAPI_Session.get()
67 aDocument = aSession.moduleDocument()
68 #=========================================================================
69 # Creation of a sketch
70 #=========================================================================
71 aSession.startOperation()
72 aSketchCommonFeature = aDocument.addFeature("Sketch")
73 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
74 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
75 origin.setValue(0, 0, 0)
76 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
77 dirx.setValue(1, 0, 0)
78 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
79 norm.setValue(0, 0, 1)
80 aSession.finishOperation()
81 aSketch = SketchAPI_Sketch(aSketchFeature)
82
83 #=========================================================================
84 # Test 1. Create an arc by three points
85 #=========================================================================
86 expectedCenter = [0., 0.]
87 expectedRadius = 10.
88 aSession.startOperation()
89 anArc = aSketchFeature.addFeature("SketchMacroArc")
90 assert (anArc.getKind() == "SketchMacroArc")
91 anArcPnt1 = geomDataAPI_Point2D(anArc.attribute("start_point_2"))
92 anArcPnt2 = geomDataAPI_Point2D(anArc.attribute("end_point_2"))
93 anArcPnt3 = geomDataAPI_Point2D(anArc.attribute("passed_point"))
94 assert (not anArcPnt1.isInitialized())
95 assert (not anArcPnt2.isInitialized())
96 assert (not anArcPnt3.isInitialized())
97 anArcType = anArc.string("arc_type")
98 assert (not anArcType.isInitialized())
99 anArcType.setValue("by_three_points")
100 anArcPnt1.setValue(expectedCenter[0] - expectedRadius, expectedCenter[1])
101 anArcPnt2.setValue(expectedCenter[0] + expectedRadius, expectedCenter[1])
102 anArcPnt3.setValue(expectedCenter[0], expectedCenter[1] + expectedRadius)
103 aSession.finishOperation()
104 assert (aSketchFeature.numberOfSubs() == 1)
105 verifyLastArc(aSketchFeature, expectedCenter[0], expectedCenter[1], expectedRadius)
106
107 #=========================================================================
108 # Test 2. Create an arc by three points coincident to other points
109 #=========================================================================
110 # get previous arc
111 aPrevArc = model.lastSubFeature(aSketchFeature, "SketchArc")
112 aPrevCenter = geomDataAPI_Point2D(aPrevArc.attribute("center_point"))
113 aPrevCenterXY = [aPrevCenter.x(), aPrevCenter.y()]
114 aPrevArcRadius = aPrevArc.real("radius").value()
115 # create additional point and line
116 aPointCoodinates = [5., 20.]
117 aLineStart = [10., 0.]
118 aLineEnd = [10., 50.]
119 aSession.startOperation()
120 aPoint = aSketchFeature.addFeature("SketchPoint")
121 aPointCoord = geomDataAPI_Point2D(aPoint.attribute("PointCoordinates"))
122 aPointCoord.setValue(aPointCoodinates[0], aPointCoodinates[1])
123 aLine = aSketchFeature.addFeature("SketchLine")
124 aStartPnt = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
125 aStartPnt.setValue(aLineStart[0], aLineStart[1])
126 aEndPnt = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
127 aEndPnt.setValue(aLineEnd[0], aLineEnd[1])
128 aSession.finishOperation()
129 # create new arc
130 aSession.startOperation()
131 anArc = aSketchFeature.addFeature("SketchMacroArc")
132 anArcPnt1 = geomDataAPI_Point2D(anArc.attribute("start_point_2"))
133 anArcPnt2 = geomDataAPI_Point2D(anArc.attribute("end_point_2"))
134 anArcPnt3 = geomDataAPI_Point2D(anArc.attribute("passed_point"))
135 anArcPnt1Ref = anArc.refattr("start_point_ref")
136 anArcPnt2Ref = anArc.refattr("end_point_ref")
137 anArcPnt3Ref = anArc.refattr("passed_point_ref")
138 anArcType = anArc.string("arc_type")
139 # initialize attributes
140 anArcType.setValue("by_three_points")
141 anArcPnt1Ref.setAttr(aPrevCenter)
142 anArcPnt1.setValue(aPrevCenter.pnt())
143 anArcPnt2Ref.setObject(aPoint.lastResult())
144 anArcPnt2.setValue(aPointCoord.pnt())
145 anArcPnt3Ref.setAttr(aStartPnt)
146 anArcPnt3.setValue(aLineStart[0], aLineStart[1])
147 aSession.finishOperation()
148 assert (aSketchFeature.numberOfSubs() == 7)
149 # check the points do not change their positions
150 model.assertPoint(aPrevCenter, aPrevCenterXY)
151 model.assertPoint(aPointCoord, aPointCoodinates)
152 model.assertPoint(aStartPnt, aLineStart)
153 # check newly created arc passes through the points
154 anArc = model.lastSubFeature(aSketchFeature, "SketchArc")
155 verifyPointOnArc(aPrevCenter, anArc)
156 verifyPointOnArc(aPointCoord, anArc)
157 verifyPointOnArc(aStartPnt, anArc)
158 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 3)
159 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 0)
160
161 #=========================================================================
162 # Test 3. Create an arc by three points an tangent with line
163 #=========================================================================
164 # create new arc
165 aSession.startOperation()
166 anArc = aSketchFeature.addFeature("SketchMacroArc")
167 anArcPnt1 = geomDataAPI_Point2D(anArc.attribute("start_point_2"))
168 anArcPnt2 = geomDataAPI_Point2D(anArc.attribute("end_point_2"))
169 anArcPnt3 = geomDataAPI_Point2D(anArc.attribute("passed_point"))
170 anArcPnt3Ref = anArc.refattr("passed_point_ref")
171 anArcType = anArc.string("arc_type")
172 # initialize attributes
173 anArcType.setValue("by_three_points")
174 anArcPnt1.setValue(20, 10)
175 anArcPnt2.setValue(20, 40)
176 anArcPnt3Ref.setObject(aLine.lastResult())
177 anArcPnt3.setValue(20, 25)
178 aSession.finishOperation()
179 assert (aSketchFeature.numberOfSubs() == 9)
180 # check the points do not change their positions
181 model.assertPoint(aPrevCenter, aPrevCenterXY)
182 model.assertPoint(aPointCoord, aPointCoodinates)
183 model.assertPoint(aStartPnt, aLineStart)
184 # check sub-features
185 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 3)
186 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 1)
187
188 #=========================================================================
189 # Test 4. Create a arc by three points:
190 #         a. first two points are coincident to extremities of the line
191 #         b. check that this line is not selectable by third point
192 #=========================================================================
193 aSession.startOperation()
194 anArc = aSketchFeature.addFeature("SketchMacroArc")
195 anArcPnt1 = geomDataAPI_Point2D(anArc.attribute("start_point_2"))
196 anArcPnt2 = geomDataAPI_Point2D(anArc.attribute("end_point_2"))
197 anArcPnt3 = geomDataAPI_Point2D(anArc.attribute("passed_point"))
198 anArcPnt1Ref = anArc.refattr("start_point_ref")
199 anArcPnt2Ref = anArc.refattr("end_point_ref")
200 anArcPnt3Ref = anArc.refattr("passed_point_ref")
201 anArcType = anArc.string("arc_type")
202 # initialize attributes
203 anArcType.setValue("by_three_points")
204 anArcPnt1Ref.setAttr(aStartPnt)
205 anArcPnt1.setValue(aStartPnt.pnt())
206 anArcPnt2Ref.setAttr(aEndPnt)
207 anArcPnt2.setValue(aEndPnt.pnt())
208 anArcPnt3Ref.setObject(aLine.lastResult())
209 anArcPnt3.setValue(aLineEnd[0], aLineEnd[1])
210 aSession.finishOperation()
211 aLastFeature = aSketchFeature.subFeature(aSketchFeature.numberOfSubs() - 1)
212 assert aLastFeature.getKind() == "SketchMacroArc", "ERROR: SketchMacroArc has NOT expected to be valid"
213 aDocument.removeFeature(anArc)
214 assert (aSketchFeature.numberOfSubs() == 9)
215
216 #=========================================================================
217 # Test 5. Create a arc by three points:
218 #         a. first two points are placed on both sides from line
219 #         b. check that this line is not selectable by third point
220 #=========================================================================
221 aDistanceFromLine = 20.
222 aSession.startOperation()
223 anArc = aSketchFeature.addFeature("SketchMacroArc")
224 anArcPnt1 = geomDataAPI_Point2D(anArc.attribute("start_point_2"))
225 anArcPnt2 = geomDataAPI_Point2D(anArc.attribute("end_point_2"))
226 anArcPnt3 = geomDataAPI_Point2D(anArc.attribute("passed_point"))
227 anArcPnt1Ref = anArc.refattr("start_point_ref")
228 anArcPnt2Ref = anArc.refattr("end_point_ref")
229 anArcPnt3Ref = anArc.refattr("passed_point_ref")
230 anArcType = anArc.string("arc_type")
231 # initialize attributes
232 anArcType.setValue("by_three_points")
233 anArcPnt1.setValue(aLineStart[0] + aDistanceFromLine, aLineStart[1])
234 anArcPnt2.setValue(aLineStart[0] - aDistanceFromLine, aLineStart[1])
235 anArcPnt3Ref.setObject(aLine.lastResult())
236 anArcPnt3.setValue(aLineEnd[0], aLineEnd[1])
237 aSession.finishOperation()
238 aLastFeature = aSketchFeature.subFeature(aSketchFeature.numberOfSubs() - 1)
239 assert aLastFeature.getKind() == "SketchMacroArc", "ERROR: SketchMacroArc has NOT expected to be valid"
240 aDocument.removeFeature(anArc)
241 assert (aSketchFeature.numberOfSubs() == 9)
242
243 #=========================================================================
244 # Test 6. Create an arc by three points:
245 #         a. check that one point IS NOT selectable as first and second points simultaneously
246 #         b. check that one segment IS selectable by first and second points
247 #=========================================================================
248 aSession.startOperation()
249 anArc = aSketchFeature.addFeature("SketchMacroArc")
250 anArcPnt1 = geomDataAPI_Point2D(anArc.attribute("start_point_2"))
251 anArcPnt2 = geomDataAPI_Point2D(anArc.attribute("end_point_2"))
252 anArcPnt3 = geomDataAPI_Point2D(anArc.attribute("passed_point"))
253 anArcPnt1Ref = anArc.refattr("start_point_ref")
254 anArcPnt2Ref = anArc.refattr("end_point_ref")
255 anArcPnt3Ref = anArc.refattr("passed_point_ref")
256 anArcType = anArc.string("arc_type")
257 # initialize attributes
258 anArcType.setValue("by_three_points")
259 anArcPnt1Ref.setAttr(aStartPnt)
260 anArcPnt1.setValue(aStartPnt.pnt())
261 anArcPnt2Ref.setAttr(aStartPnt)
262 anArcPnt2.setValue(aStartPnt.pnt())
263 anArcPnt3.setValue(0., 0.)
264 aSession.finishOperation()
265 # check the macro arc is not valid
266 aLastFeature = aSketchFeature.subFeature(aSketchFeature.numberOfSubs() - 1)
267 assert aLastFeature.getKind() == "SketchMacroArc", "ERROR: SketchMacroArc has NOT expected to be valid"
268 # reselect first points
269 aSession.startOperation()
270 anArcPnt1Ref.setObject(aLine.lastResult())
271 anArcPnt1.setValue(aStartPnt.pnt())
272 anArcPnt2Ref.setObject(aLine.lastResult())
273 anArcPnt2.setValue(aEndPnt.pnt())
274 aSession.finishOperation()
275 # check the macro arc is valid
276 aLastFeature = aSketchFeature.subFeature(aSketchFeature.numberOfSubs() - 1)
277 assert aLastFeature.getKind() != "SketchMacroArc", "ERROR: SketchMacroArc is expected to be valid"
278 assert (aSketchFeature.numberOfSubs() == 12)
279 # check sub-features
280 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 5)
281 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 1)
282
283 model.do()
284 model.end()
285
286 #=========================================================================
287 # End of test
288 #=========================================================================
289
290 from salome.shaper import model
291 assert(model.checkPythonDump())