]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/Test/TestConstraintDistance.py
Salome HOME
Bring batch tests to valid state
[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-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 = modelAPI_CompositeFeature(aSketchCommonFeature)
49 aSketchFeatureData = aSketchFeature.data()
50 origin = geomDataAPI_Point(aSketchFeatureData.attribute("Origin"))
51 origin.setValue(0, 0, 0)
52 dirx = geomDataAPI_Dir(aSketchFeatureData.attribute("DirX"))
53 dirx.setValue(1, 0, 0)
54 diry = geomDataAPI_Dir(aSketchFeatureData.attribute("DirY"))
55 diry.setValue(0, 1, 0)
56 norm = geomDataAPI_Dir(aSketchFeatureData.attribute("Norm"))
57 norm.setValue(0, 0, 1)
58 aSession.finishOperation()
59 #=========================================================================
60 # Create a point and a line
61 #=========================================================================
62 aSession.startOperation()
63 aSketchPoint = aSketchFeature.addFeature("SketchPoint")
64 aSketchPointData = aSketchPoint.data()
65 aSketchPointCoords = geomDataAPI_Point2D(
66     aSketchPointData.attribute("PointCoordindates"))
67 aSketchPointCoords.setValue(50., 50.)
68 aSketchLine = aSketchFeature.addFeature("SketchLine")
69 aLineAStartPoint = geomDataAPI_Point2D(
70     aSketchLine.data().attribute("StartPoint"))
71 aLineAEndPoint = geomDataAPI_Point2D(aSketchLine.data().attribute("EndPoint"))
72 aLineAStartPoint.setValue(0., 25.)
73 aLineAEndPoint.setValue(100., 25.)
74 aSession.finishOperation()
75 #=========================================================================
76 # Make a constraint to keep the distance
77 #=========================================================================
78 assert (distance(aSketchPointCoords, aLineAStartPoint) != 25.)
79 aSession.startOperation()
80 aConstraint = aSketchFeature.addFeature("SketchConstraintDistance")
81 aConstraintData = aConstraint.data()
82 aDistance = aConstraintData.real("ConstraintValue")
83 refattrA = aConstraintData.refattr("ConstraintEntityA")
84 refattrB = aConstraintData.refattr("ConstraintEntityB")
85 assert (not aDistance.isInitialized())
86 assert (not refattrA.isInitialized())
87 assert (not refattrB.isInitialized())
88 aDistance.setValue(25.)
89 aLineResult = aSketchLine.firstResult()
90 assert (aLineResult is not None)
91 refattrA.setAttr(aSketchPointCoords)
92 refattrB.setAttr(aLineAStartPoint)
93 aSession.finishOperation()
94 assert (aDistance.isInitialized())
95 assert (refattrA.isInitialized())
96 assert (refattrB.isInitialized())
97 #=========================================================================
98 # Move line, check that distance is constant
99 #=========================================================================
100 assert (distance(aSketchPointCoords, aLineAStartPoint) == 25.)
101 aSession.startOperation()
102 aLineAStartPoint.setValue(0., 40.)
103 aLineAEndPoint.setValue(100., 40.)
104 aSession.finishOperation()
105 assert (distance(aSketchPointCoords, aLineAStartPoint) == 25.)
106 #=========================================================================
107 # TODO: improve test
108 # 1. remove constraint, move line's start point to
109 #    check that constraint are not applied
110 # 2. check constrained distance between:
111 #    * point and line
112 #    * two lines
113 #=========================================================================
114 #=========================================================================
115 # End of test
116 #=========================================================================