Salome HOME
Fix bug 12796: Warning missed for the bad file 'test18.med'
[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 SMESH
22 import math
23
24 def GetNewNodes(mesh,Elems,OldNodes):
25     """
26     Auxilary function, which return list of nodes from
27     given Elems avoided nodes from OldNodes
28     """
29     newnodes = []
30     for i in Elems:
31         nbn = mesh.GetElemNbNodes(i)
32         for j in range(0,nbn):
33             nn = mesh.GetElemNode(i,j)
34             isold = 0
35             for k in range(0,len(newnodes)):
36                 if nn==newnodes[k]:
37                     isold = 1
38                     break
39                 pass
40             if isold: continue
41             for k in range(0,len(OldNodes)):
42                 if nn==OldNodes[k]:
43                     isold = 1
44                     break
45                 pass
46             if isold: continue
47             newnodes.append(nn)
48             pass
49         pass
50     return newnodes
51             
52     
53 # create empty mesh
54 mesh = smesh.smesh.CreateEmptyMesh()
55
56 ed = mesh.GetMeshEditor()
57 tol = 0.001
58
59 # create a cross from quadrangle faces
60 # 1. create first edge and make extrusion along 0x
61 n1 = ed.AddNode(55,-5,0)
62 n2 = ed.AddNode(55,5,0)
63 e1 = ed.AddEdge([n1,n2])
64 dir1 = SMESH.DirStruct(SMESH.PointStruct(-10,0,0))
65 ed.ExtrusionSweep([e1],dir1,11)
66 # 2. create second edge and make extrusion along 0y
67 n3 = ed.AddNode(-5,-55,0)
68 n4 = ed.AddNode(5,-55,0)
69 e2 = ed.AddEdge([n3,n4])
70 dir2 = SMESH.DirStruct(SMESH.PointStruct(0,10,0))
71 ed.ExtrusionSweep([e2],dir2,11)
72
73 # since result has coincident nodes and faces
74 # we have to make merge
75 nodes = ed.FindCoincidentNodes(0.001)
76 ed.MergeNodes(nodes)
77 ed.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     ed.ExtrusionSweep(faces,dir3,1)
91     # find top faces after each extrusion and keep them
92     res = ed.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     ed.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 = ed.AddNode(65,0,0)
128 n6 = ed.AddNode(67.5,0,0)
129 n7 = ed.AddNode(70,0,0)
130 e56 = ed.AddEdge([n5,n6])
131 e67 = ed.AddEdge([n6,n7])
132 axisr2 = SMESH.AxisStruct(65,0,0,0,1,0)
133 ed.RotationSweep([e56,e67],axisr2, math.pi/6, 12, tol)
134 res = ed.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 = ed.AddNode(-65,0,0)
145 n9 = ed.AddNode(-67.5,0,0)
146 n10 = ed.AddNode(-70,0,0)
147 e8 = ed.AddEdge([n8,n9])
148 e9 = ed.AddEdge([n9,n10])
149 axisr3 = SMESH.AxisStruct(-65,0,0,0,-1,0)
150 ed.RotationSweep([e8,e9],axisr3, math.pi/6, 12, tol)
151 res = ed.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 = ed.FindCoincidentNodes(0.001)
162 ed.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 ed.RotationSweep(faces1,axisr1, math.pi*2/nbrsteps, nbrs, tol)
176 res = ed.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         ed.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 ed.RotationSweep(faces2,axisr1, math.pi*2/nbrsteps, nbrs, tol)
196 res = ed.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         ed.MoveNode(j,xyz[0],xyz[1],xyz[2]+dz*(i+1))
205         pass
206     oldnodes = newnodes
207     pass
208
209