Salome HOME
Test cases for constraint Angle, Multi-Rotation, Multi-Translation and Fillet features
[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 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 # Create two lines
66 #=========================================================================
67 aSession.startOperation()
68 aSketchLineA = aSketchFeature.addFeature("SketchLine")
69 aStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
70 aEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
71 aStartPoint.setValue(-10., 25.)
72 aEndPoint.setValue(100., 25.)
73
74 aSketchLineB = aSketchFeature.addFeature("SketchLine")
75 aStartPoint = geomDataAPI_Point2D(aSketchLineB.attribute("StartPoint"))
76 aEndPoint = geomDataAPI_Point2D(aSketchLineB.attribute("EndPoint"))
77 aStartPoint.setValue(-20., 15.)
78 aEndPoint.setValue(80., 50.)
79 aSession.finishOperation()
80 #=========================================================================
81 # Make a constraint to keep the angle
82 #=========================================================================
83 ANGLE_DEGREE = 30.
84 aSession.startOperation()
85 aConstraint = aSketchFeature.addFeature("SketchConstraintAngle")
86 anAngleVal = aConstraint.real("ConstraintValue")
87 refattrA = aConstraint.refattr("ConstraintEntityA")
88 refattrB = aConstraint.refattr("ConstraintEntityB")
89 assert (not anAngleVal.isInitialized())
90 assert (not refattrA.isInitialized())
91 assert (not refattrB.isInitialized())
92 anAngleVal.setValue(ANGLE_DEGREE)
93 refattrA.setObject(aSketchLineA.firstResult())
94 refattrB.setObject(aSketchLineB.firstResult())
95 aConstraint.execute()
96 aSession.finishOperation()
97 assert (anAngleVal.isInitialized())
98 assert (refattrA.isInitialized())
99 assert (refattrB.isInitialized())
100 assert (angle(aSketchLineA, aSketchLineB) == ANGLE_DEGREE)
101 #=========================================================================
102 # Move line, check that angle is constant
103 #=========================================================================
104 aSession.startOperation()
105 aStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
106 aStartPoint.setValue(0., 30.)
107 aConstraint.execute()
108 aSession.finishOperation()
109 assert (angle(aSketchLineA, aSketchLineB) == ANGLE_DEGREE)
110 #=========================================================================
111 # Change angle value and check the lines are moved
112 #=========================================================================
113 NEW_ANGLE_DEGREE = 60.
114 aSession.startOperation()
115 anAngleVal.setValue(NEW_ANGLE_DEGREE)
116 aConstraint.execute()
117 aSession.finishOperation()
118 assert (angle(aSketchLineA, aSketchLineB) == NEW_ANGLE_DEGREE)
119 #=========================================================================
120 # TODO: improve test
121 # 1. remove constraint, move line's start point to
122 #    check that constraint are not applied
123 # 2. check constrained distance between:
124 #    * point and line
125 #    * two lines
126 #=========================================================================
127 #=========================================================================
128 # End of test
129 #=========================================================================