Salome HOME
Synchronize adm files
[modules/geom.git] / src / GEOM_SWIG / GEOM_TestMeasures.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2014  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, or (at your option) any later version.
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
24 def TestMeasureOperations (geompy, math):
25
26   p0   = geompy.MakeVertex(0 ,  0,  0)
27   p137 = geompy.MakeVertex(10, 30, 70)
28
29   box = geompy.MakeBoxTwoPnt(p0, p137)
30
31   p678 = geompy.MakeVertex(60, 70, 80)
32   p789 = geompy.MakeVertex(70, 80, 90)
33
34   cube = geompy.MakeBoxTwoPnt(p678, p789)
35
36   ####### PointCoordinates #######
37
38   Coords = geompy.PointCoordinates(p137)
39   if Coords[0] != 10 or Coords[1] != 30 or Coords[2] != 70:
40     print "Coordinates of p137 must be (10, 30, 70), but returned (", Coords[0], ", ", Coords[1], ", ", Coords[2], ")"
41
42   ####### CheckShape #######
43
44   (IsValid, err) = geompy.CheckShape(box, 0, 2)
45   if IsValid == 0:
46     geompy.PrintShapeError(box, err)
47     raise RuntimeError, "Invalid box created"
48   else:
49     print "\nBox is valid"
50
51   ####### Detect Self-intersections #######
52
53   [Face_1,Face_2] = geompy.SubShapes(box, [33, 23])
54   Translation_1 = geompy.MakeTranslation(Face_1, 5, -15, -40)
55   Compound_1 = geompy.MakeCompound([Face_2, Translation_1])
56   if geompy.CheckSelfIntersections(Compound_1) == True:
57     raise RuntimeError, "Existing self-intersection is not detected"
58
59   ####### WhatIs #######
60
61   Descr = geompy.WhatIs(box)
62   print "\nBox 10x30x70 description:"
63   print Descr
64
65   ####### NbShapes #######
66
67   NbSolids = geompy.NbShapes(box, geompy.ShapeType["SOLID"])
68   print "\nBox 10x30x70 quantity of solids:", NbSolids
69
70   ####### ShapeInfo #######
71
72   BoxInfo = geompy.ShapeInfo(box)
73   print "\nBox 10x30x70 shapes:"
74   print BoxInfo
75
76   ####### BasicProperties #######
77
78   Props = geompy.BasicProperties(box)
79   print "\nBox 10x30x70 Basic Properties:"
80   print " Wires length: ", Props[0]
81   print " Surface area: ", Props[1]
82   print " Volume      : ", Props[2]
83
84   dl = math.sqrt((Props[0] -   880)*(Props[0] -   880))
85   da = math.sqrt((Props[1] -  6200)*(Props[1] -  6200))
86   dv = math.sqrt((Props[2] - 21000)*(Props[2] - 21000))
87   #print "|Props[0] - 880| = ", dl
88
89   if dl > 1e-7 or da > 1e-7 or dv > 1e-7:
90     print "While must be:"
91     print " Wires length: ", 880
92     print " Surface area: ", 6200
93     print " Volume      : ", 21000
94
95   ####### BoundingBox #######
96
97   BB = geompy.BoundingBox(box)
98   print "\nBounding Box of box 10x30x70:"
99   print " Xmin = ", BB[0], ", Xmax = ", BB[1]
100   print " Ymin = ", BB[2], ", Ymax = ", BB[3]
101   print " Zmin = ", BB[4], ", Zmax = ", BB[5]
102   BB = geompy.MakeBoundingBox(box)
103   geompy.addToStudy(BB, "BoundingBox")
104
105   ####### Inertia #######
106
107   In = geompy.Inertia(box)
108   print "\nInertia matrix of box 10x30x70:"
109   print " (", In[0], ", ", In[1], ", ", In[2], ")"
110   print " (", In[3], ", ", In[4], ", ", In[5], ")"
111   print " (", In[6], ", ", In[7], ", ", In[8], ")"
112   print "Main moments of inertia of box 10x30x70:"
113   print " Ix = ", In[9], ", Iy = ", In[10], ", Iz = ", In[11]
114
115   ####### Tolerance #######
116
117   Toler = geompy.Tolerance(box)
118   print "\nBox 10x30x70 tolerance:"
119   print " Face min. tolerance: ", Toler[0]
120   print " Face max. tolerance: ", Toler[1]
121   print " Edge min. tolerance: ", Toler[2]
122   print " Edge max. tolerance: ", Toler[3]
123   print " Vertex min. tolerance: ", Toler[4]
124   print " Vertex max. tolerance: ", Toler[5]
125
126   ####### MakeCDG #######
127
128   pcdg = geompy.MakeCDG(box)
129   if pcdg is None:
130     raise RuntimeError, "MakeCDG(box) failed"
131   else:
132     print "\nCentre of gravity of box has been successfully obtained:"
133     Coords = geompy.PointCoordinates(pcdg)
134     print "(", Coords[0], ", ", Coords[1], ", ", Coords[2], ")"
135     if Coords[0] != 5 or Coords[1] != 15 or Coords[2] != 35:
136       print "But must be (5, 15, 35)"
137
138   ####### GetNormal #######
139
140   faces = geompy.SubShapeAllSortedCentres(box, geompy.ShapeType["FACE"])
141   face0 = faces[0]
142   vnorm = geompy.GetNormal(face0)
143   if vnorm is None:
144     raise RuntimeError, "GetNormal(face0) failed"
145   else:
146     geompy.addToStudy(face0, "Face0")
147     geompy.addToStudy(vnorm, "Normale to Face0")
148     print "\nNormale of face has been successfully obtained:"
149     #Coords = geompy.PointCoordinates(pcdg)
150     #print "(", Coords[0], ", ", Coords[1], ", ", Coords[2], ")"
151     #if Coords[0] != 5 or Coords[1] != 15 or Coords[2] != 35:
152     #  print "But must be (5, 15, 35)"
153
154   ####### MinDistance #######
155
156   MinDist = geompy.MinDistance(box, cube)
157
158   #print "\nMinimal distance between Box and Cube = ", MinDist[0]
159   #print "It is reached at points:"
160   #print " On Box  (", MinDist[1], ", ", MinDist[2], ", ", MinDist[3], ")"
161   #print " On Cube (", MinDist[4], ", ", MinDist[5], ", ", MinDist[6], ")"
162
163   print "\nMinimal distance between Box and Cube = ", MinDist
164
165   MinDistComps = geompy.MinDistanceComponents(box, cube)
166   print "\nMinimal distance between Box and Cube = ", MinDistComps[0]
167   print "Its components are  (", MinDistComps[1], ", ", MinDistComps[2], ", ", MinDistComps[3], ")"
168
169   # Get all closest points
170   [nbSols, listCoords] = geompy.ClosestPoints(box, cube)
171   for i in range(nbSols):
172     v1 = geompy.MakeVertex(listCoords[i*6 + 0], listCoords[i*6 + 1], listCoords[i*6 + 2])
173     v2 = geompy.MakeVertex(listCoords[i*6 + 3], listCoords[i*6 + 4], listCoords[i*6 + 5])
174
175     geompy.addToStudy(v1, 'MinDist_%d_on_Box'%(i+1))
176     geompy.addToStudy(v2, 'MinDist_%d_on_Cube'%(i+1))
177     pass
178
179   ####### Angle #######
180
181   OX  = geompy.MakeVectorDXDYDZ(10, 0,0)
182   OXY = geompy.MakeVectorDXDYDZ(10,10,0)
183
184   # in one plane
185   Angle = geompy.GetAngle(OX, OXY)
186
187   print "\nAngle between OX and OXY = ", Angle
188   if math.fabs(Angle - 45.0) > 1e-05:
189     print "  Error: returned angle is", Angle, "while must be 45.0"
190
191   Angle = geompy.GetAngleRadians(OX, OXY)
192
193   print "\nAngle between OX and OXY in radians = ", Angle
194   if math.fabs(Angle - math.pi/4) > 1e-05:
195     print "  Error: returned angle is", Angle, "while must be pi/4"
196     pass
197
198   # not in one plane
199   OXY_shift = geompy.MakeTranslation(OXY,10,-10,20)
200   Angle = geompy.GetAngle(OX, OXY_shift)
201
202   print "Angle between OX and OXY_shift = ", Angle
203   if math.fabs(Angle - 45.0) > 1e-05:
204     print "  Error: returned angle is", Angle, "while must be 45.0"
205
206   ####### Position (LCS) #######
207
208   Pos = geompy.GetPosition(box)
209   print "\nPosition(LCS) of box 10x30x70:"
210   print "Origin: (", Pos[0], ", ", Pos[1], ", ", Pos[2], ")"
211   print "Z axis: (", Pos[3], ", ", Pos[4], ", ", Pos[5], ")"
212   print "X axis: (", Pos[6], ", ", Pos[7], ", ", Pos[8], ")"
213
214   ####### KindOfShape #######
215
216   Kind = geompy.KindOfShape(box)
217   print "\nKindOfShape(box 10x30x70):", Kind
218   #if Kind[0] != geompy.kind.BOX:
219   #  print "Error: returned type is", Kind[0], "while must be", geompy.kind.BOX
220
221   Kind = geompy.KindOfShape(p137)
222   print "\nKindOfShape(p137):", Kind
223   if Kind[0] != geompy.kind.VERTEX:
224     print "  Error: returned type is", Kind[0], "while must be", geompy.kind.VERTEX
225   else:
226     dx = math.fabs(Kind[1] - 10)
227     dy = math.fabs(Kind[2] - 30)
228     dz = math.fabs(Kind[3] - 70)
229     if (dx + dy + dz) > 1e-5:
230       print "  Error: coordinates are (", Kind[1], ",", Kind[2], ",", Kind[3], ") while must be (10, 20, 30)"
231
232   pass