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