####################################################################################################
#
# Invoking X3D model self-test:
#
#   $ python ExtrusionPentagon.py
#
# Python package x3d.py package is available on PyPI for import.
#   This approach simplifies Python X3D deployment and use.
#   https://pypi.org/project/x3d
#
# Installation:
#       pip install x3d
# or
#       python -m pip install x3d
#
# Developer options for loading x3d package in other Python programs:
#
#    from x3d import *  # preferred approach, terser source that avoids x3d.* class prefixes
#
# or
#    import x3d         # traditional way to subclass x3d package, all classes require x3d.* prefix,
#                       # but python source is very verbose, for example x3d.Material x3d.Shape etc.
#                       # X3dToPython.xslt stylesheet insertPackagePrefix=true supports this option.
#
# Project home page:    # X3D Python Scene Access Interface Library (X3DPSAIL)
#                       # https://www.web3d.org/x3d/stylesheets/python/python.html
# Conversion generator: # https://www.web3d.org/x3d/stylesheets/X3dToPython.xslt
#
####################################################################################################

from x3d import *

newModel=X3D(profile='Immersive',version='3.3',
  head=head(
    children=[
    meta(content='ExtrusionPentagon.x3d',name='title'),
    meta(content='Simple regular pentagon extruded vertically, shown side-by-side with an ExtrusionCrossSection visualization.',name='description'),
    meta(content='Don Brutzman',name='creator'),
    meta(content='U.S. Naval Postgraduate School (NPS)',name='contributor'),
    meta(content='3 September 2005',name='created'),
    meta(content='20 January 2024',name='modified'),
    meta(content='Extrusion pentagon',name='subject'),
    meta(content='https://en.wikipedia.org/wiki/Extrusion',name='reference'),
    meta(content='https://www.web3d.org/x3d/content/examples/X3dForWebAuthors/Chapter06GeometryPointsLinesPolygons/ExtrusionPentagon.x3d',name='identifier'),
    meta(content='X3D-Edit 4.0, https://www.web3d.org/x3d/tools/X3D-Edit',name='generator'),
    meta(content='../license.html',name='license')]),
  Scene=Scene(
    children=[
    WorldInfo(title='ExtrusionPentagon.x3d'),
    NavigationInfo(DEF='ExamineMode'),
    NavigationInfo(DEF='FlyPanNavigation',type=["FLY","ANY"]),
    Background(skyColor=[(1,1,1)]),
    Viewpoint(description='Extrusion Pentagon',orientation=(-1,0,0,0.2),position=(0,3.17,12.85)),
    Viewpoint(description='Oblique view from above',orientation=(-1,0.015,-0.006,0.78),position=(0.25,8.98,8.99)),
    Viewpoint(description='Overhead view',orientation=(1,0,0,-1.57),position=(0,13.15,0)),
    Background(groundColor=[(0.9,0.9,0.9)],skyColor=[(0.9,0.9,0.9)]),
    Transform(translation=(-4,0,0),
      children=[
      Shape(
        # be sure to order cross-section points so that normal is upward

        geometry=Extrusion(crossSection=[(-3.5,-1),(-2.1,2.9),(2.2,2.9),(3.6,-1),(0,-3.5),(-3.5,-1)],scale=[(1,1),(1,1),(0.2,0.2)],spine=[(0,0,0),(0,1,0),(0,1.6,0)],
# spine is open, crossSection is closed
),
        appearance=Appearance(
          material=Material(diffuseColor=(0.2,0.6,0.5))))]),
    Comment('Utilize ExtrusionCrossSection ProtoInstance as a Shape node (with containerField="children") rather than an Extrusion node (with containerField="geometry").'),
    Comment('ExtrusionCrossSection is a Prototype, which are explained in Chapter 14.'),
    Comment('TODO update original version of ExtrusionCrossSectionPrototype.js at X3D Examples Archive Basic course'),
    ExternProtoDeclare(appinfo='Shape prototype for Extrusion node that also draws spine line plus oriented scaled cross sections at each spine point',name='ExtrusionCrossSection',url=["ExtrusionCrossSectionPrototype.x3d#ExtrusionCrossSection","https://www.web3d.org/x3d/content/examples/X3dForWebAuthors/Chapter06GeometryPointsLinesPolygons/ExtrusionCrossSectionPrototype.x3d#ExtrusionCrossSection","ExtrusionCrossSectionPrototype.wrl#ExtrusionCrossSection","https://www.web3d.org/x3d/content/examples/X3dForWebAuthors/Chapter06GeometryPointsLinesPolygons/ExtrusionCrossSectionPrototype.wrl#ExtrusionCrossSection"],
      field=[
      Comment('run-time eventIn Extrusion-modification interfaces for VRML 97 compatibility'),
      field(accessType='initializeOnly',appinfo='Extrusion name',name='name',type='SFString'),
      field(accessType='initializeOnly',appinfo='Extrusion field',name='crossSection',type='MFVec2f'),
      field(accessType='initializeOnly',appinfo='Extrusion field',name='spine',type='MFVec3f'),
      field(accessType='initializeOnly',appinfo='Extrusion field',name='scale',type='MFVec2f'),
      field(accessType='initializeOnly',appinfo='Extrusion field',name='orientation',type='MFRotation'),
      field(accessType='initializeOnly',appinfo='Extrusion field',name='beginCap',type='SFBool'),
      field(accessType='initializeOnly',appinfo='Extrusion field',name='endCap',type='SFBool'),
      field(accessType='initializeOnly',appinfo='Extrusion field',name='ccw',type='SFBool'),
      field(accessType='initializeOnly',appinfo='Extrusion field',name='convex',type='SFBool'),
      field(accessType='initializeOnly',appinfo='Extrusion field',name='creaseAngle',type='SFFloat'),
      field(accessType='initializeOnly',appinfo='Extrusion field',name='solid',type='SFBool'),
      field(accessType='initializeOnly',appinfo='Image to apply to Extrusion',name='extrusionImageTexture',type='SFNode'),
      field(accessType='initializeOnly',appinfo='TextureTransform coordinates of image applied to Extrusion',name='extrusionTextureTransform',type='SFNode'),
      field(accessType='inputOnly',appinfo='Extrusion field',name='set_crossSection',type='MFVec2f'),
      field(accessType='inputOnly',appinfo='Extrusion field',name='set_spine',type='MFVec3f'),
      field(accessType='inputOnly',appinfo='Extrusion field',name='set_scale',type='MFVec2f'),
      field(accessType='inputOnly',appinfo='Extrusion field',name='set_orientation',type='MFRotation'),
      field(accessType='initializeOnly',appinfo='Whether to provide initialization trace statements showing node construction',name='traceEnabled',type='SFBool'),
      field(accessType='inputOutput',appinfo='emissiveColor of spine',name='spineColor',type='SFColor'),
      field(accessType='inputOutput',appinfo='diffuseColor of crossSection',name='crossSectionColor',type='SFColor'),
      field(accessType='inputOutput',appinfo='transparency of crossSection',name='crossSectionTransparency',type='SFFloat'),
      field(accessType='inputOutput',appinfo='diffuseColor of extrusion hull',name='extrusionColor',type='SFColor'),
      field(accessType='inputOutput',appinfo='transparency of extrusion hull',name='extrusionTransparency',type='SFFloat')]),
    Transform(translation=(4,0,0),
      children=[
      ProtoInstance(DEF='PentagonExtrusion',name='ExtrusionCrossSection',
        fieldValue=[
        fieldValue(name='name',value='PentagonExtrusion'),
        fieldValue(name='crossSection',value=[(-3.5,-1),(-2.1,2.9),(2.2,2.9),(3.6,-1),(0,-3.5),(-3.5,-1)]),
        fieldValue(name='spine',value=[(0,0,0),(0,1,0),(0,1.6,0)]),
        fieldValue(name='scale',value=[(1,1),(1,1),(0.2,0.2)]),
        fieldValue(name='ccw',value=True),
        fieldValue(name='spineColor',value=(0.7,0.7,0)),
        fieldValue(name='crossSectionColor',value=(0.6,0.6,0.2)),
        fieldValue(name='extrusionColor',value=(0.3,0.6,0.6)),
        fieldValue(name='extrusionTransparency',value=0.6),
        fieldValue(name='traceEnabled',value=False)])])])
)

