Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / SketchPlugin / Test / TestCreateArcByCenterStartEnd.py
1 """
2     TestCreateArc.py
3
4     static const std::string MY_SKETCH_ARC_ID("SketchArc");
5     static const std::string MY_CENTER_ID = "center_point";
6     static const std::string MY_START_ID = "start_point";
7     static const std::string MY_END_ID = "end_point";
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
13 #=========================================================================
14 # Initialization of the test
15 #=========================================================================
16 from GeomDataAPI import *
17 from ModelAPI import *
18 import math
19 from salome.shaper import model
20
21 __updated__ = "2017-03-28"
22
23 TOLERANCE = 1.e-7
24
25 #=========================================================================
26 # Auxiliary functions
27 #=========================================================================
28
29 def verifyLastArc(theSketch, theCenter, theStart, theEnd):
30     """
31     subroutine to verify position of last arc in the sketch
32     """
33     aLastArc = model.lastSubFeature(theSketch, "SketchArc")
34     aCenterPnt = geomDataAPI_Point2D(aLastArc.attribute("center_point"))
35     aStartPnt = geomDataAPI_Point2D(aLastArc.attribute("start_point"))
36     aEndPnt = geomDataAPI_Point2D(aLastArc.attribute("end_point"))
37     if len(theCenter):
38         verifyPointCoordinates(aCenterPnt, theCenter[0], theCenter[1])
39     if len(theStart):
40         verifyPointCoordinates(aStartPnt, theStart[0], theStart[1])
41     if len(theEnd):
42         verifyPointCoordinates(aEndPnt, theEnd[0], theEnd[1])
43     model.assertSketchArc(aLastArc)
44
45 def verifyPointCoordinates(thePoint, theX, theY):
46     assert thePoint.x() == theX and thePoint.y() == theY, "Wrong '{0}' point ({1}, {2}), expected ({3}, {4})".format(thePoint.id(), thePoint.x(), thePoint.y(), theX, theY)
47
48
49 aSession = ModelAPI_Session.get()
50 aDocument = aSession.moduleDocument()
51 #=========================================================================
52 # Creation of a sketch
53 #=========================================================================
54 aSession.startOperation()
55 aSketchCommonFeature = aDocument.addFeature("Sketch")
56 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
57 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
58 origin.setValue(0, 0, 0)
59 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
60 dirx.setValue(1, 0, 0)
61 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
62 norm.setValue(0, 0, 1)
63 aSession.finishOperation()
64
65 #=========================================================================
66 # Test 1. Create an arc by center, start and end points
67 #=========================================================================
68 aCenter = [10., 10.]
69 aStart  = [0., 50.]
70 aEnd    = [50., 0.]
71 aSession.startOperation()
72 aSketchArc = aSketchFeature.addFeature("SketchArc")
73 assert (aSketchArc.getKind() == "SketchArc")
74 anArcCenter = geomDataAPI_Point2D(aSketchArc.attribute("center_point"))
75 assert (not anArcCenter.isInitialized())
76 anArcCenter.setValue(aCenter[0], aCenter[1])
77 anArcStartPoint = geomDataAPI_Point2D(aSketchArc.attribute("start_point"))
78 assert (not anArcStartPoint.isInitialized())
79 anArcStartPoint.setValue(aStart[0], aStart[1])
80 anArcEndPoint = geomDataAPI_Point2D(aSketchArc.attribute("end_point"))
81 assert (not anArcEndPoint.isInitialized())
82 anArcEndPoint.setValue(aEnd[0], aEnd[1])
83 aSession.finishOperation()
84 verifyLastArc(aSketchFeature, aCenter, aStart, aEnd)
85 # Move center
86 aCenter = [15., 20.]
87 aSession.startOperation()
88 anArcCenter.setValue(aCenter[0], aCenter[1])
89 aSession.finishOperation()
90 verifyLastArc(aSketchFeature, [], [], [])
91 # Move start point
92 deltaX, deltaY = 5., 2.
93 aStart = [anArcStartPoint.x() + deltaX, anArcStartPoint.y() + deltaY]
94 aSession.startOperation()
95 anArcStartPoint.setValue(aStart[0], aStart[1])
96 aSession.finishOperation()
97 verifyLastArc(aSketchFeature, [], [], [])
98 # Move end point
99 aEnd = [anArcEndPoint.x() - deltaX, anArcEndPoint.y() - deltaY]
100 aSession.startOperation()
101 anArcEndPoint.setValue(aEnd[0], aEnd[1])
102 aSession.finishOperation()
103 verifyLastArc(aSketchFeature, [], [], [])
104 # Check that changing the radius does not affect arc
105 aSession.startOperation()
106 anArcRadius = aSketchArc.real("radius")
107 aPrevRadius = anArcRadius.value();
108 anArcRadius.setValue(aPrevRadius + 10.)
109 aSession.finishOperation()
110 assert (math.fabs(anArcRadius.value() - aPrevRadius) < TOLERANCE)
111 verifyLastArc(aSketchFeature, [], [], [])
112 # Check that changing the angle does not affect arc
113 aSession.startOperation()
114 anArcAngle = aSketchArc.real("angle")
115 aPrevAngle = anArcAngle.value()
116 anArcAngle.setValue(aPrevAngle + 10.)
117 aSession.finishOperation()
118 assert (math.fabs(anArcAngle.value() - aPrevAngle) < TOLERANCE)
119 verifyLastArc(aSketchFeature, [], [], [])
120
121 #=========================================================================
122 # End of test
123 #=========================================================================
124
125 assert(model.checkPythonDump())