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