function dbreline(filename)
%BDRELINE redraws the lines in an existing SIMULINK block diagram window.
%	BDRELINE SLWINDOW
%	BDRELINE('SLWINDOW')
%	redraws all of the line in simulink block diagram window SLWINDOW. 
%	The function will not redraw the lines inside the groups. 
%	BDRELINE
%	redraws the lines in current SIMULINK window.
%
%	BDRELINE UNDO or BDRELINE('UNDO")
%	undo the result of the latest bdreline.
%
%	see also DBRESIZE, AUTOLINE

%	Wes Wang 2/16/93
%	Copyright (c) 1990-94 by The MathWorks, Inc.
%	All Right Reserved.

global reline_filename reline_layout

if nargin <=0
  filename = get_param;
end;
if isempty(filename)
  disp('Please input reline filename')
end;
if strcmp(filename,'UNDO') | strcmp(filename,'undo') | ...
	strcmp(filename,'Undo') | strcmp(filename,'UnDo')
  %in the case of undo
  if ~exist('reline_filename')
    disp('BDRELINE UNDO can be used only right after BDRELINE is used.')
    return;
  end;
  if isempty(reline_filename)
    disp('DERELINE UNDO can be used only right after BDRELINE is used.')
    return;
  end;

  [n_line,m_line] = size(reline_layout);
  blocks=get_param(reline_filename,'blocks');
  for i = 1:n_line
     tmp = reline_layout(i,6:m_line);
     ind = find(~isnan(tmp));
     tmp = tmp(ind);
     delete_line(reline_filename,...
	[deblank(blocks(reline_layout(i,1),:)) '/' num2str(reline_layout(i,2))],...
  	[deblank(blocks(reline_layout(i,3),:)) '/' num2str(reline_layout(i,4))])
     temp = [];
     for i=1:length(tmp)/2
       temp = [temp; tmp((i-1)*2+1:i*2)];
     end;   
     add_line(reline_filename,temp);
  end;

  reline_filename=[]; reline_layout=[];
else
  %the case of regular reline
  open_system(filename)
  blocks=get_param(filename,'blocks');
  %In the case of empty window
  if isempty(blocks)
    return;
  end;
  [n_block, m_block] = size(blocks);
  if ~isempty(find(blocks=='/'))
     for i=1:n_block
        tmp = find(blocks(i,:)=='/');
        if ~isempty(tmp)
           for j=length(tmp): -1: 1
              temp = length(blocks(i,:));
              blocks(i,tmp(j)+1:temp+1) = blocks(i,tmp(j):temp);
           end;
        end;
     end;
  end;
  %In the case of not empty
  posit=zeros(n_block,4);
  orien=zeros(n_block,1);
  for i=1:n_block
    tmp =  [filename '/' deblank(blocks(i,:))];
    posit(i,:) = get_param(tmp,'position');
    orien(i) = get_param(tmp,'orientation');
  end;
  if ~isempty(find(orien<0))
    disp('Illigal orientaiton, Call Mathworks for help');
    return;
  elseif ~isempty(find(orien>3))
    disp('Illigal orientaiton, Call Mathworks for help');
    return;
  end;
  reline_layout = get_param(filename,'lines');
  if isempty(reline_layout)
    disp('There is no line in this window')
    return
  end;
  [n_line,m_line] = size(reline_layout);
  temp = reline_layout;
  while ~isempty(temp)
     [n,m] = size(temp);
     tmp = temp(1,6:m);
     ind = find(~isnan(tmp));
     tmp = tmp(ind);
     delete_line(filename,tmp);
     temp = get_param(filename,'lines');
  end;
  temp = find(reline_layout(:,1)<=0);
  while ~isempty(temp)
     for i=1:length(temp)
        tmp = reline_layout(temp(i),5);
        if tmp 
           reline_layout(temp(i),1) = reline_layout(tmp,1);
        else
           reline_layout(temp(i),1) = -1;
        end;
     end;
     temp = find(reline_layout(:,1)==0);
  end;
  for i = 1:n_line
     if isempty(find(reline_layout(i,1:4)<=0))
        tmp_f = deblank(blocks(reline_layout(i,1),:));
        tmp_f = [tmp_f, '/' num2str(reline_layout(i,2))];
        tmp_t = deblank(blocks(reline_layout(i,3),:));
        tmp_t = [tmp_t, '/' num2str(reline_layout(i,4))];
        add_line(filename,tmp_f,tmp_t);
        drawnow;
     end;
  end;
  reline_filename=filename;
end;






