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