Salome HOME
Moving to GUI module
[modules/geom.git] / src / GEOM_SWIG / GEOM_TestMeasures.py
1 #  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 #
6 #  This library is free software; you can redistribute it and/or
7 #  modify it under the terms of the GNU Lesser General Public
8 #  License as published by the Free Software Foundation; either
9 #  version 2.1 of the License.
10 #
11 #  This library is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 #  Lesser General Public License for more details.
15 #
16 #  You should have received a copy of the GNU Lesser General Public
17 #  License along with this library; if not, write to the Free Software
18 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 #
20 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #
22 def TestMeasureOperations (geompy, math):
23
24   p0   = geompy.MakeVertex(0 ,  0,  0)
25   p137 = geompy.MakeVertex(10, 30, 70)
26
27   box = geompy.MakeBoxTwoPnt(p0, p137)
28
29   p678 = geompy.MakeVertex(60, 70, 80)
30   p789 = geompy.MakeVertex(70, 80, 90)
31
32   cube = geompy.MakeBoxTwoPnt(p678, p789)
33
34   ####### PointCoordinates #######
35
36   Coords = geompy.PointCoordinates(p137)
37   if Coords[0] != 10 or Coords[1] != 30 or Coords[2] != 70:
38     print "Coordinates of p137 must be (10, 30, 70), but returned (", Coords[0], ", ", Coords[1], ", ", Coords[2], ")"
39
40   ####### CheckShape #######
41
42   IsValid = geompy.CheckShape(box)
43   if IsValid == 0:
44     raise RuntimeError, "Invalid box created"
45   else:
46     print "\nBox is valid"
47
48   ####### WhatIs #######
49
50   Descr = geompy.WhatIs(box)
51   print "\nBox 10x30x70 description:"
52   print Descr
53
54   ####### BasicProperties #######
55
56   Props = geompy.BasicProperties(box)
57   print "\nBox 10x30x70 Basic Properties:"
58   print " Wires length: ", Props[0]
59   print " Surface area: ", Props[1]
60   print " Volume      : ", Props[2]
61
62   dl = math.sqrt((Props[0] -   880)*(Props[0] -   880))
63   da = math.sqrt((Props[1] -  6200)*(Props[1] -  6200))
64   dv = math.sqrt((Props[2] - 21000)*(Props[2] - 21000))
65   #print "|Props[0] - 880| = ", dl
66
67   if dl > 1e-7 or da > 1e-7 or dv > 1e-7:
68     print "While must be:"
69     print " Wires length: ", 880
70     print " Surface area: ", 6200
71     print " Volume      : ", 21000
72
73   ####### BoundingBox #######
74
75   BB = geompy.BoundingBox(box)
76   print "\nBounding Box of box 10x30x70:"
77   print " Xmin = ", BB[0], ", Xmax = ", BB[1]
78   print " Ymin = ", BB[2], ", Ymax = ", BB[3]
79   print " Zmin = ", BB[4], ", Zmax = ", BB[5]
80
81   ####### Inertia #######
82
83   In = geompy.Inertia(box)
84   print "\nInertia matrix of box 10x30x70:"
85   print " (", In[0], ", ", In[1], ", ", In[2], ")"
86   print " (", In[3], ", ", In[4], ", ", In[5], ")"
87   print " (", In[6], ", ", In[7], ", ", In[8], ")"
88   print "Main moments of inertia of box 10x30x70:"
89   print " Ix = ", In[9], ", Iy = ", In[10], ", Iz = ", In[11]
90
91   ####### Tolerance #######
92
93   Toler = geompy.Tolerance(box)
94   print "\nBox 10x30x70 tolerance:"
95   print " Face min. tolerance: ", Toler[0]
96   print " Face max. tolerance: ", Toler[1]
97   print " Edge min. tolerance: ", Toler[2]
98   print " Edge max. tolerance: ", Toler[3]
99   print " Vertex min. tolerance: ", Toler[4]
100   print " Vertex max. tolerance: ", Toler[5]
101
102   ####### MakeCDG #######
103
104   pcdg = geompy.MakeCDG(box)
105   if pcdg is None:
106     raise RuntimeError, "MakeCDG(box) failed"
107   else:
108     print "\nCentre of gravity of box has been successfully obtained:"
109     Coords = geompy.PointCoordinates(pcdg)
110     print "(", Coords[0], ", ", Coords[1], ", ", Coords[2], ")"
111     if Coords[0] != 5 or Coords[1] != 15 or Coords[2] != 35:
112       print "But must be (5, 15, 35)"
113
114   ####### GetNormal #######
115
116   faces = geompy.SubShapeAllSorted(box, geompy.ShapeType["FACE"])
117   face0 = faces[0]
118   vnorm = geompy.GetNormal(face0)
119   if vnorm is None:
120     raise RuntimeError, "GetNormal(face0) failed"
121   else:
122     geompy.addToStudy(face0, "Face0")
123     geompy.addToStudy(vnorm, "Normale to Face0")
124     print "\nNormale of face has been successfully obtained:"
125     #Coords = geompy.PointCoordinates(pcdg)
126     #print "(", Coords[0], ", ", Coords[1], ", ", Coords[2], ")"
127     #if Coords[0] != 5 or Coords[1] != 15 or Coords[2] != 35:
128     #  print "But must be (5, 15, 35)"
129
130   ####### MinDistance #######
131
132   MinDist = geompy.MinDistance(box, cube)
133
134   #print "\nMinimal distance between Box and Cube = ", MinDist[0]
135   #print "It is reached at points:"
136   #print " On Box  (", MinDist[1], ", ", MinDist[2], ", ", MinDist[3], ")"
137   #print " On Cube (", MinDist[4], ", ", MinDist[5], ", ", MinDist[6], ")"
138
139   print "\nMinimal distance between Box and Cube = ", MinDist
140
141   MinDistComps = geompy.MinDistanceComponents(box, cube)
142   print "\nMinimal distance between Box and Cube = ", MinDistComps[0]
143   print "Its components are  (", MinDistComps[1], ", ", MinDistComps[2], ", ", MinDistComps[3], ")"
144
145   ####### Angle #######
146
147   OX  = geompy.MakeVectorDXDYDZ(10, 0,0)
148   OXY = geompy.MakeVectorDXDYDZ(10,10,0)
149
150   # in one plane
151   Angle = geompy.GetAngle(OX, OXY)
152
153   print "\nAngle between OX and OXY = ", Angle
154   if math.fabs(Angle - 45.0) > 1e-05:
155     print "  Error: returned angle is", Angle, "while must be 45.0"
156
157   Angle = geompy.GetAngleRadians(OX, OXY)
158
159   print "\nAngle between OX and OXY in radians = ", Angle
160   if math.fabs(Angle - math.pi/4) > 1e-05:
161     print "  Error: returned angle is", Angle, "while must be pi/4"
162     pass
163
164   # not in one plane
165   OXY_shift = geompy.MakeTranslation(OXY,10,-10,20)
166   Angle = geompy.GetAngle(OX, OXY_shift)
167
168   print "Angle between OX and OXY_shift = ", Angle
169   if math.fabs(Angle - 45.0) > 1e-05:
170     print "  Error: returned angle is", Angle, "while must be 45.0"
171
172   ####### Position (LCS) #######
173
174   Pos = geompy.GetPosition(box)
175   print "\nPosition(LCS) of box 10x30x70:"
176   print "Origin: (", Pos[0], ", ", Pos[1], ", ", Pos[2], ")"
177   print "Z axis: (", Pos[3], ", ", Pos[4], ", ", Pos[5], ")"
178   print "X axis: (", Pos[6], ", ", Pos[7], ", ", Pos[8], ")"
179
180   ####### KindOfShape #######
181
182   Kind = geompy.KindOfShape(box)
183   print "\nKindOfShape(box 10x30x70):", Kind
184   #if Kind[0] != geompy.kind.BOX:
185   #  print "Error: returned type is", Kind[0], "while must be", geompy.kind.BOX
186
187   Kind = geompy.KindOfShape(p137)
188   print "\nKindOfShape(p137):", Kind
189   if Kind[0] != geompy.kind.VERTEX:
190     print "  Error: returned type is", Kind[0], "while must be", geompy.kind.VERTEX
191   else:
192     dx = math.fabs(Kind[1] - 10)
193     dy = math.fabs(Kind[2] - 30)
194     dz = math.fabs(Kind[3] - 70)
195     if (dx + dy + dz) > 1e-5:
196       print "  Error: coordinates are (", Kind[1], ",", Kind[2], ",", Kind[3], ") while must be (10, 20, 30)"
197
198   pass