Salome HOME
Fix for the issue #2753 : error when dump/load script
[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     model.assertArc(aLastArc, theCenter, theStart, theEnd)
92
93
94 #=========================================================================
95 # Start of test
96 #=========================================================================
97
98 aSession = ModelAPI_Session.get()
99 aDocument = aSession.moduleDocument()
100 #=========================================================================
101 # Creation of a sketch
102 #=========================================================================
103 aSession.startOperation()
104 aSketchCommonFeature = aDocument.addFeature("Sketch")
105 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
106 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
107 origin.setValue(0, 0, 0)
108 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
109 dirx.setValue(1, 0, 0)
110 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
111 norm.setValue(0, 0, 1)
112 aSession.finishOperation()
113 aSketch = SketchAPI_Sketch(aSketchFeature)
114
115 #=========================================================================
116 # Creation of auxiliary features
117 #=========================================================================
118 aSession.startOperation()
119 aSession.startOperation()
120 aLine = aSketchFeature.addFeature("SketchLine")
121 aLineStart = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
122 aLineStart.setValue(10., 0.)
123 aLineEnd = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
124 aLineEnd.setValue(10., 50.)
125 aSession.finishOperation()
126
127 aSession.startOperation()
128 #=========================================================================
129 # Test 1. Create an arc as a macro-feature and check all attributes are not initialized
130 #=========================================================================
131 anArc = aSketchFeature.addFeature("SketchMacroArc")
132 assert (anArc.getKind() == "SketchMacroArc")
133 aCenterPoint = geomDataAPI_Point2D(anArc.attribute("center_point"))
134 aPassedPoint = geomDataAPI_Point2D(anArc.attribute("passed_point"))
135 aStartPoint1 = geomDataAPI_Point2D(anArc.attribute("start_point_1"))
136 aStartPoint2 = geomDataAPI_Point2D(anArc.attribute("start_point_2"))
137 aEndPoint1 = geomDataAPI_Point2D(anArc.attribute("end_point_1"))
138 aEndPoint2 = geomDataAPI_Point2D(anArc.attribute("end_point_2"))
139 aEndPoint3 = geomDataAPI_Point2D(anArc.attribute("end_point_3"))
140 aTangentPoint = anArc.refattr("tangent_point")
141 aCenterPointRef = anArc.refattr("center_point_ref")
142 aStartPointRef = anArc.refattr("start_point_ref")
143 aEndPointRef = anArc.refattr("end_point_ref")
144 aPassedPointRef = anArc.refattr("passed_point_ref")
145 assertNotInitializedByCenterAndPassed(anArc)
146 assertNotInitializedByThreePoints(anArc)
147 assertNotInitializedByTangentEdge(anArc)
148 anArcType = anArc.string("arc_type")
149 assert (not anArcType.isInitialized())
150 #=========================================================================
151 # Test 2. Initialize center of arc, check the three points are not initialized
152 #=========================================================================
153 anArcType.setValue("by_center_and_points")
154 aCenterPoint.setValue(-25., -25.)
155 assertNotInitializedByThreePoints(anArc)
156 assertNotInitializedByTangentEdge(anArc)
157 #=========================================================================
158 # Test 3. Change type of circle and check the attributes related to center and passed point became uninitilized
159 #=========================================================================
160 anArcType.setValue("by_three_points")
161 assertNotInitializedByCenterAndPassed(anArc)
162 assertNotInitializedByTangentEdge(anArc)
163 #=========================================================================
164 # Test 4. Initialize two points and change type, they should became uninitialized
165 #=========================================================================
166 aStartPoint2.setValue(-10., 10.)
167 aEndPoint2.setValue(10., 10.)
168 anArcType.setValue("by_center_and_points")
169 assertNotInitializedByThreePoints(anArc)
170 assertNotInitializedByTangentEdge(anArc)
171 #=========================================================================
172 # Test 5. Initialize center and passed points then change the type
173 #=========================================================================
174 aCenterPoint.setValue(-25., -25.)
175 aStartPoint1.setValue(0., 0.)
176 aEndPoint1.setValue(-50., 0.)
177 anArcType.setValue("by_three_points")
178 assertNotInitializedByCenterAndPassed(anArc)
179 assertNotInitializedByTangentEdge(anArc)
180 #=========================================================================
181 # Test 6. Initialize all three points then change the type twice
182 #=========================================================================
183 aStartPoint2.setValue(-10., 10.)
184 aEndPoint2.setValue(10., 10.)
185 aPassedPoint.setValue(0., 0.)
186 anArcType.setValue("by_tangent_edge")
187 assertNotInitializedByThreePoints(anArc)
188 anArcType.setValue("by_center_and_points")
189 assertNotInitializedByTangentEdge(anArc)
190 anArcType.setValue("by_three_points")
191 assertNotInitializedByCenterAndPassed(anArc)
192 #=========================================================================
193 # Test 7. Initialize first and third points then change the type
194 #=========================================================================
195 aStartPointRef.setAttr(aLineStart)
196 aStartPoint2.setValue(aLineStart.pnt())
197 aPassedPointRef.setObject(aLine.lastResult())
198 aPassedPoint.setValue(aLineEnd.pnt())
199 anArcType.setValue("by_tangent_edge")
200 assertNotInitializedByCenterAndPassed(anArc)
201 assertNotInitializedByThreePoints(anArc)
202 #=========================================================================
203 # Test 8. Initialize tangent point then change type
204 #=========================================================================
205 aTangentPoint.setAttr(aLineEnd)
206 anArcType.setValue("by_three_points")
207 assertNotInitializedByCenterAndPassed(anArc)
208 assertNotInitializedByTangentEdge(anArc)
209 #=========================================================================
210 # Test 9. Initialize tangent point and end point then change type
211 #=========================================================================
212 anArcType.setValue("by_tangent_edge")
213 aTangentPoint.setAttr(aLineEnd)
214 aEndPoint3.setValue(50., 50.)
215 anArcType.setValue("by_center_and_points")
216 assertNotInitializedByThreePoints(anArc)
217 assertNotInitializedByTangentEdge(anArc)
218 #=========================================================================
219 # Test 10. Initialize center and passed points and finish operation
220 #=========================================================================
221 aCenterPointRef.setObject(aLine.lastResult())
222 aCenterPoint.setValue((aLineStart.x() + aLineEnd.x()) * 0.5, (aLineStart.y() + aLineEnd.y()) * 0.5)
223 aStartPointRef.setAttr(aLineStart)
224 aStartPoint1.setValue(aLineStart.pnt())
225 aEndPointRef.setObject(aLine.lastResult())
226 aEndPoint1.setValue(aLineEnd.pnt())
227 aSession.finishOperation()
228
229 NB_FEATURES_EXPECTED = 5 # line, arc and three coincidences
230 assert (aSketchFeature.numberOfSubs() == NB_FEATURES_EXPECTED), "Number of features in sketch {}, expected {}".format(aSketchFeature.numberOfSubs(), NB_FEATURES_EXPECTED)
231 verifyLastArc(aSketchFeature, [], [aLineStart.x(), aLineStart.y()], [])
232
233 #=========================================================================
234 # End of test
235 #=========================================================================
236
237 from salome.shaper import model
238 assert(model.checkPythonDump())