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