#-----------------------------------------------------------------------
#                   Primitive tsxPython sample script
#                      Beta-sheet (protein motif) 2
#                     blended several NURBS planes
# 
# $Id$
#-----------------------------------------------------------------------

import ptsxpy as p
from math import *

t = 0.2

org   = Vec3f( 0., 0., 0. )
xaxis = Vec3f( 1., 0., 0. )
yaxis = Vec3f( 0., 1., 0. )
zaxis = Vec3f( 0., 0., 1. )

#--------
# Make an arrowhead by deleting a vertical edge from a cube
#--------

cb1 = p.CreateCube( 1, 1., 1., t ) # make a cube with a section
p.SceneAddObject( cb1, e_tsxFALSE )

eg1 = Edge( 5, 6 ) # two of eight vertices ( 0 - 7 )
#p.PointsActivateEdit()
#p.PolyhSelectEdges( cb1, 1, eg1.p )

ret = p.PolyhKillEdge( cb1, eg1.p )
ret = p.PolyhKillVertexOnEdge( cb1, eg1.iStartVertex() )
print( "ret=", ret )
ret = p.PolyhKillVertexOnEdge( cb1, eg1.iEndVertex() )
print( "ret=", ret )

#p.PointsDeactivateEdit()
#p.PolyhDeselectEdges( cb1, 1, eg1.p )

# rotated so that the arrowtop points y = -infinity
p.GNodeRotate( cb1, org.p, zaxis.p, pi / 4., e_tsxWorldFrame )

#--------
# Make a shaft with 4 NURBS planes (for sides) and one (for end cap)
#--------
lst1 = []

np1 = p.NURBSPlaneCreate( 1, 8 )
p.SceneAddObject( np1, e_tsxFALSE )
vec1 = Vec3f( 0.5, 4., 1. )
p.GNodeScale( np1, vec1.p, e_tsxWorldFrame )
vec1 = Vec3f( 0., 4., 0. )
p.GNodeSetLocation( np1, vec1.p )
lst1.append( np1 )

wk2 = Ulong()
p.GNodeCopy( np1, wk2.p )
np2 = wk2.get()
p.SceneAddObject( np2, e_tsxFALSE )
#p.GNodeRotate( np2, org.p, yaxis.p, pi, e_tsxWorldFrame )
vec1 = Vec3f( 0., 0., t )
p.GNodeTranslate( np2, vec1.p, e_tsxWorldFrame )
lst1.append( np2 )

wk3 = Ulong()
p.GNodeCopy( np1, wk3.p )
np3 = wk3.get()
p.SceneAddObject( np3, e_tsxFALSE )
vec1 = Vec3f( t, 1., 1. )
p.GNodeScale( np3, vec1.p, e_tsxWorldFrame )
p.GNodeRotate( np3, org.p, yaxis.p, pi / 2., e_tsxWorldFrame )
vec1 = Vec3f( -0.5, 0., 0.5 * t )
p.GNodeTranslate( np3, vec1.p, e_tsxWorldFrame )
lst1.append( np3 )

wk4 = Ulong()
p.GNodeCopy( np1, wk4.p )
np4 = wk4.get()
p.SceneAddObject( np4, e_tsxFALSE )
vec1 = Vec3f( t, 1., 1. )
p.GNodeScale( np4, vec1.p, e_tsxWorldFrame )
p.GNodeRotate( np4, org.p, yaxis.p, -pi / 2., e_tsxWorldFrame )
vec1 = Vec3f( 0.5, 0., 0.5 * t )
p.GNodeTranslate( np4, vec1.p, e_tsxWorldFrame )
lst1.append( np4 )

np5 = p.NURBSPlaneCreate( 1, 1 )  # end cap
p.SceneAddObject( np5, e_tsxFALSE )
vec1 = Vec3f( 0.5, 0.5 * t, 0.5 )
p.GNodeScale( np5, vec1.p, e_tsxWorldFrame )
p.GNodeRotate( np5, org.p, xaxis.p, -pi / 2., e_tsxWorldFrame )
vec1 = Vec3f( 0., 8., 0.5 * t )
p.GNodeTranslate( np5, vec1.p, e_tsxWorldFrame )
lst1.append( np5 )

#--------
# Blend 4 side NURBS planes (i.e. meke smooth transition between patches)
#--------

#wk2 = Ulong()
#ret = p.NPatchCreate( wk2.p )
#print( "NPatchCreate() ret=", ret )
#np3p = wk2.get()
#p.SceneAddObject( np3p, e_tsxFALSE )

#ret = p.StitchSurfConstruct(
# np1p,    e_tsxNPATCH_DIR_V, 0.,
# np2p, 0, e_tsxNPATCH_DIR_V, 0.,
# e_tsxTRUE, tsxSTITCH_LEVEL_POSITION, 0., e_tsxFALSE, 0., -1., np3p )
#print( "StitchSurfConstruct() ret=", ret )

np1p = p.NPatchPolyhGetNPatch( np1 )
print( "np1=", np1, ", np1p=", np1p )
np2p = p.NPatchPolyhGetNPatch( np2 )
print( "np2=", np2, ", np2p=", np2p )
np3p = p.NPatchPolyhGetNPatch( np3 )
print( "np3=", np3, ", np3p=", np3p )
np4p = p.NPatchPolyhGetNPatch( np4 )
print( "np4=", np4, ", np4p=", np4p )

ret = p.BlendSurfConstruct(
 np1p, 0, e_tsxNPATCH_DIR_V, 0.,
 np2p, 0, e_tsxNPATCH_DIR_V, 0.,
 e_tsxTRUE,
 tsxBLEND_LEVEL_POSITION,
 tsxBLEND_LEVEL_POSITION, 0., 0., e_tsxFALSE, 0., -1., np3p )
print( "BlendSurfConstruct() ret=", ret )

ret = p.BlendSurfConstruct(
 np1p, 0, e_tsxNPATCH_DIR_V, 1.,
 np2p, 0, e_tsxNPATCH_DIR_V, 1.,
 e_tsxTRUE,
 tsxBLEND_LEVEL_POSITION,
 tsxBLEND_LEVEL_POSITION, 0., 0., e_tsxFALSE, 0., -1., np4p )
print( "BlendSurfConstruct() ret=", ret )

#--------
# Group them (including the end cap)
#--------
p.SelectSobj( cb1, e_tsxSELECT, e_tsxFALSE )
for i in range( len( lst1 ) ):
    p.GroupAtCurrobj( lst1[ i ] )
sheet1 = p.GetCurrentSelection()

#--------
# Adjust its orientation and position
#--------
p.GNodeRotate( sheet1, org.p, xaxis.p, pi / 2., e_tsxWorldFrame )
vec1 = Vec3f( 6., 0., -3. )
p.GNodeSetLocation( sheet1, vec1.p )

# Reset axes orientation
# In case of tS7 Model and tS6.6, the helix's z-axis points world +y now.
# In case of other tS, x-axis points world +y now.
ax1 = Axes3f((1.,0.,0.),(0.,1.,0.),(0.,0.,1.))
p.MNodeSetAxesOrientation( sheet1, ax1.p )

p.SceneDraw()

