function urlAppend = rtwtrace(blocks,varargin)
% RTWTRACE Trace generated code for blocks in model

% Copyright 2007-2018 The MathWorks, Inc.
urlAppend = [];

if nargin > 0
    blocks = convertStringsToChars(blocks);
end

if nargin > 1
    [varargin{:}] = convertStringsToChars(varargin{:});
end

try
    blocks = Simulink.ID.getSID(blocks);
catch
end
if ~iscell(blocks)
    blocks = {blocks};
end

model = strtok(blocks{1},':/');
if nargin > 1
    target = varargin{1};
else
    target = 'rtw';
end
switch target
    case 'rtw'

        try
            simulinkcoder.internal.util.model2code(model, blocks);
        catch
        end
        
        traceInfo = RTW.TraceInfo.instance(model);
        if isa(traceInfo,'RTW.TraceInfo') && ~isempty(traceInfo.BuildDir)
            reportInfo = traceInfo.getReportInfo;
        else             
            reportInfo = rtw.report.getReportInfo(model);
        end
        if rtw.report.ReportInfo.DisplayInCodeTrace && ~reportInfo.hasWebview
            [~, urlAppend] = rtw.report.model2code(blocks, reportInfo);
            return;
        end

        if ~isa(traceInfo,'RTW.TraceInfo')
            traceInfo = RTW.TraceInfo(model);
        end
        if isempty(traceInfo.BuildDir)
            traceInfo.setBuildDir('');
        end
        
        hasTrace = ones(length(blocks), 1);
        for i=1:length(blocks)
            r = traceInfo.getRegistry(blocks{i});
            if isempty(r.location)
                hasTrace(i) = 0;
            end
        end
        traceableBlocks = blocks(logical(hasTrace));
        if ~isempty(traceableBlocks)
            reportInfo = traceInfo.getReportInfo;
            reportInfo.highlight(traceableBlocks);
        else % display "not traceable" warning page
            if length(blocks) == 1
                traceInfo.highlight(blocks{1});
            else
                msgId = 'rtwMsg_noTraceForSelectedBlocks';
                traceInfo.displayMessage(traceInfo.getCodeGenRptFullPathName, msgId, '');
            end
        end
    case 'hdl'
        reportInfo = hdlcoder.report.ReportInfo(model);
        reportInfo.model2code(blocks{1});
    case 'plc'
        reportInfo = PLCCoder.report.ReportInfo(model);
        reportInfo.model2code(blocks{1});
end
% LocalWords:  slhdlcoder sid plc
