bl_info = {
    "name": "Arch Ceil",
    "author": "TheWhiteShadow",
    "version": (1, 0),
    "blender": (2, 75, 0),
    "location": "View3D > Add > Mesh > New Object",
    "description": "Adds a new Arch Ceil",
    "warning": "",
    "wiki_url": "",
    "category": "Add Mesh",
    }


import bpy
import math
from bpy.types import Operator
from bpy.props import IntProperty, FloatProperty, BoolProperty
from bpy_extras.object_utils import AddObjectHelper, object_data_add
from mathutils import Vector, Euler


def add_arch_ceil(self, context):
#{
    size = self.size
    steps = self.steps
    half_stepsize = math.pi / self.edges
    ceil_angle = math.degrees(math.acos((self.arch_radius-1) / self.arch_radius))
    
    
    if (self.keep_arch_width):
        # constant arch size
        arch_width = 2 * size
        size = size / math.tan(math.pi / self.edges)
    else:
        # constant ceil size
        arch_width = 2 * size * math.tan(math.pi / self.edges)
    
    diagonal = size / math.cos(math.pi / self.edges)
    
    #print("ceil_angle: " + str(ceil_angle))
    #print("arch_width: " + str(arch_width))
    #print("diagonal: " + str(diagonal))
    
    verts = []
    edges = []
    faces = []
    
    # create head_vertic
    z = math.sin(math.radians(ceil_angle)) * self.arch_radius * arch_width/2
    print("ceil_height: " + str(z))
    
    verts.append((0, 0, z))
    
    ang = 0
    while ang < 2*math.pi:
    #{
        x = math.cos(ang) * size
        y = math.sin(ang) * size
        verts.append((x, y, z))
        
        ang += 2*half_stepsize
    #}

    ang = half_stepsize
    for edge_index in range(1, self.edges+1):
    #{
        eul1 = Euler((0.0, 0.0, ang-half_stepsize), 'XYZ')
        eul2 = Euler((0.0, 0.0, ang+half_stepsize), 'XYZ')
        
        # create base_vertics
        vec = Vector((size, arch_width / 2, 0))
        vec.rotate(eul1)
        verts.append(vec)
        
        step_size = (ceil_angle) / steps
        z_angle = step_size
        for loop in range(0, steps-1):
        #{
            dist = math.cos(math.radians(z_angle)) * self.arch_radius - (self.arch_radius-1)
            z = math.sin(math.radians(z_angle)) * self.arch_radius * arch_width/2
            z_angle += step_size
            
            vec = Vector((size, dist * arch_width / 2, z))
            vec.rotate(eul1)
            verts.append(vec)
            
            vec = Vector((size, -dist * arch_width / 2, z))
            vec.rotate(eul2)
            verts.append(vec)
            
            x = math.cos(ang) * dist * diagonal
            y = math.sin(ang) * dist * diagonal
            verts.append( Vector((x, y, z)) )

            if loop == 0:
            #{
                last = len(verts)-1
                faces.append([last-2, last-3, last])
                faces.append([last-3, last-1, last])
            #}
            else:
            #{
                last = len(verts)-1
                faces.append([last-2, last-5, last-3, last])
                faces.append([last-3, last-4, last-1, last])
            #}
        #}
        
        # close ceil
        faces.append([edge_index, last-2, last, 0])
        if edge_index < self.edges:
            faces.append([last, last-1, edge_index+1, 0])
        else:
            faces.append([last, last-1, 1, 0])
        
        ang += 2*half_stepsize
    #}

    mesh = bpy.data.meshes.new(name="New Object Mesh")
    mesh.from_pydata(verts, edges, faces)
    # useful for development when the mesh may be invalid.
    # mesh.validate(verbose=True)
    object_data_add(context, mesh, operator=self)
#}


class OBJECT_OT_add_arch_ceil(Operator, AddObjectHelper):
    """Create a new Mesh Object"""
    bl_idname = "mesh.add_arch_ceil"
    bl_label = "Add Mesh Object"
    bl_options = {'REGISTER', 'UNDO'}

    size = FloatProperty(
            name="Size",
            default=1.0,
            description="Size of the arch ceil.",
            precision=3
            )
            
    arch_radius = FloatProperty(
            name="Arch Radius",
            min=1,
            max=100.0,
            default=2.0,
            description="Size of the arch radius.",
            precision=3
            )
            
    steps = IntProperty(
            name="Steps",
            min=2,
            max=128,
            default=12,
            description="Steps of the arch curve.",
            )
            
    edges = IntProperty(
            name="Edges",
            min=3,
            max=32,
            default=4,
            description="Edges of the arch.",
            )
            
    keep_arch_width = BoolProperty(
            name="Keep Arch width",
            default=True,
            description="Increase size with more edges, instead of scaling down arches."
            )

    def execute(self, context):

        add_arch_ceil(self, context)

        return {'FINISHED'}


# Registration

def add_object_button(self, context):
    self.layout.operator(
        OBJECT_OT_add_arch_ceil.bl_idname,
        text="Lancet Arch",
        icon='PLUGIN')


# This allows you to right click on a button and link to the manual
#def add_object_manual_map():
#    url_manual_prefix = "http://wiki.blender.org/index.php/Doc:2.6/Manual/"
#    url_manual_mapping = (
#        ("bpy.ops.mesh.add_arch", "Modeling/Objects"),
#        )
#    return url_manual_prefix, url_manual_mapping


def register():
    bpy.utils.register_class(OBJECT_OT_add_arch_ceil)
    #bpy.utils.register_manual_map(add_object_manual_map)
    bpy.types.INFO_MT_mesh_add.append(add_object_button)


def unregister():
    bpy.utils.unregister_class(OBJECT_OT_add_arch_ceil)
    #bpy.utils.unregister_manual_map(add_object_manual_map)
    bpy.types.INFO_MT_mesh_add.remove(add_object_button)


if __name__ == "__main__":
    register()
