function [objH, errStr] = getObjH(obj, checkSFOptions)
%

%   Copyright 2009-2021 The MathWorks, Inc.
    if nargin<2
        checkSFOptions = false;
    end

    errStr = '';
    objH = [];
    if ischar(obj) || isstring(obj)
        [objH, errStr] = getHandle(obj);
    elseif isa(obj, 'Simulink.BlockPath')
        % If obj is a Simulink.BlockPath, return the leaf element
        lastIndex = obj.getLength;      % Get last element index
        [objH, errStr] = getHandle(obj.getBlock(lastIndex));    % Get last element handle
    else               
        if ishandle(obj)
            if checkSFOptions
                if isa(obj,'Stateflow.Chart')
                    [objH, errStr] = getHandle(obj.Path);
                elseif isa(obj,'Stateflow.AtomicSubchart')                            
                    objH = sf('get',obj.Id,'.simulink.blockHandle');                
                elseif isa(obj,'double')
                    [objH, errStr] = getHandle(obj);            
                end  
            else 
                [objH, errStr] = getHandle(obj);
            end
        else
            errStr = getString(message('Sldv:shared:utils:InvalidObject'));
        end
    end
end      

function [objH, errStr] = getHandle(obj)
    errStr = '';
    objH = [];
    try
        objH = get_param(obj,'Handle');
    catch Mex
        errStr = Mex.message;
    end
end
