]> SALOME platform Git repositories - modules/shaper.git/blob - src/PrimitivesPlugin/Test/UnitTestBox.py
Salome HOME
Tests fix.
[modules/shaper.git] / src / PrimitivesPlugin / Test / UnitTestBox.py
1 """
2     UnitTestBox.py
3     Unit Test of PrimitivesPlugin_Box class
4
5 class PrimitivesPlugin_Box : public ModelAPI_Feature
6     static const std::string MY_BOX_ID("Box");
7     static const std::string METHOD_ATTR("CreationMethod");
8     static const std::string MY_POINT_FIRST("FirstPoint");
9     static const std::string MY_POINT_SECOND("SecondPoint");
10     static const std::string MY_DX("dx");
11     static const std::string MY_DY("dy");
12     static const std::string MY_DZ("dz");
13
14     data()->addAttribute(METHOD(), ModelAPI_AttributeString::typeId());
15     data()->addAttribute(POINT_FIRST(), ModelAPI_AttributeSelection::typeId());
16     data()->addAttribute(POINT_SECOND(), ModelAPI_AttributeSelection::typeId());
17     data()->addAttribute(DX(), ModelAPI_AttributeDouble::typeId());
18     data()->addAttribute(DY(), ModelAPI_AttributeDouble::typeId());
19     data()->addAttribute(DZ(), ModelAPI_AttributeDouble::typeId());
20
21 """
22
23 #=========================================================================
24 # Initialization of the test
25 #=========================================================================
26 from ModelAPI import *
27 from GeomDataAPI import *
28 from GeomAlgoAPI import *
29 from GeomAPI import *
30 import math
31
32 __updated__ = "2016-01-04"
33
34 aSession = ModelAPI_Session.get()
35 aDocument = aSession.moduleDocument()
36 # Create a part for creation of a box
37 aSession.startOperation()
38 aPartFeature = aDocument.addFeature("Part")
39 aSession.finishOperation()
40 assert (len(aPartFeature.results()) == 1)
41
42 aPartResult = modelAPI_ResultPart(aPartFeature.firstResult())
43 aPart = aPartResult.partDoc()
44 #=========================================================================
45 # Creation of a Box by coordinates
46 #=========================================================================
47 aSession.startOperation()
48 aBoxBy3Dims = aPart.addFeature("Box")
49 assert (aBoxBy3Dims.getKind() == "Box")
50
51 aBoxBy3Dims.string("CreationMethod").setValue("BoxByDimensions")
52 aBoxBy3Dims.real("dx").setValue(1.6)
53 aBoxBy3Dims.real("dy").setValue(1.6)
54 aBoxBy3Dims.real("dz").setValue(1.6)
55 aBoxBy3Dims.execute()
56
57 # Check box results
58 assert (len(aBoxBy3Dims.results()) > 0)
59 aBoxResult = modelAPI_ResultBody(aBoxBy3Dims.firstResult())
60 assert (aBoxResult is not None)
61
62 # Check box volume
63 aRefVolume = 1.6 * 1.6 * 1.6
64 aResVolume = GeomAlgoAPI_ShapeTools_volume(aBoxResult.shape())
65 assert (math.fabs(aResVolume - aRefVolume) < 10 ** -5)
66
67
68 #Check the naming by selecting a face and making a plane out of it
69 aPlaneTop = aPart.addFeature("Plane")
70 assert(aPlaneTop.getKind() == "Plane")
71 aPlaneTop.string("creation_method").setValue("by_other_plane")
72 aSelectionAttr = aPlaneTop.selection("plane")
73 aSelectionAttr.selectSubShape("face", "Box_1_1/Top_1")
74 aPlaneTop.string("by_other_plane_option").setValue("by_distance_from_other")
75 aPlaneTop.real("distance").setValue(0.4)
76 aPlaneTop.execute()
77
78 #The face should be at 1.6, so the plane should be at 1.6 + 0.4 = 2.
79 aRefPlaneTopLocation = 2.
80
81 #location() is a method from GeomAPI_Face that returns a GeomAPI_Pnt, from which we can extract the z coordinate
82 aPlaneTopResult = aPlaneTop.firstResult()
83 aPlaneTopFace = GeomAPI_Face(aPlaneTopResult.shape())
84 aPlaneTestLocation = aPlaneTopFace.getPlane()
85 aPlaneLocation = aPlaneTestLocation.location().z()
86 assert(math.fabs(aPlaneLocation - aRefPlaneTopLocation) < 10 ** -5)
87
88 aSession.finishOperation()
89
90 #=========================================================================
91 # Creation of a Box by two points
92 #=========================================================================
93
94 aSession.startOperation()
95
96 #Create two points
97 aPoint1 = aPart.addFeature("Point")
98 aPoint2 = aPart.addFeature("Point")
99 assert(aPoint1.getKind() == "Point")
100 assert(aPoint2.getKind() == "Point")
101 # aPoint1.string("creation_method").setValue("by_xyz")
102 aPoint1.real("x").setValue(2.0)
103 aPoint1.real("y").setValue(2.0)
104 aPoint1.real("z").setValue(2.0)
105 # aPoint2.string("creation_method").setValue("by_xyz")
106 aPoint2.real("x").setValue(2.5)
107 aPoint2.real("y").setValue(2.5)
108 aPoint2.real("z").setValue(2.5)
109 aPoint1.execute()
110 aPoint2.execute()
111 aPoint1Result = aPoint1.firstResult()
112 aPoint2Result = aPoint2.firstResult()
113 aPoint1Vertex = aPoint1Result.shape()
114 aPoint2Vertex = aPoint2Result.shape()
115
116 aBoxBy2Pts = aPart.addFeature("Box")
117 assert (aBoxBy2Pts.getKind() == "Box")
118 aBoxBy2Pts.string("CreationMethod").setValue("BoxByTwoPoints")
119 aBoxBy2Pts.selection("FirstPoint").setValue(aPoint1Result, aPoint1Vertex)
120 aBoxBy2Pts.selection("SecondPoint").setValue(aPoint2Result, aPoint2Vertex)
121 aBoxBy2Pts.execute()
122
123 # Check box volume
124 aBoxResult2 = modelAPI_ResultBody(aBoxBy2Pts.firstResult())
125 aRefVolume2 = 0.5 * 0.5 * 0.5
126 aResVolume2 = GeomAlgoAPI_ShapeTools_volume(aBoxResult2.shape())
127 assert (math.fabs(aResVolume2 - aRefVolume2) < 10 ** -5)
128
129 #Check the naming by selecting a face and making a plane out of it
130 aPlaneRight = aPart.addFeature("Plane")
131 assert(aPlaneRight.getKind() == "Plane")
132 aPlaneRight.string("creation_method").setValue("by_other_plane")
133 aSelectionAttr = aPlaneRight.selection("plane")
134 aSelectionAttr.selectSubShape("face", "Box_2_1/Right_1")
135 aPlaneRight.string("by_other_plane_option").setValue("by_distance_from_other")
136 aPlaneRight.real("distance").setValue(0.5)
137 aPlaneRight.execute()
138
139 #The face should be at 2.5, so the plane should be at 2.5 + 0.5 = 3.
140 aRefPlaneRightLocation = 3.
141
142 #location() is a method from GeomAPI_Face that returns a GeomAPI_Pnt,
143 #from which we can extract the y coordinate
144 aPlaneRightResult = aPlaneRight.firstResult()
145 aPlaneRightFace = GeomAPI_Face(aPlaneRightResult.shape())
146 aPlaneTestLocation = aPlaneRightFace.getPlane()
147 aPlaneLocation = aPlaneTestLocation.location().y()
148 assert(math.fabs(aPlaneLocation - aRefPlaneRightLocation) < 10 ** -5)
149
150 aSession.finishOperation()
151
152 #=========================================================================
153 # End of test
154 #=========================================================================