3 Unit test of SketchPlugin_ConstraintAngle class
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());
14 from GeomDataAPI import *
15 from ModelAPI import *
19 #=========================================================================
21 #=========================================================================
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"))
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)
37 aDot = aDirX1 * aDirX2 + aDirY1 * aDirY2
39 anAngle = math.acos(aDot / aLen1 / aLen2)
40 return round(anAngle * 180. / math.pi, 6)
43 #=========================================================================
44 # Initialization of the test
45 #=========================================================================
47 __updated__ = "2015-09-18"
50 ANGLE_COMPLEMENTARY = 1
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 #=========================================================================
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.)
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 #=========================================================================
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 #=========================================================================
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 #=========================================================================
137 # 1. remove constraint, move line's start point to
138 # check that constraint are not applied
139 # 2. check constrained distance between:
142 #=========================================================================
143 #=========================================================================
145 #=========================================================================