function bdresize(Percentage,SimulinkBlock)
%BDRESIZE resize SIMULINK block diagram window at given rate
%	BDRESIZE RATE SIMULINKBLOCK
%	or
%	RESIZE(RATE,'SIMULINKBLOCK')
%	resize the block diagram window and rearrange the window size of 
%	SIMULINK window indicated in SIMULINKBLOCK. If RATE is omited, 
%	BDRESIZE will fit the current SIMULINK window size. If SIMULINKBLOCK
%	is omitted, BDRESIZE will resize the current window.

%	Wes Wang 3/2/93
%	Copyright (c) 1990-94 by The MathWorks, Inc.
%	$Revision: 1.9 $  $Date: 1994/01/25 21:32:48 $
%	All Right Reserved.
%

global resize_slna resize_bllc resize_line resize_loca

if nargin == 1
   if strcmp(Percentage,'UNDO') |...
	strcmp(Percentage,'undo') |...
	strcmp(Percentage,'Undo') |...
	strcmp(Percentage,'UnDo')
     if ~exist('resize_slna')
	disp('BDRESIZE UNDO can be used only right after BDRESIZE is used.');
        return;
     end;

     bloc = get_param(resize_slna,'blocks');
     if isempty(bloc)
       disp('BDRESIZE UNDO can be used only right after BDRESIZE is used.');
       return;
     end;
     [n_block, m_block] = size(bloc);
     if ~isempty(find(bloc=='/'))
        for i=1:n_block
           tmp = find(bloc(i,:)=='/');
           if ~isempty(tmp)
              for j=length(tmp): -1: 1
                 temp = length(bloc(i,:));
                 bloc(i,tmp(j)+1:temp+1) = bloc(i,tmp(j):temp);
              end;
           end;
        end;
     end;

     line = get_param(resize_slna,'lines');
     [n_line, m_line] = size(line);
     temp = line;
     while ~isempty(temp)
       [n,m] = size(temp);
       tmp = temp(1,6:m);
       ind = find(~isnan(tmp));
       tmp = tmp(ind);
       delete_line(resize_slna,tmp);
       temp = get_param(resize_slna,'lines');
     end;

     [n_bl,m_bl] = size(resize_bllc);

     for i = 1:n_bl
       set_param([resize_slna '/' deblank(bloc(i,:))],'position',resize_bllc(i,:));
     end;

     drawnow;  
     [n_line, m_line] = size(resize_line);
     for i = 1:n_line
       tmp = resize_line(i,6:m_line);
       ind = find(~isnan(tmp));
       tmp = tmp(ind);
       temp = ones(length(tmp)/2,2);
       temp(:,1) = tmp(1:2:length(tmp))';
       temp(:,2) = tmp(2:2:length(tmp))';
       add_line(resize_slna,temp);
     end;
     set_param(resize_slna,'Location',resize_loca)
     resize_slna=[]; resize_bllc=[]; resize_line=[]; resize_loca=[];
     clear resize_slna resize_bllc resize_line resize_loca
     return;
   end;
end;
if nargin == 1
   if isstr(Percentage)
      SimulinkBlock = Percentage;
      Percentage = [];
   else
      SimulinkBlock = get_param;
   end;
elseif nargin == 0
   SimulinkBlock = get_param;
   Percentage = [];
end;
if isempty(SimulinkBlock)
   SimulinkBlock = get_param;
end;
if isempty(SimulinkBlock)
   disp('Please specify your SIMULINK filename')
   return
end;

loca = get_param(SimulinkBlock,'Location');
if isempty(loca)
  fprintf('Simulink Block ');fprintf(SimulinkBlock); 
  fprintf(' does not exist.\n')
  return;
end;

%get the parameters from the window
bloc = get_param(SimulinkBlock,'blocks');
     [n_block, m_block] = size(bloc);
     if ~isempty(find(bloc=='/'))
        for i=1:n_block
           tmp = find(bloc(i,:)=='/');
           if ~isempty(tmp)
              for j=length(tmp): -1: 1
                 temp = length(bloc(i,:));
                 bloc(i,tmp(j)+1:temp+1) = bloc(i,tmp(j):temp);
              end;
           end;
        end;
     end;
