]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/Test/TestCreateCircleByCenterAndPassed.py
Salome HOME
Merge remote-tracking branch 'origin/Toolbars_Management'
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateCircleByCenterAndPassed.py
1 ## Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 ##
3 ## This library is free software; you can redistribute it and/or
4 ## modify it under the terms of the GNU Lesser General Public
5 ## License as published by the Free Software Foundation; either
6 ## version 2.1 of the License, or (at your option) any later version.
7 ##
8 ## This library is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 ## Lesser General Public License for more details.
12 ##
13 ## You should have received a copy of the GNU Lesser General Public
14 ## License along with this library; if not, write to the Free Software
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 ##
17 ## See http:##www.salome-platform.org/ or
18 ## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 ##
20
21 """
22     TestCreateCircleByCenterAndPassed.py
23
24     Test creation methods of a circle built by a center and a passed point
25 """
26
27 #=========================================================================
28 # Initialization of the test
29 #=========================================================================
30 from GeomDataAPI import *
31 from GeomAPI import *
32 from ModelAPI import *
33 from SketchAPI import SketchAPI_Sketch
34 from salome.shaper import model
35 import math
36
37 __updated__ = "2017-03-22"
38
39
40 #=========================================================================
41 # Auxiliary functions
42 #=========================================================================
43 TOLERANCE = 1.e-7
44
45 def verifyLastCircle(theSketch, theX, theY, theR):
46     """
47     subroutine to verify position of last circle in the sketch
48     """
49     aLastCircle = model.lastSubFeature(theSketch, "SketchCircle")
50     model.assertCircle(aLastCircle, [theX, theY], theR)
51
52 def verifyPointOnLine(thePoint, theLine):
53     aDistance = model.distancePointLine(thePoint, theLine)
54     assert aDistance < TOLERANCE, "Point is not on Line, distance: {0}".format(aDistance)
55
56 def verifyTangentCircles(theCircle1, theCircle2):
57     aCenter1 = geomDataAPI_Point2D(theCircle1.attribute("circle_center"))
58     aCenter2 = geomDataAPI_Point2D(theCircle2.attribute("circle_center"))
59     aDistCC = model.distancePointPoint(aCenter1, aCenter2)
60     aRadius1 = theCircle1.real("circle_radius").value()
61     aRadius2 = theCircle2.real("circle_radius").value()
62     aRSum = aRadius1 + aRadius2
63     aRDiff = math.fabs(aRadius1 - aRadius2)
64     assert math.fabs(aRSum - aDistCC) < TOLERANCE or math.fabs(aRDiff - aDistCC) < TOLERANCE, "Circles do not tangent"
65
66 def verifyTangentCircleLine(theCircle, theLine):
67     aCenter = geomDataAPI_Point2D(theCircle.attribute("circle_center"))
68     aRadius = theCircle.real("circle_radius").value()
69     aDistCL = model.distancePointLine(aCenter, theLine)
70     assert math.fabs(aDistCL - aRadius) < TOLERANCE, "Circle and line are not tangent"
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 a circle by center and radius
96 #=========================================================================
97 aSession.startOperation()
98 aCircle = aSketchFeature.addFeature("SketchCircle")
99 assert (aCircle.getKind() == "SketchCircle")
100 aCircleCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
101 assert (not aCircleCenter.isInitialized())
102 aCircleRadius = aCircle.real("circle_radius")
103 assert (type(aCircleRadius) == ModelAPI_AttributeDouble)
104 # ModelAPI_AttributeDouble.typeId() is checked in ModelAPI_TestConstants
105 assert (aCircleRadius.attributeType() == ModelAPI_AttributeDouble.typeId())
106 aCircleCenter.setValue(-25., -25)
107 aCircleRadius.setValue(25.)
108 aSession.finishOperation()
109 verifyLastCircle(aSketchFeature, -25., -25., 25.)
110 #=========================================================================
111 # Edit the Circle
112 # 1. check that changing the center of a circle does not affect radius and vise versa
113 # 2. also check that int is acceptable as well as a real
114 #=========================================================================
115 aSession.startOperation()
116 aCircleCenter.setValue(10, 60)
117 aSession.finishOperation()
118 verifyLastCircle(aSketchFeature, 10., 60., 25.)
119 aSession.startOperation()
120 aCircleRadius.setValue(int(20))
121 aSession.finishOperation()
122 verifyLastCircle(aSketchFeature, 10., 60., 20.)
123
124 #=========================================================================
125 # Test 2. Create a circle as a macro-feature by center and passed point
126 #=========================================================================
127 aSession.startOperation()
128 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
129 assert (aCircle.getKind() == "SketchMacroCircle")
130 aCircleCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
131 assert (not aCircleCenter.isInitialized())
132 aCirclePassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
133 assert (not aCirclePassed.isInitialized())
134 aCircleType = aCircle.string("circle_type")
135 assert (not aCircleType.isInitialized())
136 aCircleType.setValue("circle_type_by_center_and_passed_points")
137 aCircleCenter.setValue(-25., -25)
138 aCirclePassed.setValue(0., -25.)
139 aRadius = model.distancePointPoint(aCircleCenter, aCirclePassed)
140 aSession.finishOperation()
141 assert (aSketchFeature.numberOfSubs() == 2)
142 verifyLastCircle(aSketchFeature, -25., -25., aRadius)
143
144 #=========================================================================
145 # Test 3. Create a circle as a macro-feature by center and passed point coincident to other points
146 #=========================================================================
147 # get previous circle
148 aPrevCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
149 aPrevCenter = geomDataAPI_Point2D(aPrevCircle.attribute("circle_center"))
150 # create additional point
151 aPointCoordinates = [0., 0.]
152 aSession.startOperation()
153 aPoint = aSketchFeature.addFeature("SketchPoint")
154 aPointCoord = geomDataAPI_Point2D(aPoint.attribute("PointCoordinates"))
155 aPointCoord.setValue(aPointCoordinates[0], aPointCoordinates[1])
156 aSession.finishOperation()
157 # create new circle
158 aSession.startOperation()
159 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
160 aCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
161 aCenterRef = aCircle.refattr("center_point_ref")
162 assert (not aCenterRef.isInitialized())
163 aPassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
164 aPassedRef = aCircle.refattr("passed_point_ref")
165 assert (not aPassedRef.isInitialized())
166 aCircleType = aCircle.string("circle_type")
167 assert (not aCircleType.isInitialized())
168 # initialize attributes
169 aCircleType.setValue("circle_type_by_center_and_passed_points")
170 aCenterRef.setObject(aPoint.lastResult())
171 aCenter.setValue(aPointCoord.pnt())
172 aPassedRef.setAttr(aPrevCenter)
173 aPassed.setValue(aPrevCenter.pnt())
174 aRadius = model.distancePointPoint(aPrevCenter, aPointCoord)
175 aSession.finishOperation()
176 assert (aSketchFeature.numberOfSubs() == 6)
177 model.assertPoint(aPointCoord, aPointCoordinates)
178 verifyLastCircle(aSketchFeature, aPointCoord.x(), aPointCoord.y(), aRadius)
179 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 2)
180
181 #=========================================================================
182 # Test 4. Create a circle as a macro-feature by center on a line and passed point on another circle
183 #=========================================================================
184 # get previous circle
185 aPrevCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
186 aPrevCenter = geomDataAPI_Point2D(aPrevCircle.attribute("circle_center"))
187 aPrevCenterXY = [aPrevCenter.x(), aPrevCenter.y()]
188 aPrevRadius = aPrevCircle.real("circle_radius").value()
189 # create additional line
190 aLineStart = [10., 0.]
191 aLineEnd = [10., 50.]
192 aSession.startOperation()
193 aLine = aSketchFeature.addFeature("SketchLine")
194 aStartPnt = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
195 aStartPnt.setValue(aLineStart[0], aLineStart[1])
196 aEndPnt = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
197 aEndPnt.setValue(aLineEnd[0], aLineEnd[1])
198 aSession.finishOperation()
199 # create new circle
200 aSession.startOperation()
201 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
202 aCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
203 aCenterRef = aCircle.refattr("center_point_ref")
204 aPassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
205 aPassedRef = aCircle.refattr("passed_point_ref")
206 aCircleType = aCircle.string("circle_type")
207 # initialize attributes
208 aCircleType.setValue("circle_type_by_center_and_passed_points")
209 aCenterRef.setObject(aLine.lastResult())
210 anExpectedCenter = [(aLineStart[0] + aLineEnd[0]) * 0.5, (aLineStart[1] + aLineEnd[1]) * 0.5]
211 aCenter.setValue(anExpectedCenter[0], anExpectedCenter[1])
212 aPassedRef.setObject(aPrevCircle.lastResult())
213 aPassed.setValue(aPrevCenter.x() + aPrevRadius, aPrevCenter.y())
214 aRadius = model.distancePointPoint(aCenter, aPassed)
215 aSession.finishOperation()
216 assert (aSketchFeature.numberOfSubs() == 10)
217 # check connected features do not change their positions
218 model.assertPoint(aPrevCenter, aPrevCenterXY)
219 assert(aPrevCircle.real("circle_radius").value() == aPrevRadius)
220 model.assertLine(aLine, aLineStart, aLineEnd)
221 # verify newly created circle
222 aCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
223 aCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
224 model.assertPoint(aCenter, anExpectedCenter)
225 verifyPointOnLine(aCenter, aLine)
226 verifyTangentCircles(aCircle, aPrevCircle)
227 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 3)
228 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 1)
229
230 #=========================================================================
231 # Test 5. Create a circle as a macro-feature by center and passed point on line
232 #=========================================================================
233 # create new circle
234 aSession.startOperation()
235 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
236 aCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
237 aCenterRef = aCircle.refattr("center_point_ref")
238 aPassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
239 aPassedRef = aCircle.refattr("passed_point_ref")
240 aCircleType = aCircle.string("circle_type")
241 # initialize attributes
242 aCircleType.setValue("circle_type_by_center_and_passed_points")
243 anExpectedCenter = [(aLineStart[0] + aLineEnd[0]) * 0.5 + 10., (aLineStart[1] + aLineEnd[1]) * 0.5]
244 aCenter.setValue(anExpectedCenter[0], anExpectedCenter[1])
245 aPassedRef.setObject(aLine.lastResult())
246 aPassed.setValue(aLineStart[0], aLineStart[1])
247 aSession.finishOperation()
248 assert (aSketchFeature.numberOfSubs() == 12)
249 # check connected features do not change their positions
250 model.assertLine(aLine, aLineStart, aLineEnd)
251 # verify newly created circle
252 aCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
253 aCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
254 model.assertPoint(aCenter, anExpectedCenter)
255 verifyTangentCircleLine(aCircle, aLine)
256 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 3)
257 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 2)
258
259 #=========================================================================
260 # Test 5. Create a circle as a macro-feature by center and passed point placed on the same line
261 #         Check the circle is not created
262 #=========================================================================
263 aSession.startOperation()
264 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
265 aCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
266 aCenterRef = aCircle.refattr("center_point_ref")
267 aPassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
268 aPassedRef = aCircle.refattr("passed_point_ref")
269 aCircleType = aCircle.string("circle_type")
270 # initialize attributes
271 aCircleType.setValue("circle_type_by_center_and_passed_points")
272 aCenterRef.setObject(aLine.lastResult())
273 aCenter.setValue(anExpectedCenter[0], anExpectedCenter[1])
274 aPassedRef.setObject(aLine.lastResult())
275 aPassed.setValue(aLineStart[0], aLineStart[1])
276 aSession.finishOperation()
277 aLastFeature = aSketchFeature.subFeature(aSketchFeature.numberOfSubs() - 1)
278 assert aLastFeature.getKind() == "SketchMacroCircle", "ERROR: SketchMacroCircle has NOT expected to be valid"
279 aSession.startOperation()
280 aDocument.removeFeature(aCircle)
281 aSession.finishOperation()
282 assert (aSketchFeature.numberOfSubs() == 12)
283
284 #=========================================================================
285 # End of test
286 #=========================================================================
287
288 from salome.shaper import model
289 assert(model.checkPythonDump())