Salome HOME
Merge branch 'master' into cgt/devCEA
[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
67 #=========================================================================
68 # Start of test
69 #=========================================================================
70
71 aSession = ModelAPI_Session.get()
72 aDocument = aSession.moduleDocument()
73 #=========================================================================
74 # Creation of a sketch
75 #=========================================================================
76 aSession.startOperation()
77 aSketchCommonFeature = aDocument.addFeature("Sketch")
78 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
79 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
80 origin.setValue(0, 0, 0)
81 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
82 dirx.setValue(1, 0, 0)
83 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
84 norm.setValue(0, 0, 1)
85 aSession.finishOperation()
86 aSketch = SketchAPI_Sketch(aSketchFeature)
87
88 #=========================================================================
89 # Test 1. Create a circle by center and radius
90 #=========================================================================
91 aSession.startOperation()
92 aCircle = aSketchFeature.addFeature("SketchCircle")
93 assert (aCircle.getKind() == "SketchCircle")
94 aCircleCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
95 assert (not aCircleCenter.isInitialized())
96 aCircleRadius = aCircle.real("circle_radius")
97 assert (type(aCircleRadius) == ModelAPI_AttributeDouble)
98 # ModelAPI_AttributeDouble.typeId() is checked in ModelAPI_TestConstants
99 assert (aCircleRadius.attributeType() == ModelAPI_AttributeDouble.typeId())
100 aCircleCenter.setValue(-25., -25)
101 aCircleRadius.setValue(25.)
102 aSession.finishOperation()
103 verifyLastCircle(aSketchFeature, -25., -25., 25.)
104 #=========================================================================
105 # Edit the Circle
106 # 1. check that changing the center of a circle does not affect radius and vise versa
107 # 2. also check that int is acceptable as well as a real
108 #=========================================================================
109 aSession.startOperation()
110 aCircleCenter.setValue(10, 60)
111 aSession.finishOperation()
112 verifyLastCircle(aSketchFeature, 10., 60., 25.)
113 aSession.startOperation()
114 aCircleRadius.setValue(int(20))
115 aSession.finishOperation()
116 verifyLastCircle(aSketchFeature, 10., 60., 20.)
117
118 #=========================================================================
119 # Test 2. Create a circle as a macro-feature by center and passed point
120 #=========================================================================
121 aSession.startOperation()
122 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
123 assert (aCircle.getKind() == "SketchMacroCircle")
124 aCircleCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
125 assert (not aCircleCenter.isInitialized())
126 aCirclePassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
127 assert (not aCirclePassed.isInitialized())
128 aCircleType = aCircle.string("circle_type")
129 assert (not aCircleType.isInitialized())
130 aCircleType.setValue("circle_type_by_center_and_passed_points")
131 aCircleCenter.setValue(-25., -25)
132 aCirclePassed.setValue(0., -25.)
133 aRadius = model.distancePointPoint(aCircleCenter, aCirclePassed)
134 aSession.finishOperation()
135 assert (aSketchFeature.numberOfSubs() == 2)
136 verifyLastCircle(aSketchFeature, -25., -25., aRadius)
137
138 #=========================================================================
139 # Test 3. Create a circle as a macro-feature by center and passed point coincident to other points
140 #=========================================================================
141 # get previous circle
142 aPrevCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
143 aPrevCenter = geomDataAPI_Point2D(aPrevCircle.attribute("circle_center"))
144 # create additional point
145 aPointCoordinates = [0., 0.]
146 aSession.startOperation()
147 aPoint = aSketchFeature.addFeature("SketchPoint")
148 aPointCoord = geomDataAPI_Point2D(aPoint.attribute("PointCoordinates"))
149 aPointCoord.setValue(aPointCoordinates[0], aPointCoordinates[1])
150 aSession.finishOperation()
151 # create new circle
152 aSession.startOperation()
153 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
154 aCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
155 aCenterRef = aCircle.refattr("center_point_ref")
156 assert (not aCenterRef.isInitialized())
157 aPassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
158 aPassedRef = aCircle.refattr("passed_point_ref")
159 assert (not aPassedRef.isInitialized())
160 aCircleType = aCircle.string("circle_type")
161 assert (not aCircleType.isInitialized())
162 # initialize attributes
163 aCircleType.setValue("circle_type_by_center_and_passed_points")
164 aCenterRef.setObject(aPoint.lastResult())
165 aCenter.setValue(aPointCoord.pnt())
166 aPassedRef.setAttr(aPrevCenter)
167 aPassed.setValue(aPrevCenter.pnt())
168 aRadius = model.distancePointPoint(aPrevCenter, aPointCoord)
169 aSession.finishOperation()
170 assert (aSketchFeature.numberOfSubs() == 6)
171 model.assertPoint(aPointCoord, aPointCoordinates)
172 verifyLastCircle(aSketchFeature, aPointCoord.x(), aPointCoord.y(), aRadius)
173 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 2)
174
175 #=========================================================================
176 # Test 4. Create a circle as a macro-feature by center on a line and passed point on another circle
177 #=========================================================================
178 # get previous circle
179 aPrevCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
180 aPrevCenter = geomDataAPI_Point2D(aPrevCircle.attribute("circle_center"))
181 aPrevCenterXY = [aPrevCenter.x(), aPrevCenter.y()]
182 aPrevRadius = aPrevCircle.real("circle_radius").value()
183 # create additional line
184 aLineStart = [10., 0.]
185 aLineEnd = [10., 50.]
186 aSession.startOperation()
187 aLine = aSketchFeature.addFeature("SketchLine")
188 aStartPnt = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
189 aStartPnt.setValue(aLineStart[0], aLineStart[1])
190 aEndPnt = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
191 aEndPnt.setValue(aLineEnd[0], aLineEnd[1])
192 aSession.finishOperation()
193 # create new circle
194 aSession.startOperation()
195 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
196 aCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
197 aCenterRef = aCircle.refattr("center_point_ref")
198 aPassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
199 aPassedRef = aCircle.refattr("passed_point_ref")
200 aCircleType = aCircle.string("circle_type")
201 # initialize attributes
202 aCircleType.setValue("circle_type_by_center_and_passed_points")
203 aCenterRef.setObject(aLine.lastResult())
204 anExpectedCenter = [(aLineStart[0] + aLineEnd[0]) * 0.5, (aLineStart[1] + aLineEnd[1]) * 0.5]
205 aCenter.setValue(anExpectedCenter[0], anExpectedCenter[1])
206 aPassedRef.setObject(aPrevCircle.lastResult())
207 aPassed.setValue(aPrevCenter.x() + aPrevRadius, aPrevCenter.y())
208 aRadius = model.distancePointPoint(aCenter, aPassed)
209 aSession.finishOperation()
210 assert (aSketchFeature.numberOfSubs() == 10)
211 # check connected features do not change their positions
212 model.assertPoint(aPrevCenter, aPrevCenterXY)
213 assert(aPrevCircle.real("circle_radius").value() == aPrevRadius)
214 model.assertLine(aLine, aLineStart, aLineEnd)
215 # verify newly created circle
216 aCircle = model.lastSubFeature(aSketchFeature, "SketchCircle")
217 aCenter = geomDataAPI_Point2D(aCircle.attribute("circle_center"))
218 model.assertPoint(aCenter, anExpectedCenter)
219 verifyPointOnLine(aCenter, aLine)
220 verifyTangentCircles(aCircle, aPrevCircle)
221 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 3)
222 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 1)
223
224 #=========================================================================
225 # Test 5. Create a circle as a macro-feature by center and passed point placed on the same line
226 #         Check the circle is not created
227 #=========================================================================
228 aSession.startOperation()
229 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
230 aCenter = geomDataAPI_Point2D(aCircle.attribute("center_point"))
231 aCenterRef = aCircle.refattr("center_point_ref")
232 aPassed = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
233 aPassedRef = aCircle.refattr("passed_point_ref")
234 aCircleType = aCircle.string("circle_type")
235 # initialize attributes
236 aCircleType.setValue("circle_type_by_center_and_passed_points")
237 aCenterRef.setObject(aLine.lastResult())
238 aCenter.setValue(anExpectedCenter[0], anExpectedCenter[1])
239 aPassedRef.setObject(aLine.lastResult())
240 aPassed.setValue(aLineStart[0], aLineStart[1])
241 aSession.finishOperation()
242 aLastFeature = aSketchFeature.subFeature(aSketchFeature.numberOfSubs() - 1)
243 assert aLastFeature.getKind() == "SketchMacroCircle", "ERROR: SketchMacroCircle has NOT expected to be valid"
244 aSession.startOperation()
245 aDocument.removeFeature(aCircle)
246 aSession.finishOperation()
247 assert (aSketchFeature.numberOfSubs() == 10)
248
249 #=========================================================================
250 # End of test
251 #=========================================================================
252
253 from salome.shaper import model
254 assert(model.checkPythonDump())