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