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