Salome HOME
09506082bc58b0dce6c238d9ee2413e58521f972
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintMiddlePoint.py
1 """
2     TestConstraintCoincidence.py
3     Unit test of SketchPlugin_ConstraintCoincidence class
4     
5     SketchPlugin_Constraint
6         static const std::string MY_CONSTRAINT_VALUE("ConstraintValue");
7         static const std::string MY_FLYOUT_VALUE_PNT("ConstraintFlyoutValuePnt");
8         static const std::string MY_ENTITY_A("ConstraintEntityA");
9         static const std::string MY_ENTITY_B("ConstraintEntityB");
10         static const std::string MY_ENTITY_C("ConstraintEntityC");
11         static const std::string MY_ENTITY_D("ConstraintEntityD");
12         
13     SketchPlugin_ConstraintCoincidence
14         static const std::string MY_CONSTRAINT_COINCIDENCE_ID("SketchConstraintCoincidence");
15         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
16         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
17
18 """
19 from GeomDataAPI import *
20 from ModelAPI import *
21 import math
22 #=========================================================================
23 # Initialization of the test
24 #=========================================================================
25
26 __updated__ = "2016-01-29"
27 TOLERANCE = 1.e-7
28
29
30 #=========================================================================
31 # Auxiliary functions
32 #=========================================================================
33 def checkMiddlePoint(point, line):
34     aStart = geomDataAPI_Point2D(line.attribute("StartPoint"))
35     aEnd   = geomDataAPI_Point2D(line.attribute("EndPoint"))
36     assert math.fabs(aStart.x() + aEnd.x() - 2.0 * point.x()) <= TOLERANCE, "x1={0}, x2={1}, mid={2}".format(aStart.x(), aEnd.x(), point.x())
37     assert math.fabs(aStart.y() + aEnd.y() - 2.0 * point.y()) <= TOLERANCE, "y1={0}, y2={1}, mid={2}".format(aStart.y(), aEnd.y(), point.y())
38
39
40 #=========================================================================
41 # Start of test
42 #=========================================================================
43 aSession = ModelAPI_Session.get()
44 aDocument = aSession.moduleDocument()
45 # add an origin
46 aSession.startOperation()
47 aFeature = aDocument.addFeature("Point")
48 aFeature.real("x").setValue(0.)
49 aFeature.real("y").setValue(0.)
50 aFeature.real("z").setValue(0.)
51 anOriginName = aFeature.name()
52 aSession.finishOperation()
53 #=========================================================================
54 # Creation of a sketch
55 #=========================================================================
56 aSession.startOperation()
57 aSketchFeature = featureToCompositeFeature(aDocument.addFeature("Sketch"))
58 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
59 origin.setValue(0, 0, 0)
60 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
61 dirx.setValue(1, 0, 0)
62 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
63 norm.setValue(0, 0, 1)
64 aSession.finishOperation()
65 #=========================================================================
66 # Create a two lines
67 #=========================================================================
68 aSession.startOperation()
69 aLine1 = aSketchFeature.addFeature("SketchLine")
70 aStartPoint1 = geomDataAPI_Point2D(aLine1.attribute("StartPoint"))
71 aEndPoint1 = geomDataAPI_Point2D(aLine1.attribute("EndPoint"))
72 aStartPoint1.setValue(50., 0.)
73 aEndPoint1.setValue(100., 25.)
74 aLine2 = aSketchFeature.addFeature("SketchLine")
75 aStartPoint2 = geomDataAPI_Point2D(aLine2.attribute("StartPoint"))
76 aEndPoint2 = geomDataAPI_Point2D(aLine2.attribute("EndPoint"))
77 aStartPoint2.setValue(10., 100.)
78 aEndPoint2.setValue(100., 25.)
79 aSession.finishOperation()
80 #=========================================================================
81 # Make end point of second line middle point on first line
82 #=========================================================================
83 aSession.startOperation()
84 aConstraint = aSketchFeature.addFeature("SketchConstraintMiddle")
85 reflistA = aConstraint.refattr("ConstraintEntityA")
86 reflistB = aConstraint.refattr("ConstraintEntityB")
87 reflistA.setAttr(aEndPoint2)
88 reflistB.setObject(aLine1.lastResult())
89 aConstraint.execute()
90 aSession.finishOperation()
91 #=========================================================================
92 # Check values and move one constrainted object
93 #=========================================================================
94 checkMiddlePoint(aEndPoint2, aLine1)
95 deltaX, deltaY = -80.0, -40.0
96 aSession.startOperation()
97 aStartPoint1.setValue(aStartPoint1.x() + deltaX, aStartPoint1.y() + deltaY)
98 aSession.finishOperation()
99 checkMiddlePoint(aEndPoint2, aLine1)
100 #=========================================================================
101 # Remove constraint and move the line
102 #=========================================================================
103 aCurX, aCurY = aEndPoint2.x(), aEndPoint2.y()
104 aSession.startOperation()
105 aDocument.removeFeature(aConstraint)
106 aSession.finishOperation()
107 aSession.startOperation()
108 aEndPoint1.setValue(90., 0.)
109 aSession.finishOperation()
110 assert (aEndPoint2.x() == aCurX and aEndPoint2.y() == aCurY)
111
112 #=========================================================================
113 # Set external point as a middle point
114 #=========================================================================
115 # create origin
116 aSession.startOperation()
117 anOrigRes = modelAPI_Result(aDocument.objectByName("Construction", anOriginName))
118 assert (anOrigRes)
119 anOrigShape = anOrigRes.shape()
120 assert (anOrigShape)
121 anOrigin = aSketchFeature.addFeature("SketchPoint")
122 anOriginCoord = geomDataAPI_Point2D(anOrigin.attribute("PointCoordindates"))
123 anOriginCoord.setValue(0., 0.)
124 anOrigin.selection("External").setValue(anOrigRes, anOrigShape)
125 aSession.finishOperation()
126 # middle point constraint
127 aSession.startOperation()
128 aConstraint = aSketchFeature.addFeature("SketchConstraintMiddle")
129 reflistA = aConstraint.refattr("ConstraintEntityA")
130 reflistB = aConstraint.refattr("ConstraintEntityB")
131 reflistA.setObject(aLine2.lastResult())
132 reflistB.setObject(anOrigin.lastResult())
133 aSession.finishOperation()
134 checkMiddlePoint(anOriginCoord, aLine2)
135 #=========================================================================
136 # Check origin coordinates does not changed
137 #=========================================================================
138 assert (anOriginCoord.x() == 0. and anOriginCoord.y() == 0.)
139 #=========================================================================
140 # End of test
141 #=========================================================================