Salome HOME
Merge remote-tracking branch 'remotes/origin/master'
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateArcChangeType.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     TestCreateArcChangeType.py
23
24     Test attributes reset when changing creation method of an arc on-the-fly
25 """
26
27 #=========================================================================
28 # Initialization of the test
29 #=========================================================================
30 from GeomDataAPI import *
31 from GeomAPI import *
32 from ModelAPI import *
33 from SketchAPI import SketchAPI_Sketch
34 from salome.shaper import model
35 import math
36
37 __updated__ = "2017-04-06"
38
39
40 ##=========================================================================
41 ## Auxiliary functions
42 ##=========================================================================
43
44 def assertNotInitializedByCenterAndPassed(theMacroArc):
45     # check points
46     aCenterPoint = geomDataAPI_Point2D(theMacroArc.attribute("center_point"))
47     aStartPoint = geomDataAPI_Point2D(theMacroArc.attribute("start_point_1"))
48     aEndPoint = geomDataAPI_Point2D(theMacroArc.attribute("end_point_1"))
49     assert (not aCenterPoint.isInitialized())
50     assert (not aStartPoint.isInitialized())
51     assert (not aEndPoint.isInitialized())
52     # check references
53     aCenterPointRef = theMacroArc.refattr("center_point_ref")
54     aStartPointRef = theMacroArc.refattr("start_point_ref")
55     aEndPointRef = theMacroArc.refattr("end_point_ref")
56     assert (not aCenterPointRef.isInitialized())
57     assert (not aStartPointRef.isInitialized())
58     assert (not aEndPointRef.isInitialized())
59
60 def assertNotInitializedByThreePoints(theMacroArc):
61     # check points
62     aStartPoint = geomDataAPI_Point2D(theMacroArc.attribute("start_point_2"))
63     aEndPoint = geomDataAPI_Point2D(theMacroArc.attribute("end_point_2"))
64     aPassedPoint = geomDataAPI_Point2D(theMacroArc.attribute("passed_point"))
65     assert (not aStartPoint.isInitialized())
66     assert (not aEndPoint.isInitialized())
67     assert (not aPassedPoint.isInitialized())
68     # check references
69     aStartPointRef = theMacroArc.refattr("start_point_ref")
70     aEndPointRef = theMacroArc.refattr("end_point_ref")
71     aPassedPointRef = theMacroArc.refattr("passed_point_ref")
72     assert (not aStartPointRef.isInitialized())
73     assert (not aEndPointRef.isInitialized())
74     assert (not aPassedPointRef.isInitialized())
75
76 def assertNotInitializedByTangentEdge(theMacroArc):
77     # check end point
78     aEndPoint = geomDataAPI_Point2D(theMacroArc.attribute("end_point_3"))
79     assert (not aEndPoint.isInitialized())
80     # check references
81     aTangentPointRef = theMacroArc.refattr("passed_point_ref")
82     aEndPointRef = theMacroArc.refattr("end_point_ref")
83     assert (not aTangentPointRef.isInitialized())
84     assert (not aEndPointRef.isInitialized())
85
86 def verifyLastArc(theSketch, theCenter, theStart, theEnd):
87     """
88     subroutine to verify position of last arc in the sketch
89     """
90     aLastArc = model.lastSubFeature(theSketch, "SketchArc")
91     aCenterPnt = geomDataAPI_Point2D(aLastArc.attribute("center_point"))
92     aStartPnt = geomDataAPI_Point2D(aLastArc.attribute("start_point"))
93     aEndPnt = geomDataAPI_Point2D(aLastArc.attribute("end_point"))
94     if len(theCenter):
95         verifyPointCoordinates(aCenterPnt, theCenter[0], theCenter[1])
96     if len(theStart):
97         verifyPointCoordinates(aStartPnt, theStart[0], theStart[1])
98     if len(theEnd):
99         verifyPointCoordinates(aEndPnt, theEnd[0], theEnd[1])
100     model.assertSketchArc(aLastArc)
101
102 def verifyPointCoordinates(thePoint, theX, theY):
103     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)
104
105
106 #=========================================================================
107 # Start of test
108 #=========================================================================
109
110 aSession = ModelAPI_Session.get()
111 aDocument = aSession.moduleDocument()
112 #=========================================================================
113 # Creation of a sketch
114 #=========================================================================
115 aSession.startOperation()
116 aSketchCommonFeature = aDocument.addFeature("Sketch")
117 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
118 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
119 origin.setValue(0, 0, 0)
120 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
121 dirx.setValue(1, 0, 0)
122 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
123 norm.setValue(0, 0, 1)
124 aSession.finishOperation()
125 aSketch = SketchAPI_Sketch(aSketchFeature)
126
127 #=========================================================================
128 # Creation of auxiliary features
129 #=========================================================================
130 aSession.startOperation()
131 aSession.startOperation()
132 aLine = aSketchFeature.addFeature("SketchLine")
133 aLineStart = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
134 aLineStart.setValue(10., 0.)
135 aLineEnd = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
136 aLineEnd.setValue(10., 50.)
137 aSession.finishOperation()
138
139 aSession.startOperation()
140 #=========================================================================
141 # Test 1. Create an arc as a macro-feature and check all attributes are not initialized
142 #=========================================================================
143 anArc = aSketchFeature.addFeature("SketchMacroArc")
144 assert (anArc.getKind() == "SketchMacroArc")
145 aCenterPoint = geomDataAPI_Point2D(anArc.attribute("center_point"))
146 aPassedPoint = geomDataAPI_Point2D(anArc.attribute("passed_point"))
147 aStartPoint1 = geomDataAPI_Point2D(anArc.attribute("start_point_1"))
148 aStartPoint2 = geomDataAPI_Point2D(anArc.attribute("start_point_2"))
149 aEndPoint1 = geomDataAPI_Point2D(anArc.attribute("end_point_1"))
150 aEndPoint2 = geomDataAPI_Point2D(anArc.attribute("end_point_2"))
151 aEndPoint3 = geomDataAPI_Point2D(anArc.attribute("end_point_3"))
152 aTangentPoint = anArc.refattr("tangent_point")
153 aCenterPointRef = anArc.refattr("center_point_ref")
154 aStartPointRef = anArc.refattr("start_point_ref")
155 aEndPointRef = anArc.refattr("end_point_ref")
156 aPassedPointRef = anArc.refattr("passed_point_ref")
157 assertNotInitializedByCenterAndPassed(anArc)
158 assertNotInitializedByThreePoints(anArc)
159 assertNotInitializedByTangentEdge(anArc)
160 anArcType = anArc.string("arc_type")
161 assert (not anArcType.isInitialized())
162 #=========================================================================
163 # Test 2. Initialize center of arc, check the three points are not initialized
164 #=========================================================================
165 anArcType.setValue("by_center_and_points")
166 aCenterPoint.setValue(-25., -25.)
167 assertNotInitializedByThreePoints(anArc)
168 assertNotInitializedByTangentEdge(anArc)
169 #=========================================================================
170 # Test 3. Change type of circle and check the attributes related to center and passed point became uninitilized
171 #=========================================================================
172 anArcType.setValue("by_three_points")
173 assertNotInitializedByCenterAndPassed(anArc)
174 assertNotInitializedByTangentEdge(anArc)
175 #=========================================================================
176 # Test 4. Initialize two points and change type, they should became uninitialized
177 #=========================================================================
178 aStartPoint2.setValue(-10., 10.)
179 aEndPoint2.setValue(10., 10.)
180 anArcType.setValue("by_center_and_points")
181 assertNotInitializedByThreePoints(anArc)
182 assertNotInitializedByTangentEdge(anArc)
183 #=========================================================================
184 # Test 5. Initialize center and passed points then change the type
185 #=========================================================================
186 aCenterPoint.setValue(-25., -25.)
187 aStartPoint1.setValue(0., 0.)
188 aEndPoint1.setValue(-50., 0.)
189 anArcType.setValue("by_three_points")
190 assertNotInitializedByCenterAndPassed(anArc)
191 assertNotInitializedByTangentEdge(anArc)
192 #=========================================================================
193 # Test 6. Initialize all three points then change the type twice
194 #=========================================================================
195 aStartPoint2.setValue(-10., 10.)
196 aEndPoint2.setValue(10., 10.)
197 aPassedPoint.setValue(0., 0.)
198 anArcType.setValue("by_tangent_edge")
199 assertNotInitializedByThreePoints(anArc)
200 anArcType.setValue("by_center_and_points")
201 assertNotInitializedByTangentEdge(anArc)
202 anArcType.setValue("by_three_points")
203 assertNotInitializedByCenterAndPassed(anArc)
204 #=========================================================================
205 # Test 7. Initialize first and third points then change the type
206 #=========================================================================
207 aStartPointRef.setAttr(aLineStart)
208 aStartPoint2.setValue(aLineStart.pnt())
209 aPassedPointRef.setObject(aLine.lastResult())
210 aPassedPoint.setValue(aLineEnd.pnt())
211 anArcType.setValue("by_tangent_edge")
212 assertNotInitializedByCenterAndPassed(anArc)
213 assertNotInitializedByThreePoints(anArc)
214 #=========================================================================
215 # Test 8. Initialize tangent point then change type
216 #=========================================================================
217 aTangentPoint.setAttr(aLineEnd)
218 anArcType.setValue("by_three_points")
219 assertNotInitializedByCenterAndPassed(anArc)
220 assertNotInitializedByTangentEdge(anArc)
221 #=========================================================================
222 # Test 9. Initialize tangent point and end point then change type
223 #=========================================================================
224 anArcType.setValue("by_tangent_edge")
225 aTangentPoint.setAttr(aLineEnd)
226 aEndPoint3.setValue(50., 50.)
227 anArcType.setValue("by_center_and_points")
228 assertNotInitializedByThreePoints(anArc)
229 assertNotInitializedByTangentEdge(anArc)
230 #=========================================================================
231 # Test 10. Initialize center and passed points and finish operation
232 #=========================================================================
233 aCenterPointRef.setObject(aLine.lastResult())
234 aCenterPoint.setValue((aLineStart.x() + aLineEnd.x()) * 0.5, (aLineStart.y() + aLineEnd.y()) * 0.5)
235 aStartPointRef.setAttr(aLineStart)
236 aStartPoint1.setValue(aLineStart.pnt())
237 aEndPointRef.setObject(aLine.lastResult())
238 aEndPoint1.setValue(aLineEnd.pnt())
239 aSession.finishOperation()
240
241 NB_FEATURES_EXPECTED = 5 # line, arc and three coincidences
242 assert (aSketchFeature.numberOfSubs() == NB_FEATURES_EXPECTED), "Number of features in sketch {}, expected {}".format(aSketchFeature.numberOfSubs(), NB_FEATURES_EXPECTED)
243 verifyLastArc(aSketchFeature, [], [aLineStart.x(), aLineStart.y()], [])
244
245 #=========================================================================
246 # End of test
247 #=========================================================================
248
249 from salome.shaper import model
250 assert(model.checkPythonDump())