function fd_load

% function fd_load
%
% This function is used in conjunction with FD3.  It loads flow diagrams which
% were previously saved by the program.
%
% ACP

% Last update:  July 29, 1993
% Author:       Adrian C. Penisson

global FD3STATUS FD3FIGURE FD3MODULES FD3STREAMS FD3BOXES FD3TEXT FD3MENUS
global FD3AXES FD3TITLE

% Turn off all the menus so user doesn't do anything silly.

set_menu(1:6,'off')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Save Diagram if Necessary %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if FD3STATUS(4)

   % If the diagram needs to be saved, ask the user if he wishes to do so.

   choice=di_choic('','Save Current Diagram First?','YES','NO');

   if choice==1

      % Save the file if the user asks to.

      fd_save(0);

   end

end

%%%%%%%%%%%%%%%%
% Get Filename %
%%%%%%%%%%%%%%%%

wflag=1;

while wflag

   % Querry user for file name

   [file,flag]=di_inp_c('Load what file?','',0);

   if flag

      % If user cancels, abort load process.

      di_warn('','Load Aborted','CONTINUE');
      fd_reset(0)
      return

   else

      % Otherwise, try and open the file for reading.

      [fid,message]=fopen(file,'r','b');

      if length(message)>0

	 % If this branch is followed, there was an error when trying to read
         % the file.

	 di_warn('FILE ERROR','File does not exist or cannot be read.',...
	    'CONTINUE');

      else

	 % If this branch is followed, the file was openable.  Set wflag to
	 % break out of loop.

	 wflag=0;

      end

   end

end

%%%%%%%%%%%%%%%%%%%%%%%%%%
% Erase old flow diagram %
%%%%%%%%%%%%%%%%%%%%%%%%%%

fd_clear

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The Load Begins (Finally!) %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Get version (not used at this point in time -- will be used to make up for
% differences in file types at a later date).

filever=fread(fid,1,'integer*1');

% Get # of modules, streams, texts, and boxes.

lens=fread(fid,4,'integer*1');

%%%%%%%%%%%%%%%%%%%%
% Recreate Modules %
%%%%%%%%%%%%%%%%%%%%

for i=2:lens(1)

   % Read in the name of the m-file used to create the module.  Then read
   % in the data associated with that module and its label.

   module=fgetl(fid);
   record=fread(fid,9,'float32');
   txt=fgetl(fid);

   % read font info which is currently not used

   fgetl(fid);
   fgetl(fid);
   fgetl(fid);
   fread(fid,1,'integer*1');

   % Call the correct statement for creating the module.  It is dependent on
   % the number of arguments -- record(3).

   if record(3)==1
      data=eval(sprintf([module '([%18.14f,%18.14f])'],record(1),record(2)));
   elseif record(3)==2
      data=eval(sprintf([module '([%18.14f,%18.14f],%18.14f)'],record(1),...
	 record(2),record(4)));
   else
      data=eval(sprintf([module '([%18.14f,%18.14f],%18.14f,%18.14f)'],...
	 record(1),record(2),record(4),record(5)));
   end

   % Store the m-file's name in the object's UserData

   set(data(1),'User',module)

   % Make label of module string stored in text since the user may not have
   % used the default label.

   if ~data(2)
      axes(FD3AXES(2))
      data(2)=nice_txt(data(4),data(5),txt,0);
   else
      set(data(2),'String',txt);	
   end

   % Make the module and label clickable. Give the text appropriate rotation.
   % Add module to FD3MODULES.

   set(data(1:2),'ButtonDownFcn','clk_obj','Inter','yes')
   set(data(2),'Rotation',round(record(9)))

   % Make secondary object, if present, clickable

   if data(3)
      set(data(3),'ButtonDownFcn','clk_obj','Inter','yes')
   end

   FD3MODULES=[FD3MODULES data'];

   % Give module appropriate rotation and resizing.

   mod_rot(i,record(6));
   mod_resz(i,record(7),record(8));

end

%%%%%%%%%%%%%%%%%%%%
% Recreate Streams %
%%%%%%%%%%%%%%%%%%%%

axes(FD3AXES(2))

for i=2:lens(2)

   % Read index of feed module, index of product module, x coord for text,
   % y coord for text, and text rotation.  Read 'type' of stream (1==HandV,
   % 2==straight).  Read number of xdata pts and then read xdata and ydata.
   % Read label.

   record=fread(fid,5,'float32');
   type=fread(fid,1,'integer*1');
   pts=fread(fid,1,'integer*1');
   xdata=fread(fid,pts,'float32');
   ydata=fread(fid,pts,'float32');
   txt=fgetl(fid);

   % Read font info which is currently not used.

   fgetl(fid);
   fgetl(fid);
   fgetl(fid);
   fread(fid,1,'integer*1');

   % Make stream, label, and arrow.

   h=line(xdata,ydata);
   th=nice_txt(record(3),record(4),txt,round(record(5)));
   ah=make_arw(xdata,ydata);

   % Set button down function and store proper information in FD3STREAMS.

   set([h th ah],'ButtonDownFcn','clk_obj','Inter','yes')
   mod1=FD3MODULES(1,record(1));
   mod2=FD3MODULES(1,record(2));
   FD3STREAMS=[FD3STREAMS [h th ah mod1 mod2 type]'];

end

%%%%%%%%%%%%%%%%%%
% Recreate Texts %
%%%%%%%%%%%%%%%%%%

for i=1:lens(3)

   % Read x, y, rot, and text.

   record=fread(fid,3,'float32');
   txt=fgetl(fid);

   % read font info.  currently not used.

   fgetl(fid);
   fgetl(fid);
   fgetl(fid);
   fgetl(fid);
   fread(fid,1,'integer*1');

   % Create text object, make clickable, and add to FD3TEXT.

   th=nice_txt(record(1),record(2),txt,round(record(3)));
   set(th,'ButtonDownFcn','clk_obj','Inter','yes');
   FD3TEXT=[FD3TEXT th];

end

%%%%%%%%%%%%%%%%%%
% Recreate Boxes %
%%%%%%%%%%%%%%%%%%

axes(FD3AXES(1))

for i=2:lens(4)

   % Read type (dashed==1, solid==2), number of pts (should always be 4),
   % xdata, and ydata.

   type=fread(fid,1,'integer*1');
   pts=fread(fid,1,'integer*1');
   xdata=fread(fid,pts,'float32');
   ydata=fread(fid,pts,'float32');

   % Make box

   bh=line(xdata,ydata);

   % If dashed, make it so.

   if type==1
      set(bh,'LineStyle','--');
   end

   % Make box clickable add to FD3BOXES.

   set(bh,'ButtonDownFcn','clk_obj','Inter','yes');
   FD3BOXES=[FD3BOXES [bh type]'];

end

%%%%%%%%%
% Bumps %
%%%%%%%%%

% Set FD3STATUS(1) to the opposite of what it was when it was saved since
% tgl bmps flip/flops the status.

FD3STATUS(1)=~fread(fid,1,'integer*1');
tgl_bmp;

%%%%%%%%%
% Title %
%%%%%%%%%

FD3TITLE=fgetl(fid);

% read font info.  currently not used.

fgetl(fid);
fgetl(fid);
fgetl(fid);
fread(fid,1,'integer*1');

fclose(fid);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% miscellania (if that's a word) %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Set figure name

set(FD3FIGURE,'Name',['FROG Draw 3: ' file])

% Restack all the modules

restack

% Set status to indicate file has a name and does not currently need saving.

FD3STATUS(3)=0;
fd_reset(2)
