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