Salome HOME
Fix problems in sketch unit tests
[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(-10., 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 aSession.startOperation()
103 aFlyoutPoint = geomDataAPI_Point2D(aConstraint.attribute("ConstraintFlyoutValuePnt"))
104 aFlyoutPoint.setValue(50.0, 100.0)
105 aSession.finishOperation()
106 aSession.abortOperation()
107 assert (anAngleVal.isInitialized())
108 assert (refattrA.isInitialized())
109 assert (refattrB.isInitialized())
110 assert (angle(aSketchLineA, aSketchLineB) == ANGLE_DEGREE)
111 #=========================================================================
112 # Move line, check that angle is constant
113 #=========================================================================
114 aSession.startOperation()
115 aStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
116 aStartPoint.setValue(0., -30.)
117 aConstraint.execute()
118 aSession.finishOperation()
119 assert (angle(aSketchLineA, aSketchLineB) == ANGLE_DEGREE)
120 #=========================================================================
121 # Change angle value and check the lines are moved
122 #=========================================================================
123 NEW_ANGLE_DEGREE = 60.
124 aSession.startOperation()
125 anAngleVal.setValue(NEW_ANGLE_DEGREE)
126 aConstraint.execute()
127 aSession.finishOperation()
128 assert (angle(aSketchLineA, aSketchLineB) == NEW_ANGLE_DEGREE)
129 #=========================================================================
130 # Change angle type
131 #=========================================================================
132 aSession.startOperation()
133 aConstraint.integer("AngleType").setValue(ANGLE_COMPLEMENTARY)
134 aSession.finishOperation()
135 assert (angle(aSketchLineA, aSketchLineB) == NEW_ANGLE_DEGREE)
136 aSession.startOperation()
137 aConstraint.integer("AngleType").setValue(ANGLE_BACKWARD)
138 aSession.finishOperation()
139 assert (angle(aSketchLineA, aSketchLineB) == NEW_ANGLE_DEGREE)
140 #=========================================================================
141 # TODO: improve test
142 # 1. remove constraint, move line's start point to
143 #    check that constraint are not applied
144 # 2. check constrained distance between:
145 #    * point and line
146 #    * two lines
147 #=========================================================================
148 #=========================================================================
149 # End of test
150 #=========================================================================
151
152 import model
153 assert(model.checkPythonDump())