function [origSys,sliceRootSys] = generateModelForSubsystemSlice(sliceFileName,ssIOActivity, msObj)

    % Generate slice model for subsystem slicing.
    
    % Copyright 2014-2021 The MathWorks, Inc.
        
    import Transform.SubsystemSliceUtils.*  %#ok<*NSTIMP>
    import Transform.ModelRefUtils.*
    isDynamicSlice = ~isempty(msObj.cvd);
    
    bd_warning_obj = warning('query',...
        'Simulink:Harness:BlockDiagramHarnessNameChange');
    old_bd_warning_state = bd_warning_obj.state;
    sync_warning_obj = warning('query',...
        'Simulink:Harness:SyncFailedAsSourceIsCompiled');
    old_sync_warning_state1 = sync_warning_obj.state;
    sync_warning_obj = warning('query',...
        'Simulink:Harness:SyncWillFailAsSourceIsCompiled');
    old_sync_warning_state2 = sync_warning_obj.state;
    
    warning('off', 'Simulink:Harness:BlockDiagramHarnessNameChange');
    warning('off', 'Simulink:Harness:SyncFailedAsSourceIsCompiled');
    warning('off', 'Simulink:Harness:SyncWillFailAsSourceIsCompiled');

    cleanupObj = onCleanup(@() restoreWarnings(old_bd_warning_state,...
                                                old_sync_warning_state1,old_sync_warning_state2));
    
    if ~isempty(msObj.simHandler)
        origCovFilter = msObj.simHandler.getOriginalCovFilter;
    else
        origCovFilter = '';
    end
    if ~isDynamicSlice
        % Static subsystem slice
        if ~Simulink.SubsystemType.isModelBlock(msObj.sliceSubSystemH)
            extractSubsystemObj = Sldv.SubSystemExtract('slicerextract');

            [status, extModelH, err_msg] = extractSubsystemObj.extract(msObj.sliceSubSystemH, ...
                false, false, false, [], true);

            if ~status
                rethrow(err_msg);
            end
        else
            extModelH = extractMdlRefBlk(msObj.sliceSubSystemH,ssIOActivity);
        end
        % Removing inactive Inport/Outport/DSMs
        removingInactiveInterface(extModelH,ssIOActivity);
        % Setting DSW priorities
        setDSWPriorities(extModelH,ssIOActivity,msObj.sliceSubSystemH);
        testUnitH = extModelH;
        save_system(extModelH,sliceFileName);
        applyCoverageFilter(msObj.modelH, msObj.sliceSubSystemH, extModelH, sliceFileName,origCovFilter);
    else
        % Dynamic subsystem slice
        
        % Slicing inactive port info in sldvData to generate sliced signal
        % builder block and test harness.
        simDataLog = msObj.simDataLog;
        % Extracting slice subsystem
        if ~Simulink.SubsystemType.isModelBlock(msObj.sliceSubSystemH)
            extractSubsystemObj = Sldv.SubSystemExtract('slicerextract');
            [status, extModelH, err_msg] = extractSubsystemObj.extract(msObj.sliceSubSystemH, ...
                false, false, false, [], true);
            if ~status
                rethrow(err_msg);
            end
            InputPortInfo = simDataLog.LoggedTestUnitInfo.SubsystemLogging.TestSubsystem.InputPortInfo;
            deadLogIdx= identfyInactivePortIndexInLog(InputPortInfo, ssIOActivity);
            simDataLog.LoggedTestUnitInfo.SubsystemLogging.TestSubsystem.InputPortInfo(deadLogIdx) = [];
            simDataLog.TestCases.dataValues(deadLogIdx) = [];
        else
            extModelH = extractMdlRefBlk(msObj.sliceSubSystemH,ssIOActivity);
            if isfield(simDataLog.LoggedTestUnitInfo,'ModelBlock')
                InputPortInfo = simDataLog.LoggedTestUnitInfo.ModelBlock.ReferencedModel.InputPortInfo;
                deadLogIdx= identfyInactivePortIndexInLog(InputPortInfo, ssIOActivity);
                simDataLog.LoggedTestUnitInfo.ModelBlock.ReferencedModel.InputPortInfo(deadLogIdx) = [];
                simDataLog.TestCases.dataValues(deadLogIdx) = [];
                if isfield(simDataLog.LoggedTestUnitInfo,'SldvHarnessModel')
                   simDataLog.LoggedTestUnitInfo.SldvHarnessModel.TestUnitModel.InputPortInfo(deadLogIdx) = [];
                end
            end
        end
        
        % Removing inactive Inport/Outport/DSMs
        removingInactiveInterface(extModelH,ssIOActivity);
        % Setting DSW priorities
        setDSWPriorities(extModelH,ssIOActivity,msObj.sliceSubSystemH);
        
        % Generate harness model
        hopts = sldvharnessopts();
        hopts.harnessSource = 'Signal Builder';
        hopts.modelRefHarness = false;
        hopts.noDocBlock = true;
        hopts.harnessFilePath = sliceFileName;
        [harnessPath, warnmsgs] = ...
            Sldv.HarnessUtils.make_model_harness(extModelH, simDataLog, hopts, 'slicemakeharness'); %#ok<ASGLU>
        [~,sliceMdl] = fileparts(sliceFileName);
        close_system(extModelH,0);
        save_system(sliceMdl);
        close_system(sliceMdl);
        load_system(sliceFileName);
        set_param(sliceMdl,'StopTime',num2str(max(simDataLog.TestCases.timeValues(:))));
        testUnit = find_system(sliceMdl,'RegExp','on','SearchDepth',1,'Name','^Test_Unit');
        testUnitH = get_param(testUnit{1},'Handle');
        set_param(testUnitH,'Permissions','ReadWrite')
        
        if Simulink.SubsystemType.isModelBlock(msObj.sliceSubSystemH)
            set_param(testUnitH,'Name','Test_Unit');
        end

        applyCoverageFilter(msObj.modelH, msObj.sliceSubSystemH, get_param(sliceMdl,'Handle'), sliceFileName, origCovFilter);

    end
    
    if ~isempty(msObj.simHandler)
        msObj.simHandler.applyOrigCovParams(get_param(bdroot(testUnitH),'Handle'));
    end
    
    origSys = getfullname(msObj.sliceSubSystemH);
    sliceRootSys = getfullname(testUnitH);
    
    function restoreWarnings(old_bd_warning_state,old_sync_warning_state1,old_sync_warning_state2)
        warning(old_bd_warning_state, 'Simulink:Harness:BlockDiagramHarnessNameChange');
        warning(old_sync_warning_state1, 'Simulink:Harness:SyncFailedAsSourceIsCompiled');
        warning(old_sync_warning_state2, 'Simulink:Harness:SyncWillFailAsSourceIsCompiled');
    end
end