line = get_param(SimulinkBlock,'lines');

[n_bl,m_bl] = size(bloc);
bllc = zeros(n_bl,4);
for i=1:n_bl
  bllc(i,:) = get_param([SimulinkBlock '/' deblank(bloc(i,:))],'position');
  orien(i) = get_param([SimulinkBlock '/' deblank(bloc(i,:))],'orientation');
end;

%max/min
x_mi = min(bllc(:,1));
x_ma = max(bllc(:,3));
y_mi = min(bllc(:,2));
y_ma = max(bllc(:,4));

%set the ratio
if isempty(Percentage)
  rati_x = (loca(3) - loca(1))/(x_ma - x_mi + 40);
  rati_y = (loca(4) - loca(2))/(y_ma - y_mi + 40);
  Percentage = min(rati_x,rati_y);
else
  if isstr(Percentage)
    Percentage = str2num(Percentage);
  end;
  rati_x = Percentage;
  rati_y = Percentage;
end;

if (Percentage <= 0.001) | (Percentage >= 1000)
  disp('The resize index is unresonable');
  return
end;

%Shifting. Make the margin equals 20
shif_x = 20 - x_mi;
shif_y = 20 - y_mi;

%remove old lines
[n_line, m_line] = size(line);
temp = line;
while ~isempty(temp)
  [n,m] = size(temp);
  tmp = temp(1,6:m);
  ind = find(~isnan(tmp));
  tmp = tmp(ind);
  delete_line(SimulinkBlock,tmp);
  temp = get_param(SimulinkBlock,'lines');
end;

%relocate blocks
new_bllc(:,1) = (bllc(:,1) + shif_x) * rati_x;
new_bllc(:,3) = (bllc(:,3) + shif_x) * rati_x;
new_bllc(:,2) = (bllc(:,2) + shif_y) * rati_y;
new_bllc(:,4) = (bllc(:,4) + shif_y) * rati_y;

tmp = new_bllc(:,3) - new_bllc(:,1);
temp = find(tmp<3);
if ~isempty(temp)
  new_bllc(temp,3) = new_bllc(temp,1)+3;
end;

tmp = new_bllc(:,4) - new_bllc(:,2);
temp = find(tmp < 3);
if ~isempty(temp)
  new_bllc(temp,4) = new_bllc(temp,2) + 3;
end;

for i = 1:n_bl
  set_param([SimulinkBlock '/' deblank(bloc(i,:))],'position',new_bllc(i,:));
end;

%reset the window size if it is necessary.
resize_loca = loca;
x_ma = max(new_bllc(:,3));
y_ma = max(new_bllc(:,4));
if nargin >= 1
  loca(3) = loca(1) + x_ma + 20;
  loca(4) = loca(2) + y_ma + 20;
  set_param(SimulinkBlock,'Location',loca)
end;

drawnow
%rearrange lines

  temp = find(line(:,1)<=0);
  while ~isempty(temp)
     for i=1:length(temp)
        tmp = line(temp(i),5);
        if tmp 
           line(temp(i),1) = line(tmp,1);
        else
           line(temp(i),1) = -1;
        end;
     end;
     temp = find(line(:,1)==0);
  end; 
for i = 1:n_line
   if isempty(find(line(i,1:4)<=0))
      tmp_f = deblank(bloc(line(i,1),:));
      tmp_f = [tmp_f, '/', num2str(line(i,2))];
      tmp_t = deblank(bloc(line(i,3),:));
      tmp_t = [tmp_t, '/', num2str(line(i,4))];     
      add_line(SimulinkBlock,tmp_f,tmp_t);
   else
     tmp = line(i,6:m_line);
     ind = find(~isnan(tmp));
     tmp = tmp(ind);
     temp = ones(length(tmp)/2,2);
     temp(:,1) = (tmp(1:2:length(tmp))' + shif_x) * rati_x;
     temp(:,2) = (tmp(2:2:length(tmp))' + shif_y) * rati_y;
     add_line(SimulinkBlock,temp);
  end;
  drawnow
end;

resize_slna = SimulinkBlock;
resize_bllc = bllc;
resize_line = line;


