Salome HOME
Update unit-tests for SketchPlugin. Test case for the Projection has been added
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintAngle.py
1 """
2     TestConstraintAngle.py
3     Unit test of SketchPlugin_ConstraintAngle class
4         
5     SketchPlugin_ConstraintAngle
6         static const std::string MY_CONSTRAINT_ANGLE_ID("SketchConstraintAngle");
7         data()->addAttribute(SketchPlugin_Constraint::VALUE(),    ModelAPI_AttributeDouble::typeId());
8         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
9         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
10         data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
11         
12     
13 """
14 from GeomDataAPI import *
15 from ModelAPI import *
16 import os
17 import math
18
19 #=========================================================================
20 # Auxiliary functions
21 #=========================================================================
22
23 def angle(theLine1, theLine2):
24     # subroutine to angle between two lines
25     aStartPoint1 = geomDataAPI_Point2D(theLine1.attribute("StartPoint"))
26     aEndPoint1   = geomDataAPI_Point2D(theLine1.attribute("EndPoint"))
27     aStartPoint2 = geomDataAPI_Point2D(theLine2.attribute("StartPoint"))
28     aEndPoint2   = geomDataAPI_Point2D(theLine2.attribute("EndPoint"))
29     
30     aDirX1 = aEndPoint1.x() - aStartPoint1.x()
31     aDirY1 = aEndPoint1.y() - aStartPoint1.y()
32     aLen1 = math.hypot(aDirX1, aDirY1)
33     aDirX2 = aEndPoint2.x() - aStartPoint2.x()
34     aDirY2 = aEndPoint2.y() - aStartPoint2.y()
35     aLen2 = math.hypot(aDirX2, aDirY2)
36     
37     aDot = aDirX1 * aDirX2 + aDirY1 * aDirY2
38     
39     anAngle = math.acos(aDot / aLen1 / aLen2)
40     return round(anAngle * 180. / math.pi, 6)
41
42
43 #=========================================================================
44 # Initialization of the test
45 #=========================================================================
46
47 __updated__ = "2015-09-18"
48
49 ANGLE_DIRECT = 0
50 ANGLE_COMPLEMENTARY = 1
51 ANGLE_BACKWARD = 2
52
53 aSession = ModelAPI_Session.get()
54 aDocument = aSession.moduleDocument()
55 #=========================================================================
56 # Creation of a sketch
57 #=========================================================================
58 aSession.startOperation()
59 aSketchCommonFeature = aDocument.addFeature("Sketch")
60 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
61 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
62 origin.setValue(0, 0, 0)
63 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
64 dirx.setValue(1, 0, 0)
65 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
66 norm.setValue(0, 0, 1)
67 aSession.finishOperation()
68 #=========================================================================
69 # Create two lines
70 #=========================================================================
71 aSession.startOperation()
72 aSketchLineA = aSketchFeature.addFeature("SketchLine")
73 aStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
74 aEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
75 aStartPoint.setValue(-10., 25.)
76 aEndPoint.setValue(100., 25.)
77
78 aSketchLineB = aSketchFeature.addFeature("SketchLine")
79 aStartPoint = geomDataAPI_Point2D(aSketchLineB.attribute("StartPoint"))
80 aEndPoint = geomDataAPI_Point2D(aSketchLineB.attribute("EndPoint"))
81 aStartPoint.setValue(-20., 15.)
82 aEndPoint.setValue(80., 50.)
83 aSession.finishOperation()
84 #=========================================================================
85 # Make a constraint to keep the angle
86 #=========================================================================
87 ANGLE_DEGREE = 30.
88 aSession.startOperation()
89 aConstraint = aSketchFeature.addFeature("SketchConstraintAngle")
90 aConstraint.integer("AngleType").setValue(ANGLE_DIRECT)
91 anAngleVal = aConstraint.real("AngleValue")
92 refattrA = aConstraint.refattr("ConstraintEntityA")
93 refattrB = aConstraint.refattr("ConstraintEntityB")
94 assert (not anAngleVal.isInitialized())
95 assert (not refattrA.isInitialized())
96 assert (not refattrB.isInitialized())
97 refattrA.setObject(aSketchLineA.firstResult())
98 refattrB.setObject(aSketchLineB.firstResult())
99 anAngleVal.setValue(ANGLE_DEGREE)
100 aConstraint.execute()
101 aSession.finishOperation()
102 assert (anAngleVal.isInitialized())
103 assert (refattrA.isInitialized())
104 assert (refattrB.isInitialized())
105 assert (angle(aSketchLineA, aSketchLineB) == ANGLE_DEGREE)
106 #=========================================================================
107 # Move line, check that angle is constant
108 #=========================================================================
109 aSession.startOperation()
110 aStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
111 aStartPoint.setValue(0., 30.)
112 aConstraint.execute()
113 aSession.finishOperation()
114 assert (angle(aSketchLineA, aSketchLineB) == ANGLE_DEGREE)
115 #=========================================================================
116 # Change angle value and check the lines are moved
117 #=========================================================================
118 NEW_ANGLE_DEGREE = 60.
119 aSession.startOperation()
120 anAngleVal.setValue(NEW_ANGLE_DEGREE)
121 aConstraint.execute()
122 aSession.finishOperation()
123 assert (angle(aSketchLineA, aSketchLineB) == NEW_ANGLE_DEGREE)
124 #=========================================================================
125 # Change angle type
126 #=========================================================================
127 aSession.startOperation()
128 aConstraint.integer("AngleType").setValue(ANGLE_COMPLEMENTARY)
129 aSession.finishOperation()
130 assert (angle(aSketchLineA, aSketchLineB) == NEW_ANGLE_DEGREE)
131 aSession.startOperation()
132 aConstraint.integer("AngleType").setValue(ANGLE_BACKWARD)
133 aSession.finishOperation()
134 assert (angle(aSketchLineA, aSketchLineB) == NEW_ANGLE_DEGREE)
135 #=========================================================================
136 # TODO: improve test
137 # 1. remove constraint, move line's start point to
138 #    check that constraint are not applied
139 # 2. check constrained distance between:
140 #    * point and line
141 #    * two lines
142 #=========================================================================
143 #=========================================================================
144 # End of test
145 #=========================================================================