Salome HOME
Merge remote-tracking branch 'remotes/origin/EDF_2020_Lot2'
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateCircleChangeType.py
1 # Copyright (C) 2014-2020  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     TestCreateCircleChangeType.py
22
23     Test attributes reset when changing creation method of a circle 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-03-22"
37
38
39 ##=========================================================================
40 ## Auxiliary functions
41 ##=========================================================================
42
43 def assertNotInitializedByCenterAndPassed(theMacroCircle):
44     # check points
45     aPassedPoint = geomDataAPI_Point2D(theMacroCircle.attribute("passed_point"))
46     assert (not aPassedPoint.isInitialized())
47     # check references
48     aCenterPointRef = theMacroCircle.refattr("center_point_ref")
49     aPassedPointRef = theMacroCircle.refattr("passed_point_ref")
50     assert (not aCenterPointRef.isInitialized())
51     assert (not aPassedPointRef.isInitialized())
52
53 def assertNotInitializedByThreePoints(theMacroCircle):
54     # check points
55     aFirstPoint = geomDataAPI_Point2D(theMacroCircle.attribute("first_point"))
56     aSecondPoint = geomDataAPI_Point2D(theMacroCircle.attribute("second_point"))
57     aThirdPoint = geomDataAPI_Point2D(theMacroCircle.attribute("third_point"))
58     assert (not aFirstPoint.isInitialized())
59     assert (not aSecondPoint.isInitialized())
60     assert (not aThirdPoint.isInitialized())
61     # check references
62     aFirstPointRef = theMacroCircle.refattr("first_point_ref")
63     aSecondPointRef = theMacroCircle.refattr("second_point_ref")
64     aThirdPointRef = theMacroCircle.refattr("third_point_ref")
65     assert (not aFirstPointRef.isInitialized())
66     assert (not aSecondPointRef.isInitialized())
67     assert (not aThirdPointRef.isInitialized())
68
69 def verifyLastCircle(theSketch, theX, theY, theR):
70     """
71     subroutine to verify position of last circle in the sketch
72     """
73     aLastCircle = model.lastSubFeature(theSketch, "SketchCircle")
74     model.assertCircle(aLastCircle, [theX, theY], theR)
75
76
77 #=========================================================================
78 # Start of test
79 #=========================================================================
80
81 aSession = ModelAPI_Session.get()
82 aDocument = aSession.moduleDocument()
83 #=========================================================================
84 # Creation of a sketch
85 #=========================================================================
86 aSession.startOperation()
87 aSketchCommonFeature = aDocument.addFeature("Sketch")
88 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
89 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
90 origin.setValue(0, 0, 0)
91 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
92 dirx.setValue(1, 0, 0)
93 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
94 norm.setValue(0, 0, 1)
95 aSession.finishOperation()
96 aSketch = SketchAPI_Sketch(aSketchFeature)
97
98 #=========================================================================
99 # Creation of auxiliary features
100 #=========================================================================
101 aSession.startOperation()
102 aSession.startOperation()
103 aLine = aSketchFeature.addFeature("SketchLine")
104 aLineStart = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
105 aLineStart.setValue(10., 0.)
106 aLineEnd = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
107 aLineEnd.setValue(10., 50.)
108 aSession.finishOperation()
109
110 aSession.startOperation()
111 #=========================================================================
112 # Test 1. Create a circle as a macro-feature and check all attributes are not initialized
113 #=========================================================================
114 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
115 assert (aCircle.getKind() == "SketchMacroCircle")
116 aCenterPoint = geomDataAPI_Point2D(aCircle.attribute("center_point"))
117 aPassedPoint = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
118 aFirstPoint = geomDataAPI_Point2D(aCircle.attribute("first_point"))
119 aSecondPoint = geomDataAPI_Point2D(aCircle.attribute("second_point"))
120 aThirdPoint = geomDataAPI_Point2D(aCircle.attribute("third_point"))
121 aCenterPointRef = aCircle.refattr("center_point_ref")
122 aPassedPointRef = aCircle.refattr("passed_point_ref")
123 aFirstPointRef = aCircle.refattr("first_point_ref")
124 aSecondPointRef = aCircle.refattr("second_point_ref")
125 aThirdPointRef = aCircle.refattr("third_point_ref")
126 assertNotInitializedByCenterAndPassed(aCircle)
127 assertNotInitializedByThreePoints(aCircle)
128 aCircleType = aCircle.string("circle_type")
129 assert (not aCircleType.isInitialized())
130 #=========================================================================
131 # Test 2. Initialize center of circle, check the three points are not initialized
132 #=========================================================================
133 aCircleType.setValue("circle_type_by_center_and_passed_points")
134 aCenterPoint.setValue(-25., -25.)
135 assertNotInitializedByThreePoints(aCircle)
136 #=========================================================================
137 # Test 3. Change type of circle and check the attributes related to center and passed point became uninitilized
138 #=========================================================================
139 aCircleType.setValue("circle_type_by_three_points")
140 assertNotInitializedByCenterAndPassed(aCircle)
141 #=========================================================================
142 # Test 4. Initialize two points and change type, they should became uninitialized
143 #=========================================================================
144 aFirstPoint.setValue(-10., 10.)
145 aSecondPoint.setValue(10., 10.)
146 aCircleType.setValue("circle_type_by_center_and_passed_points")
147 assertNotInitializedByThreePoints(aCircle)
148 #=========================================================================
149 # Test 5. Initialize center and passed point then change the type
150 #=========================================================================
151 aCenterPoint.setValue(-25., -25.)
152 aPassedPoint.setValue(0., 0.)
153 aCircleType.setValue("circle_type_by_three_points")
154 assertNotInitializedByCenterAndPassed(aCircle)
155 #=========================================================================
156 # Test 6. Initialize all three points then change the type twice
157 #=========================================================================
158 aFirstPoint.setValue(-10., 10.)
159 aSecondPoint.setValue(10., 10.)
160 aThirdPoint.setValue(0., 0.)
161 aCircleType.setValue("circle_type_by_center_and_passed_points")
162 assertNotInitializedByThreePoints(aCircle)
163 aCircleType.setValue("circle_type_by_three_points")
164 assertNotInitializedByCenterAndPassed(aCircle)
165 #=========================================================================
166 # Test 7. Initialize first and third points then change the type
167 #=========================================================================
168 aFirstPointRef.setAttr(aLineStart)
169 aFirstPoint.setValue(aLineStart.pnt())
170 aThirdPointRef.setObject(aLine.lastResult())
171 aThirdPoint.setValue(aLineEnd.pnt())
172 aCircleType.setValue("circle_type_by_center_and_passed_points")
173 assertNotInitializedByThreePoints(aCircle)
174 #=========================================================================
175 # Test 8. Initialize center and passed points and finish operation
176 #=========================================================================
177 aCenterPointRef.setAttr(aLineStart)
178 aCenterPoint.setValue(aLineStart.pnt())
179 aPassedPointRef.setAttr(aLineEnd)
180 aPassedPoint.setValue(aLineEnd.pnt())
181 aSession.finishOperation()
182
183 aRadius = model.distancePointPoint(aLineStart, aLineEnd)
184 NB_FEATURES_EXPECTED = 4 # line, circle and two coincidences
185 assert (aSketchFeature.numberOfSubs() == NB_FEATURES_EXPECTED), "Number of features in sketch {}, expected {}".format(aSketchFeature.numberOfSubs(), NB_FEATURES_EXPECTED)
186 verifyLastCircle(aSketchFeature, aLineStart.x(), aLineStart.y(), aRadius)
187
188 #=========================================================================
189 # End of test
190 #=========================================================================
191
192 from salome.shaper import model
193 assert(model.checkPythonDump())