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