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