#######################################################################
#
# $Id: ImportGDSII.py,v 1.3 2008/06/23 06:21:59 lorenz Exp $
# 
# Copyright (C) 2004-2008 Petr Lorenz
# All rights reserved.
# 
# ##### BEGIN LICENSE BLOCK #####
#
# Version: MPL 1.1 / GPL 2.0 / LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
# 
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
# License for the specific language governing rights and limitations
# under the License.
# 
# The Original Code is Petr Lorenz emGine Graphical User Interface (GUI).
# 
# The Initial Developer of the Original Code is Petr Lorenz.
# Portions created by the Initial Developer are Copyright (C) 2004-2008.
# All Rights Reserved.
# 
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the  "GPL"), or the
# GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
# case the provisions of the GPL or the LGPL are applicable instead of those
# above.  If you wish to allow use of your version of this file only under the
# terms of either the GPL or the LGPL and not to allow others to use your
# version of this file under the MPL, indicate your decision by deleting the
# provisions above and replace them with the notice and other provisions
# required by the GPL or LGPL.  If you do not delete the provisions above, a
# recipient may use your version of this file under the terms of any one of the
# MPL License, the GPL or the LGPL.
#
# ##### END LICENSE BLOCK #####
#
#######################################################################
# 
# $Log: ImportGDSII.py,v $
# Revision 1.3  2008/06/23 06:21:59  lorenz
# Bugfix for multiple elements on the same layer returning one layer number.
#
# Revision 1.2  2008-06-12 15:11:44  lorenz
# Included support for GDSII files.
#
# Revision 1.1.2.2  2008-06-12 05:11:34  lorenz
# Changes from dos2unix.
#
# Revision 1.1.2.1  2008-06-11 15:15:00  lorenz
# Initial commit.
#
#
#
#######################################################################

# June 2008

import locallibs.ipkiss.all as ipkiss
#import ipkiss.gds_key as gds_key

class ImportGDSII:
  def __init__(self, fname, prefix = "emGine_imported_"):
    self.fname = fname
    self.input_gds = ipkiss.gds_input_gdsii(file(fname, "rb"))
    self.input_gds.prefix = prefix

    self.library_gds = self.input_gds.read()
    self.top_structure = self.library_gds.top_layout()

    self.elements = None

    #self.structures = gds_key.import_gds(name = "emGine_imported", filename = self.fname, prefix = "emGine_imp1_")

  def get_top_layout(self):
    return self.top_structure

  def get_name(self):
    return self.top_structure.name

  def get_units(self, struct_ind = 0):
    return self.library_gds.unit
    #return self.top_structure.get_unit()

  def get_grid(self, struct_ind = 0):
    return self.library_gds.grid
    #return self.structures[struct_ind].get_grid()

  def get_shapes_on_layer(self, layer_ind = 0, struct_ind = 0):
    None
    #elems = self.structures[struct_ind].get_elements()
    #return elems

  def get_all_coords_on_layer(self, layer_ind = 0, struct_ind = 0):
    None
    #shapes = self.get_shapes_on_layer(layer_ind, struct_ind)
    #coords = list()
    #for shape in shapes:
      #if shape.layer.number == layer_ind:
	#coords.append(shape.shape.get_points())

    #return coords

  def get_elements(self):
    if self.elements != None:
      return self.elements

    self.elements = self.top_structure.get_elements()
    return self.elements

  def get_elements_on_layer(self, laynum):
    elems = self.get_elements()

    elem_list = []

    lay_required = "LAYER" + str(laynum)
    for elem in elems:
      lay_verb = str(elem.layer)
      if lay_verb == lay_required:
	elem_list.append(elem)

    return elem_list

  def get_layers(self):
    elems = self.get_elements()
    layer_list = []
    for elem in elems:
      lay_verb = str(elem.layer)
      laynum = int(lay_verb.split("LAYER")[1])
      if laynum not in layer_list:
	layer_list.append(laynum)

    return layer_list

## Module functions
def ImportGDSII_check_element_type(element, type):
  #print element 
  if type == 'polygon':
    if isinstance(element, ipkiss.gds_layer_shape_element):
      return True
  elif type == 'patch':
    if isinstance(element, ipkiss.gds_box_element):
      return True

  return False


### Testing the module
if __name__ == '__main__':
  imp1 = ImportGDSII('test1.gds')
  structure_top = imp1.get_top_layout()		# top structure
  print "units:\t", imp1.get_units()
  print "grid:\t", imp1.get_grid()

  print ""
  print "created:\t", structure_top.created
  print "modified:\t", structure_top.modified
  print "comment:\t", structure_top.comment
  print "name:\t\t", structure_top.name

  print ""
  print "is_empty: ", structure_top.is_empty()
  print "size_info: ", structure_top.size_info()

  print ""
  print "elements:\t", imp1.get_elements()

  layers = imp1.get_layers()
  print ""
  print "layers:\t", layers

  for lay in layers:
    print "\n"
    print "** elements on layer ", str(lay), ":"
    elements = imp1.get_elements_on_layer(lay)
    print elements
    #elem1 = elements[0]
    #print elem1
    #print isinstance(elem1, ipkiss.gds_box_element)
  
  #print structure_top.flatten()
