Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateArcByThreePoints.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     TestCreateArcByThreePoints.py
23
24     Test creation methods of arc built by three points
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 verifyLastArc(theSketch, theX, theY, theR):
46     """
47     subroutine to verify position of last arc in the sketch
48     """
49     aLastArc = model.lastSubFeature(theSketch, "SketchArc")
50     aCenter = geomDataAPI_Point2D(aLastArc.attribute("center_point"))
51     verifyPointCoordinates(aCenter, theX, theY)
52     aRadius = aLastArc.real("radius")
53     assert aRadius.value() == theR, "Wrong radius {0}, expected {1}".format(aRadius.value(), theR)
54
55 def verifyPointCoordinates(thePoint, theX, theY):
56     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)
57
58 def verifyPointOnArc(thePoint, theArc):
59     aCenter = geomDataAPI_Point2D(theArc.attribute("center_point"))
60     aDistCP = model.distancePointPoint(aCenter, thePoint)
61     aRadius = theArc.real("radius").value()
62     assert math.fabs(aDistCP - aRadius) < TOLERANCE, "Point is not on arc, distance: {0}, radius of arc: {1}".format(aDistCP, aRadius)
63
64 def verifyTangentCircleArc(theCircle, theArc):
65     aCircleCenter = geomDataAPI_Point2D(theCircle.attribute("circle_center"))
66     anArcCenter = geomDataAPI_Point2D(theArc.attribute("center_point"))
67     anArcStart = geomDataAPI_Point2D(theArc.attribute("start_point"))
68     aDistCC = model.distancePointPoint(aCircleCenter, anArcCenter)
69     aCircleRadius = theCircle.real("circle_radius").value()
70     anArcRadius = model.distancePointPoint(anArcCenter, anArcStart)
71     verifyTangentCircular(aDistCC, aCircleRadius, anArcRadius)
72
73 def verifyTangentCircular(theDistBetweenCenters, theRadius1, theRadius2):
74     aRSum = theRadius1 + theRadius2
75     aRDiff = math.fabs(theRadius1 - theRadius2)
76     assert math.fabs(aRSum - theDistBetweenCenters) < TOLERANCE or math.fabs(aRDiff - theDistBetweenCenters) < TOLERANCE, "Two circulars are not tangent"
77
78 def verifyTangentArcLine(theArc, theLine):
79     aCenter = geomDataAPI_Point2D(theArc.attribute("center_point"))
80     aRadius = theArc.real("radius").value()
81     aDistCL = distancePointLine(aCenter, theLine)
82     assert math.fabs(aDistCL - aRadius) < TOLERANCE, "Circle and line are not tangent"
83
84 def distancePointLine(thePoint, theLine):
85     aLineStart = geomDataAPI_Point2D(theLine.attribute("StartPoint")).pnt().xy()
86     aLineEnd = geomDataAPI_Point2D(theLine.attribute("EndPoint")).pnt().xy()
87     aLineDir = aLineEnd.decreased(aLineStart)
88     aLineLen = aLineEnd.distance(aLineStart)
89     aPntDir = thePoint.pnt().xy().decreased(aLineStart)
90     return math.fabs(aPntDir.cross(aLineDir) / aLineLen)
91
92
93 #=========================================================================
94 # Start of test
95 #=========================================================================
96
97 aSession = ModelAPI_Session.get()
98 aDocument = aSession.moduleDocument()
99 #=========================================================================
100 # Creation of a sketch
101 #=========================================================================
102 aSession.startOperation()
103 aSketchCommonFeature = aDocument.addFeature("Sketch")
104 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
105 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
106 origin.setValue(0, 0, 0)
107 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
108 dirx.setValue(1, 0, 0)
109 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
110 norm.setValue(0, 0, 1)
111 aSession.finishOperation()
112 aSketch = SketchAPI_Sketch(aSketchFeature)
113
114 #=========================================================================
115 # Test 1. Create an arc by three points
116 #=========================================================================
117 expectedCenter = [0., 0.]
118 expectedRadius = 10.
119 aSession.startOperation()
120 anArc = aSketchFeature.addFeature("SketchMacroArc")
121 assert (anArc.getKind() == "SketchMacroArc")
122 anArcPnt1 = geomDataAPI_Point2D(anArc.attribute("start_point_2"))
123 anArcPnt2 = geomDataAPI_Point2D(anArc.attribute("end_point_2"))
124 anArcPnt3 = geomDataAPI_Point2D(anArc.attribute("passed_point"))
125 assert (not anArcPnt1.isInitialized())
126 assert (not anArcPnt2.isInitialized())
127 assert (not anArcPnt3.isInitialized())
128 anArcType = anArc.string("arc_type")
129 assert (not anArcType.isInitialized())
130 anArcType.setValue("by_three_points")
131 anArcPnt1.setValue(expectedCenter[0] - expectedRadius, expectedCenter[1])
132 anArcPnt2.setValue(expectedCenter[0] + expectedRadius, expectedCenter[1])
133 anArcPnt3.setValue(expectedCenter[0], expectedCenter[1] + expectedRadius)
134 aSession.finishOperation()
135 assert (aSketchFeature.numberOfSubs() == 1)
136 verifyLastArc(aSketchFeature, expectedCenter[0], expectedCenter[1], expectedRadius)
137
138 #=========================================================================
139 # Test 2. Create an arc by three points coincident to other points
140 #=========================================================================
141 # get previous arc
142 aPrevArc = model.lastSubFeature(aSketchFeature, "SketchArc")
143 aPrevCenter = geomDataAPI_Point2D(aPrevArc.attribute("center_point"))
144 aPrevCenterXY = [aPrevCenter.x(), aPrevCenter.y()]
145 aPrevArcRadius = aPrevArc.real("radius").value()
146 # create additional point and line
147 aPointCoodinates = [5., 20.]
148 aLineStart = [10., 0.]
149 aLineEnd = [10., 50.]
150 aSession.startOperation()
151 aPoint = aSketchFeature.addFeature("SketchPoint")
152 aPointCoord = geomDataAPI_Point2D(aPoint.attribute("PointCoordinates"))
153 aPointCoord.setValue(aPointCoodinates[0], aPointCoodinates[1])
154 aLine = aSketchFeature.addFeature("SketchLine")
155 aStartPnt = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
156 aStartPnt.setValue(aLineStart[0], aLineStart[1])
157 aEndPnt = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
158 aEndPnt.setValue(aLineEnd[0], aLineEnd[1])
159 aSession.finishOperation()
160 # create new arc
161 aSession.startOperation()
162 anArc = aSketchFeature.addFeature("SketchMacroArc")
163 anArcPnt1 = geomDataAPI_Point2D(anArc.attribute("start_point_2"))
164 anArcPnt2 = geomDataAPI_Point2D(anArc.attribute("end_point_2"))
165 anArcPnt3 = geomDataAPI_Point2D(anArc.attribute("passed_point"))
166 anArcPnt1Ref = anArc.refattr("start_point_ref")
167 anArcPnt2Ref = anArc.refattr("end_point_ref")
168 anArcPnt3Ref = anArc.refattr("passed_point_ref")
169 anArcType = anArc.string("arc_type")
170 # initialize attributes
171 anArcType.setValue("by_three_points")
172 anArcPnt1Ref.setAttr(aPrevCenter)
173 anArcPnt1.setValue(aPrevCenter.pnt())
174 anArcPnt2Ref.setObject(aPoint.lastResult())
175 anArcPnt2.setValue(aPointCoord.pnt())
176 anArcPnt3Ref.setAttr(aStartPnt)
177 anArcPnt3.setValue(aLineStart[0], aLineStart[1])
178 aSession.finishOperation()
179 assert (aSketchFeature.numberOfSubs() == 7)
180 # check the points do not change their positions
181 verifyPointCoordinates(aPrevCenter, aPrevCenterXY[0], aPrevCenterXY[1])
182 verifyPointCoordinates(aPointCoord, aPointCoodinates[0], aPointCoodinates[1])
183 verifyPointCoordinates(aStartPnt, aLineStart[0], aLineStart[1])
184 # check newly created arc passes through the points
185 anArc = model.lastSubFeature(aSketchFeature, "SketchArc")
186 verifyPointOnArc(aPrevCenter, anArc)
187 verifyPointOnArc(aPointCoord, anArc)
188 verifyPointOnArc(aStartPnt, anArc)
189 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 3)
190 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 0)
191
192 #=========================================================================
193 # Test 3. Create an arc by three points an tangent with line
194 #=========================================================================
195 # create new arc
196 aSession.startOperation()
197 anArc = aSketchFeature.addFeature("SketchMacroArc")
198 anArcPnt1 = geomDataAPI_Point2D(anArc.attribute("start_point_2"))
199 anArcPnt2 = geomDataAPI_Point2D(anArc.attribute("end_point_2"))
200 anArcPnt3 = geomDataAPI_Point2D(anArc.attribute("passed_point"))
201 anArcPnt3Ref = anArc.refattr("passed_point_ref")
202 anArcType = anArc.string("arc_type")
203 # initialize attributes
204 anArcType.setValue("by_three_points")
205 anArcPnt1.setValue(20, 10)
206 anArcPnt2.setValue(20, 40)
207 anArcPnt3Ref.setObject(aLine.lastResult())
208 anArcPnt3.setValue(20, 25)
209 aSession.finishOperation()
210 assert (aSketchFeature.numberOfSubs() == 9)
211 # check the points do not change their positions
212 verifyPointCoordinates(aPrevCenter, aPrevCenterXY[0], aPrevCenterXY[1])
213 verifyPointCoordinates(aPointCoord, aPointCoodinates[0], aPointCoodinates[1])
214 verifyPointCoordinates(aStartPnt, aLineStart[0], aLineStart[1])
215 # check sub-features
216 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 3)
217 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 1)
218
219 #=========================================================================
220 # Test 4. Create a arc by three points:
221 #         a. first two points are coincident to extremities of the line
222 #         b. check that this line is not selectable by third point
223 #=========================================================================
224 aSession.startOperation()
225 anArc = aSketchFeature.addFeature("SketchMacroArc")
226 anArcPnt1 = geomDataAPI_Point2D(anArc.attribute("start_point_2"))
227 anArcPnt2 = geomDataAPI_Point2D(anArc.attribute("end_point_2"))
228 anArcPnt3 = geomDataAPI_Point2D(anArc.attribute("passed_point"))
229 anArcPnt1Ref = anArc.refattr("start_point_ref")
230 anArcPnt2Ref = anArc.refattr("end_point_ref")
231 anArcPnt3Ref = anArc.refattr("passed_point_ref")
232 anArcType = anArc.string("arc_type")
233 # initialize attributes
234 anArcType.setValue("by_three_points")
235 anArcPnt1Ref.setAttr(aStartPnt)
236 anArcPnt1.setValue(aStartPnt.pnt())
237 anArcPnt2Ref.setAttr(aEndPnt)
238 anArcPnt2.setValue(aEndPnt.pnt())
239 anArcPnt3Ref.setObject(aLine.lastResult())
240 anArcPnt3.setValue(aLineEnd[0], aLineEnd[1])
241 aSession.finishOperation()
242 aLastFeature = aSketchFeature.subFeature(aSketchFeature.numberOfSubs() - 1)
243 assert aLastFeature.getKind() == "SketchMacroArc", "ERROR: SketchMacroArc has NOT expected to be valid"
244 aDocument.removeFeature(anArc)
245 assert (aSketchFeature.numberOfSubs() == 9)
246
247 #=========================================================================
248 # Test 5. Create a arc by three points:
249 #         a. first two points are placed on both sides from line
250 #         b. check that this line is not selectable by third point
251 #=========================================================================
252 aDistanceFromLine = 20.
253 aSession.startOperation()
254 anArc = aSketchFeature.addFeature("SketchMacroArc")
255 anArcPnt1 = geomDataAPI_Point2D(anArc.attribute("start_point_2"))
256 anArcPnt2 = geomDataAPI_Point2D(anArc.attribute("end_point_2"))
257 anArcPnt3 = geomDataAPI_Point2D(anArc.attribute("passed_point"))
258 anArcPnt1Ref = anArc.refattr("start_point_ref")
259 anArcPnt2Ref = anArc.refattr("end_point_ref")
260 anArcPnt3Ref = anArc.refattr("passed_point_ref")
261 anArcType = anArc.string("arc_type")
262 # initialize attributes
263 anArcType.setValue("by_three_points")
264 anArcPnt1.setValue(aLineStart[0] + aDistanceFromLine, aLineStart[1])
265 anArcPnt2.setValue(aLineStart[0] - aDistanceFromLine, aLineStart[1])
266 anArcPnt3Ref.setObject(aLine.lastResult())
267 anArcPnt3.setValue(aLineEnd[0], aLineEnd[1])
268 aSession.finishOperation()
269 aLastFeature = aSketchFeature.subFeature(aSketchFeature.numberOfSubs() - 1)
270 assert aLastFeature.getKind() == "SketchMacroArc", "ERROR: SketchMacroArc has NOT expected to be valid"
271 aDocument.removeFeature(anArc)
272 assert (aSketchFeature.numberOfSubs() == 9)
273
274 #=========================================================================
275 # Test 6. Create an arc by three points:
276 #         a. check that one point IS NOT selectable as first and second points simultaneously
277 #         b. check that one segment IS selectable by first and second points
278 #=========================================================================
279 aSession.startOperation()
280 anArc = aSketchFeature.addFeature("SketchMacroArc")
281 anArcPnt1 = geomDataAPI_Point2D(anArc.attribute("start_point_2"))
282 anArcPnt2 = geomDataAPI_Point2D(anArc.attribute("end_point_2"))
283 anArcPnt3 = geomDataAPI_Point2D(anArc.attribute("passed_point"))
284 anArcPnt1Ref = anArc.refattr("start_point_ref")
285 anArcPnt2Ref = anArc.refattr("end_point_ref")
286 anArcPnt3Ref = anArc.refattr("passed_point_ref")
287 anArcType = anArc.string("arc_type")
288 # initialize attributes
289 anArcType.setValue("by_three_points")
290 anArcPnt1Ref.setAttr(aStartPnt)
291 anArcPnt1.setValue(aStartPnt.pnt())
292 anArcPnt2Ref.setAttr(aStartPnt)
293 anArcPnt2.setValue(aStartPnt.pnt())
294 anArcPnt3.setValue(0., 0.)
295 aSession.finishOperation()
296 # check the macro arc is not valid
297 aLastFeature = aSketchFeature.subFeature(aSketchFeature.numberOfSubs() - 1)
298 assert aLastFeature.getKind() == "SketchMacroArc", "ERROR: SketchMacroArc has NOT expected to be valid"
299 # reselect first points
300 aSession.startOperation()
301 anArcPnt1Ref.setObject(aLine.lastResult())
302 anArcPnt1.setValue(aStartPnt.pnt())
303 anArcPnt2Ref.setObject(aLine.lastResult())
304 anArcPnt2.setValue(aEndPnt.pnt())
305 aSession.finishOperation()
306 # check the macro arc is valid
307 aLastFeature = aSketchFeature.subFeature(aSketchFeature.numberOfSubs() - 1)
308 assert aLastFeature.getKind() != "SketchMacroArc", "ERROR: SketchMacroArc is expected to be valid"
309 assert (aSketchFeature.numberOfSubs() == 12)
310 # check sub-features
311 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 5)
312 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 1)
313
314 model.do()
315 model.end()
316
317 #=========================================================================
318 # End of test
319 #=========================================================================
320
321 from salome.shaper import model
322 assert(model.checkPythonDump())