]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/Test/TestCreateArcByCenterStartEnd.py
Salome HOME
Simplification and refactoring of unit tests for SketchPlugin
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateArcByCenterStartEnd.py
1 """
2     TestCreateArc.py
3
4     static const std::string MY_SKETCH_ARC_ID("SketchArc");
5     static const std::string MY_CENTER_ID = "center_point";
6     static const std::string MY_START_ID = "start_point";
7     static const std::string MY_END_ID = "end_point";
8     data()->addAttribute(SketchPlugin_Arc::CENTER_ID(), GeomDataAPI_Point2D::typeId());
9     data()->addAttribute(SketchPlugin_Arc::START_ID(),  GeomDataAPI_Point2D::typeId());
10     data()->addAttribute(SketchPlugin_Arc::END_ID(),    GeomDataAPI_Point2D::typeId());
11 """
12
13 #=========================================================================
14 # Initialization of the test
15 #=========================================================================
16 from GeomDataAPI import *
17 from ModelAPI import *
18 from SketchAPI import SketchAPI_Sketch
19 import math
20 from salome.shaper import model
21
22 __updated__ = "2017-03-28"
23
24 TOLERANCE = 1.e-7
25
26 #=========================================================================
27 # Auxiliary functions
28 #=========================================================================
29
30 def verifyLastArc(theSketch, theCenter, theStart, theEnd):
31     """
32     subroutine to verify position of last arc in the sketch
33     """
34     aLastArc = model.lastSubFeature(theSketch, "SketchArc")
35     model.assertArc(aLastArc, theCenter, theStart, theEnd)
36
37 def verifyPointOnLine(thePoint, theLine):
38     aDistance = model.distancePointLine(thePoint, theLine)
39     assert aDistance < TOLERANCE, "Point is not on Line, distance: {0}".format(aDistance)
40
41 def verifyPointOnCircle(thePoint, theCircular):
42     if theCircular.getKind() == "SketchArc":
43         aCenterPoint = geomDataAPI_Point2D(theCircular.attribute("center_point"))
44         aStartPoint = geomDataAPI_Point2D(theCircular.attribute("start_point"))
45         aRadius = model.distancePointPoint(aCenterPoint, aStartPoint)
46     elif theCircular.getKind() == "SketchCircle":
47         aCenterPoint = geomDataAPI_Point2D(theCircular.attribute("circle_center"))
48         aRadius = theCircular.real("circle_radius").value()
49     else:
50         return
51     assert math.fabs(model.distancePointPoint(aCenterPoint, thePoint) - aRadius) < TOLERANCE
52
53
54 aSession = ModelAPI_Session.get()
55 aDocument = aSession.moduleDocument()
56 #=========================================================================
57 # Creation of a sketch
58 #=========================================================================
59 aSession.startOperation()
60 aSketchCommonFeature = aDocument.addFeature("Sketch")
61 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
62 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
63 origin.setValue(0, 0, 0)
64 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
65 dirx.setValue(1, 0, 0)
66 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
67 norm.setValue(0, 0, 1)
68 aSession.finishOperation()
69 aSketch = SketchAPI_Sketch(aSketchFeature)
70
71 #=========================================================================
72 # Test 1. Create an arc by center, start and end points
73 #=========================================================================
74 aCenter = [10., 10.]
75 aStart  = [0., 50.]
76 aEnd    = [50., 0.]
77 aSession.startOperation()
78 aSketchArc = aSketchFeature.addFeature("SketchArc")
79 assert (aSketchArc.getKind() == "SketchArc")
80 anArcCenter = geomDataAPI_Point2D(aSketchArc.attribute("center_point"))
81 assert (not anArcCenter.isInitialized())
82 anArcCenter.setValue(aCenter[0], aCenter[1])
83 anArcStartPoint = geomDataAPI_Point2D(aSketchArc.attribute("start_point"))
84 assert (not anArcStartPoint.isInitialized())
85 anArcStartPoint.setValue(aStart[0], aStart[1])
86 anArcEndPoint = geomDataAPI_Point2D(aSketchArc.attribute("end_point"))
87 assert (not anArcEndPoint.isInitialized())
88 anArcEndPoint.setValue(aEnd[0], aEnd[1])
89 aSession.finishOperation()
90 verifyLastArc(aSketchFeature, aCenter, aStart, aEnd)
91 # Move center
92 aCenter = [15., 20.]
93 aSession.startOperation()
94 anArcCenter.setValue(aCenter[0], aCenter[1])
95 aSession.finishOperation()
96 verifyLastArc(aSketchFeature, [], [], [])
97 # Move start point
98 deltaX, deltaY = 5., 2.
99 aStart = [anArcStartPoint.x() + deltaX, anArcStartPoint.y() + deltaY]
100 aSession.startOperation()
101 anArcStartPoint.setValue(aStart[0], aStart[1])
102 aSession.finishOperation()
103 verifyLastArc(aSketchFeature, [], [], [])
104 # Move end point
105 aEnd = [anArcEndPoint.x() - deltaX, anArcEndPoint.y() - deltaY]
106 aSession.startOperation()
107 anArcEndPoint.setValue(aEnd[0], aEnd[1])
108 aSession.finishOperation()
109 verifyLastArc(aSketchFeature, [], [], [])
110 # Check that changing the radius does not affect arc
111 aSession.startOperation()
112 anArcRadius = aSketchArc.real("radius")
113 aPrevRadius = anArcRadius.value();
114 anArcRadius.setValue(aPrevRadius + 10.)
115 aSession.finishOperation()
116 assert (math.fabs(anArcRadius.value() - aPrevRadius) < TOLERANCE)
117 verifyLastArc(aSketchFeature, [], [], [])
118 # Check that changing the angle does not affect arc
119 aSession.startOperation()
120 anArcAngle = aSketchArc.real("angle")
121 aPrevAngle = anArcAngle.value()
122 anArcAngle.setValue(aPrevAngle + 10.)
123 aSession.finishOperation()
124 assert (math.fabs(anArcAngle.value() - aPrevAngle) < TOLERANCE)
125 verifyLastArc(aSketchFeature, [], [], [])
126
127 #=========================================================================
128 # Test 2. Create an arc as a macro-feature by center, start and end points
129 #=========================================================================
130 aSession.startOperation()
131 anArc = aSketchFeature.addFeature("SketchMacroArc")
132 assert (anArc.getKind() == "SketchMacroArc")
133 anArcCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
134 assert (not anArcCenter.isInitialized())
135 anArcStart = geomDataAPI_Point2D(anArc.attribute("start_point_1"))
136 assert (not anArcStart.isInitialized())
137 anArcEnd = geomDataAPI_Point2D(anArc.attribute("end_point_1"))
138 assert (not anArcEnd.isInitialized())
139 anArcType = anArc.string("arc_type")
140 assert (not anArcType.isInitialized())
141 anArcType.setValue("by_center_and_points")
142 anArcCenter.setValue(aCenter[0], aCenter[1])
143 anArcStart.setValue(aStart[0], aStart[1])
144 anArcEnd.setValue(aEnd[0], aEnd[1])
145 aSession.finishOperation()
146 assert (aSketchFeature.numberOfSubs() == 2)
147 verifyLastArc(aSketchFeature, [], [], [])
148
149 #=========================================================================
150 # Test 3. Create an arc by center and two points coincident to other points
151 #=========================================================================
152 # get previous arc
153 aPrevArc = model.lastSubFeature(aSketchFeature, "SketchArc")
154 aPrevArcStart = geomDataAPI_Point2D(aPrevArc.attribute("start_point"))
155 aPrevArcEnd = geomDataAPI_Point2D(aPrevArc.attribute("end_point"))
156 # create additional point
157 aPointCoordinates = [0., 0.]
158 aSession.startOperation()
159 aPoint = aSketchFeature.addFeature("SketchPoint")
160 aPointCoord = geomDataAPI_Point2D(aPoint.attribute("PointCoordinates"))
161 aPointCoord.setValue(aPointCoordinates[0], aPointCoordinates[1])
162 aPoint.selection("External").selectSubShape("VERTEX", "Origin")
163 aSession.finishOperation()
164 # create additional line
165 aLineStart = [20., -5.]
166 aLineEnd = [20., 20]
167 aSession.startOperation()
168 aLine = aSketchFeature.addFeature("SketchLine")
169 aLineStartPoint = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
170 aLineEndPoint = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
171 aLineStartPoint.setValue(aLineStart[0], aLineStart[1])
172 aLineEndPoint.setValue(aLineEnd[0], aLineEnd[1])
173 aSession.finishOperation()
174 # create new arc
175 aSession.startOperation()
176 anArc = aSketchFeature.addFeature("SketchMacroArc")
177 aCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
178 aCenterRef = anArc.refattr("center_point_ref")
179 assert (not aCenterRef.isInitialized())
180 aStart = geomDataAPI_Point2D(anArc.attribute("start_point_1"))
181 aStartRef = anArc.refattr("start_point_ref")
182 assert (not aStartRef.isInitialized())
183 aEnd = geomDataAPI_Point2D(anArc.attribute("end_point_1"))
184 aEndRef = anArc.refattr("end_point_ref")
185 assert (not aEndRef.isInitialized())
186 anArcType = anArc.string("arc_type")
187 assert (not anArcType.isInitialized())
188 # initialize attributes
189 anArcType.setValue("by_center_and_points")
190 aCenterRef.setAttr(aPrevArcStart)
191 aCenter.setValue(aPrevArcStart.pnt())
192 aStartRef.setObject(aPoint.lastResult())
193 aStart.setValue(aPointCoord.pnt())
194 aEndRef.setAttr(aPrevArcEnd)
195 aEnd.setValue(aPrevArcEnd.pnt())
196 aSession.finishOperation()
197 # check the MacroArc is not valid (selection of end point is not possible)
198 aLastFeature = aSketchFeature.subFeature(aSketchFeature.numberOfSubs() - 1)
199 assert aLastFeature.getKind() == "SketchMacroArc", "ERROR: SketchMacroArc has NOT expected to be valid"
200 # change end point reference to a line
201 aSession.startOperation()
202 aEndRef.setObject(aLine.lastResult())
203 aEnd.setValue(aLineStart[0], aLineStart[1])
204 aSession.finishOperation()
205 assert (aSketchFeature.numberOfSubs() == 8), "Number of subs {}".format(aSketchFeature.numberOfSubs())
206 model.assertPoint(aPointCoord, aPointCoordinates)
207 verifyLastArc(aSketchFeature, [aPrevArcStart.x(), aPrevArcStart.y()], aPointCoordinates, [])
208 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 3)
209
210 #=========================================================================
211 # Test 4. Create an arc by center and points coincident to other features
212 #=========================================================================
213 # get previous arc
214 aPrevArc = model.lastSubFeature(aSketchFeature, "SketchArc")
215 aPrevArcCenter = geomDataAPI_Point2D(aPrevArc.attribute("center_point"))
216 aPrevArcStart = geomDataAPI_Point2D(aPrevArc.attribute("start_point"))
217 aPrevArcEnd = geomDataAPI_Point2D(aPrevArc.attribute("end_point"))
218 aPrevArcCenterXY = [aPrevArcCenter.x(), aPrevArcCenter.y()]
219 aPrevArcStartXY = [aPrevArcStart.x(), aPrevArcStart.y()]
220 aPrevArcEndXY = [aPrevArcEnd.x(), aPrevArcEnd.y()]
221 # create new arc
222 aSession.startOperation()
223 anArc = aSketchFeature.addFeature("SketchMacroArc")
224 aCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
225 aCenterRef = anArc.refattr("center_point_ref")
226 aStart = geomDataAPI_Point2D(anArc.attribute("start_point_1"))
227 aStartRef = anArc.refattr("start_point_ref")
228 aEnd = geomDataAPI_Point2D(anArc.attribute("end_point_1"))
229 aEndRef = anArc.refattr("end_point_ref")
230 anArcType = anArc.string("arc_type")
231 # initialize attributes
232 anArcType.setValue("by_center_and_points")
233 aCenterRef.setObject(aLine.lastResult())
234 aCenter.setValue(aLineStartPoint.pnt())
235 aStartRef.setObject(aPrevArc.lastResult())
236 aStart.setValue(aPrevArcStart.pnt())
237 aEndRef.setObject(aLine.lastResult())
238 aEnd.setValue(aLineEndPoint.pnt())
239 aSession.finishOperation()
240 assert (aSketchFeature.numberOfSubs() == 12), "Number of subs {}".format(aSketchFeature.numberOfSubs())
241 # check connected features do not change their positions
242 model.assertArc(aPrevArc, aPrevArcCenterXY, aPrevArcStartXY, aPrevArcEndXY)
243 model.assertLine(aLine, aLineStart, aLineEnd)
244 # verify newly created arc
245 anArc = model.lastSubFeature(aSketchFeature, "SketchArc")
246 aCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
247 aStart = geomDataAPI_Point2D(anArc.attribute("start_point"))
248 aEnd = geomDataAPI_Point2D(anArc.attribute("end_point"))
249 verifyPointOnLine(aCenter, aLine)
250 verifyPointOnLine(aEnd, aLine)
251 verifyPointOnCircle(aStart, aPrevArc)
252 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 6)
253 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 0)
254
255 #=========================================================================
256 # End of test
257 #=========================================================================
258
259 assert(model.checkPythonDump())