Salome HOME
882e686d8989b2f5c30f2a90f94d568b5a7ccab9
[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     aCenter = geomDataAPI_Point2D(aLastCircle.attribute("circle_center"))
76     verifyPointCoordinates(aCenter, theX, theY)
77     aRadius = aLastCircle.real("circle_radius")
78     assert aRadius.value() == theR, "Wrong radius {0}, expected {1}".format(aRadius.value(), theR)
79
80 def verifyPointCoordinates(thePoint, theX, theY):
81     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)
82
83
84 #=========================================================================
85 # Start of test
86 #=========================================================================
87
88 aSession = ModelAPI_Session.get()
89 aDocument = aSession.moduleDocument()
90 #=========================================================================
91 # Creation of a sketch
92 #=========================================================================
93 aSession.startOperation()
94 aSketchCommonFeature = aDocument.addFeature("Sketch")
95 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
96 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
97 origin.setValue(0, 0, 0)
98 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
99 dirx.setValue(1, 0, 0)
100 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
101 norm.setValue(0, 0, 1)
102 aSession.finishOperation()
103 aSketch = SketchAPI_Sketch(aSketchFeature)
104
105 #=========================================================================
106 # Creation of auxiliary features
107 #=========================================================================
108 aSession.startOperation()
109 aSession.startOperation()
110 aLine = aSketchFeature.addFeature("SketchLine")
111 aLineStart = geomDataAPI_Point2D(aLine.attribute("StartPoint"))
112 aLineStart.setValue(10., 0.)
113 aLineEnd = geomDataAPI_Point2D(aLine.attribute("EndPoint"))
114 aLineEnd.setValue(10., 50.)
115 aSession.finishOperation()
116
117 aSession.startOperation()
118 #=========================================================================
119 # Test 1. Create a circle as a macro-feature and check all attributes are not initialized
120 #=========================================================================
121 aCircle = aSketchFeature.addFeature("SketchMacroCircle")
122 assert (aCircle.getKind() == "SketchMacroCircle")
123 aCenterPoint = geomDataAPI_Point2D(aCircle.attribute("center_point"))
124 aPassedPoint = geomDataAPI_Point2D(aCircle.attribute("passed_point"))
125 aFirstPoint = geomDataAPI_Point2D(aCircle.attribute("first_point"))
126 aSecondPoint = geomDataAPI_Point2D(aCircle.attribute("second_point"))
127 aThirdPoint = geomDataAPI_Point2D(aCircle.attribute("third_point"))
128 aCenterPointRef = aCircle.refattr("center_point_ref")
129 aPassedPointRef = aCircle.refattr("passed_point_ref")
130 aFirstPointRef = aCircle.refattr("first_point_ref")
131 aSecondPointRef = aCircle.refattr("second_point_ref")
132 aThirdPointRef = aCircle.refattr("third_point_ref")
133 assertNotInitializedByCenterAndPassed(aCircle)
134 assertNotInitializedByThreePoints(aCircle)
135 aCircleType = aCircle.string("circle_type")
136 assert (not aCircleType.isInitialized())
137 #=========================================================================
138 # Test 2. Initialize center of circle, check the three points are not initialized
139 #=========================================================================
140 aCircleType.setValue("circle_type_by_center_and_passed_points")
141 aCenterPoint.setValue(-25., -25.)
142 assertNotInitializedByThreePoints(aCircle)
143 #=========================================================================
144 # Test 3. Change type of circle and check the attributes related to center and passed point became uninitilized
145 #=========================================================================
146 aCircleType.setValue("circle_type_by_three_points")
147 assertNotInitializedByCenterAndPassed(aCircle)
148 #=========================================================================
149 # Test 4. Initialize two points and change type, they should became uninitialized
150 #=========================================================================
151 aFirstPoint.setValue(-10., 10.)
152 aSecondPoint.setValue(10., 10.)
153 aCircleType.setValue("circle_type_by_center_and_passed_points")
154 assertNotInitializedByThreePoints(aCircle)
155 #=========================================================================
156 # Test 5. Initialize center and passed point then change the type
157 #=========================================================================
158 aCenterPoint.setValue(-25., -25.)
159 aPassedPoint.setValue(0., 0.)
160 aCircleType.setValue("circle_type_by_three_points")
161 assertNotInitializedByCenterAndPassed(aCircle)
162 #=========================================================================
163 # Test 6. Initialize all three points then change the type twice
164 #=========================================================================
165 aFirstPoint.setValue(-10., 10.)
166 aSecondPoint.setValue(10., 10.)
167 aThirdPoint.setValue(0., 0.)
168 aCircleType.setValue("circle_type_by_center_and_passed_points")
169 assertNotInitializedByThreePoints(aCircle)
170 aCircleType.setValue("circle_type_by_three_points")
171 assertNotInitializedByCenterAndPassed(aCircle)
172 #=========================================================================
173 # Test 7. Initialize first and third points then change the type
174 #=========================================================================
175 aFirstPointRef.setAttr(aLineStart)
176 aFirstPoint.setValue(aLineStart.pnt())
177 aThirdPointRef.setObject(aLine.lastResult())
178 aThirdPoint.setValue(aLineEnd.pnt())
179 aCircleType.setValue("circle_type_by_center_and_passed_points")
180 assertNotInitializedByThreePoints(aCircle)
181 #=========================================================================
182 # Test 8. Initialize center and passed points and finish operation
183 #=========================================================================
184 aCenterPointRef.setAttr(aLineStart)
185 aCenterPoint.setValue(aLineStart.pnt())
186 aPassedPointRef.setAttr(aLineEnd)
187 aPassedPoint.setValue(aLineEnd.pnt())
188 aSession.finishOperation()
189
190 aRadius = model.distancePointPoint(aLineStart, aLineEnd)
191 NB_FEATURES_EXPECTED = 4 # line, circle and two coincidences
192 assert (aSketchFeature.numberOfSubs() == NB_FEATURES_EXPECTED), "Number of features in sketch {}, expected {}".format(aSketchFeature.numberOfSubs(), NB_FEATURES_EXPECTED)
193 verifyLastCircle(aSketchFeature, aLineStart.x(), aLineStart.y(), aRadius)
194
195 #=========================================================================
196 # End of test
197 #=========================================================================
198
199 from salome.shaper import model
200 assert(model.checkPythonDump())