Salome HOME
Merge branch 'master' into Dev_1.1.0
[modules/shaper.git] / src / SketchPlugin / Test / TestConstraintDistance.py
1 """
2     TestConstraintDistance.py
3     Unit test of SketchPlugin_ConstraintDistance 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_ConstraintDistance
14         static const std::string MY_CONSTRAINT_DISTANCE_ID("SketchConstraintDistance");
15         data()->addAttribute(SketchPlugin_Constraint::VALUE(),    ModelAPI_AttributeDouble::typeId());
16         data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::typeId());
17         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
18         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
19         
20     
21 """
22 from GeomDataAPI import *
23 from ModelAPI import *
24 import math
25 #=========================================================================
26 # Initialization of the test
27 #=========================================================================
28
29 __updated__ = "2014-10-28"
30
31
32 def distance(pointA, pointB):
33     """
34     subroutine to calculate distance between two points
35     result of calculated distance is has 10**-5 precision
36     """
37     xdiff = math.pow((pointA.x() - pointB.x()), 2)
38     ydiff = math.pow((pointA.y() - pointB.y()), 2)
39     return round(math.sqrt(xdiff + ydiff), 5)
40
41 aSession = ModelAPI_Session.get()
42 aDocument = aSession.moduleDocument()
43 #=========================================================================
44 # Creation of a sketch
45 #=========================================================================
46 aSession.startOperation()
47 aSketchCommonFeature = aDocument.addFeature("Sketch")
48 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
49 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
50 origin.setValue(0, 0, 0)
51 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
52 dirx.setValue(1, 0, 0)
53 diry = geomDataAPI_Dir(aSketchFeature.attribute("DirY"))
54 diry.setValue(0, 1, 0)
55 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
56 norm.setValue(0, 0, 1)
57 aSession.finishOperation()
58 #=========================================================================
59 # Create a point and a line
60 #=========================================================================
61 aSession.startOperation()
62 aSketchPoint = aSketchFeature.addFeature("SketchPoint")
63 aSketchPointCoords = geomDataAPI_Point2D(
64     aSketchPoint.attribute("PointCoordindates"))
65 aSketchPointCoords.setValue(50., 50.)
66 aSketchLine = aSketchFeature.addFeature("SketchLine")
67 aLineAStartPoint = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
68 aLineAEndPoint = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
69 aLineAStartPoint.setValue(0., 25.)
70 aLineAEndPoint.setValue(100., 25.)
71 aSession.finishOperation()
72 #=========================================================================
73 # Make a constraint to keep the distance
74 #=========================================================================
75 assert (distance(aSketchPointCoords, aLineAStartPoint) != 25.)
76 aSession.startOperation()
77 aConstraint = aSketchFeature.addFeature("SketchConstraintDistance")
78 aDistance = aConstraint.real("ConstraintValue")
79 refattrA = aConstraint.refattr("ConstraintEntityA")
80 refattrB = aConstraint.refattr("ConstraintEntityB")
81 assert (not aDistance.isInitialized())
82 assert (not refattrA.isInitialized())
83 assert (not refattrB.isInitialized())
84 aDistance.setValue(25.)
85 aLineResult = aSketchLine.firstResult()
86 assert (aLineResult is not None)
87 refattrA.setAttr(aSketchPointCoords)
88 refattrB.setAttr(aLineAStartPoint)
89 aSession.finishOperation()
90 assert (aDistance.isInitialized())
91 assert (refattrA.isInitialized())
92 assert (refattrB.isInitialized())
93 #=========================================================================
94 # Move line, check that distance is constant
95 #=========================================================================
96 assert (distance(aSketchPointCoords, aLineAStartPoint) == 25.)
97 aSession.startOperation()
98 aLineAStartPoint.setValue(0., 40.)
99 aLineAEndPoint.setValue(100., 40.)
100 aSession.finishOperation()
101 assert (distance(aSketchPointCoords, aLineAStartPoint) == 25.)
102 #=========================================================================
103 # TODO: improve test
104 # 1. remove constraint, move line's start point to
105 #    check that constraint are not applied
106 # 2. check constrained distance between:
107 #    * point and line
108 #    * two lines
109 #=========================================================================
110 #=========================================================================
111 # End of test
112 #=========================================================================