Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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::type());
16         data()->addAttribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT(), GeomDataAPI_Point2D::type());
17         data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::type());
18         data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::type());
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-07-29"
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 aPluginManager = ModelAPI_PluginManager.get()
42 aDocument = aPluginManager.rootDocument()
43 #=========================================================================
44 # Creation of a sketch
45 #=========================================================================
46 aDocument.startOperation()
47 aSketchFeature = aDocument.addFeature("Sketch")
48 aSketchFeatureData = aSketchFeature.data()
49 origin = geomDataAPI_Point(aSketchFeatureData.attribute("Origin"))
50 origin.setValue(0, 0, 0)
51 dirx = geomDataAPI_Dir(aSketchFeatureData.attribute("DirX"))
52 dirx.setValue(1, 0, 0)
53 diry = geomDataAPI_Dir(aSketchFeatureData.attribute("DirY"))
54 diry.setValue(0, 1, 0)
55 norm = geomDataAPI_Dir(aSketchFeatureData.attribute("Norm"))
56 norm.setValue(0, 0, 1)
57 aDocument.finishOperation()
58 #=========================================================================
59 # Create a point and a line
60 #=========================================================================
61 aDocument.startOperation()
62 aSketchReflist = aSketchFeatureData.reflist("Features")
63 aSketchPoint = aDocument.addFeature("SketchPoint")
64 aSketchReflist.append(aSketchPoint)
65 aSketchPointData = aSketchPoint.data()
66 aSketchPointCoords = geomDataAPI_Point2D(
67     aSketchPointData.attribute("PointCoordindates"))
68 aSketchPointCoords.setValue(50., 50.)
69 aSketchLine = aDocument.addFeature("SketchLine")
70 aSketchReflist.append(aSketchLine)
71 aLineAStartPoint = geomDataAPI_Point2D(
72     aSketchLine.data().attribute("StartPoint"))
73 aLineAEndPoint = geomDataAPI_Point2D(aSketchLine.data().attribute("EndPoint"))
74 aLineAStartPoint.setValue(0., 25.)
75 aLineAEndPoint.setValue(100., 25.)
76 aDocument.finishOperation()
77 #=========================================================================
78 # Make a constraint to keep the distance
79 #=========================================================================
80 assert (distance(aSketchPointCoords, aLineAStartPoint) != 25.)
81 aDocument.startOperation()
82 aConstraint = aDocument.addFeature("SketchConstraintDistance")
83 aSketchReflist.append(aConstraint)
84 aConstraintData = aConstraint.data()
85 aDistance = aConstraintData.real("ConstraintValue")
86 refattrA = aConstraintData.refattr("ConstraintEntityA")
87 refattrB = aConstraintData.refattr("ConstraintEntityB")
88 assert (not aDistance.isInitialized())
89 assert (not refattrA.isInitialized())
90 assert (not refattrB.isInitialized())
91 aDistance.setValue(25.)
92 aLineResult = aSketchLine.firstResult()
93 assert (aLineResult is not None)
94 refattrA.setAttr(aSketchPointCoords)
95 refattrB.setAttr(aLineAStartPoint)
96 aDocument.finishOperation()
97 assert (aDistance.isInitialized())
98 assert (refattrA.isInitialized())
99 assert (refattrB.isInitialized())
100 #=========================================================================
101 # Move line, check that distance is constant
102 #=========================================================================
103 assert (distance(aSketchPointCoords, aLineAStartPoint) == 25.)
104 aDocument.startOperation()
105 aLineAStartPoint.setValue(0., 40.)
106 aLineAEndPoint.setValue(100., 40.)
107 aDocument.finishOperation()
108 assert (distance(aSketchPointCoords, aLineAStartPoint) == 25.)
109 #=========================================================================
110 # TODO: improve test
111 # 1. remove constraint, move line's start point to
112 #    check that constraint are not applied
113 # 2. check constrained distance between:
114 #    * point and line
115 #    * two lines
116 #=========================================================================
117 #=========================================================================
118 # End of test
119 #=========================================================================