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