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