function out=execute(c)
%EXECUTE generates report contents
%   OUT=EXECUTE(C)

%   Copyright (c) 1997-2000 by The MathWorks, Inc.
%   $Revision: 1.16 $  $Date: 2000/03/23 19:37:42 $

%Take snapshots of scope blocks

currContext=getparentloop(c);
scopeList=searchblocktype(c.zslmethods,...
   {'BlockType','Scope'},...
   currContext);


[xyFigureList,xyBlockList]=LocXYfigures(c.zslmethods,...
   searchblocktype(c.zslmethods,...
   {'MaskType','XY scope.'},...
   currContext));

numScopes=length(scopeList);
numXYGraphs=length(xyFigureList);

if numScopes>0 | numXYGraphs>0
   snapComp=c.rptcomponent.comps.chgfigsnap;
   
   att=snapComp.att;
   att.isCapture=logical(0);
   att.ImageFormat=c.att.ImageFormat;
   att.PaperOrientation=c.att.PaperOrientation;
   att.isResizeFigure='manual';
   att.PrintSize=c.att.PaperSize;
   att.PrintUnits=c.att.PaperUnits;
   if c.att.isInvertHardcopy
      att.InvertHardcopy='on';
   else
      att.InvertHardcopy='off';
   end
   
   
   snapComp.att=att;
   
   for i=numScopes+numXYGraphs:-1:numXYGraphs+1
      [out{i},errString]=LocTakeSnapshot(scopeList{i-numXYGraphs},...
         snapComp,...
         c.att.isForceOpen,...
         c.att.isInvertHardcopy,...
         c.att.CaptionType);
      if length(errString)>0
         status(c,errString,1);
      end
   end
   
   if numXYGraphs>0
      snapComp.att.InvertHardcopy='off';
      
      for i=numXYGraphs:-1:1
         figH=xyFigureList(i);
         
         snapComp.att.FigureHandle=figH;
         snapComp.att.ImageTitle=xyBlockList{i};
         snapComp.att.Caption=LocGetCaption(c.att.CaptionType,xyBlockList{i});
         
         if c.att.isInvertHardcopy
            oldLineColors=LocDarkenLines(figH);
            oldFigColor=get(figH,'Color');
            set(figH,'color',[ 1 1 1]);
         end
         
         out{i}=runcomponent(snapComp,5);
         
         if c.att.isInvertHardcopy
            set(figH,'color',oldFigColor);
            LocDarkenLines(figH,oldLineColors);
         end
      end
   end
else
   %if no scopes were found
   out='';
end

%----------Take XY Graph Snapshots


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [figTag,errString]=LocTakeSnapshot(scopeBlock,...
   snapComp,...
   isForceOpen,...
   isInvert,...
   capType)  

errString={};

try
   scopeHandle=get_param(scopeBlock,'Figure');
catch
   scopeHandle=[-1];
end

try
   scopeName=get_param(scopeBlock,'Name');
catch
   scopeName='';
end


openedScope=0;
%0 = not opened
%1 = opened
if ishandle(scopeHandle)
   try
      isVisible=strcmp(get(scopeHandle,'Visible'),'on');
   catch
      isVisible='off';
   end
   
   if ~isForceOpen & ~isVisible
      %if the scope exists but is invisible and force open is
      %not on, we don't want to report on the scope
      scopeHandle=[-1];
   end
else %if scopeHandle is not a valid handle
   if isForceOpen
      try
         set_param(scopeBlock,'Open','on');
         scopeHandle=get_param(scopeBlock,'Figure');
         openedScope=1;
      catch
         errString{end+1}=sprintf('Error - could not open scope "%s"', scopeName );
      end
   else
      scopeHandle=[-1];
   end
end
      
if ishandle(scopeHandle)
   tempHandle=simscope('PrintFigure',scopeHandle);
   

   if isInvert
      LocDarkenLines(tempHandle);
   else
      set(tempHandle,'color',[ .5 .5 .5 ]);
      %axText=findall(allchild(tempHandle),...
      %   'type','text');
      %set(axText,'Color',[0 0 0]);
      %allAx=findall(allchild(tempHandle),'flat',...
      %   'type','axes');
      %set(allAx,...
      %   'Xcolor',[0 0 0],...
      %   'Ycolor',[0 0 0],...
      %   'Zcolor',[0 0 0]);
   end
   
   
   snapComp.att.FigureHandle=tempHandle;
   snapComp.att.ImageTitle=scopeName;
   snapComp.att.Caption=LocGetCaption(capType,scopeBlock);
   
   figTag=runcomponent(snapComp,5);      
   
   delete(tempHandle);
else
   figTag='';
end
      
if openedScope==1
   try
      set_param(scopeBlock,'Open','off');         
   catch
         errString{end+1}=sprintf('Error - could not open scope "%s"', scopeName );
   end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [foundFigs,foundBlocks]=LocXYfigures(z,allBlocks)

foundFigs=[];
foundBlocks={};

if ~isempty(allBlocks)
   allFigs=findall(allchild(0),'flat',...
      'Tag','SIMULINK_XYGRAPH_FIGURE');
   
   allBlockHandles=getparam(z,allBlocks,'handle');
   allBlockHandles=[allBlockHandles{:}];
   
   allBlockNames=getparam(z,allBlocks,'name');
   
   for i=1:length(allFigs)
      ud=get(allFigs(i),'UserData');
      if isstruct(ud) & isfield(ud,'Block')
         blockIndex=find(allBlockHandles==ud.Block);
         
         if ~isempty(blockIndex)
            blockIndex=blockIndex(1);
            
            foundFigs(end+1)=allFigs(i);
            foundBlocks(end+1)=allBlockNames(blockIndex);
         end
      end
   end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
function  prevColor=LocDarkenLines(figHandle,setColor);
%The yellow lines on scope plots are hard
%to see when printed with inverthardcopy

if nargin<2
   setColor=[.1 .1 .1];
end

lineHandles=findall(allchild(figHandle),...
   'type','line');

prevColor=get(lineHandles,'color');

if iscell(setColor)
   colorName={'color'};
else
   colorName='color';
end

set(lineHandles,colorName,setColor);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function caption=LocGetCaption(capType,blk);

switch capType
case '$auto'
   desc=getparam(zslmethods,blk,'Description');
   caption=desc{1};
case '$manual'
   caption=parsevartext(c,c.att.CaptionString);
otherwise
   caption='';
end