Salome HOME
Issue #803: Put all the python modules in the same python package newgeom
[modules/shaper.git] / src / SketchPlugin / Test / TestSketchArcCircle.py
1 """
2     TestSketchArcCircle.py
3     
4     static const std::string MY_SKETCH_ARC_ID("SketchArc");
5     static const std::string MY_CENTER_ID = "ArcCenter";
6     static const std::string MY_START_ID = "ArcStartPoint";
7     static const std::string MY_END_ID = "ArcEndPoint";
8     data()->addAttribute(SketchPlugin_Arc::CENTER_ID(), GeomDataAPI_Point2D::typeId());
9     data()->addAttribute(SketchPlugin_Arc::START_ID(),  GeomDataAPI_Point2D::typeId());
10     data()->addAttribute(SketchPlugin_Arc::END_ID(),    GeomDataAPI_Point2D::typeId());
11     
12     static const std::string MY_CIRCLE_ID("SketchCircle");
13     static const std::string MY_CIRCLE_CENTER_ID("CircleCenter");
14     static const std::string MY_CIRCLE_RADIUS_ID("CircleRadius");
15     data()->addAttribute(SketchPlugin_Circle::CENTER_ID(), GeomDataAPI_Point2D::typeId());
16     data()->addAttribute(SketchPlugin_Circle::RADIUS_ID(), ModelAPI_AttributeDouble::typeId());
17 """
18
19 #=========================================================================
20 # Initialization of the test
21 #=========================================================================
22 from GeomDataAPI import *
23 from ModelAPI import *
24 import math
25
26 __updated__ = "2014-10-28"
27
28
29 #=========================================================================
30 # Auxiliary functions
31 #=========================================================================
32
33 def angle(theCenter, theFirst, theLast):
34     """
35     subroutine to calculate angle given by 3 points
36     """
37     aDirX1 = theFirst.x() - theCenter.x()
38     aDirY1 = theFirst.y() - theCenter.y()
39     aLen1 = math.hypot(aDirX1, aDirY1)
40     aDirX2 = theLast.x() - theCenter.x()
41     aDirY2 = theLast.y() - theCenter.y()
42     aLen2 = math.hypot(aDirX2, aDirY2)
43     aDot = aDirX1 * aDirX2 + aDirY1 * aDirY2
44     anAngle = math.acos(aDot / aLen1 / aLen2)
45     return round(anAngle * 180. / math.pi, 6)
46
47 def distancePointPoint(thePointA, thePointB):
48     """
49     subroutine to calculate distance between two points
50     result of calculated distance is has 10**-5 precision
51     """
52     xdiff = math.pow((thePointA.x() - thePointB.x()), 2)
53     ydiff = math.pow((thePointA.y() - thePointB.y()), 2)
54     return round(math.sqrt(xdiff + ydiff), 5)
55
56 def dot(thePoint11, thePoint12, thePoint21, thePoint22):
57     """
58     subroutine to calculate dit product between lines given by their points
59     """
60     aDirX1 = thePoint12.x() - thePoint11.x()
61     aDirY1 = thePoint12.y() - thePoint11.y()
62     aLen1 = math.hypot(aDirX1, aDirY1)
63     aDirX2 = thePoint22.x() - thePoint21.x()
64     aDirY2 = thePoint22.y() - thePoint21.y()
65     aLen2 = math.hypot(aDirX2, aDirY2)
66     aDot = aDirX1 * aDirX2 + aDirY1 * aDirY2
67     return aDot / aLen1 / aLen2
68
69
70 aSession = ModelAPI_Session.get()
71 aDocument = aSession.moduleDocument()
72 #=========================================================================
73 # Creation of a sketch
74 #=========================================================================
75 aSession.startOperation()
76 #aSketchFeature = aDocument.addFeature("Sketch")
77 aSketchCommonFeature = aDocument.addFeature("Sketch")
78 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
79 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
80 origin.setValue(0, 0, 0)
81 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
82 dirx.setValue(1, 0, 0)
83 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
84 norm.setValue(0, 0, 1)
85 aSession.finishOperation()
86 #=========================================================================
87 # Creation of an arc
88 # 1. Test SketchPlugin_Arc attributes
89 # 2.
90 #=========================================================================
91 aSession.startOperation()
92 aSketchReflist = aSketchFeature.reflist("Features")
93 assert (not aSketchReflist.isInitialized())
94 assert (aSketchReflist.size() == 0)
95 assert (len(aSketchReflist.list()) == 0)
96 aSketchArc = aSketchFeature.addFeature("SketchArc")
97 assert (aSketchArc.getKind() == "SketchArc")
98 anArcCentr = geomDataAPI_Point2D(aSketchArc.attribute("ArcCenter"))
99 assert (not anArcCentr.isInitialized())
100 anArcCentr.setValue(10., 10.)
101 anArcStartPoint = geomDataAPI_Point2D(
102     aSketchArc.attribute("ArcStartPoint"))
103 assert (not anArcStartPoint.isInitialized())
104 anArcStartPoint.setValue(0., 50.)
105 anArcEndPoint = geomDataAPI_Point2D(aSketchArc.attribute("ArcEndPoint"))
106 assert (not anArcEndPoint.isInitialized())
107 anArcEndPoint.setValue(50., 0.)
108 aSession.finishOperation()
109 # check that values have been changed
110 aSketchReflist = aSketchFeature.reflist("Features")
111 assert (aSketchReflist.size() == 1)
112 assert (len(aSketchReflist.list()) == 1)
113 assert (anArcCentr.x() == 10.0)
114 assert (anArcCentr.y() == 10.0)
115 assert (anArcStartPoint.x() == 0.0)
116 assert (anArcStartPoint.y() == 50.0)
117 assert (anArcEndPoint.x() == 50.0)
118 assert (anArcEndPoint.y() == 0.0)
119 #=========================================================================
120 # Edit the arc:
121 # 1. Move whole arc
122 # 2. Change the start point
123 # 3. Change the radius of arc
124 # 4. Change arc's angle
125 #=========================================================================
126 aSession.startOperation()
127 deltaX, deltaY = 5., 10.
128 anArcCentr.setValue(anArcCentr.x() + deltaX, anArcCentr.y() + deltaY)
129 aSession.finishOperation()
130 assert (anArcCentr.x() == 15)
131 assert (anArcCentr.y() == 20)
132 assert (math.fabs(distancePointPoint(anArcCentr, anArcStartPoint) - distancePointPoint(anArcCentr, anArcEndPoint)) < 1.e-10)
133 # Change the start point
134 aSession.startOperation()
135 anArcStartPoint.setValue(anArcStartPoint.x() + deltaX, anArcStartPoint.y())
136 aSession.finishOperation()
137 assert (math.fabs(distancePointPoint(anArcCentr, anArcStartPoint) - distancePointPoint(anArcCentr, anArcEndPoint)) < 1.e-10)
138 # Change radius
139 RADIUS = 50
140 aSession.startOperation()
141 anArcRadius = aSketchArc.real("ArcRadius")
142 anArcRadius.setValue(RADIUS)
143 aSession.finishOperation()
144 assert (math.fabs(distancePointPoint(anArcCentr, anArcStartPoint) - RADIUS) < 1.e-10)
145 assert (math.fabs(distancePointPoint(anArcCentr, anArcEndPoint) - RADIUS) < 1.e-10)
146 # Change angle
147 ANGLE = 120
148 aSession.startOperation()
149 anArcAngle = aSketchArc.real("ArcAngle")
150 anArcAngle.setValue(ANGLE)
151 aSession.finishOperation()
152 assert (math.fabs(angle(anArcCentr, anArcStartPoint, anArcEndPoint) - ANGLE) < 1.e-7)
153 #=========================================================================
154 # Check results of the Arc
155 #=========================================================================
156 aResult = aSketchArc.firstResult()
157 aResultConstruction = modelAPI_ResultConstruction(aResult)
158 aShape = aResultConstruction.shape()
159 assert (aShape is not None)
160 assert (not aShape.isNull())
161 #=========================================================================
162 # Create a circle
163 # 1. Test SketchPlugin_Circle.h attributes
164 # 2. ModelAPI_AttributeDouble attribute
165 #=========================================================================
166 aSession.startOperation()
167 aSketchReflist = aSketchFeature.reflist("Features")
168 # Arc is already added
169 assert (aSketchReflist.size() == 1)
170 assert (len(aSketchReflist.list()) == 1)
171 aSketchCircle = aSketchFeature.addFeature("SketchCircle")
172 assert (aSketchCircle.getKind() == "SketchCircle")
173 anCircleCentr = geomDataAPI_Point2D(aSketchCircle.attribute("CircleCenter"))
174 assert (not anCircleCentr.isInitialized())
175 aCircleRadius = aSketchCircle.real("CircleRadius")
176 assert (type(aCircleRadius) == ModelAPI_AttributeDouble)
177 # ModelAPI_AttributeDouble.typeId() is checked in ModelAPI_TestConstants
178 assert (aCircleRadius.attributeType() == ModelAPI_AttributeDouble.typeId())
179 anCircleCentr.setValue(-25., -25)
180 aCircleRadius.setValue(25.)
181 assert (anCircleCentr.x() == -25)
182 assert (anCircleCentr.y() == -25)
183 assert (aCircleRadius.value() == 25)
184 aSession.finishOperation()
185 #=========================================================================
186 # Edit the Cricle
187 # 1. Check that changing the centr of a circle does not affects radius
188 # 2. and vise versa; also check that int is acceptable as well as a real
189 #=========================================================================
190 aSession.startOperation()
191 anCircleCentr.setValue(10, 60)
192 aSession.finishOperation()
193 assert (anCircleCentr.x() == 10)
194 assert (anCircleCentr.y() == 60)
195 assert (aCircleRadius.value() == 25)
196 aSession.startOperation()
197 aCircleRadius.setValue(int(20))
198 aSession.finishOperation()
199 assert (anCircleCentr.x() == 10)
200 assert (anCircleCentr.y() == 60)
201 assert (aCircleRadius.value() == 20)
202 #=========================================================================
203 # Create an arc, tangent to the line
204 #=========================================================================
205 aSession.startOperation()
206 aSketchLine = aSketchFeature.addFeature("SketchLine")
207 aLineStart = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
208 aLineEnd = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
209 aLineStart.setValue(0., 0.)
210 aLineEnd.setValue(50., 0.)
211 aSession.finishOperation()
212 aSession.startOperation()
213 aFixed = aSketchFeature.addFeature("SketchConstraintRigid")
214 aRefObjectA = aFixed.refattr("ConstraintEntityA")
215 aRefObjectA.setObject(modelAPI_ResultConstruction(aSketchLine.lastResult()))
216 aFixed.execute()
217 aSession.finishOperation()
218 aSession.startOperation()
219 aSketchArcTangent = aSketchFeature.addFeature("SketchArc")
220 aSketchArcTangent.string("ArcType").setValue("Tangent")
221 anArcEndPoint = geomDataAPI_Point2D(aSketchArcTangent.attribute("ArcEndPoint"))
222 aTangent = aSketchArcTangent.refattr("ArcTangentPoint")
223 aTangent.setAttr(aLineEnd)
224 anArcEndPoint.setValue(51., 1.)
225 aSession.finishOperation()
226 aSession.startOperation()
227 anArcEndPoint.setValue(100., 25.)
228 aSession.finishOperation()
229 anArcCenter = geomDataAPI_Point2D(aSketchArcTangent.attribute("ArcCenter"))
230 aDot = dot(anArcCenter, aLineEnd, aLineStart, aLineEnd)
231 assert math.fabs(aDot) <= 2.e-4, "Observed dot product: {0}".format(aDot)
232 #=========================================================================
233 # Create an arc, tangent to the previous arc
234 #=========================================================================
235 aSession.startOperation()
236 aSketchArcTangent2 = aSketchFeature.addFeature("SketchArc")
237 aSketchArcTangent2.string("ArcType").setValue("Tangent")
238 anArcEndPoint2 = geomDataAPI_Point2D(aSketchArcTangent2.attribute("ArcEndPoint"))
239 aTangent = aSketchArcTangent2.refattr("ArcTangentPoint")
240 aTangent.setAttr(anArcEndPoint)
241 anArcEndPoint2.setValue(50., 150.)
242 aSession.finishOperation()
243 #=========================================================================
244 # End of test
245 #=========================================================================
246
247 from salome.shaper import model
248 assert(model.checkPythonDump())