Salome HOME
Rename test case for arc creating by three points
[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     verifyPointCoordinates(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 verifyPointCoordinates(thePoint, theX, theY):
36     assert thePoint.x() == theX and thePoint.y() == theY, "Wrong '{0}' point ({1}, {2}), expected ({3}, {4})".format(thePoint.attributeType(), thePoint.x(), thePoint.y(), theX, theY)
37
38 def verifyPointOnArc(thePoint, theArc):
39     aCenter = geomDataAPI_Point2D(theArc.attribute("center_point"))
40     aDistCP = model.distancePointPoint(aCenter, thePoint)
41     aRadius = theArc.real("radius").value()
42     assert math.fabs(aDistCP - aRadius) < TOLERANCE, "Point is not on arc, distance: {0}, radius of arc: {1}".format(aDistCP, aRadius)
43
44 def verifyTangentCircleArc(theCircle, theArc):
45     aCircleCenter = geomDataAPI_Point2D(theCircle.attribute("circle_center"))
46     anArcCenter = geomDataAPI_Point2D(theArc.attribute("center_point"))
47     anArcStart = geomDataAPI_Point2D(theArc.attribute("start_point"))
48     aDistCC = model.distancePointPoint(aCircleCenter, anArcCenter)
49     aCircleRadius = theCircle.real("circle_radius").value()
50     anArcRadius = model.distancePointPoint(anArcCenter, anArcStart)
51     verifyTangentCircular(aDistCC, aCircleRadius, anArcRadius)
52
53 def verifyTangentCircular(theDistBetweenCenters, theRadius1, theRadius2):
54     aRSum = theRadius1 + theRadius2
55     aRDiff = math.fabs(theRadius1 - theRadius2)
56     assert math.fabs(aRSum - theDistBetweenCenters) < TOLERANCE or math.fabs(aRDiff - theDistBetweenCenters) < TOLERANCE, "Two circulars are not tangent"
57
58 def verifyTangentArcLine(theArc, theLine):
59     aCenter = geomDataAPI_Point2D(theArc.attribute("center_point"))
60     aRadius = theArc.real("radius").value()
61     aDistCL = distancePointLine(aCenter, theLine)
62     assert math.fabs(aDistCL - aRadius) < TOLERANCE, "Circle and line are not tangent"
63
64 def distancePointLine(thePoint, theLine):
65     aLineStart = geomDataAPI_Point2D(theLine.attribute("StartPoint")).pnt().xy()
66     aLineEnd = geomDataAPI_Point2D(theLine.attribute("EndPoint")).pnt().xy()
67     aLineDir = aLineEnd.decreased(aLineStart)
68     aLineLen = aLineEnd.distance(aLineStart)
69     aPntDir = thePoint.pnt().xy().decreased(aLineStart)
70     return math.fabs(aPntDir.cross(aLineDir) / aLineLen)
71
72
73 #=========================================================================
74 # Start of test
75 #=========================================================================
76
77 aSession = ModelAPI_Session.get()
78 aDocument = aSession.moduleDocument()
79 #=========================================================================
80 # Creation of a sketch
81 #=========================================================================
82 aSession.startOperation()
83 aSketchCommonFeature = aDocument.addFeature("Sketch")
84 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
85 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
86 origin.setValue(0, 0, 0)
87 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
88 dirx.setValue(1, 0, 0)
89 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
90 norm.setValue(0, 0, 1)
91 aSession.finishOperation()
92 aSketch = SketchAPI_Sketch(aSketchFeature)
93
94 #=========================================================================
95 # Test 1. Create an arc by three points
96 #=========================================================================
97 expectedCenter = [0., 0.]
98 expectedRadius = 10.
99 aSession.startOperation()
100 anArc = aSketchFeature.addFeature("SketchMacroArc")
101 assert (anArc.getKind() == "SketchMacroArc")
102 anArcPnt1 = geomDataAPI_Point2D(anArc.attribute("start_point_2"))
103 anArcPnt2 = geomDataAPI_Point2D(anArc.attribute("end_point_2"))
104 anArcPnt3 = geomDataAPI_Point2D(anArc.attribute("passed_point"))
105 assert (not anArcPnt1.isInitialized())
106 assert (not anArcPnt2.isInitialized())
107 assert (not anArcPnt3.isInitialized())
108 anArcType = anArc.string("arc_type")
109 assert (not anArcType.isInitialized())
110 anArcType.setValue("by_three_points")
111 anArcPnt1.setValue(expectedCenter[0] - expectedRadius, expectedCenter[1])
112 anArcPnt2.setValue(expectedCenter[0] + expectedRadius, expectedCenter[1])
113 anArcPnt3.setValue(expectedCenter[0], expectedCenter[1] + expectedRadius)
114 aSession.finishOperation()
115 assert (aSketchFeature.numberOfSubs() == 1)
116 verifyLastArc(aSketchFeature, expectedCenter[0], expectedCenter[1], expectedRadius)
117
118 #=========================================================================
119 # Test 2. Create an arc by three points coincident to other points
120 #=========================================================================
121 # get previous arc
122 aPrevArc = model.lastSubFeature(aSketchFeature, "SketchArc")
123 aPrevCenter = geomDataAPI_Point2D(aPrevArc.attribute("center_point"))
124 aPrevCenterXY = [aPrevCenter.x(), aPrevCenter.y()]
125 aPrevArcRadius = aPrevArc.real("radius").value()
126 # create additional point and line
127 aPointCoodinates = [5., 20.]
128 aLineStart = [10., 0.]
129 aLineEnd = [10., 50.]
130 aSession.startOperation()
131 aPoint = aSketchFeature.addFeature("SketchPoint")
132 aPointCoord = geomDataAPI_Point2D(aPoint.attribute("PointCoordinates"))
133 aPointCoord.setValue(aPointCoodinates[0], aPointCoodinates[1])
134 aLine = aSketchFeature.addFeature("SketchLine")
135 aStartPnt = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
136 aStartPnt.setValue(aLineStart[0], aLineStart[1])
137 aEndPnt = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
138 aEndPnt.setValue(aLineEnd[0], aLineEnd[1])
139 aSession.finishOperation()
140 # create new arc
141 aSession.startOperation()
142 anArc = aSketchFeature.addFeature("SketchMacroArc")
143 anArcPnt1 = geomDataAPI_Point2D(anArc.attribute("start_point_2"))
144 anArcPnt2 = geomDataAPI_Point2D(anArc.attribute("end_point_2"))
145 anArcPnt3 = geomDataAPI_Point2D(anArc.attribute("passed_point"))
146 anArcPnt1Ref = anArc.refattr("start_point_ref")
147 anArcPnt2Ref = anArc.refattr("end_point_ref")
148 anArcPnt3Ref = anArc.refattr("passed_point_ref")
149 anArcType = anArc.string("arc_type")
150 # initialize attributes
151 anArcType.setValue("by_three_points")
152 anArcPnt1Ref.setAttr(aPrevCenter)
153 anArcPnt1.setValue(aPrevCenter.pnt())
154 anArcPnt2Ref.setObject(aPoint.lastResult())
155 anArcPnt2.setValue(aPointCoord.pnt())
156 anArcPnt3Ref.setAttr(aStartPnt)
157 anArcPnt3.setValue(aLineStart[0], aLineStart[1])
158 aSession.finishOperation()
159 assert (aSketchFeature.numberOfSubs() == 7)
160 # check the points do not change their positions
161 verifyPointCoordinates(aPrevCenter, aPrevCenterXY[0], aPrevCenterXY[1])
162 verifyPointCoordinates(aPointCoord, aPointCoodinates[0], aPointCoodinates[1])
163 verifyPointCoordinates(aStartPnt, aLineStart[0], aLineStart[1])
164 # check newly created arc passes through the points
165 anArc = model.lastSubFeature(aSketchFeature, "SketchArc")
166 verifyPointOnArc(aPrevCenter, anArc)
167 verifyPointOnArc(aPointCoord, anArc)
168 verifyPointOnArc(aStartPnt, anArc)
169 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 3)
170 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 0)
171
172 #=========================================================================
173 # Test 3. Create an arc by three points an tangent with line
174 #=========================================================================
175 # create new arc
176 aSession.startOperation()
177 anArc = aSketchFeature.addFeature("SketchMacroArc")
178 anArcPnt1 = geomDataAPI_Point2D(anArc.attribute("start_point_2"))
179 anArcPnt2 = geomDataAPI_Point2D(anArc.attribute("end_point_2"))
180 anArcPnt3 = geomDataAPI_Point2D(anArc.attribute("passed_point"))
181 anArcPnt3Ref = anArc.refattr("passed_point_ref")
182 anArcType = anArc.string("arc_type")
183 # initialize attributes
184 anArcType.setValue("by_three_points")
185 anArcPnt1.setValue(20, 10)
186 anArcPnt2.setValue(20, 40)
187 anArcPnt3Ref.setObject(aLine.lastResult())
188 anArcPnt3.setValue(20, 25)
189 aSession.finishOperation()
190 assert (aSketchFeature.numberOfSubs() == 9)
191 # check the points do not change their positions
192 verifyPointCoordinates(aPrevCenter, aPrevCenterXY[0], aPrevCenterXY[1])
193 verifyPointCoordinates(aPointCoord, aPointCoodinates[0], aPointCoodinates[1])
194 verifyPointCoordinates(aStartPnt, aLineStart[0], aLineStart[1])
195 # check sub-features
196 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 3)
197 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 1)
198
199 #=========================================================================
200 # Test 4. Create a arc by three points:
201 #         a. first two points are coincident to extremities of the line
202 #         b. check that this line is not selectable by third point
203 #=========================================================================
204 aSession.startOperation()
205 anArc = aSketchFeature.addFeature("SketchMacroArc")
206 anArcPnt1 = geomDataAPI_Point2D(anArc.attribute("start_point_2"))
207 anArcPnt2 = geomDataAPI_Point2D(anArc.attribute("end_point_2"))
208 anArcPnt3 = geomDataAPI_Point2D(anArc.attribute("passed_point"))
209 anArcPnt1Ref = anArc.refattr("start_point_ref")
210 anArcPnt2Ref = anArc.refattr("end_point_ref")
211 anArcPnt3Ref = anArc.refattr("passed_point_ref")
212 anArcType = anArc.string("arc_type")
213 # initialize attributes
214 anArcType.setValue("by_three_points")
215 anArcPnt1Ref.setAttr(aStartPnt)
216 anArcPnt1.setValue(aStartPnt.pnt())
217 anArcPnt2Ref.setAttr(aEndPnt)
218 anArcPnt2.setValue(aEndPnt.pnt())
219 anArcPnt3Ref.setObject(aLine.lastResult())
220 anArcPnt3.setValue(aLineEnd[0], aLineEnd[1])
221 aSession.finishOperation()
222 aLastFeature = aSketchFeature.subFeature(aSketchFeature.numberOfSubs() - 1)
223 assert aLastFeature.getKind() == "SketchMacroArc", "ERROR: SketchMacroArc has NOT expected to be valid"
224 aDocument.removeFeature(anArc)
225 assert (aSketchFeature.numberOfSubs() == 9)
226
227 #=========================================================================
228 # Test 5. Create a arc by three points:
229 #         a. first two points are placed on both sides from line
230 #         b. check that this line is not selectable by third point
231 #=========================================================================
232 aDistanceFromLine = 20.
233 aSession.startOperation()
234 anArc = aSketchFeature.addFeature("SketchMacroArc")
235 anArcPnt1 = geomDataAPI_Point2D(anArc.attribute("start_point_2"))
236 anArcPnt2 = geomDataAPI_Point2D(anArc.attribute("end_point_2"))
237 anArcPnt3 = geomDataAPI_Point2D(anArc.attribute("passed_point"))
238 anArcPnt1Ref = anArc.refattr("start_point_ref")
239 anArcPnt2Ref = anArc.refattr("end_point_ref")
240 anArcPnt3Ref = anArc.refattr("passed_point_ref")
241 anArcType = anArc.string("arc_type")
242 # initialize attributes
243 anArcType.setValue("by_three_points")
244 anArcPnt1.setValue(aLineStart[0] + aDistanceFromLine, aLineStart[1])
245 anArcPnt2.setValue(aLineStart[0] - aDistanceFromLine, aLineStart[1])
246 anArcPnt3Ref.setObject(aLine.lastResult())
247 anArcPnt3.setValue(aLineEnd[0], aLineEnd[1])
248 aSession.finishOperation()
249 aLastFeature = aSketchFeature.subFeature(aSketchFeature.numberOfSubs() - 1)
250 assert aLastFeature.getKind() == "SketchMacroArc", "ERROR: SketchMacroArc has NOT expected to be valid"
251 aDocument.removeFeature(anArc)
252 assert (aSketchFeature.numberOfSubs() == 9)
253
254 model.do()
255 model.end()
256
257 #=========================================================================
258 # End of test
259 #=========================================================================
260
261 from salome.shaper import model
262 assert(model.checkPythonDump())