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