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