Salome HOME
Update Arc unit test
[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     aCenterPnt = geomDataAPI_Point2D(aLastArc.attribute("center_point"))
36     aStartPnt = geomDataAPI_Point2D(aLastArc.attribute("start_point"))
37     aEndPnt = geomDataAPI_Point2D(aLastArc.attribute("end_point"))
38     if len(theCenter):
39         verifyPointCoordinates(aCenterPnt, theCenter[0], theCenter[1])
40     if len(theStart):
41         verifyPointCoordinates(aStartPnt, theStart[0], theStart[1])
42     if len(theEnd):
43         verifyPointCoordinates(aEndPnt, theEnd[0], theEnd[1])
44     model.assertSketchArc(aLastArc)
45
46 def verifyPointCoordinates(thePoint, theX, theY):
47     assert thePoint.x() == theX and thePoint.y() == theY, "Wrong '{0}' point ({1}, {2}), expected ({3}, {4})".format(thePoint.id(), thePoint.x(), thePoint.y(), theX, theY)
48
49
50 aSession = ModelAPI_Session.get()
51 aDocument = aSession.moduleDocument()
52 #=========================================================================
53 # Creation of a sketch
54 #=========================================================================
55 aSession.startOperation()
56 aSketchCommonFeature = aDocument.addFeature("Sketch")
57 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
58 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
59 origin.setValue(0, 0, 0)
60 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
61 dirx.setValue(1, 0, 0)
62 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
63 norm.setValue(0, 0, 1)
64 aSession.finishOperation()
65 aSketch = SketchAPI_Sketch(aSketchFeature)
66
67 #=========================================================================
68 # Test 1. Create an arc by center, start and end points
69 #=========================================================================
70 aCenter = [10., 10.]
71 aStart  = [0., 50.]
72 aEnd    = [50., 0.]
73 aSession.startOperation()
74 aSketchArc = aSketchFeature.addFeature("SketchArc")
75 assert (aSketchArc.getKind() == "SketchArc")
76 anArcCenter = geomDataAPI_Point2D(aSketchArc.attribute("center_point"))
77 assert (not anArcCenter.isInitialized())
78 anArcCenter.setValue(aCenter[0], aCenter[1])
79 anArcStartPoint = geomDataAPI_Point2D(aSketchArc.attribute("start_point"))
80 assert (not anArcStartPoint.isInitialized())
81 anArcStartPoint.setValue(aStart[0], aStart[1])
82 anArcEndPoint = geomDataAPI_Point2D(aSketchArc.attribute("end_point"))
83 assert (not anArcEndPoint.isInitialized())
84 anArcEndPoint.setValue(aEnd[0], aEnd[1])
85 aSession.finishOperation()
86 verifyLastArc(aSketchFeature, aCenter, aStart, aEnd)
87 # Move center
88 aCenter = [15., 20.]
89 aSession.startOperation()
90 anArcCenter.setValue(aCenter[0], aCenter[1])
91 aSession.finishOperation()
92 verifyLastArc(aSketchFeature, [], [], [])
93 # Move start point
94 deltaX, deltaY = 5., 2.
95 aStart = [anArcStartPoint.x() + deltaX, anArcStartPoint.y() + deltaY]
96 aSession.startOperation()
97 anArcStartPoint.setValue(aStart[0], aStart[1])
98 aSession.finishOperation()
99 verifyLastArc(aSketchFeature, [], [], [])
100 # Move end point
101 aEnd = [anArcEndPoint.x() - deltaX, anArcEndPoint.y() - deltaY]
102 aSession.startOperation()
103 anArcEndPoint.setValue(aEnd[0], aEnd[1])
104 aSession.finishOperation()
105 verifyLastArc(aSketchFeature, [], [], [])
106 # Check that changing the radius does not affect arc
107 aSession.startOperation()
108 anArcRadius = aSketchArc.real("radius")
109 aPrevRadius = anArcRadius.value();
110 anArcRadius.setValue(aPrevRadius + 10.)
111 aSession.finishOperation()
112 assert (math.fabs(anArcRadius.value() - aPrevRadius) < TOLERANCE)
113 verifyLastArc(aSketchFeature, [], [], [])
114 # Check that changing the angle does not affect arc
115 aSession.startOperation()
116 anArcAngle = aSketchArc.real("angle")
117 aPrevAngle = anArcAngle.value()
118 anArcAngle.setValue(aPrevAngle + 10.)
119 aSession.finishOperation()
120 assert (math.fabs(anArcAngle.value() - aPrevAngle) < TOLERANCE)
121 verifyLastArc(aSketchFeature, [], [], [])
122
123 #=========================================================================
124 # Test 2. Create an arc as a macro-feature by center, start and end points
125 #=========================================================================
126 aSession.startOperation()
127 anArc = aSketchFeature.addFeature("SketchMacroArc")
128 assert (anArc.getKind() == "SketchMacroArc")
129 anArcCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
130 assert (not anArcCenter.isInitialized())
131 anArcStart = geomDataAPI_Point2D(anArc.attribute("start_point_1"))
132 assert (not anArcStart.isInitialized())
133 anArcEnd = geomDataAPI_Point2D(anArc.attribute("end_point_1"))
134 assert (not anArcEnd.isInitialized())
135 anArcType = anArc.string("arc_type")
136 assert (not anArcType.isInitialized())
137 anArcType.setValue("by_center_and_points")
138 anArcCenter.setValue(aCenter[0], aCenter[1])
139 anArcStart.setValue(aStart[0], aStart[1])
140 anArcEnd.setValue(aEnd[0], aEnd[1])
141 aSession.finishOperation()
142 assert (aSketchFeature.numberOfSubs() == 2)
143 verifyLastArc(aSketchFeature, [], [], [])
144
145 #=========================================================================
146 # Test 3. Create an arc by center and two points coincident to other points
147 #=========================================================================
148 # get previous arc
149 aPrevArc = model.lastSubFeature(aSketchFeature, "SketchArc")
150 aPrevArcStart = geomDataAPI_Point2D(aPrevArc.attribute("start_point"))
151 aPrevArcEnd = geomDataAPI_Point2D(aPrevArc.attribute("end_point"))
152 # create additional point
153 aPointCoordinates = [0., 0.]
154 aSession.startOperation()
155 aPoint = aSketchFeature.addFeature("SketchPoint")
156 aPointCoord = geomDataAPI_Point2D(aPoint.attribute("PointCoordinates"))
157 aPointCoord.setValue(aPointCoordinates[0], aPointCoordinates[1])
158 aPoint.selection("External").selectSubShape("VERTEX", "Origin")
159 aSession.finishOperation()
160 # create additional line
161 aLineStart = [20., -5.]
162 aLineEnd = [20., 20]
163 aSession.startOperation()
164 aLine = aSketchFeature.addFeature("SketchLine")
165 aLineStartPoint = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
166 aLineEndPoint = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
167 aLineStartPoint.setValue(aLineStart[0], aLineStart[1])
168 aLineEndPoint.setValue(aLineEnd[0], aLineEnd[1])
169 aSession.finishOperation()
170 # create new arc
171 aSession.startOperation()
172 anArc = aSketchFeature.addFeature("SketchMacroArc")
173 aCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
174 aCenterRef = anArc.refattr("center_point_ref")
175 assert (not aCenterRef.isInitialized())
176 aStart = geomDataAPI_Point2D(anArc.attribute("start_point_1"))
177 aStartRef = anArc.refattr("start_point_ref")
178 assert (not aStartRef.isInitialized())
179 aEnd = geomDataAPI_Point2D(anArc.attribute("end_point_1"))
180 aEndRef = anArc.refattr("end_point_ref")
181 assert (not aEndRef.isInitialized())
182 anArcType = anArc.string("arc_type")
183 assert (not anArcType.isInitialized())
184 # initialize attributes
185 anArcType.setValue("by_center_and_points")
186 aCenterRef.setAttr(aPrevArcStart)
187 aCenter.setValue(aPrevArcStart.pnt())
188 aStartRef.setObject(aPoint.lastResult())
189 aStart.setValue(aPointCoord.pnt())
190 aEndRef.setAttr(aPrevArcEnd)
191 aEnd.setValue(aPrevArcEnd.pnt())
192 aSession.finishOperation()
193 # check the MacroArc is not valid (selection of end point is not possible)
194 aLastFeature = aSketchFeature.subFeature(aSketchFeature.numberOfSubs() - 1)
195 assert aLastFeature.getKind() == "SketchMacroArc", "ERROR: SketchMacroArc has NOT expected to be valid"
196 # change end point reference to a line
197 aSession.startOperation()
198 aEndRef.setObject(aLine.lastResult())
199 aEnd.setValue(aLineStart[0], aLineStart[1])
200 aSession.finishOperation()
201 assert (aSketchFeature.numberOfSubs() == 8), "Number of subs {}".format(aSketchFeature.numberOfSubs())
202 verifyPointCoordinates(aPointCoord, aPointCoordinates[0], aPointCoordinates[1])
203 verifyLastArc(aSketchFeature, [aPrevArcStart.x(), aPrevArcStart.y()], aPointCoordinates, [])
204 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 3)
205
206 #=========================================================================
207 # Test 4. Create an arc by center and points coincident to other features
208 #=========================================================================
209 # get previous arc
210 aPrevArc = model.lastSubFeature(aSketchFeature, "SketchArc")
211 aPrevArcCenter = geomDataAPI_Point2D(aPrevArc.attribute("center_point"))
212 aPrevArcStart = geomDataAPI_Point2D(aPrevArc.attribute("start_point"))
213 aPrevArcEnd = geomDataAPI_Point2D(aPrevArc.attribute("end_point"))
214 aPrevArcCenterXY = [aPrevArcCenter.x(), aPrevArcCenter.y()]
215 aPrevArcStartXY = [aPrevArcStart.x(), aPrevArcStart.y()]
216 aPrevArcEndXY = [aPrevArcEnd.x(), aPrevArcEnd.y()]
217 # create new arc
218 aSession.startOperation()
219 anArc = aSketchFeature.addFeature("SketchMacroArc")
220 aCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
221 aCenterRef = anArc.refattr("center_point_ref")
222 aStart = geomDataAPI_Point2D(anArc.attribute("start_point_1"))
223 aStartRef = anArc.refattr("start_point_ref")
224 aEnd = geomDataAPI_Point2D(anArc.attribute("end_point_1"))
225 aEndRef = anArc.refattr("end_point_ref")
226 anArcType = anArc.string("arc_type")
227 # initialize attributes
228 anArcType.setValue("by_center_and_points")
229 aCenterRef.setObject(aLine.lastResult())
230 aCenter.setValue(aLineStartPoint.pnt())
231 aStartRef.setObject(aPrevArc.lastResult())
232 aStart.setValue(aPrevArcStart.pnt())
233 aEndRef.setObject(aLine.lastResult())
234 aEnd.setValue(aLineEndPoint.pnt())
235 aSession.finishOperation()
236 assert (aSketchFeature.numberOfSubs() == 12), "Number of subs {}".format(aSketchFeature.numberOfSubs())
237 # check connected features do not change their positions
238 verifyPointCoordinates(aPrevArcCenter, aPrevArcCenterXY[0], aPrevArcCenterXY[1])
239 verifyPointCoordinates(aPrevArcStart, aPrevArcStartXY[0], aPrevArcStartXY[1])
240 verifyPointCoordinates(aPrevArcEnd, aPrevArcEndXY[0], aPrevArcEndXY[1])
241 verifyPointCoordinates(aLineStartPoint, aLineStart[0], aLineStart[1])
242 verifyPointCoordinates(aLineEndPoint, aLineEnd[0], aLineEnd[1])
243 # verify newly created arc
244 anArc = model.lastSubFeature(aSketchFeature, "SketchArc")
245 aCenter = geomDataAPI_Point2D(anArc.attribute("center_point"))
246 aStart = geomDataAPI_Point2D(anArc.attribute("start_point"))
247 aEnd = geomDataAPI_Point2D(anArc.attribute("end_point"))
248 verifyPointOnLine(aCenter, aLine)
249 verifyPointOnLine(aEnd, aLine)
250 verifyPointOnCircle(aStart, aPrevArc)
251 model.testNbSubFeatures(aSketch, "SketchConstraintCoincidence", 6)
252 model.testNbSubFeatures(aSketch, "SketchConstraintTangent", 0)
253
254 #=========================================================================
255 # End of test
256 #=========================================================================
257
258 assert(model.checkPythonDump())