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