Back to index

salome-smesh  6.5.0
SMESH_AdvancedEditor.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00003 #
00004 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00005 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00006 #
00007 # This library is free software; you can redistribute it and/or
00008 # modify it under the terms of the GNU Lesser General Public
00009 # License as published by the Free Software Foundation; either
00010 # version 2.1 of the License.
00011 #
00012 # This library is distributed in the hope that it will be useful,
00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 # Lesser General Public License for more details.
00016 #
00017 # You should have received a copy of the GNU Lesser General Public
00018 # License along with this library; if not, write to the Free Software
00019 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020 #
00021 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00022 #
00023 
00024 import salome
00025 import smesh
00026 import math
00027 
00028 salome.salome_init()
00029 
00030 def GetNewNodes(mesh,Elems,OldNodes):
00031     """
00032     Auxilary function, which return list of nodes from
00033     given Elems avoided nodes from OldNodes
00034     """
00035     newnodes = []
00036     for i in Elems:
00037         nbn = mesh.GetElemNbNodes(i)
00038         for j in range(0,nbn):
00039             nn = mesh.GetElemNode(i,j)
00040             isold = 0
00041             for k in range(0,len(newnodes)):
00042                 if nn==newnodes[k]:
00043                     isold = 1
00044                     break
00045                 pass
00046             if isold: continue
00047             for k in range(0,len(OldNodes)):
00048                 if nn==OldNodes[k]:
00049                     isold = 1
00050                     break
00051                 pass
00052             if isold: continue
00053             newnodes.append(nn)
00054             pass
00055         pass
00056     return newnodes
00057             
00058 smesh.SetCurrentStudy(salome.myStudy)
00059 
00060 # create empty mesh
00061 mesh = smesh.Mesh()
00062 
00063 tol = 0.001
00064 
00065 # create a cross from quadrangle faces
00066 # 1. create first edge and make extrusion along 0x
00067 n1 = mesh.AddNode(55,-5,0)
00068 n2 = mesh.AddNode(55,5,0)
00069 e1 = mesh.AddEdge([n1,n2])
00070 dir1 = smesh.DirStruct(smesh.PointStruct(-10,0,0))
00071 mesh.ExtrusionSweep([e1],dir1,11)
00072 # 2. create second edge and make extrusion along 0y
00073 n3 = mesh.AddNode(-5,-55,0)
00074 n4 = mesh.AddNode(5,-55,0)
00075 e2 = mesh.AddEdge([n3,n4])
00076 dir2 = smesh.DirStruct(smesh.PointStruct(0,10,0))
00077 mesh.ExtrusionSweep([e2],dir2,11)
00078 
00079 # since result has coincident nodes and faces
00080 # we have to make merge
00081 nodes = mesh.FindCoincidentNodes(0.001)
00082 mesh.MergeNodes(nodes)
00083 mesh.MergeEqualElements()
00084 
00085 # make extrusion faces along 0z
00086 faces = mesh.GetElementsByType(smesh.FACE)
00087 nbf = len(faces)
00088 maxang = 2.0
00089 zstep = 5
00090 nbzsteps = 50
00091 dir3 = smesh.DirStruct(smesh.PointStruct(0,0,zstep))
00092 newfaces = [] # list for keeping created top faces
00093               # during extrusion
00094 
00095 for i in range(0,nbzsteps):
00096     mesh.ExtrusionSweep(faces,dir3,1)
00097     # find top faces after each extrusion and keep them
00098     res = mesh.GetLastCreatedElems()
00099     nbr = len(res)
00100     nfaces = []
00101     for j in res:
00102         nbn = mesh.GetElemNbNodes(j)
00103         if nbn!=4: continue
00104         nn1 = mesh.GetElemNode(j,0)
00105         xyz1 = mesh.GetNodeXYZ(nn1)
00106         nn2 = mesh.GetElemNode(j,1)
00107         xyz2 = mesh.GetNodeXYZ(nn2)
00108         nn3 = mesh.GetElemNode(j,2)
00109         xyz3 = mesh.GetNodeXYZ(nn3)
00110         if abs(xyz1[2]-xyz2[2])<tol and abs(xyz1[2]-xyz3[2])<tol :
00111             # this face is a top face
00112             nfaces.append(j)
00113             pass
00114         pass
00115     if len(nfaces)!=nbf:
00116         print "len(nfaces)!=nbf"
00117         break
00118     newfaces.append(nfaces)
00119     # update faces for before next step of extrusion
00120     faces = nfaces
00121     pass
00122     
00123 # rotate faces from newfaces
00124 axisr1 = smesh.AxisStruct(0,0,0,0,0,1)
00125 for i in range(0,nbzsteps):
00126     ang = maxang*(1-math.cos((i+1)*math.pi/nbzsteps))
00127     mesh.Rotate(newfaces[i],axisr1,ang,0)
00128 
00129 
00130 # create circles
00131 # create two edges and rotate them for creation
00132 # full circle
00133 n5 = mesh.AddNode(65,0,0)
00134 n6 = mesh.AddNode(67.5,0,0)
00135 n7 = mesh.AddNode(70,0,0)
00136 e56 = mesh.AddEdge([n5,n6])
00137 e67 = mesh.AddEdge([n6,n7])
00138 axisr2 = smesh.AxisStruct(65,0,0,0,1,0)
00139 mesh.RotationSweep([e56,e67],axisr2, math.pi/6, 12, tol)
00140 res = mesh.GetLastCreatedElems()
00141 faces1 = []
00142 for i in res:
00143     nbn = mesh.GetElemNbNodes(i)
00144     if nbn>2: faces1.append(i)
00145     pass
00146 nbf1 = len(faces1)
00147 
00148 # create other two edges and rotate them for creation
00149 # other full circle
00150 n8 = mesh.AddNode(-65,0,0)
00151 n9 = mesh.AddNode(-67.5,0,0)
00152 n10 = mesh.AddNode(-70,0,0)
00153 e8 = mesh.AddEdge([n8,n9])
00154 e9 = mesh.AddEdge([n9,n10])
00155 axisr3 = smesh.AxisStruct(-65,0,0,0,-1,0)
00156 mesh.RotationSweep([e8,e9],axisr3, math.pi/6, 12, tol)
00157 res = mesh.GetLastCreatedElems()
00158 faces2 = []
00159 for i in res:
00160     nbn = mesh.GetElemNbNodes(i)
00161     if nbn>2: faces2.append(i)
00162     pass
00163 nbf2 = len(faces2)
00164 
00165 # there are coincident nodes after rotation
00166 # therefore we have to merge nodes
00167 nodes = mesh.FindCoincidentNodes(0.001)
00168 mesh.MergeNodes(nodes)
00169 
00170 nbcircs = 2
00171 nbrsteps = 24
00172 nbrs = nbcircs*nbrsteps
00173 dz = nbzsteps*zstep/nbrs
00174 
00175 # create first spiral
00176 oldnodes = []
00177 newnodes = GetNewNodes(mesh,faces1,oldnodes)
00178 oldnodes = newnodes
00179 
00180 nodes = []
00181 mesh.RotationSweep(faces1,axisr1, math.pi*2/nbrsteps, nbrs, tol)
00182 res = mesh.GetLastCreatedElems()
00183 
00184 for i in range(0,nbrs):
00185     volumes = []
00186     for j in range(0,nbf1): volumes.append(res[i+j*nbrs])
00187     newnodes = GetNewNodes(mesh,volumes,oldnodes)
00188     for j in newnodes:
00189         xyz = mesh.GetNodeXYZ(j)
00190         mesh.MoveNode(j,xyz[0],xyz[1],xyz[2]+dz*(i+1))
00191         pass
00192     oldnodes = newnodes
00193     pass
00194 
00195 # create second spiral
00196 oldnodes = []
00197 newnodes = GetNewNodes(mesh,faces2,oldnodes)
00198 oldnodes = newnodes
00199 
00200 nodes = []
00201 mesh.RotationSweep(faces2,axisr1, math.pi*2/nbrsteps, nbrs, tol)
00202 res = mesh.GetLastCreatedElems()
00203 
00204 for i in range(0,nbrs):
00205     volumes = []
00206     for j in range(0,nbf2): volumes.append(res[i+j*nbrs])
00207     newnodes = GetNewNodes(mesh,volumes,oldnodes)
00208     for j in newnodes:
00209         xyz = mesh.GetNodeXYZ(j)
00210         mesh.MoveNode(j,xyz[0],xyz[1],xyz[2]+dz*(i+1))
00211         pass
00212     oldnodes = newnodes
00213     pass
00214 
00215 smesh.salome.sg.updateObjBrowser(1)