from plugins.mesh import Mesh,connectMesh

clear()
smoothwire()

nx = 4
ny = 3
nz = 7

# A rectangular mesh
M1 = simple.rectangle(nx,ny).toMesh().setProp(1)
# Same mesh, rotated and translated
M2 = M1.rotate(45,0).translate([1.,-1.,nz]).setProp(3)
draw([M1,M2])
sleep(1)

# leave out the first and the last two elements
e1 = M1.elems[1:-2]
m1 = Mesh(M1.coords,e1)
m2 = Mesh(M2.coords,e1)
clear()
draw([m1,m2],view=None)
sleep(1)

# Connect both meshes to a hexaeder mesh
m = connectMesh(m1,m2,nz)
clear()
draw(m,view=None)

# End
Quarter = Body + Filled + Flange1 + Flange2 + Flange3

Half = Quarter + Quarter.mirror(1).reverse()

Full = Half + Half.mirror(0).reverse()

draw(Full,color=red)

method = ask("Choose extrude method:",['Cancel','Sweep','Connect'])
if method == 'Cancel':
    exit()

nodesF,elemsF = Full.feModel()

if method == 'Sweep':
    path = simple.line([0,0,0],[0,0,l],el)
    nodesF = nodesF.rollAxes(1)
    nodes,elems = mesh.sweepGrid(nodesF,elemsF,path,a1='last',a2='last')

else:
    nodesF1 = nodesF.trl([0,0,l])#.scale(2)
    nodes,elems = mesh.connectMesh(nodesF,nodesF1,elemsF,el)

smooth()
clear()
Beam = Formex(nodes[elems].reshape(-1,8,3),eltype='Hex8')
draw(Beam,color='red',linewidth=2)


# End
#pause()

method = ask("Choose extrude method:",['Cancel','Sweep','Connect','Extrude'])


if method == 'Sweep':
    L = simple.line([0,0,0],[0,0,l],el)
    x = concatenate([L.f[:,0],L.f[-1:,1]])
    path = curve.PolyLine(x)
    Beam = Section.sweep(path,normal=[0.,0.,1.])

elif method == 'Connect':
    Section1 = Section.copy()
    Section1.coords = Section1.coords.trl([0,0,l])
    Beam = connectMesh(Section,Section1,el)

elif method == 'Extrude':
    Beam = Section.extrude(el,step=l/el,dir=2)

else:
    exit()
    
smooth()
clear()
Beam.eltype = 'hex8'
draw(Beam,color='red',linewidth=2)
export({'Beam':Beam})


# End
#pause()

method = ask("Choose extrude method:",['Cancel','Sweep','Connect','Extrude'])

M = Full.toMesh()

if method == 'Sweep':
    L = simple.line([0,0,0],[0,0,l],el)
    x = concatenate([L.f[:,0],L.f[-1:,1]])
    path = curve.PolyLine(x)
    Beam = M.sweep(path,normal=[0.,0.,1.])

elif method == 'Connect':
    M1 = M.copy()
    M1.coords = M1.coords.trl([0,0,l])
    Beam = connectMesh(M,M1,el)

elif method == 'Extrude':
    Beam = M.extrude(el,step=l/el,dir=2)

else:
    exit()
    
smooth()
clear()
Beam.eltype = 'hex8'
draw(Beam,color='red',linewidth=2)
export({'Beam':Beam})


# End
nx = 4
ny = 3
nz = 7
F = simple.rectangle(nx,ny).setProp(1)

c1,e1 = F.feModel()
c2 = c1.rotate(45,0).translate([1.,-1.,nz])

G = Formex(c2[e1]).setProp(3)
draw([F,G])


e1 = e1[1:-2]

m1 = Mesh(c1,e1)
m2 = Mesh(c2,e1)

clear()
m1.draw()
m2.draw()

x,e = connectMesh(c1,c2,e1,nz)

F = Formex(x[e])
F.setProp(1)
F.eltype = 'hex8'
clear()
draw(F)

# End