]> SALOME platform Git repositories - modules/geom.git/blob - src/Tools/t_shape/t_shape_builder.py
Salome HOME
progress bar on t_shape_builder plugin
[modules/geom.git] / src / Tools / t_shape / t_shape_builder.py
1 # -*- coding: utf-8 -*-
2
3 import sys
4 import salome
5
6 import GEOM
7 from salome.geom import geomBuilder
8 import math
9 import SALOMEDS
10
11 geompy = None
12
13 def demidisk(study, r1, a1, roty=0, solid_thickness=0):
14   if solid_thickness < 1e-7:
15     with_solid = False
16   else:
17     with_solid = True
18
19   #geompy = geomBuilder.New(study)
20   
21   O = geompy.MakeVertex(0, 0, 0)
22   OX = geompy.MakeVectorDXDYDZ(1, 0, 0) 
23   OY = geompy.MakeVectorDXDYDZ(0, 1, 0)
24   OZ = geompy.MakeVectorDXDYDZ(0, 0, 1)
25   
26   v=range(8)
27   l=range(8)
28   v0 = geompy.MakeVertex(0, 0, 0)
29   v[0] = geompy.MakeVertex(0, r1/2.0, 0)
30   v[1] = geompy.MakeVertex(0, r1, 0)
31   l[1] = geompy.MakeLineTwoPnt(v[0], v[1])
32   l[2] = geompy.MakeRotation(l[1], OX, a1*math.pi/180.0)
33   v[4] = geompy.MakeRotation(v[0], OX, a1*math.pi/180.0)
34   v[6] = geompy.MakeRotation(v[1], OX, a1*math.pi/180.0)
35
36   v[2] = geompy.MakeVertex(0, -r1/2.0, 0)
37   v[3] = geompy.MakeVertex(0, -r1, 0)
38   l[3] = geompy.MakeLineTwoPnt(v[2], v[3])
39   l[4] = geompy.MakeRotation(l[3], OX, -a1*math.pi/180.0)
40   v[5] = geompy.MakeRotation(v[2], OX, -a1*math.pi/180.0)
41   v[7] = geompy.MakeRotation(v[3], OX, -a1*math.pi/180.0)
42
43   l[5] = geompy.MakeLineTwoPnt(v[4], v[5])
44   l[6] = geompy.MakeLineTwoPnt(v[0], v[4])
45   l[7] = geompy.MakeLineTwoPnt(v[2], v[5])
46
47   v7 = geompy.MakeVertex(0, 0, r1)
48   arc1 = geompy.MakeArc(v[1], v7, v[3])
49   l[0] = geompy.MakeLineTwoPnt(v[1], v[3])
50   face1 = geompy.MakeFaceWires([arc1, l[0]], 1)
51   part1 = geompy.MakePartition([face1], [l[2], l[4], l[5], l[6], l[7]], [], [], geompy.ShapeType["FACE"], 0, [], 0, True)
52   
53   if with_solid:
54     # Add some faces corresponding to the solid layer outside
55     # the fluid part
56     
57     # --- Vertices
58     v0 = geompy.MakeVertex(0, r1 + solid_thickness, 0)
59     v1 = geompy.MakeRotation(v0, OX, a1*math.pi/180.0)
60     v2 = geompy.MakeRotation(v0, OX, math.pi - (a1*math.pi/180.0))
61     v3 = geompy.MakeRotation(v0, OX, math.pi)
62     v.extend([v0,v1,v3,v2]) # The order is important for use in pointsProjetes
63     # --- Lines
64     l0 = geompy.MakeLineTwoPnt(v[1], v0)
65     l2 = geompy.MakeRotation(l0, OX, a1*math.pi/180.0)
66     l3 = geompy.MakeRotation(l0, OX, math.pi - (a1*math.pi/180.0))
67     # --- Faces
68     face2 = geompy.MakeRevolution(l0, OX, a1*math.pi/180.0)
69     face3 = geompy.MakeRevolution(l2, OX, math.pi - 2*a1*math.pi/180.0)
70     face4 = geompy.MakeRevolution(l3, OX, a1*math.pi/180.0)
71     # --- Compound of the "fluid part" of the divided disk and the additional faces
72     compound1 = geompy.MakeCompound([part1, face2, face3, face4])
73     # --- Glue edges
74     part1 = geompy.MakeGlueEdges(compound1,1e-7)
75  
76   if roty != 0:
77     vrot = [ geompy.MakeRotation(vert, OY, roty*math.pi/180.0) for vert in v ]
78     lrot = [ geompy.MakeRotation(lin, OY, roty*math.pi/180.0) for lin in l ]
79     arc = geompy.MakeRotation(arc1, OY, roty*math.pi/180.0)
80     part = geompy.MakeRotation(part1, OY, roty*math.pi/180.0)
81     return vrot, lrot, arc, part
82   else:
83     return v, l, arc1, part1
84
85 def pointsProjetes(study, vref, face):
86   #geompy = geomBuilder.New(study)
87   vface = geompy.ExtractShapes(face, geompy.ShapeType["VERTEX"], True)
88   vord = range(len(vref))
89   plan = geompy.MakePlaneThreePnt(vref[0], vref[1], vref[-1], 10000)
90   vproj = [ geompy.MakeProjection(vert, plan) for vert in vface ]
91   for i,v in enumerate(vproj):
92     dist = [ (geompy.MinDistance(v, vr), j) for j,vr in enumerate(vref) ]
93     dist.sort()
94     if dist[0][0] < 1.e-3:
95       vord[dist[0][1]] = vface[i]
96   return vord
97
98 def arcsProjetes(study, vf, face):
99   #geompy = geomBuilder.New(study)
100   lface = geompy.ExtractShapes(face, geompy.ShapeType["EDGE"], True)
101   lord = range(3)
102   ends = [vf[1], vf[6], vf[7], vf[3]]
103   for i in range(3):
104     for lf in lface:
105       pts = geompy.ExtractShapes(lf, geompy.ShapeType["VERTEX"], True)
106       if (((geompy.MinDistance(pts[0], ends[i]) < 0.001) and (geompy.MinDistance(pts[1], ends[i+1]) < 0.001)) or
107           ((geompy.MinDistance(pts[1], ends[i]) < 0.001) and (geompy.MinDistance(pts[0], ends[i+1]) < 0.001))):
108         lord[i] = lf
109         #print "arc_%d OK"%i
110         break
111     pass
112   return lord
113  
114 def build_shape(study, r1, r2, h1, h2, solid_thickness=0, progressBar=None ):
115   """ Builds the final shape """
116   
117   if solid_thickness < 1e-7:
118     with_solid = False
119   else:
120     with_solid = True
121   
122   global geompy
123   geompy = geomBuilder.New(study)
124   
125   O = geompy.MakeVertex(0, 0, 0)
126   OX = geompy.MakeVectorDXDYDZ(1, 0, 0) 
127   OY = geompy.MakeVectorDXDYDZ(0, 1, 0)
128   OZ = geompy.MakeVectorDXDYDZ(0, 0, 1)
129   
130   a1 = 45.0
131   seuilmax = 0.1
132   ratio = float(r2)/float(r1)
133   if ratio > (1.0 -seuilmax):
134     a1 = 45.0*(1.0 -ratio)/seuilmax
135
136   # --- Creation of the jonction faces
137   [faci, sect45, arc1, l1, lord90, lord45, edges, arcextru] = jonction(study, r1, r2,\
138                                                                        h1, h2, a1)
139   if progressBar is not None:
140     progressBar.addSteps(1)
141     
142   if with_solid:
143     # The same code is executed again with different external radiuses in order
144     # to get the needed faces and edges to build the solid layer of the pipe
145     [faci_ext, sect45_ext, arc1_ext, l1_ext, \
146      lord90_ext, lord45_ext, edges_ext, arcextru_ext] = jonction(study, r1 + solid_thickness, r2 + solid_thickness,\
147                                                                  h1, h2, a1)
148     faces_jonction_ext = []
149     for i,l in enumerate(lord90):
150       faces_jonction_ext.append(geompy.MakeQuad2Edges(lord90[i],lord90_ext[i]))
151     for i in [1, 3, 6, 7]:
152       faces_jonction_ext.append(geompy.MakeQuad2Edges(edges[i],edges_ext[i]))
153     for i,l in enumerate(lord45):
154       faces_jonction_ext.append(geompy.MakeQuad2Edges(lord45[i],lord45_ext[i]))
155    
156   if progressBar is not None:
157     progressBar.addSteps(1)
158     
159   # --- extrusion droite des faces de jonction, pour reconstituer les demi cylindres
160   if with_solid:    
161     sect45 = geompy.MakeCompound([sect45]+faces_jonction_ext[-3:])
162     sect45 = geompy.MakeGlueEdges(sect45, 1e-7)
163     
164   if progressBar is not None:
165     progressBar.addSteps(1)
166     
167   extru1 = geompy.MakePrismVecH(sect45, OX, h1+10)
168
169   faces_coupe = faci[5:]
170   if with_solid:
171     faces_coupe = faci[5:]+faces_jonction_ext[:3]
172   base2 = geompy.MakePartition(faces_coupe, [], [], [], geompy.ShapeType["FACE"], 0, [], 0, True)
173   extru2 = geompy.MakePrismVecH(base2, OZ, h2)
174
175   if progressBar is not None:
176     progressBar.addSteps(1)
177     
178   # --- partition et coupe
179
180   if with_solid:
181      demiDisque = geompy.MakeFaceWires([arc1_ext, l1_ext[0]], 1)
182   else:
183      demiDisque = geompy.MakeFaceWires([arc1, l1[0]], 1)
184   demiCylindre = geompy.MakePrismVecH(demiDisque, OX, h1)
185
186   if progressBar is not None:
187     progressBar.addSteps(1)
188     
189   box = geompy.MakeBox(0, -2*(r1+h1), -2*(r1+h1), 2*(r1+h1), 2*(r1+h1), 2*(r1+h1))
190   rot = geompy.MakeRotation(box, OY, 45*math.pi/180.0)
191
192   # NOTE: The following Cut takes almost half of the total execution time
193   garder = geompy.MakeCutList(demiCylindre, [extru2, rot], True)
194   
195   if progressBar is not None:
196     progressBar.addSteps(10)
197     
198   faces_coupe = faci[:5]
199   if with_solid:
200     faces_coupe.extend(faces_jonction_ext[-7:])
201   raccord = geompy.MakePartition([garder], faces_coupe + [arcextru], [], [], geompy.ShapeType["SOLID"], 0, [], 0, True)
202   assemblage = geompy.MakeCompound([raccord, extru1, extru2])
203   assemblage = geompy.MakeGlueFaces(assemblage, 1e-7)
204
205   if progressBar is not None:
206     progressBar.addSteps(2)
207
208   box = geompy.MakeBox(-1, -(r1+r2+2*solid_thickness), -1, h1, r1+r2+2*solid_thickness, h2)
209   
210   # NOTE: This operation takes about 1/4 of the total execution time
211   final = geompy.MakeCommonList([box, assemblage], True)
212   
213   if progressBar is not None:
214     progressBar.addSteps(5)
215     
216   # --- Partie inférieure
217   
218   v3, l3, arc3, part3 = demidisk(study, r1, a1, 180.0, solid_thickness)
219   extru3 = geompy.MakePrismVecH(part3, OX, h1)
220
221   # --- Symétrie
222
223   compound = geompy.MakeCompound([final, extru3])
224   plane = geompy.MakePlane(O,OX,2000)
225   compound_mirrored = geompy.MakeMirrorByPlane(compound, plane)
226   final = geompy.MakeCompound([compound, compound_mirrored])
227
228   if progressBar is not None:
229     progressBar.addSteps(1)
230       
231   return final
232
233
234 def jonction(study, r1, r2, h1, h2, a1):
235   """ Builds the jonction faces and
236   returns what is needed to build the whole pipe
237   """
238   #geompy = geomBuilder.New(study)
239   
240   O = geompy.MakeVertex(0, 0, 0)
241   OX = geompy.MakeVectorDXDYDZ(1, 0, 0) 
242   OY = geompy.MakeVectorDXDYDZ(0, 1, 0)
243   OZ = geompy.MakeVectorDXDYDZ(0, 0, 1)
244   
245   # --- sections droites des deux demi cylindres avec le partionnement
246   v1, l1, arc1, part1 = demidisk(study, r1, a1, 0.)
247   v2, l2, arc2, part2 = demidisk(study, r2, a1, 90.0)
248  
249   # --- extrusion des sections --> demi cylindres de travail, pour en extraire les sections utilisées au niveau du Té
250   #     et enveloppe cylindrique du cylindre principal
251
252   demicyl1 = geompy.MakePrismVecH(part1, OX, h1)
253   demicyl2 = geompy.MakePrismVecH(part2, OZ, h2)
254   arcextru = geompy.MakePrismVecH(arc1, OX, h1)
255   
256   # --- plan de coupe à 45° sur le cylindre principal,
257   #     section à 45° du cylndre principal,
258   #     section du cylindre secondaire par l'enveloppe cylindrique du cylindre principal
259
260   plan1 = geompy.MakePlane(O, OX, 4*r1)
261   planr = geompy.MakeRotation(plan1, OY, 45*math.pi/180.0)
262
263   sect45 = geompy.MakeCommonList([demicyl1, planr], True)
264   sect90 = geompy.MakeCommonList([demicyl2, arcextru], True)
265   #geompy.addToStudy(sect90, "sect90")
266
267   # --- liste ordonnée des points projetés sur les deux sections
268
269   vord45 = pointsProjetes(study, v1, sect45)
270   vord90 = pointsProjetes(study, v2, sect90)
271
272   # --- identification des projections des trois arcs de cercle, sur les deux sections.
273   
274   lord45 = arcsProjetes(study, vord45, sect45)
275   lord90 = arcsProjetes(study, vord90, sect90)
276  
277   # --- abaissement des quatre points centraux de la section du cylindre secondaire
278   
279   dz = -r2/2.0
280   for i in (0, 2, 4, 5):
281     vord90[i] = geompy.TranslateDXDYDZ(vord90[i], 0, 0, dz, True)
282     #geompy.addToStudyInFather(sect90, vord90[i], 'vm%d'%i)
283     
284   # --- création des deux arêtes curvilignes sur l'enveloppe cylindrique du cylindre principal, à la jonction
285
286   curv = [None for i in range(4)] # liaisons entre les points 1, 3, 6 et 7 des 2 sections
287
288   curv[0] = geompy.MakeArcCenter(O, vord90[1] , vord45[1], False)
289   curv[1] = geompy.MakeArcCenter(O, vord90[3] , vord45[3], False)
290
291   lipts = ((6, 6, 4), (7, 7, 5))
292   for i, ipts in enumerate(lipts):
293     #print i, ipts
294     p0 = vord90[ipts[0]]
295     p1 = vord45[ipts[1]]
296     p2 = vord45[ipts[2]]
297     plan = geompy.MakePlaneThreePnt(p0, p1, p2, 10000)
298     #geompy.addToStudy(plan, "plan%d"%i)
299     section = geompy.MakeSection(plan, arcextru, True)
300     secpart = geompy.MakePartition([section], [sect45, sect90], [], [], geompy.ShapeType["EDGE"], 0, [], 0, True)
301     #geompy.addToStudy(secpart, "secpart%d"%i)
302     lsec = geompy.ExtractShapes(secpart, geompy.ShapeType["EDGE"], True)
303
304     for l in lsec:
305       pts = geompy.ExtractShapes(l, geompy.ShapeType["VERTEX"], True)
306       if (((geompy.MinDistance(pts[0], p0) < 0.001) and (geompy.MinDistance(pts[1], p1) < 0.001)) or
307           ((geompy.MinDistance(pts[1], p0) < 0.001) and (geompy.MinDistance(pts[0], p1) < 0.001))):
308         curv[i+2] =l
309         #print "curv_%d OK"%i
310         break
311     
312   # --- creation des arêtes droites manquantes, des faces et volumes pour les quatre volumes de la jonction
313
314   edges = [None for i in range(8)]
315   edges[0] = geompy.MakeLineTwoPnt(vord45[0], vord90[0])
316   edges[1] = curv[0]
317   edges[2] = geompy.MakeLineTwoPnt(vord45[2], vord90[2])
318   edges[3] = curv[1]
319   edges[4] = geompy.MakeLineTwoPnt(vord45[4], vord90[4])
320   edges[5] = geompy.MakeLineTwoPnt(vord45[5], vord90[5])
321   edges[6] = curv[2]
322   edges[7] = curv[3]
323  
324   ed45 = [None for i in range(8)]
325   ed45[0] = geompy.MakeLineTwoPnt(vord45[0], vord45[2])
326   ed45[1] = geompy.MakeLineTwoPnt(vord45[0], vord45[1])
327   ed45[2] = geompy.MakeLineTwoPnt(vord45[4], vord45[6])
328   ed45[3] = geompy.MakeLineTwoPnt(vord45[2], vord45[3])
329   ed45[4] = geompy.MakeLineTwoPnt(vord45[5], vord45[7])
330   ed45[5] = geompy.MakeLineTwoPnt(vord45[4], vord45[5])
331   ed45[6] = geompy.MakeLineTwoPnt(vord45[0], vord45[4])
332   ed45[7] = geompy.MakeLineTwoPnt(vord45[2], vord45[5])
333  
334   ed90 = [None for i in range(8)]
335   ed90[0] = geompy.MakeLineTwoPnt(vord90[0], vord90[2])
336   ed90[1] = geompy.MakeLineTwoPnt(vord90[0], vord90[1])
337   ed90[2] = geompy.MakeLineTwoPnt(vord90[4], vord90[6])
338   ed90[3] = geompy.MakeLineTwoPnt(vord90[2], vord90[3])
339   ed90[4] = geompy.MakeLineTwoPnt(vord90[5], vord90[7])
340   ed90[5] = geompy.MakeLineTwoPnt(vord90[4], vord90[5])
341   ed90[6] = geompy.MakeLineTwoPnt(vord90[0], vord90[4])
342   ed90[7] = geompy.MakeLineTwoPnt(vord90[2], vord90[5])
343
344   faci = []
345   faci.append(geompy.MakeFaceWires([ed45[6], edges[0], ed90[6], edges[4]], 0))
346   faci.append(geompy.MakeFaceWires([ed45[7], edges[2], ed90[7], edges[5]], 0))
347   faci.append(geompy.MakeFaceWires([ed45[2], edges[4], ed90[2], edges[6]], 0))
348   faci.append(geompy.MakeFaceWires([ed45[5], edges[4], ed90[5], edges[5]], 0))
349   faci.append(geompy.MakeFaceWires([ed45[4], edges[5], ed90[4], edges[7]], 0))
350   faci.append(geompy.MakeFaceWires([ed90[0], ed90[6],  ed90[5], ed90[7]], 0))
351   faci.append(geompy.MakeFaceWires([ed90[1], ed90[6],  ed90[2], lord90[0]], 0))
352   faci.append(geompy.MakeFaceWires([ed90[2], ed90[5],  ed90[4], lord90[1]], 0))
353   faci.append(geompy.MakeFaceWires([ed90[3], ed90[7],  ed90[4], lord90[2]], 0))
354  
355   return faci, sect45, arc1, l1, lord90, lord45, edges, arcextru
356
357 def test_t_shape_builder():
358   """For testing purpose"""
359   salome.salome_init()
360   theStudy = salome.myStudy
361   geompy = geomBuilder.New(theStudy)
362   for r1 in [1., 100.]:
363     for r2 in [0.9*r1, 0.5*r1, 0.1*r1, 0.05*r1]:
364       for thickness in [r1/100., r1/10., r1/2.]:
365         print r1, r2, thickness
366         h1 = r1 * 2.0
367         h2 = h1
368         try:
369           res = build_shape(theStudy, r1, r2, h1, h2, thickness)
370           geompy.addToStudy(res, "res_%f_%f_%f"%(r1,r2, thickness))
371         except:
372           print "problem with res_%f_%f_%f"%(r1,r2, thickness)
373   
374 if __name__=="__main__":
375   """For testing purpose"""
376   test_t_shape_builder()