How can I load GDS II into matlab?

I want to load GDS II layout format into matlab in the form of a matrix, how can I do that?

 Accepted Answer

José-Luis
José-Luis on 2 Jun 2014
  1. The hard way: find out how the GDSII binary is structured and read it directly into Matlab.
  2. The compromise way: Use a GDSII to ascii converter (Google will find you one in less than 30s) and then read that into Matlab. importdata(), textscan(), are then your friends.
  3. The easy way: Look in the file exchange to see if anyone has done that before

7 Comments

Reem
Reem on 3 Jun 2014
Edited: Walter Roberson on 10 May 2024
Has anyone tried the GDSII Toolbox from:
thanks, Reem
Hi,
I tried it and it works decently well generating gds from MATLAB. I'm trying to find out if it can do the reverse - something like converting gds patterns into MATLAB matrices or images but it doesn't seem so easy.
Thanks,
Jonathan
Did you succeed? I'm eager to know! Thx a lot!!!!
You can use GDSII Toolbox for this. You get get the Toolbox at https://sites.google.com/site/ulfgri/numerical/gdsii-toolbox.
When you read GDSII Toolbox manual it's easy to see how to make a GDS file from a set of polygon points but it's not obvious how to read the GDS file and get the polygon points back into matlab, but it can be done. With a little help from Ulf Griesmann the creater of GDSII Toolbox I was able to figure it out.
Below is a modifed verion of the Example_basic.m file in the GDSII Toolbox manual that should help others read the GDS files. This modifed version has a second section that recovers the orignal polygon points.
%
% makes a basic gds file and then reads the contents of gds file
%
clear all
%% Makes a GDS file with two polygons
% create a structure to hold elements
gs = gds_structure('BASIC');
% create two polygons
xy1 = 1000 * [0,0; 0,1; 1,1; 1,0; 0,0]; % 1mm x 1mm
xy2 = bsxfun(@plus, xy1, [1000, 1000]);
% create boundary elements and add to the structure (on different layers)
gs(end+1) = gds_element('boundary', 'xy',xy1, 'layer',1);
gs(end+1) = gds_element('boundary', 'xy',xy2, 'layer',2);
% create a library to hold the structure
glib = gds_library('TWO_BLOCKS', 'uunit',1e-6, 'dbunit',1e-9, gs);
% finally write the library to a file
write_gds_library(glib, '!basic.gds');
%% Recovers the two polygons from the GDS file
% A GDS library (file) can be thought of as an array of structures, each of which is an array of elements.
% Read GDS file
L = read_gds_library('basic.gds');
treeview(L) % shows there is only one structure
S = L(1); % setting the structure equal to S which has 2 elements
% Recover the first polygon
S1 = S(1);
xy1_recovered =S1.xy;
xy1_recovered = xy1_recovered{1}
% Recover the second polygon
S2 = S(2);
xy2_recovered =S2.xy;
xy2_recovered = xy2_recovered{1}
%Another method to recover the polygons
vector_1 = L.BASIC(1).xy;
xy1_recovered = vector_1{1}
vector_1 = L.BASIC(2).xy;
xy2_recovered = vector_1{1}
Andrew, this is super helpful, thanks! Do you know if there is a way to extract the number of elements in the gds structure in order to loop through an unknown number of elements?
Yes, you can loop through the elements. See example below. The key command is numel(GDS_structure) which gives you the number of elements.
% Read GDS file
GDS_Library = read_gds_library(filename); % file must be in the same folder as this matlab script
%treeview(GDS_Library) % shows the structures
GDS_box = bbox(GDS_Library); % Calculates the rectangular bounding box of a gds_structure object.
Point_units = get(GDS_Library,'uunit'); % (m) defualt is 10-6 m = 1 um
GDS_structure = GDS_Library(1); % setting the 1st structure equal to GDS_structure which can have multiple elements
% Number_of_elements = numel(GDS_Library);
Number_of_elements = numel(GDS_structure); % or size(GDS_structure(:),2) gives the number of elements in structure or Library
% Recover the polygon points for each element
for i= 1:Number_of_elements
GDS_elements = GDS_structure(i); % Get each element in the structure
xy_recovered = GDS_elements.xy; % or xy_recovered = get(GDS_elements, 'xy'), sometimes cell array sometimes numeric array
%xy_recovered = xy_recovered{1};
%xy_point{i} = xy_recovered; % This is a matlab cell structure. Normal matlab rules apply to it.
if iscell(xy_recovered)==1
xy_point{i}=cell2mat(xy_recovered); % This is a matlab cell structure
elseif isnumeric(xy_recovered) == 1
xy_point{i} = xy_recovered; % This is a matlab cell structure
else
whos xy_recovered
end
end
Hello I guess i never got notified of you response but I had found and made numel work at the time. Your solution to catch the cell vs numeric format is really helpful though, I was just catching and skipping otherwise, since mostly the errors were empty anyways.
I found this post again in trying to solve a new problem I'm running into. Im still importing preexisting gds, but now it's a set of tiered cells (many separate gds structures within the library). I can pull out the number of structures and then nest to get all the elements but it doesnt capture their global coordinates relative to the mask. So all the structures are placed at 0,0. Is there a way to pull the coordinate of each each structure relative to the whole mask? bbox didnt seem to help, they still mostly originated at 0,0.
val is a GDSII library:
Library name : Absorber.DB
Database unit : 5e-08 m
User unit : 1e-06 m
Structures : 21
1 ... Absorber_Overlay (16)
2 ... 6mm_die (21)
3 ... Alignment_marks_3 (192)
.... etc
gds = read_gds_library(path); %read in the gyro gds
%Point_units = get(gds,'uunit');
%Struct = gds(1); %extract the structure
%ElNum = numel(Struct);
ElNumTot = numel(gds); %det how many elements (shapes) w/in the set of structure
LCC_XY=[];
%cnt=0;
for kk = 1:length(gds)
Struct = gds(kk); %extract the structure
elTemp = numel(Struct);
bboxStruct = bbox(Struct); %
xStart=0;%bboxStruct(1)*1e3;
yStart=0;%bboxStruct(2)*1e3;
for ii=1:elTemp
LCC = Struct(ii);
xy_recovered = LCC.xy; %xy_recovered = get(GDS_elements, 'xy');
%xy_recovered = xy_recovered{1};
%xy_point{i} = xy_recovered; % This is a matlab cell structure. Normal matlab rules apply to it.
if iscell(xy_recovered)==1
LCC_XY{ii}=cell2mat(xy_recovered).*1e3; % This is a matlab cell structure
elseif isnumeric(xy_recovered) == 1
LCC_XY{ii} = xy_recovered.*1e3; % This is a matlab cell structure
else
whos xy_recovered
end
% try
% LCC_XY = xy_recovered{1}*1e3+[xStart,yStart];
LCC_out_discrete = round(LCC_XY{ii}/grid)*grid; % round to discrete points on the grid
%catch
%skip
% end
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Asked:

on 2 Jun 2014

Edited:

on 1 Nov 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!