Salome HOME
3eeae8e74d3863e2dada1e74d10f042b94efa577
[modules/smesh.git] / src / SMESH_SWIG / SMESH_AdvancedEditor.py
1 #  -*- coding: iso-8859-1 -*-
2 #  Copyright (C) 2007-2008  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 import salome
24 import smesh
25 import math
26
27 def GetNewNodes(mesh,Elems,OldNodes):
28     """
29     Auxilary function, which return list of nodes from
30     given Elems avoided nodes from OldNodes
31     """
32     newnodes = []
33     for i in Elems:
34         nbn = mesh.GetElemNbNodes(i)
35         for j in range(0,nbn):
36             nn = mesh.GetElemNode(i,j)
37             isold = 0
38             for k in range(0,len(newnodes)):
39                 if nn==newnodes[k]:
40                     isold = 1
41                     break
42                 pass
43             if isold: continue
44             for k in range(0,len(OldNodes)):
45                 if nn==OldNodes[k]:
46                     isold = 1
47                     break
48                 pass
49             if isold: continue
50             newnodes.append(nn)
51             pass
52         pass
53     return newnodes
54             
55 smesh.SetCurrentStudy(salome.myStudy)
56
57 # create empty mesh
58 mesh = smesh.Mesh()
59
60 tol = 0.001
61
62 # create a cross from quadrangle faces
63 # 1. create first edge and make extrusion along 0x
64 n1 = mesh.AddNode(55,-5,0)
65 n2 = mesh.AddNode(55,5,0)
66 e1 = mesh.AddEdge([n1,n2])
67 dir1 = smesh.DirStruct(smesh.PointStruct(-10,0,0))
68 mesh.ExtrusionSweep([e1],dir1,11)
69 # 2. create second edge and make extrusion along 0y
70 n3 = mesh.AddNode(-5,-55,0)
71 n4 = mesh.AddNode(5,-55,0)
72 e2 = mesh.AddEdge([n3,n4])
73 dir2 = smesh.DirStruct(smesh.PointStruct(0,10,0))
74 mesh.ExtrusionSweep([e2],dir2,11)
75
76 # since result has coincident nodes and faces
77 # we have to make merge
78 nodes = mesh.FindCoincidentNodes(0.001)
79 mesh.MergeNodes(nodes)
80 mesh.MergeEqualElements()
81
82 # make extrusion faces along 0z
83 faces = mesh.GetElementsByType(smesh.FACE)
84 nbf = len(faces)
85 maxang = 2.0
86 zstep = 5
87 nbzsteps = 50
88 dir3 = smesh.DirStruct(smesh.PointStruct(0,0,zstep))
89 newfaces = [] # list for keeping created top faces
90               # during extrusion
91
92 for i in range(0,nbzsteps):
93     mesh.ExtrusionSweep(faces,dir3,1)
94     # find top faces after each extrusion and keep them
95     res = mesh.GetLastCreatedElems()
96     nbr = len(res)
97     nfaces = []
98     for j in res:
99         nbn = mesh.GetElemNbNodes(j)
100         if nbn!=4: continue
101         nn1 = mesh.GetElemNode(j,0)
102         xyz1 = mesh.GetNodeXYZ(nn1)
103         nn2 = mesh.GetElemNode(j,1)
104         xyz2 = mesh.GetNodeXYZ(nn2)
105         nn3 = mesh.GetElemNode(j,2)
106         xyz3 = mesh.GetNodeXYZ(nn3)
107         if abs(xyz1[2]-xyz2[2])<tol and abs(xyz1[2]-xyz3[2])<tol :
108             # this face is a top face
109             nfaces.append(j)
110             pass
111         pass
112     if len(nfaces)!=nbf:
113         print "len(nfaces)!=nbf"
114         break
115     newfaces.append(nfaces)
116     # update faces for before next step of extrusion
117     faces = nfaces
118     pass
119     
120 # rotate faces from newfaces
121 axisr1 = smesh.AxisStruct(0,0,0,0,0,1)
122 for i in range(0,nbzsteps):
123     ang = maxang*(1-math.cos((i+1)*math.pi/nbzsteps))
124     mesh.Rotate(newfaces[i],axisr1,ang,0)
125
126
127 # create circles
128 # create two edges and rotate them for creation
129 # full circle
130 n5 = mesh.AddNode(65,0,0)
131 n6 = mesh.AddNode(67.5,0,0)
132 n7 = mesh.AddNode(70,0,0)
133 e56 = mesh.AddEdge([n5,n6])
134 e67 = mesh.AddEdge([n6,n7])
135 axisr2 = smesh.AxisStruct(65,0,0,0,1,0)
136 mesh.RotationSweep([e56,e67],axisr2, math.pi/6, 12, tol)
137 res = mesh.GetLastCreatedElems()
138 faces1 = []
139 for i in res:
140     nbn = mesh.GetElemNbNodes(i)
141     if nbn>2: faces1.append(i)
142     pass
143 nbf1 = len(faces1)
144
145 # create other two edges and rotate them for creation
146 # other full circle
147 n8 = mesh.AddNode(-65,0,0)
148 n9 = mesh.AddNode(-67.5,0,0)
149 n10 = mesh.AddNode(-70,0,0)
150 e8 = mesh.AddEdge([n8,n9])
151 e9 = mesh.AddEdge([n9,n10])
152 axisr3 = smesh.AxisStruct(-65,0,0,0,-1,0)
153 mesh.RotationSweep([e8,e9],axisr3, math.pi/6, 12, tol)
154 res = mesh.GetLastCreatedElems()
155 faces2 = []
156 for i in res:
157     nbn = mesh.GetElemNbNodes(i)
158     if nbn>2: faces2.append(i)
159     pass
160 nbf2 = len(faces2)
161
162 # there are coincident nodes after rotation
163 # therefore we have to merge nodes
164 nodes = mesh.FindCoincidentNodes(0.001)
165 mesh.MergeNodes(nodes)
166
167 nbcircs = 2
168 nbrsteps = 24
169 nbrs = nbcircs*nbrsteps
170 dz = nbzsteps*zstep/nbrs
171
172 # create first spiral
173 oldnodes = []
174 newnodes = GetNewNodes(mesh,faces1,oldnodes)
175 oldnodes = newnodes
176
177 nodes = []
178 mesh.RotationSweep(faces1,axisr1, math.pi*2/nbrsteps, nbrs, tol)
179 res = mesh.GetLastCreatedElems()
180
181 for i in range(0,nbrs):
182     volumes = []
183     for j in range(0,nbf1): volumes.append(res[i+j*nbrs])
184     newnodes = GetNewNodes(mesh,volumes,oldnodes)
185     for j in newnodes:
186         xyz = mesh.GetNodeXYZ(j)
187         mesh.MoveNode(j,xyz[0],xyz[1],xyz[2]+dz*(i+1))
188         pass
189     oldnodes = newnodes
190     pass
191
192 # create second spiral
193 oldnodes = []
194 newnodes = GetNewNodes(mesh,faces2,oldnodes)
195 oldnodes = newnodes
196
197 nodes = []
198 mesh.RotationSweep(faces2,axisr1, math.pi*2/nbrsteps, nbrs, tol)
199 res = mesh.GetLastCreatedElems()
200
201 for i in range(0,nbrs):
202     volumes = []
203     for j in range(0,nbf2): volumes.append(res[i+j*nbrs])
204     newnodes = GetNewNodes(mesh,volumes,oldnodes)
205     for j in newnodes:
206         xyz = mesh.GetNodeXYZ(j)
207         mesh.MoveNode(j,xyz[0],xyz[1],xyz[2]+dz*(i+1))
208         pass
209     oldnodes = newnodes
210     pass
211
212 smesh.salome.sg.updateObjBrowser(1)