Salome HOME
Issue #803: Put all the python modules in the same python package newgeom
[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 distancePointPoint(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 def distancePointLine(point, line):
42     """
43     subroutine to calculate distance between point and line
44     result of calculated distance is has 10**-5 precision
45     """
46     aStartPoint = geomDataAPI_Point2D(line.attribute("StartPoint"))
47     aEndPoint = geomDataAPI_Point2D(line.attribute("EndPoint"))
48     # orthogonal direction
49     aDirX = -(aEndPoint.y() - aStartPoint.y())
50     aDirY = (aEndPoint.x() - aStartPoint.x())
51     aLen = math.sqrt(aDirX**2 + aDirY**2)
52     aDirX = aDirX / aLen
53     aDirY = aDirY / aLen
54     aVecX = point.x() - aStartPoint.x()
55     aVecY = point.y() - aStartPoint.y()
56     return round(math.fabs(aVecX * aDirX + aVecY * aDirY), 5)
57
58 aSession = ModelAPI_Session.get()
59 aDocument = aSession.moduleDocument()
60 #=========================================================================
61 # Creation of a sketch
62 #=========================================================================
63 aSession.startOperation()
64 aSketchCommonFeature = aDocument.addFeature("Sketch")
65 aSketchFeature = featureToCompositeFeature(aSketchCommonFeature)
66 origin = geomDataAPI_Point(aSketchFeature.attribute("Origin"))
67 origin.setValue(0, 0, 0)
68 dirx = geomDataAPI_Dir(aSketchFeature.attribute("DirX"))
69 dirx.setValue(1, 0, 0)
70 norm = geomDataAPI_Dir(aSketchFeature.attribute("Norm"))
71 norm.setValue(0, 0, 1)
72 aSession.finishOperation()
73 #=========================================================================
74 # Create a point and a line
75 #=========================================================================
76 aSession.startOperation()
77 aSketchPoint = aSketchFeature.addFeature("SketchPoint")
78 aSketchPointCoords = geomDataAPI_Point2D(
79     aSketchPoint.attribute("PointCoordindates"))
80 aSketchPointCoords.setValue(50., 50.)
81 aSketchLine = aSketchFeature.addFeature("SketchLine")
82 aLineAStartPoint = geomDataAPI_Point2D(aSketchLine.attribute("StartPoint"))
83 aLineAEndPoint = geomDataAPI_Point2D(aSketchLine.attribute("EndPoint"))
84 aLineAStartPoint.setValue(0., 25.)
85 aLineAEndPoint.setValue(100., 25.)
86 aSession.finishOperation()
87 #=========================================================================
88 # Make a constraint to keep the distance
89 #=========================================================================
90 PT_PT_DIST = 25.
91 aDist = distancePointPoint(aSketchPointCoords, aLineAStartPoint);
92 assert (aDist != PT_PT_DIST)
93 aSession.startOperation()
94 aConstraint = aSketchFeature.addFeature("SketchConstraintDistance")
95 aDistance = aConstraint.real("ConstraintValue")
96 refattrA = aConstraint.refattr("ConstraintEntityA")
97 refattrB = aConstraint.refattr("ConstraintEntityB")
98 assert (not aDistance.isInitialized())
99 assert (not refattrA.isInitialized())
100 assert (not refattrB.isInitialized())
101 aLineResult = aSketchLine.firstResult()
102 assert (aLineResult is not None)
103 # the following line is necessary to check automatic calculation of a distance
104 aConstraint.execute()
105 refattrA.setAttr(aSketchPointCoords)
106 refattrB.setAttr(aLineAStartPoint)
107 aSession.finishOperation()
108 # set flyout point then abort operation, after that check the Distance is correct
109 aSession.startOperation()
110 aFlyoutPoint = geomDataAPI_Point2D(aConstraint.attribute("ConstraintFlyoutValuePnt"))
111 aFlyoutPoint.setValue(50.0, 100.0)
112 aSession.abortOperation()
113 assert (refattrA.isInitialized())
114 assert (refattrB.isInitialized())
115 assert (aDistance.isInitialized())
116 assert math.fabs(aDistance.value() - aDist) < 1.e-4, "Distance values are different: {0} != {1}".format(aDistance.value(), aDist)
117 #=========================================================================
118 # Change distance value
119 #=========================================================================
120 aSession.startOperation()
121 aDistance.setValue(PT_PT_DIST)
122 aSession.finishOperation()
123 assert (math.fabs(distancePointPoint(aSketchPointCoords, aLineAStartPoint) - PT_PT_DIST) < 1.e-10)
124 #=========================================================================
125 # Move line, check that distance is constant
126 #=========================================================================
127 aSession.startOperation()
128 aLineAStartPoint.setValue(0., 40.)
129 aLineAEndPoint.setValue(100., 40.)
130 aSession.finishOperation()
131 assert (math.fabs(distancePointPoint(aSketchPointCoords, aLineAStartPoint) - PT_PT_DIST) < 1.e-10)
132 #=========================================================================
133 # Remove constraint, check the points are unconstrained now
134 #=========================================================================
135 aSession.startOperation()
136 aDocument.removeFeature(aConstraint)
137 aSession.finishOperation()
138 aSession.startOperation()
139 aSketchPointCoords.setValue(0., 0.)
140 aSession.finishOperation()
141 assert (math.fabs(distancePointPoint(aSketchPointCoords, aLineAStartPoint) - PT_PT_DIST) > 1.e-10)
142
143 #=========================================================================
144 # Add distance between point and line
145 #=========================================================================
146 PT_LINE_DIST = 50.
147 aDist = distancePointLine(aSketchPointCoords, aSketchLine)
148 aSession.startOperation()
149 aConstraint = aSketchFeature.addFeature("SketchConstraintDistance")
150 aDistance = aConstraint.real("ConstraintValue")
151 refattrA = aConstraint.refattr("ConstraintEntityA")
152 refattrB = aConstraint.refattr("ConstraintEntityB")
153 assert (not aDistance.isInitialized())
154 assert (not refattrA.isInitialized())
155 assert (not refattrB.isInitialized())
156 aLineResult = aSketchLine.firstResult()
157 assert (aLineResult is not None)
158 refattrA.setObject(aLineResult)
159 refattrB.setAttr(aSketchPointCoords)
160 aSession.finishOperation()
161 # set flyout point then abort operation, after that check the Distance is correct
162 aSession.startOperation()
163 aFlyoutPoint = geomDataAPI_Point2D(aConstraint.attribute("ConstraintFlyoutValuePnt"))
164 aFlyoutPoint.setValue(50.0, 100.0)
165 aSession.abortOperation()
166 assert (refattrA.isInitialized())
167 assert (refattrB.isInitialized())
168 assert (aDistance.isInitialized())
169 assert math.fabs(aDistance.value() - aDist) < 1.e-4, "Distance values are different: {0} != {1}".format(aDistance.value(), aDist)
170 #=========================================================================
171 # Change distance value
172 #=========================================================================
173 aSession.startOperation()
174 aDistance.setValue(PT_LINE_DIST)
175 aSession.finishOperation()
176 assert (math.fabs(distancePointLine(aSketchPointCoords, aSketchLine) - PT_LINE_DIST) < 1.e-10)
177 #=========================================================================
178 # Set distance between line boundaries
179 #=========================================================================
180 aSession.startOperation()
181 refattrA.setAttr(aLineAStartPoint)
182 refattrB.setAttr(aLineAEndPoint)
183 aSession.finishOperation()
184 assert (math.fabs(distancePointPoint(aLineAStartPoint, aLineAEndPoint) - PT_LINE_DIST) < 1.e-10)
185 #=========================================================================
186 # End of test
187 #=========================================================================
188
189 from salome.shaper import model
190 assert(model.checkPythonDump())