Salome HOME
46bd27340cc2fdbc08b9455c53951600a9b20f6a
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintCollinear.py
1 """
2     TestConstraintCollinear.py
3     Unit test of SketchPlugin_ConstraintCollinear class
4         
5     SketchPlugin_ConstraintCollinear
6         static const std::string MY_CONSTRAINT_COLLINEAR_ID("SketchConstraintCollinear");
7         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
8         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
9
10 """
11 from GeomDataAPI import *
12 from ModelAPI import *
13 import math
14 #=========================================================================
15 # Initialization of the test
16 #=========================================================================
17
18 __updated__ = "2016-01-28"
19
20
21 def checkCollinearVec(theX1, theY1, theX2, theY2):
22     TOL = 1.e-6
23     aLen1 = math.hypot(theX1, theY1)
24     aLen2 = math.hypot(theX2, theY2)
25     aDot = theX1 * theX2 + theY1 * theY2
26     assert math.fabs(math.fabs(aDot) - aLen1 * aLen2) < TOL**2, "aDot = {0}, aLen1 = {1}, aLen2 = {2}, aDiff = {3}".format(aDot, aLen1, aLen2, math.fabs(aDot) - aLen1 * aLen2)
27
28 def checkCollinear(theLine1, theLine2):
29     aStartPoint1 = geomDataAPI_Point2D(theLine1.attribute("StartPoint"))
30     aEndPoint1   = geomDataAPI_Point2D(theLine1.attribute("EndPoint"))
31     aStartPoint2 = geomDataAPI_Point2D(theLine2.attribute("StartPoint"))
32     aEndPoint2   = geomDataAPI_Point2D(theLine2.attribute("EndPoint"))
33     
34     aDir1x, aDir1y = aEndPoint1.x() - aStartPoint1.x(), aEndPoint1.y() - aStartPoint1.y()
35     aDir2x, aDir2y = aEndPoint2.x() - aStartPoint1.x(), aEndPoint2.y() - aStartPoint1.y()
36     aDir3x, aDir3y = aStartPoint2.x() - aStartPoint1.x(), aStartPoint2.y() - aStartPoint1.y()
37     checkCollinearVec(aDir1x, aDir1y, aDir2x, aDir2y)
38     checkCollinearVec(aDir1x, aDir1y, aDir3x, aDir3y)
39
40
41
42 aSession = ModelAPI_Session.get()
43 aDocument = aSession.moduleDocument()
44 #=========================================================================
45 # Creation of a sketch
46 #=========================================================================
47 aSession.startOperation()
48 aSketchCommonFeature = aDocument.addFeature("Sketch")
49 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
50 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
51 origin.setValue(0, 0, 0)
52 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
53 dirx.setValue(1, 0, 0)
54 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
55 norm.setValue(0, 0, 1)
56 aSession.finishOperation()
57 #=========================================================================
58 # Create two lines
59 #=========================================================================
60 aSession.startOperation()
61 # line A
62 aSketchLineA = aSketchFeature.addFeature("SketchLine")
63 aLineAStartPoint = geomDataAPI_Point2D(aSketchLineA.attribute("StartPoint"))
64 aLineAEndPoint = geomDataAPI_Point2D(aSketchLineA.attribute("EndPoint"))
65 aSketchLineB = aSketchFeature.addFeature("SketchLine")
66 aLineAStartPoint.setValue(0., 25)
67 aLineAEndPoint.setValue(85., 25)
68 # line B
69 aLineBStartPoint = geomDataAPI_Point2D(aSketchLineB.attribute("StartPoint"))
70 aLineBEndPoint = geomDataAPI_Point2D(aSketchLineB.attribute("EndPoint"))
71 aLineBStartPoint.setValue(0., 50)
72 aLineBEndPoint.setValue(80., 75)
73 aSession.finishOperation()
74 #=========================================================================
75 # Link lines with collinear constraint
76 #=========================================================================
77 aSession.startOperation()
78 aParallelConstraint = aSketchFeature.addFeature("SketchConstraintCollinear")
79 refattrA = aParallelConstraint.refattr("ConstraintEntityA")
80 refattrB = aParallelConstraint.refattr("ConstraintEntityB")
81 refattrA.setObject(aSketchLineA.firstResult())
82 refattrB.setObject(aSketchLineB.firstResult())
83 aParallelConstraint.execute()
84 aSession.finishOperation()
85 checkCollinear(aSketchLineA, aSketchLineB)
86 #=========================================================================
87 # Check values and move one constrainted object
88 #=========================================================================
89 deltaX = deltaY = 10.
90 # rotate line, check that reference's line points are moved also
91 aLineBStartPointPrev = (aLineBStartPoint.x(), aLineBStartPoint.y())
92 aLineBEndPointPrev = (aLineBEndPoint.x(), aLineBEndPoint.y())
93 aSession.startOperation()
94 aLineAStartPoint.setValue(aLineAStartPoint.x() + deltaX,
95                           aLineAStartPoint.y() + deltaY)
96 aLineAEndPoint.setValue(aLineAEndPoint.x() - deltaX,
97                         aLineAEndPoint.y() - deltaY)
98 aSession.finishOperation()
99 checkCollinear(aSketchLineA, aSketchLineB)
100 #=========================================================================
101 # End of test
102 #=========================================================================