Salome HOME
75bce1b5a10c6d38f5983690bd36fcc35da026b5
[modules/geom.git] / src / GEOM_SWIG / GEOM_Spanner.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2023  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 #  File   : GEOM_Spanner.py
24 #  Author : Julia DOROVSKIKH
25 #  Module : GEOM
26 #
27 # ! Please, if you edit this example file, update also
28 # ! GEOM_SRC/doc/salome/gui/GEOM/input/tui_test_spanner.doc
29 # ! as some sequences of symbols from this example are used during
30 # ! documentation generation to identify certain places of this file
31 #
32 ############# MakeSpanner #############
33 def MakeSpanner (geompy, math, isBlocksTest = 0, isMeshTest = 0, smesh = None):
34
35   ### Variables ###
36
37   th = 4.0
38
39   ### Basic points and directions ###
40
41   p0  = geompy.MakeVertex(0., 0., 0.)
42   pth = geompy.MakeVertex(0., 0., th)
43
44   vx = geompy.MakeVectorDXDYDZ(1., 0., 0.)
45   vy = geompy.MakeVectorDXDYDZ(0., 1., 0.)
46   vz = geompy.MakeVectorDXDYDZ(0., 0., 1.)
47
48   vxy = geompy.MakeVectorDXDYDZ(1., 1., 0.)
49
50   ### Block 1 ###
51
52   p_10_0  = geompy.MakeVertex(10.,  0., 0.)
53   p_10_25 = geompy.MakeVertex(10., 25., 0.)
54   p_0_35  = geompy.MakeVertex( 0., 35., 0.)
55   p_0_10  = geompy.MakeVertex( 0., 10., 0.)
56
57   Face11 = geompy.MakeQuad4Vertices(p_10_0, p_10_25, p_0_10, p_0_35);
58   id_face11 = geompy.addToStudy(Face11, "1 Face for Block 1")
59
60   Face12 = geompy.MakeTranslationTwoPoints(Face11, p0, pth)
61   id_face12 = geompy.addToStudy(Face12, "2 Face for Block 1")
62
63   Block1 = geompy.MakeHexa2Faces(Face11, Face12)
64   id_block1 = geompy.addToStudy(Block1, "Block 1")
65
66   ### Block 2 ###
67
68   p_15_0  = geompy.MakeVertex(15.,  0., 0.)
69   p_15_20 = geompy.MakeVertex(15., 20., 0.)
70
71   Edge21 = geompy.MakeEdge(p_15_0, p_15_20)
72   id_edge21 = geompy.addToStudy(Edge21, "1 Edge for Block 2")
73
74   Edge22 = geompy.GetEdge(Block1, p_10_0, p_10_25)
75   id_edge22 = geompy.addToStudy(Edge22, "2 Edge for Block 2")
76
77   Face21 = geompy.MakeQuad2Edges(Edge21, Edge22);
78   id_face21 = geompy.addToStudy(Face21, "1 Face for Block 2")
79
80   Face22 = geompy.MakeTranslationTwoPoints(Face21, p0, pth)
81   id_face22 = geompy.addToStudy(Face22, "2 Face for Block 2")
82
83   Block2 = geompy.MakeHexa2Faces(Face21, Face22)
84   id_block2 = geompy.addToStudy(Block2, "Block 2")
85
86   ### Block 3 ###
87
88   p_15_10_1 = geompy.MakeVertex(15., 10., 1.)
89
90   Face31 = geompy.GetFaceNearPoint(Block2, p_15_10_1)
91   id_face31 = geompy.addToStudy(Face31, "1 Face for Block 3")
92
93   Prism1 = geompy.MakePrismVecH(Face31, vx, 5);
94   id_prism1 = geompy.addToStudy(Prism1, "Prism to be Block 3")
95
96   nbFaces = geompy.ShapesOp.NumberOfFaces(Prism1)
97   if nbFaces == 6:
98     print("Prism 1 is a hexahedral solid")
99   else:
100     print("Prism 1 is not a hexahedral solid")
101
102   Prism1_faces = geompy.SubShapeAllSortedCentres(Prism1, geompy.ShapeType["FACE"])
103   ii = 1
104   for aFace in  Prism1_faces:
105     name = geompy.SubShapeName(aFace, Prism1)
106     Id_Face = geompy.addToStudyInFather(Prism1, aFace, name)
107     ii = ii + 1
108
109   Block3 = geompy.MakeHexa(Prism1_faces[0], Prism1_faces[1],
110                            Prism1_faces[2], Prism1_faces[3],
111                            Prism1_faces[4], Prism1_faces[5]);
112   id_block3 = geompy.addToStudy(Block3, "Block 3")
113
114   ### Block 4 ###
115
116   Face41 = geompy.GetOppositeFace(Block3, Face31)
117   id_face41 = geompy.addToStudy(Face41, "1 Face for Block 4")
118
119   p_25_5  = geompy.MakeVertex(25.,  5., 0.)
120   p_25_20 = geompy.MakeVertex(25., 20., 0.)
121
122   Edge41 = geompy.MakeEdge(p_25_5, p_25_20)
123   id_edge41 = geompy.addToStudy(Edge41, "1 Edge for Block 4")
124
125   p_25_5_2  = geompy.MakeVertex(25.,  5., th)
126   p_25_20_2 = geompy.MakeVertex(25., 20., th)
127
128   Edge42 = geompy.MakeEdge(p_25_5_2, p_25_20_2)
129   id_edge42 = geompy.addToStudy(Edge42, "2 Edge for Block 4")
130
131   Edge43 = geompy.MakeEdge(p_25_5, p_25_5_2)
132   id_edge43 = geompy.addToStudy(Edge43, "3 Edge for Block 4")
133
134   Edge44 = geompy.MakeEdge(p_25_20, p_25_20_2)
135   id_edge44 = geompy.addToStudy(Edge44, "4 Edge for Block 4")
136
137   Face42 = geompy.MakeQuad(Edge41, Edge42, Edge43, Edge44)
138   id_face42 = geompy.addToStudy(Face42, "2 Face for Block 4")
139
140   Block4 = geompy.MakeHexa2Faces(Face41, Face42)
141   id_block4 = geompy.addToStudy(Block4, "Block 4")
142
143   ### Block 5 ###
144
145   p_25_30   = geompy.MakeVertex(25., 30., 0.)
146   p_25_30_1 = geompy.MakeVertex(25., 30., 1)
147   axis_rot = geompy.MakeVector(p_25_30, p_25_30_1)
148
149   Face51 = Face42
150   Face52 = geompy.MakeRotation(Face51, axis_rot, math.pi/2.)
151   id_face52 = geompy.addToStudy(Face52, "2 Face for Block 5")
152
153   Block5 = geompy.MakeHexa2Faces(Face51, Face52)
154   id_block5 = geompy.addToStudy(Block5, "Block 5")
155
156   ### Compound of blocks 1 - 4 ###
157
158   Comp1234 = geompy.MakeCompound([Block1, Block2, Block3, Block4])
159   Glue1234 = geompy.MakeGlueFaces(Comp1234, 1e-5)
160   id_glue1234 = geompy.addToStudy(Glue1234, "Glue 1234")
161
162   plane_mirror = geompy.MakePlane(p_25_30, vxy, 100.)
163   #id_plane_mirror = geompy.addToStudy(plane_mirror, "Plane for mirror")
164
165   Mirror1 = geompy.MakeMirrorByPlane(Glue1234, plane_mirror)
166   id_mirror1 = geompy.addToStudy(Mirror1, "Mirror 1")
167
168   ### The second end ###
169
170   p_18_0 = geompy.MakeVertex(18., 0., 0.)
171   p_20_0 = geompy.MakeVertex(20., 0., 0.)
172   p_40_0 = geompy.MakeVertex(40., 0., 0.)
173
174   Edge1s = geompy.MakeEdge(p_20_0, p_40_0)
175   id_edge1s = geompy.addToStudy(Edge1s, "1 Edge for Block 1s")
176
177   Edge2s = geompy.MakeRotation(Edge1s, vz, math.pi/12.)
178   id_edge2s = geompy.addToStudy(Edge2s, "2 Edge for Block 1s")
179
180   p_18_0_r = geompy.MakeRotation(p_18_0, vz, math.pi/24.)
181   p_20_0_r = geompy.MakeRotation(p_20_0, vz, math.pi/12.)
182   p_40_0_r = geompy.MakeRotation(p_40_0, vz, math.pi/12.)
183
184   Edge3s = geompy.MakeEdge(p_40_0, p_40_0_r)
185   id_edge3s = geompy.addToStudy(Edge3s, "3 Edge for Block 1s")
186
187   Edge4s = geompy.MakeArc(p_20_0, p_18_0_r, p_20_0_r)
188   id_edge4s = geompy.addToStudy(Edge4s, "4 Edge for Block 1s")
189
190   Face1s = geompy.MakeQuad(Edge1s, Edge2s, Edge3s, Edge4s)
191   id_face1s = geompy.addToStudy(Face1s, "1 Face for Block 1s")
192
193   Block1s = geompy.MakePrismVecH(Face1s, vz, th);
194   id_block1s = geompy.addToStudy(Block1s, "Block 1s")
195
196   p_30_0_1 = geompy.MakeVertex(30., 0., 1.)
197
198   Face2s = geompy.GetFaceNearPoint(Block1s, p_30_0_1)
199   id_face2s = geompy.addToStudy(Face2s, "2 Face of Block 1s")
200
201   f2s_ind = geompy.LocalOp.GetSubShapeIndex(Block1s, Face2s)
202
203   Block1s_tr1 = geompy.MakeMultiTransformation1D(Block1s, f2s_ind, 0, 24)
204   id_block1s_tr1 = geompy.addToStudy(Block1s_tr1, "Multi-transformed Block 1s")
205
206   p_60_150_0 = geompy.MakeVertex(60., -150., 0.)
207   Gear = geompy.MakeTranslationTwoPoints(Block1s_tr1, p0, p_60_150_0)
208   id_gear = geompy.addToStudy(Gear, "Gear")
209
210   ### Link two parts ###
211
212   ### Linking Block 1 ###
213
214   p_55_0_1 = geompy.MakeVertex(55., 0., 1.)
215   p_55_110_1 = geompy.MakeVertex(55., -110., 1.)
216
217   Face11l = geompy.GetFaceNearPoint(Block5, p_55_0_1)
218   id_face11l = geompy.addToStudy(Face11l, "1 Face for Linking Block 1")
219
220   Face12l = geompy.GetFaceNearPoint(Gear, p_55_110_1)
221   id_face12l = geompy.addToStudy(Face12l, "2 Face for Linking Block 1")
222
223   Block1l = geompy.MakeHexa2Faces(Face11l, Face12l)
224   id_block1l = geompy.addToStudy(Block1l, "Linking Block 1")
225
226   ### Linking Block 2 ###
227
228   p_25_0_1 = geompy.MakeVertex(25., 0., 1.)
229   p_45_110_1 = geompy.MakeVertex(45., -110., 1.)
230
231   Face21l = geompy.GetFaceNearPoint(Block4, p_25_0_1)
232   id_face21l = geompy.addToStudy(Face21l, "1 Face for Linking Block 2")
233
234   Face22l = geompy.GetFaceNearPoint(Gear, p_45_110_1)
235   id_face22l = geompy.addToStudy(Face22l, "2 Face for Linking Block 2")
236
237   Block2l = geompy.MakeHexa2Faces(Face21l, Face22l)
238   id_block2l = geompy.addToStudy(Block2l, "Linking Block 2")
239
240   ### Linking Block 3 ###
241
242   p_55_30_1 = geompy.MakeVertex(55., 30., 1.)
243   p_65_110_1 = geompy.MakeVertex(65., -110., 1.)
244
245   Face31l = geompy.GetFaceNearPoint(Mirror1, p_55_30_1)
246   id_face31l = geompy.addToStudy(Face31l, "1 Face for Linking Block 3")
247
248   Face32l = geompy.GetFaceNearPoint(Gear, p_65_110_1)
249   id_face32l = geompy.addToStudy(Face32l, "2 Face for Linking Block 3")
250
251   Block3l = geompy.MakeHexa2Faces(Face31l, Face32l)
252   id_block3l = geompy.addToStudy(Block3l, "Linking Block 3")
253
254   ### Handle ###
255
256   CompLB = geompy.MakeCompound([Block1l, Block2l, Block3l])
257   Handle = geompy.MakeGlueFaces(CompLB, 1e-5)
258   id_handle = geompy.addToStudy(Handle, "Handle")
259
260   Block1h = geompy.GetBlockNearPoint(Handle, p_45_110_1)
261   id_block1h = geompy.addToStudyInFather(Handle, Block1h, "Block 1 of Handle")
262
263   Block2h = geompy.GetBlockNearPoint(Handle, p_65_110_1)
264   id_block2h = geompy.addToStudyInFather(Handle, Block2h, "Block 2 of Handle")
265
266   Face11h = geompy.GetFaceByNormale(Block1h, vx)
267   id_face11h = geompy.addToStudyInFather(Block1h, Face11h, "Face 1")
268
269   Face12h = geompy.GetOppositeFace(Block1h, Face11h)
270   id_face12h = geompy.addToStudyInFather(Block1h, Face12h, "Face 2")
271
272   Face21h = geompy.GetFaceByNormale(Block2h, vx)
273   id_face21h = geompy.addToStudyInFather(Block2h, Face21h, "Face 1")
274
275   Face22h = geompy.GetOppositeFace(Block2h, Face21h)
276   id_face22h = geompy.addToStudyInFather(Block2h, Face22h, "Face 2")
277
278   try:
279     Block3h = geompy.GetBlockByParts(Handle, [Face11h, Face21h])
280   except RuntimeError:
281     try:
282       Block3h = geompy.GetBlockByParts(Handle, [Face11h, Face22h])
283     except RuntimeError:
284       try:
285         Block3h = geompy.GetBlockByParts(Handle, [Face12h, Face21h])
286       except RuntimeError:
287         try:
288           Block3h = geompy.GetBlockByParts(Handle, [Face12h, Face22h])
289         except RuntimeError:
290           print("ERROR: BlocksOp.GetBlockByParts() failed : ", geompy.BlocksOp.GetErrorCode())
291         else:
292           id_block3h = geompy.addToStudyInFather(Handle, Block3h, "Block 3 of Handle")
293
294   ### The whole shape ###
295
296   CompALL = geompy.MakeCompound([Glue1234, Block5, Mirror1, Handle, Gear])
297   Spanner = geompy.MakeGlueFaces(CompALL, 1e-5)
298   id_glueALL = geompy.addToStudy(Spanner, "The Spanner")
299
300   ### Check the Spanner ###
301
302   isCompOfBlocks6 = geompy.CheckCompoundOfBlocks(Spanner)
303   if isCompOfBlocks6 == 0:
304     print("Spanner is not a compound of hexahedral solids")
305     (NonBlocks, NonQuads) = geompy.GetNonBlocks(Spanner)
306     if NonBlocks is not None:
307       geompy.addToStudyInFather(Spanner, NonBlocks, "Group of non-hexahedral solids")
308     if NonQuads is not None:
309       geompy.addToStudyInFather(Spanner, NonQuads, "Group of non-quadrangular faces")
310   else:
311     print("Spanner is a compound of hexahedral solids")
312
313   if isBlocksTest == 1:
314
315     print("##################### Test More #####################")
316
317     ### Get Blocks 4 and 5 from the spanner ###
318
319     Face42_sp = geompy.GetFaceByEdges(Spanner, Edge41, Edge42)
320     id_face42_sp = geompy.addToStudyInFather(Spanner, Face42_sp, "Face 4_2")
321
322     Blocks_f4_sp = geompy.GetBlocksByParts(Spanner, [Face42_sp])
323
324     isMRot2D = 0
325
326     for aBlock in Blocks_f4_sp:
327       name = geompy.SubShapeName(aBlock, Spanner)
328       Id_block = geompy.addToStudyInFather(Spanner, aBlock, name)
329
330       f42_sp_ind = geompy.LocalOp.GetSubShapeIndex(aBlock, Face42_sp)
331
332       Face_5_horiz = geompy.GetFaceByNormale(aBlock, vz)
333       f_5_horiz_ind = geompy.LocalOp.GetSubShapeIndex(aBlock, Face_5_horiz)
334
335       MRot = None
336       is2Dok = True
337       try:
338         MRot = geompy.MakeMultiTransformation2D(aBlock, f42_sp_ind, 0, 3, f_5_horiz_ind, 0, 5)
339       except:
340         is2Dok = False
341       if is2Dok and MRot is not None:
342         isMRot2D = 1
343         p_z100 = geompy.MakeVertex(0., 0., 100.)
344         MRot_tr = geompy.MakeTranslationTwoPoints(MRot, p0, p_z100)
345         id_MRot_tr = geompy.addToStudy(MRot_tr, "Multi-rotated block 5")
346
347     if isMRot2D == 0:
348       print("2D Multi Transformation failed")
349
350     ### Get one face of the Gear ###
351
352     p_20_0_tr   = geompy.MakeTranslationTwoPoints(p_20_0  , p0, p_60_150_0)
353     p_20_0_r_tr = geompy.MakeTranslationTwoPoints(p_20_0_r, p0, p_60_150_0)
354     p_40_0_tr   = geompy.MakeTranslationTwoPoints(p_40_0  , p0, p_60_150_0)
355     p_40_0_r_tr = geompy.MakeTranslationTwoPoints(p_40_0_r, p0, p_60_150_0)
356
357     Face_g_1 = geompy.GetFaceByPoints(Gear, p_20_0_tr, p_20_0_r_tr, p_40_0_tr, p_40_0_r_tr)
358     id_face_g_1 = geompy.addToStudyInFather(Gear, Face_g_1, "Face of Gear by four points")
359
360     edgesNb = geompy.ShapesOp.NumberOfEdges(Face_g_1)
361     print("Face of Gear has ", edgesNb, " edges")
362
363     Face_g_1_tr = geompy.MakeTranslationTwoPoints(Face_g_1, p0, pth)
364     id_face_g_1_tr = geompy.addToStudyInFather(Gear, Face_g_1_tr, "Face of Gear by four points, translated")
365
366     ### equivalent of Block1s, but made as block
367
368     Block_new = geompy.MakeHexa2Faces(Face_g_1, Face_g_1_tr)
369     id_block_new = geompy.addToStudy(Block_new, "Block New")
370
371   if isMeshTest == 1 and smesh is not None:
372
373     print("##################### Build Mesh #####################")
374
375
376     # ---- add a middle block of spanner handle in study
377
378     p_45_0_1 = geompy.MakeVertex(45., 0., 1.)
379     BlockMh = geompy.GetBlockNearPoint(Spanner, p_45_0_1)
380     id_blockMh = geompy.addToStudyInFather(Spanner, BlockMh, "Middle Block of Spanner Handle")
381
382     # ---- add the top face of the middle block in study
383
384     FaceTop = geompy.GetFaceByNormale(BlockMh, vz)
385     id_facetop = geompy.addToStudyInFather(BlockMh, FaceTop, "Top Face")
386
387     # ---- add long edges of the top face in study
388
389     FaceTop_edges = geompy.SubShapeAllSortedCentres(FaceTop, geompy.ShapeType["EDGE"])
390     Edge1 = FaceTop_edges[0]
391     Edge2 = FaceTop_edges[3]
392     Id_Edge1 = geompy.addToStudyInFather(FaceTop, Edge1, "Edge 1")
393     Id_Edge2 = geompy.addToStudyInFather(FaceTop, Edge2, "Edge 2")
394
395     print("-------------------------- Algorithm and Hypothesis")
396
397     print("---- Init a Mesh with the Spanner")
398
399     mesh = smesh.Mesh(Spanner, "Meshed Spanner")
400
401     print("-------------------------- add hypothesis to Spanner")
402
403     print("-------------------------- NumberOfSegments")
404     algoReg = mesh.Segment()
405     listHyp = algoReg.GetCompatibleHypothesis()
406     for hyp in listHyp:
407       print(hyp)
408     print(algoReg.GetName())
409     print(algoReg.GetId())
410     algoReg.SetName("Regular_1D")
411
412
413     hypNbSeg3 = algoReg.NumberOfSegments(3)
414     print(hypNbSeg3.GetName())
415     print(hypNbSeg3.GetId())
416     print(hypNbSeg3.GetNumberOfSegments())
417     smesh.SetName(hypNbSeg3, "NumberOfSegments_3")
418
419     print("-------------------------- Quadrangle_2D")
420
421     algoQuad = mesh.Quadrangle()
422     listHyp = algoQuad.GetCompatibleHypothesis()
423     for hyp in listHyp:
424         print(hyp)
425     print(algoQuad.GetName())
426     print(algoQuad.GetId())
427     algoQuad.SetName("Quadrangle_2D")
428
429     print("-------------------------- add hypothesis to the Middle Block")
430
431     print("-------------------------- LocalLength")
432     algoRegMb = mesh.Segment(BlockMh)
433     hypLen1 = algoRegMb.LocalLength(10)
434     print(hypLen1.GetName())
435     print(hypLen1.GetId())
436     print(hypLen1.GetLength())
437     smesh.SetName(hypLen1, "Local_Length_10")
438
439     print("-------------------------- add hypothesis to the long edges of the Top Face of the Middle Block")
440
441     algoRegE1 = mesh.Segment(Edge1)
442     hypPropE1 = algoRegE1.Propagation()
443     print(hypPropE1.GetName())
444     print(hypPropE1.GetId())
445     smesh.SetName(hypPropE1, "Propagation hypothesis")
446     smesh.SetName(algoRegE1.GetSubMesh(), "SubMesh Edge 1 of Top Face")
447
448     algoRegE2 = mesh.Segment(Edge2)
449     hypPropE2 = algoRegE2.Propagation()
450     print(hypPropE2.GetName())
451     print(hypPropE2.GetId())
452     smesh.SetName(hypPropE2, "Propagation hypothesis")
453     smesh.SetName(algoRegE2.GetSubMesh(), "SubMesh Edge 2 of Top Face")
454
455     print("-------------------------- compute the mesh")
456     mesh.Compute()
457
458     print("Information about the Mesh:")
459     print("Number of nodes       : ", mesh.NbNodes())
460     print("Number of edges       : ", mesh.NbEdges())
461     print("Number of faces       : ", mesh.NbFaces())
462     print("Number of triangles   : ", mesh.NbTriangles())
463     print("Number of quadrangles : ", mesh.NbQuadrangles())
464     print("Number of volumes     : ", mesh.NbVolumes())
465     print("Number of tetrahedrons: ", mesh.NbTetras())
466
467   return Spanner