Salome HOME
Adjust test cases according to redesigned Arc and Circle features
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateCircleByCenterAndPassed.py
1 """
2     TestCreateCircleByCenterAndPassed.py
3
4     Test creation methods of a circle built by a center and a passed point
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 verifyLastCircle(theSketch, theX, theY, theR):
26     """
27     subroutine to verify position of last circle in the sketch
28     """
29     aLastCircle = model.lastSubFeature(theSketch, "SketchCircle")
30     aCenter = geomDataAPI_Point2D(aLastCircle.attribute("circle_center"))
31     verifyPointCoordinates(aCenter, theX, theY)
32     aRadius = aLastCircle.real("circle_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.id(), thePoint.x(), thePoint.y(), theX, theY)
37
38 def verifyPointOnLine(thePoint, theLine):
39     aDistance = distancePointLine(thePoint, theLine)
40     assert aDistance < TOLERANCE, "Point is not on Line, distance: {0}".format(aDistance)
41
42 def verifyTangentCircles(theCircle1, theCircle2):
43     aCenter1 = geomDataAPI_Point2D(theCircle1.attribute("circle_center"))
44     aCenter2 = geomDataAPI_Point2D(theCircle2.attribute("circle_center"))
45     aDistCC = model.distancePointPoint(aCenter1, aCenter2)
46     aRadius1 = theCircle1.real("circle_radius").value()
47     aRadius2 = theCircle2.real("circle_radius").value()
48     aRSum = aRadius1 + aRadius2
49     aRDiff = math.fabs(aRadius1 - aRadius2)
50     assert math.fabs(aRSum - aDistCC) < TOLERANCE or math.fabs(aRDiff - aDistCC) < TOLERANCE, "Circles do not tangent"
51
52 def distancePointLine(thePoint, theLine):
53     aLineStart = geomDataAPI_Point2D(theLine.attribute("StartPoint")).pnt().xy()
54     aLineEnd = geomDataAPI_Point2D(theLine.attribute("EndPoint")).pnt().xy()
55     aLineDir = aLineEnd.decreased(aLineStart)
56     aLineLen = aLineEnd.distance(aLineStart)
57     aPntDir = thePoint.pnt().xy().decreased(aLineStart)
58     return math.fabs(aPntDir.cross(aLineDir) / aLineLen)
59
60
61 #=========================================================================
62 # Start of test
63 #=========================================================================
64
65 aSession = ModelAPI_Session.get()
66 aDocument = aSession.moduleDocument()
67 #=========================================================================
68 # Creation of a sketch
69 #=========================================================================
70 aSession.startOperation()
71 aSketchCommonFeature = aDocument.addFeature("Sketch")
72 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
73 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
74 origin.setValue(0, 0, 0)
75 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
76 dirx.setValue(1, 0, 0)
77 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
78 norm.setValue(0, 0, 1)
79 aSession.finishOperation()
80 aSketch = SketchAPI_Sketch(aSketchFeature)
81
82 #=========================================================================
83 # Test 1. Create a circle by center and radius
84 #=========================================================================
85 aSession.startOperation()
86 aCircle = aSketchFeature.addFeature("SketchCircle")
87 assert (aCircle.getKind() == "SketchCircle")
88 aCircleCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
89 assert (not aCircleCenter.isInitialized())
90 aCircleRadius = aCircle.real("circle_radius")
91 assert (type(aCircleRadius) == ModelAPI_AttributeDouble)
92 # ModelAPI_AttributeDouble.typeId() is checked in ModelAPI_TestConstants
93 assert (aCircleRadius.attributeType() == ModelAPI_AttributeDouble.typeId())
94 aCircleCenter.setValue(-25., -25)
95 aCircleRadius.setValue(25.)
96 aSession.finishOperation()
97 verifyLastCircle(aSketchFeature, -25., -25., 25.)
98 #=========================================================================
99 # Edit the Circle
100 # 1. check that changing the center of a circle does not affect radius and vise versa
101 # 2. also check that int is acceptable as well as a real
102 #=========================================================================
103 aSession.startOperation()
104 aCircleCenter.setValue(10, 60)
105 aSession.finishOperation()
106 verifyLastCircle(aSketchFeature, 10., 60., 25.)
107 aSession.startOperation()
108 aCircleRadius.setValue(int(20))
109 aSession.finishOperation()
110 verifyLastCircle(aSketchFeature, 10., 60., 20.)
111
112 #=========================================================================
113 # Test 2. Create a circle as a macro-feature by center and passed point
114 #=========================================================================
115 aSession.startOperation()
116 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
117 assert (aCircle.getKind() == "SketchMacroCircle")
118 aCircleCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
119 assert (not aCircleCenter.isInitialized())
120 aCirclePassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
121 assert (not aCirclePassed.isInitialized())
122 aCircleType = aCircle.string("circle_type")
123 assert (not aCircleType.isInitialized())
124 aCircleType.setValue("circle_type_by_center_and_passed_points")
125 aCircleCenter.setValue(-25., -25)
126 aCirclePassed.setValue(0., -25.)
127 aRadius = model.distancePointPoint(aCircleCenter, aCirclePassed)
128 aSession.finishOperation()
129 assert (aSketchFeature.numberOfSubs() == 2)
130 verifyLastCircle(aSketchFeature, -25., -25., aRadius)
131
132 #=========================================================================
133 # Test 3. Create a circle as a macro-feature by center and passed point coincident to other points
134 #=========================================================================
135 # get previous circle
136 aPrevCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
137 aPrevCenter = geomDataAPI_Point2D(aPrevCircle.attribute("circle_center"))
138 # create additional point
139 aPointCoodinates = [0., 0.]
140 aSession.startOperation()
141 aPoint = aSketchFeature.addFeature("SketchPoint")
142 aPointCoord = geomDataAPI_Point2D(aPoint.attribute("PointCoordindates"))
143 aPointCoord.setValue(aPointCoodinates[0], aPointCoodinates[1])
144 aSession.finishOperation()
145 # create new circle
146 aSession.startOperation()
147 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
148 aCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
149 aCenterRef = aCircle.refattr("center_point_ref")
150 assert (not aCenterRef.isInitialized())
151 aPassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
152 aPassedRef = aCircle.refattr("passed_point_ref")
153 assert (not aPassedRef.isInitialized())
154 aCircleType = aCircle.string("circle_type")
155 assert (not aCircleType.isInitialized())
156 # initialize attributes
157 aCircleType.setValue("circle_type_by_center_and_passed_points")
158 aCenterRef.setObject(aPoint.lastResult())
159 aCenter.setValue(aPointCoord.pnt())
160 aPassedRef.setAttr(aPrevCenter)
161 aPassed.setValue(aPrevCenter.pnt())
162 aRadius = model.distancePointPoint(aPrevCenter, aPointCoord)
163 aSession.finishOperation()
164 assert (aSketchFeature.numberOfSubs() == 6)
165 verifyPointCoordinates(aPointCoord, aPointCoodinates[0], aPointCoodinates[1])
166 verifyLastCircle(aSketchFeature, aPointCoord.x(), aPointCoord.y(), aRadius)
167 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 2)
168
169 #=========================================================================
170 # Test 4. Create a circle as a macro-feature by center on a line and passed point on another circle
171 #=========================================================================
172 # get previous circle
173 aPrevCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
174 aPrevCenter = geomDataAPI_Point2D(aPrevCircle.attribute("circle_center"))
175 aPrevCenterXY = [aPrevCenter.x(), aPrevCenter.y()]
176 aPrevRadius = aPrevCircle.real("circle_radius").value()
177 # create additional line
178 aLineStart = [10., 0.]
179 aLineEnd = [10., 50.]
180 aSession.startOperation()
181 aLine = aSketchFeature.addFeature("SketchLine")
182 aStartPnt = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
183 aStartPnt.setValue(aLineStart[0], aLineStart[1])
184 aEndPnt = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
185 aEndPnt.setValue(aLineEnd[0], aLineEnd[1])
186 aSession.finishOperation()
187 # create new circle
188 aSession.startOperation()
189 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
190 aCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
191 aCenterRef = aCircle.refattr("center_point_ref")
192 aPassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
193 aPassedRef = aCircle.refattr("passed_point_ref")
194 aCircleType = aCircle.string("circle_type")
195 # initialize attributes
196 aCircleType.setValue("circle_type_by_center_and_passed_points")
197 aCenterRef.setObject(aLine.lastResult())
198 anExpectedCenter = [(aLineStart[0] + aLineEnd[0]) * 0.5, (aLineStart[1] + aLineEnd[1]) * 0.5]
199 aCenter.setValue(anExpectedCenter[0], anExpectedCenter[1])
200 aPassedRef.setObject(aPrevCircle.lastResult())
201 aPassed.setValue(aPrevCenter.x() + aPrevRadius, aPrevCenter.y())
202 aRadius = model.distancePointPoint(aCenter, aPassed)
203 aSession.finishOperation()
204 assert (aSketchFeature.numberOfSubs() == 10)
205 # check connected features do not change their positions
206 verifyPointCoordinates(aPrevCenter, aPrevCenterXY[0], aPrevCenterXY[1])
207 assert(aPrevCircle.real("circle_radius").value() == aPrevRadius)
208 verifyPointCoordinates(aStartPnt, aLineStart[0], aLineStart[1])
209 verifyPointCoordinates(aEndPnt, aLineEnd[0], aLineEnd[1])
210 # verify newly created circle
211 aCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
212 aCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
213 verifyPointCoordinates(aCenter, anExpectedCenter[0], anExpectedCenter[1])
214 verifyPointOnLine(aCenter, aLine)
215 verifyTangentCircles(aCircle, aPrevCircle)
216 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 3)
217 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 1)
218
219 #=========================================================================
220 # Test 5. Create a circle as a macro-feature by center and passed point placed on the same line
221 #         Check the circle is not created
222 #=========================================================================
223 aSession.startOperation()
224 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
225 aCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
226 aCenterRef = aCircle.refattr("center_point_ref")
227 aPassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
228 aPassedRef = aCircle.refattr("passed_point_ref")
229 aCircleType = aCircle.string("circle_type")
230 # initialize attributes
231 aCircleType.setValue("circle_type_by_center_and_passed_points")
232 aCenterRef.setObject(aLine.lastResult())
233 aCenter.setValue(anExpectedCenter[0], anExpectedCenter[1])
234 aPassedRef.setObject(aLine.lastResult())
235 aPassed.setValue(aLineStart[0], aLineStart[1])
236 aSession.finishOperation()
237 aLastFeature = aSketchFeature.subFeature(aSketchFeature.numberOfSubs() - 1)
238 assert aLastFeature.getKind() == "SketchMacroCircle", "ERROR: SketchMacroCircle has NOT expected to be valid"
239 aDocument.removeFeature(aCircle)
240 assert (aSketchFeature.numberOfSubs() == 10)
241
242 #=========================================================================
243 # End of test
244 #=========================================================================
245
246 from salome.shaper import model
247 assert(model.checkPythonDump())