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