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