# Filename: tri_solid_from_polygon.py
# Purpose: Create solid from polygon.
# Returns: Void.

from maptek import vulcan
from maptek import vulcan_gui

dgd_file = "pydemo.dgd.isis" # design database to access
new_tri = 'solid_from_poly.00t' # name of new triangulation

# select layer from the screen
sel = vulcan_gui.selection("Select Polygon")
sel.criteria = ["LINE", "POLYGON"]
sel.single_object = True
for obj in sel:
    layer_name = obj.layer

# open layer and extract points
with vulcan.dgd(dgd_file, "r") as dgd:
    if dgd.is_layer(layer_name): # check if layer exists
        layer = dgd.get_layer(layer_name) # open layer
    else:
        print("Layer does not exist")
        exit() # if layer does not exist, exit script
    
    for pt in layer:
        points = pt.get_coordinates() # read coordinates of each point

with vulcan.triangulation(new_tri, 'w') as tri:
    [tri.add_node(*pt) for pt in points]
    width = 30 # the height of the new solid
    poly_position = 1 # 0 = top, 1 = middle, 2 = bottom
    tri.solid_from_polygon(width, poly_position)