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