Salome HOME
Update copyright information
[modules/smesh.git] / src / SMESH_SWIG / SMESH_AdvancedEditor.py
1 #  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 #
6 #  This library is free software; you can redistribute it and/or
7 #  modify it under the terms of the GNU Lesser General Public
8 #  License as published by the Free Software Foundation; either
9 #  version 2.1 of the License.
10 #
11 #  This library is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 #  Lesser General Public License for more details.
15 #
16 #  You should have received a copy of the GNU Lesser General Public
17 #  License along with this library; if not, write to the Free Software
18 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 #
20 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #
22 import smesh
23 import math
24
25 def GetNewNodes(mesh,Elems,OldNodes):
26     """
27     Auxilary function, which return list of nodes from
28     given Elems avoided nodes from OldNodes
29     """
30     newnodes = []
31     for i in Elems:
32         nbn = mesh.GetElemNbNodes(i)
33         for j in range(0,nbn):
34             nn = mesh.GetElemNode(i,j)
35             isold = 0
36             for k in range(0,len(newnodes)):
37                 if nn==newnodes[k]:
38                     isold = 1
39                     break
40                 pass
41             if isold: continue
42             for k in range(0,len(OldNodes)):
43                 if nn==OldNodes[k]:
44                     isold = 1
45                     break
46                 pass
47             if isold: continue
48             newnodes.append(nn)
49             pass
50         pass
51     return newnodes
52             
53     
54 # create empty mesh
55 mesh = smesh.Mesh()
56
57 tol = 0.001
58
59 # create a cross from quadrangle faces
60 # 1. create first edge and make extrusion along 0x
61 n1 = mesh.AddNode(55,-5,0)
62 n2 = mesh.AddNode(55,5,0)
63 e1 = mesh.AddEdge([n1,n2])
64 dir1 = smesh.DirStruct(smesh.PointStruct(-10,0,0))
65 mesh.ExtrusionSweep([e1],dir1,11)
66 # 2. create second edge and make extrusion along 0y
67 n3 = mesh.AddNode(-5,-55,0)
68 n4 = mesh.AddNode(5,-55,0)
69 e2 = mesh.AddEdge([n3,n4])
70 dir2 = smesh.DirStruct(smesh.PointStruct(0,10,0))
71 mesh.ExtrusionSweep([e2],dir2,11)
72
73 # since result has coincident nodes and faces
74 # we have to make merge
75 nodes = mesh.FindCoincidentNodes(0.001)
76 mesh.MergeNodes(nodes)
77 mesh.MergeEqualElements()
78
79 # make extrusion faces along 0z
80 faces = mesh.GetElementsByType(smesh.FACE)
81 nbf = len(faces)
82 maxang = 2.0
83 zstep = 5
84 nbzsteps = 50
85 dir3 = smesh.DirStruct(smesh.PointStruct(0,0,zstep))
86 newfaces = [] # list for keeping created top faces
87               # during extrusion
88
89 for i in range(0,nbzsteps):
90     mesh.ExtrusionSweep(faces,dir3,1)
91     # find top faces after each extrusion and keep them
92     res = mesh.GetLastCreatedElems()
93     nbr = len(res)
94     nfaces = []
95     for j in res:
96         nbn = mesh.GetElemNbNodes(j)
97         if nbn!=4: continue
98         nn1 = mesh.GetElemNode(j,0)
99         xyz1 = mesh.GetNodeXYZ(nn1)
100         nn2 = mesh.GetElemNode(j,1)
101         xyz2 = mesh.GetNodeXYZ(nn2)
102         nn3 = mesh.GetElemNode(j,2)
103         xyz3 = mesh.GetNodeXYZ(nn3)
104         if abs(xyz1[2]-xyz2[2])<tol and abs(xyz1[2]-xyz3[2])<tol :
105             # this face is a top face
106             nfaces.append(j)
107             pass
108         pass
109     if len(nfaces)!=nbf:
110         print "len(nfaces)!=nbf"
111         break
112     newfaces.append(nfaces)
113     # update faces for before next step of extrusion
114     faces = nfaces
115     pass
116     
117 # rotate faces from newfaces
118 axisr1 = smesh.AxisStruct(0,0,0,0,0,1)
119 for i in range(0,nbzsteps):
120     ang = maxang*(1-math.cos((i+1)*math.pi/nbzsteps))
121     mesh.Rotate(newfaces[i],axisr1,ang,0)
122
123
124 # create circles
125 # create two edges and rotate them for creation
126 # full circle
127 n5 = mesh.AddNode(65,0,0)
128 n6 = mesh.AddNode(67.5,0,0)
129 n7 = mesh.AddNode(70,0,0)
130 e56 = mesh.AddEdge([n5,n6])
131 e67 = mesh.AddEdge([n6,n7])
132 axisr2 = smesh.AxisStruct(65,0,0,0,1,0)
133 mesh.RotationSweep([e56,e67],axisr2, math.pi/6, 12, tol)
134 res = mesh.GetLastCreatedElems()
135 faces1 = []
136 for i in res:
137     nbn = mesh.GetElemNbNodes(i)
138     if nbn>2: faces1.append(i)
139     pass
140 nbf1 = len(faces1)
141
142 # create other two edges and rotate them for creation
143 # other full circle
144 n8 = mesh.AddNode(-65,0,0)
145 n9 = mesh.AddNode(-67.5,0,0)
146 n10 = mesh.AddNode(-70,0,0)
147 e8 = mesh.AddEdge([n8,n9])
148 e9 = mesh.AddEdge([n9,n10])
149 axisr3 = smesh.AxisStruct(-65,0,0,0,-1,0)
150 mesh.RotationSweep([e8,e9],axisr3, math.pi/6, 12, tol)
151 res = mesh.GetLastCreatedElems()
152 faces2 = []
153 for i in res:
154     nbn = mesh.GetElemNbNodes(i)
155     if nbn>2: faces2.append(i)
156     pass
157 nbf2 = len(faces2)
158
159 # there are coincident nodes after rotation
160 # therefore we have to merge nodes
161 nodes = mesh.FindCoincidentNodes(0.001)
162 mesh.MergeNodes(nodes)
163
164 nbcircs = 2
165 nbrsteps = 24
166 nbrs = nbcircs*nbrsteps
167 dz = nbzsteps*zstep/nbrs
168
169 # create first spiral
170 oldnodes = []
171 newnodes = GetNewNodes(mesh,faces1,oldnodes)
172 oldnodes = newnodes
173
174 nodes = []
175 mesh.RotationSweep(faces1,axisr1, math.pi*2/nbrsteps, nbrs, tol)
176 res = mesh.GetLastCreatedElems()
177
178 for i in range(0,nbrs):
179     volumes = []
180     for j in range(0,nbf1): volumes.append(res[i+j*nbrs])
181     newnodes = GetNewNodes(mesh,volumes,oldnodes)
182     for j in newnodes:
183         xyz = mesh.GetNodeXYZ(j)
184         mesh.MoveNode(j,xyz[0],xyz[1],xyz[2]+dz*(i+1))
185         pass
186     oldnodes = newnodes
187     pass
188
189 # create second spiral
190 oldnodes = []
191 newnodes = GetNewNodes(mesh,faces2,oldnodes)
192 oldnodes = newnodes
193
194 nodes = []
195 mesh.RotationSweep(faces2,axisr1, math.pi*2/nbrsteps, nbrs, tol)
196 res = mesh.GetLastCreatedElems()
197
198 for i in range(0,nbrs):
199     volumes = []
200     for j in range(0,nbf2): volumes.append(res[i+j*nbrs])
201     newnodes = GetNewNodes(mesh,volumes,oldnodes)
202     for j in newnodes:
203         xyz = mesh.GetNodeXYZ(j)
204         mesh.MoveNode(j,xyz[0],xyz[1],xyz[2]+dz*(i+1))
205         pass
206     oldnodes = newnodes
207     pass
208
209 smesh.salome.sg.updateObjBrowser(1)