### X3D model conversion complete ###

####################################################################################################
# Self-test diagnostics
####################################################################################################

print('Self-test diagnostics for ExtrusionPentagon.py:')
if        metaDiagnostics(newModel): # built-in utility method in X3D class
    print(metaDiagnostics(newModel)) # display meta info, hint, warning, error, TODO values in this model
# print('check newModel.XML() serialization...')
newModelXML= newModel.XML() # test export method XML() for exceptions during export
newModel.XMLvalidate()
# print(newModelXML) # diagnostic

try:
#   print('check newModel.VRML() serialization...')
    newModelVRML=newModel.VRML() # test export method VRML() for exceptions during export
    # print(prependLineNumbers(newModelVRML)) # debug
    print("Python-to-VRML export of VRML output successful", flush=True)
except Exception as err: # usually BaseException
    # https://stackoverflow.com/questions/18176602/how-to-get-the-name-of-an-exception-that-was-caught-in-python
    print("*** Python-to-VRML export of VRML output failed:", type(err).__name__, err)
    if newModelVRML: # may have failed to generate
        print(prependLineNumbers(newModelVRML, err.lineno))

try:
#   print('check newModel.JSON() serialization...')
    newModelJSON=newModel.JSON() # test export method JSON() for exceptions during export
#   print(prependLineNumbers(newModelJSON)) # debug
    print("Python-to-JSON export of JSON output successful (under development)")
except Exception as err: # usually SyntaxError
    print("*** Python-to-JSON export of JSON output failed:", type(err).__name__, err)
    if newModelJSON: # may have failed to generate
        print(prependLineNumbers(newModelJSON,err.lineno))

print("python ExtrusionPentagon.py load and self-test diagnostics complete.")
