% Get the handles to the copy of the blocks in the model copy
    
%   Copyright 2013-2014 The MathWorks, Inc.
function [bhCopy, vbh] = getCopyHandles(bh, refMdlToMdlBlk, mdl, mdlCopy)
    
    assert(isa(mdlCopy, 'char'));
    assert(isa(bh, 'double'));
    
    if isempty(strfind(mdl,'/'))
        isSubsystemSlice = false;
    else
        isSubsystemSlice = true;
    end
    util = Transform.MappingUtil(mdl, mdlCopy, refMdlToMdlBlk, isSubsystemSlice);
    bhCopy = zeros(size(bh));
    filt = false(size(bh));
    for i=1:length(bh)
        bhCopy(i) = getBlockCopyHandle(bh(i));
        filt(i) = bhCopy(i) > 0;
    end
    
    bhCopy = bhCopy(filt);
    vbh = bh(filt);
    
    function bhCopy = getBlockCopyHandle(h)
        copyPath = util.getCopyPath(h);
        try
            bhCopy = get_param(copyPath, 'Handle');
        catch ex
            if strcmp(ex.identifier, 'Simulink:Commands:InvSimulinkObjectName')
                % Skip synthesized block
                bhCopy = -1;
            else
                rethrow(ex);
            end
        end
    end
    
end

