#!/usr/bin/perl
use strict;
use GDS2;
use Getopt::Std;
sub RectangleStructure($$$) {
    my $W = shift;
    my $L = shift;
    my $gds2File = shift;
    my $name = sprintf("rectangle-%3.1fx%3.1f", $W, $L);

    $gds2File -> printBgnstr( -name => $name);
    $gds2File -> printBoundary (
	-xy => [ -$W/2, -$L/2,
		  $W/2, -$L/2,
		  $W/2,  $L/2,
		 -$W/2,  $L/2]);
    $gds2File -> printEndstr();
    return $name;
}
#  -----------------------------------------------------------
#                M A I N   R O U T I N E 
#  -----------------------------------------------------------

my $project = $0;

my ($gds2File);
    $gds2File = new GDS2(-fileName => '>'. $project. '.gds');
    $gds2File -> printInitLib(-name =>'SampleLib');

# this size small box
my $Xrect = 5.5;
my $Yrect = 3.1;

# call subroutine to make small structure and get its name
my ($name) = RectangleStructure($Xrect, $Yrect, $gds2File);

#  -----------------------------------------------------------
#             M A I N  ( T O P)   S T R U C T U R E 
#  -----------------------------------------------------------

my $size    =   50; # outer size, including one unit gap

my $Xpitch   =  10;
my $Ypitch   =   5;

my $columns = int($size/$Xpitch);
my $rows    = int($size/$Ypitch);

# adjust the size if they are not even (multiple of pitch);
my ($Xspan) = $columns  * $Xpitch ;
my ($Yspan) = $rows     * $Ypitch ;

# small amount to shift each to make the cetner coord (0,0);
my ($Xoffset) =  0.5 * $Xpitch;
my ($Yoffset) =  0.5 * $Ypitch;

$gds2File -> printBgnstr(-name  => 'TOP');

# ---------------------------------
#     place array of Rectangle
# ---------------------------------
# place an array, with above offset
$gds2File -> printAref  (
    -name  =>	$name, 
    -columns =>	$columns,
    -rows =>	$rows,
    -xy =>
    [  -$Xspan/2 + $Xoffset, -$Yspan/2 + $Yoffset,
        $Xspan/2 + $Xoffset, -$Yspan/2 + $Yoffset,
       -$Xspan/2 + $Xoffset,  $Yspan/2 + $Yoffset  ]
    );

# -------------------------------------
# place another rectangle on top of array with layer 2 for reference
# -------------------------------------
my $W = $size - $Xpitch     + $Xrect;
my $L = $size - $Ypitch     + $Yrect;
    $gds2File -> printBoundary (
	-xy => [ -$W/2, -$L/2,
		  $W/2, -$L/2,
		  $W/2,  $L/2,
		 -$W/2,  $L/2],
	-layer=> 2
    );
printf "Width: %3.1f, Height: %3.1f\n", $W, $L;
# ---------------------------------
# conclude 'TOP' structure
# ---------------------------------
$gds2File -> printEndstr;

# ---------------------------------
# conclude library
# ---------------------------------
$gds2File -> printEndlib;
$gds2File -> close;

__END__
