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