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