from sys import *
from math import *

Key_Angle_Step = 1.0   #degrees

DEG2RAD = pi/180.0
RAD2DEG = 180.0/pi

#################################################################
## Default library and output module (for simple script output
#################################################################
Key_Default_Library = None
Key_Current_Library = None

Key_Default_Output = None
Key_Current_Output = None

Key_LayerList = None

#----------------------------------------------------------------------------
#Some global constants
#----------------------------------------------------------------------------
TEXT_ALIGN_LEFT = 0
TEXT_ALIGN_CENTER = 1
TEXT_ALIGN_RIGHT = 2

TEXT_ALIGN_TOP = 0
TEXT_ALIGN_MIDDLE = 1
TEXT_ALIGN_BOTTOM = 2

Text_Style_Polygons = 1
Text_Style_Labels = 2
Key_Text_Style = Text_Style_Polygons

PATH_TYPE_NORMAL = 0
PATH_TYPE_ROUNDED = 1
PATH_TYPE_EXTENDED = 2

Key_Name_Scrambling_Off = 0
Key_Name_Scrambling_On = 1

Key_Paths_To_Boundaries_Off = 0
Key_Paths_To_Boundaries_On = 1

Key_Header_Written = False

GDSII_MAX_COORDINATES = 200


#----------------------------------------------------------------------------
#Set library and output
#----------------------------------------------------------------------------

def get_current_library():
	return Key_Current_Library

def set_current_library(library):
	global Key_Current_Library
	Key_Current_Library = library

def get_current_output ():
	return Key_Current_Output

def set_current_output (output):
	global Key_Current_Output
	Key_Current_Output = output

def get_current_layerlist():
	return Key_LayerList

def set_current_layerlist(layerlist):
	global Key_LayerList
	Key_LayerList = layerlist


#----------------------------------------------------------------------------
# Getting and Setting library variables
#----------------------------------------------------------------------------

# deprecated: get vallue directly from library
def get_grid(library = None):
	if library == None: library = get_current_library()
	return library.grid

# deprecated: get vallue directly from library
def get_unit(library = None):
	if library == None: library = get_current_library()
	return library.unit

# deprecated: set value directly in library
def set_grid (grid_step , library = None):
	if library == None: library = get_current_library()
	library.set_grid(grid_step)

# deprecated: set value directly in library
def set_unit (unit ,library = None):
	if library == None: library = get_current_library()
	library.set_unit(unit)

def get_grids_per_unit(library = None):
	if library == None: library = get_current_library()
	return library.grids_per_unit

#round a distance to a grid value
# deprecated: set value directly in library
def snap_value (value, grids_per_unit = None):
	if grids_per_unit == None: grids_per_unit = get_grids_per_unit()
	return floor(value * grids_per_unit + 0.5) / (grids_per_unit)

def snap_coordinate(coordinate, grids_per_unit = None):
	if grids_per_unit == None: grids_per_unit = get_grids_per_unit()
	return coord2(floor(coordinate[0] * grids_per_unit +0.5) / (grids_per_unit),
		      floor(coordinate[1] * grids_per_unit + 0.5) / (grids_per_unit))

def snap_shape(coordinates, grids_per_unit = None):
	if grids_per_unit == None: grids_per_unit == get_grids_per_unit()
	sh = gds_shape(coordinates).snap_to_grid(grids_per_unit)
	return sh

#----------------------------------------------------------------------------
#Setting global variables
#----------------------------------------------------------------------------

def get_angle_step ():
	return Key_Angle_Step

def set_angle_step (step = Key_Angle_Step):
	global Key_Angle_Step
	Key_Angle_Step = step

def get_text_style ():
	return Key_Text_Style

def set_text_style (style = Text_Style_Polygons):
	global Key_Text_Style
	Key_Text_Style = style

#----------------------------------------------------------------------------
# Initialization
#----------------------------------------------------------------------------
from gk_library import *
from gk_output_gdsii import *
import gk_layer 

def ipkiss_initialise():
	global Key_Default_Library
	global Key_Default_Output
	global Key_LayerList
	
	Key_Default_Library = gds_stream_library("GK_Default", 1E-6, 5E-9)
	set_current_library(Key_Default_Library)

	Key_Default_Output = gds_output_gdsii()
	set_current_output(Key_Default_Output)

	Key_LayerList = gk_layer.gds_layerlist()