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