Simulink递归生成各层级输入输出接口

在这里插入图片描述
Extend函数: Simulink递归延申接口方法
PortArrRecur函数: Simulink递归接口排布

classdef PortExtend < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure      matlab.ui.Figure
        Message       matlab.ui.control.TextArea
        Input         matlab.ui.control.EditField
        Output        matlab.ui.control.EditField
        Label_5       matlab.ui.control.Label
        ArrMethod     matlab.ui.control.DropDown
        Label_4       matlab.ui.control.Label
        VA_Param      matlab.ui.control.EditField
        Label_3       matlab.ui.control.Label
        GetPath       matlab.ui.control.Button
        Label_2       matlab.ui.control.Label
        ExtendButton  matlab.ui.control.Button
        DropDown      matlab.ui.control.DropDown
        Label         matlab.ui.control.Label
    end


    properties (Access = private)
        curPorts;
        Type;
        Dist;
        Depth;
        Path;
        VsIndex;
        ArrangeMethod;
        Incount;
        Outcount;
        VSFlag;
    end

    methods (Access = private)
        function VsIndex = Check(~,Path)
            index = strfind(Path,'/');
            index = [index length(Path)];
            VsIndex = 0;
            for i = length(index):-1 : 2
                if (strcmp(get_param(Path(1:index(i)),'Variant'),'on'))
                    VsIndex = i;
                    break
                end
            end
        end
    end


    % Callbacks that handle component events
    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app)
            warning('off');
            app.Input.Editable = 'off';
            app.Output.Editable = 'off';
            app.ExtendButton.Enable = 'off';
            app.VA_Param.Editable = 'off';
            app.ArrangeMethod = app.ArrMethod.Value;
            app.VsIndex = 0;
            app.VSFlag = false;
        end

        % Button pushed function: GetPath
        function GetPathPushed(app, event)
            %各个窗口的初始化
            app.Input.Editable = 'off';
            app.Output.Editable = 'off';
            app.Input.Value = '';
            app.Output.Value = '';
            app.VA_Param.Editable = 'off';
            app.VA_Param.Value = '';
            app.VsIndex = 0;
            app.Message.Value = '';
            
            Inport = find_system(gcs,'FindAll','on','SearchDepth',1,'selected','on','BlockType','Inport');
            Outport = find_system(gcs,'FindAll','on','SearchDepth',1,'selected','on','BlockType','Outport');
            Ports = [Inport;Outport];
            if isempty(Ports)
                return;
            end
            app.Type = get(Ports,'BlockType');
            app.curPorts = Ports;
            app.Path = get(Ports(1),'Parent');
            levels = strsplit(app.Path,'/');
            levels = levels(2:end);
            lcnt = 1;
            for i = 1 : length(levels)
                levels{i} = [num2str(lcnt),':',levels{i}];
                lcnt = lcnt + 1;
            end
            app.DropDown.Items = levels;
            app.Depth = length(levels);
            app.DropDown.ItemsData = 1:app.Depth;
            app.Dist = app.Depth - app.DropDown.Value + 1;
            app.ExtendButton.Enable = 'on';
            OutSigs = '';
            app.Incount = 0;
            app.Outcount = 0;
            
            for p = 1 : length(Ports)
                if length(Ports) == 1
                    curPortType = app.Type;
                else
                    curPortType = app.Type{p};
                end
                if strcmp(curPortType,'Inport')
                    app.Input.Editable = 'on';
                    app.Incount = app.Incount + 1;
                else
                    Ins = get(app.curPorts(p),'InputSignalNames');
                    Ins = Ins{1};
                    if isempty(Ins)
                        lineH = get(app.curPorts(p),'LineHandles');
                        Ins = get(get(lineH.Inport,'SrcPortHandle'),'PropagatedSignals');
                    end
                    if contains(Ins,'<')
                        Outputname = Ins(2:end-1);
                    else
                        Outputname = Ins;
                    end
                    if strcmp(Outputname,'') || isempty(Outputname)
                        app.Message.Value = '输出信号先接线';
                        app.ExtendButton.Enable = 'off';
                        return;
                    end
                    OutSigs = [OutSigs,Outputname,';'];
                    app.Outcount = app.Outcount + 1;
                end
            end
            if ~isempty(OutSigs)
                app.Output.Value = OutSigs(1:end - 1);
                app.VsIndex = Check(app,app.Path);
                if app.DropDown.Value <= app.VsIndex
                    app.VA_Param.Editable = 'on';
                    app.Message.Value = ['路径中有变体子系统,输入变体输出信号名',newline, ...
                        '若未指定名称则使用当前线上名称'];
                    app.VSFlag = true;
                end
            else
                app.Output.Value = '';
            end
        end

        % Button pushed function: ExtendButton
        function ExtendButtonButtonPushed(app, event)
            app.Message.Value = '';
            InSigs = strtrim(strsplit(app.Input.Value,';','CollapseDelimiters',false));
            OutSigs = strtrim(strsplit(app.Output.Value,';','CollapseDelimiters',false));
            VSSigs = strtrim(strsplit(app.VA_Param.Value,';','CollapseDelimiters',false));
            if length(VSSigs) == 1 && isempty(VSSigs{1}) && app.VSFlag
                VSSigs = OutSigs;
                app.VA_Param.Value = strjoin(VSSigs,';');
            end
            if ~isempty(app.Input.Value) 
                if length(InSigs) < app.Incount
                    app.Message.Value = ['输入信号和接口数量不匹配,缺少',...
                                        num2str(app.Incount - length(InSigs)),'个'];
                    return
                elseif length(InSigs) > app.Incount
                    app.Message.Value = ['输入信号和接口数量不匹配,多余',...
                                        num2str(length(InSigs) - app.Incount),'个'];
                    return
                end
            end
            if ~isempty(app.VA_Param.Value) 
                if app.VSFlag && length(VSSigs) ~= app.Outcount
                    if length(VSSigs) < app.Outcount
                        app.Message.Value = ['变体信号名和Outport接口数量不匹配,缺少',...
                            num2str(app.Outcount - length(VSSigs)),'个'];
                    elseif length(VSSigs) > app.Outcount
                        app.Message.Value = ['变体信号名和Outport接口数量不匹配,多余',...
                            num2str(length(VSSigs) - app.Outcount),'个'];
                    end
                    return
                end
            end
            Iidx = 1;
            Oidx = 1;
            for i = 1 : length(app.curPorts)
                if length(app.curPorts) == 1
                    curPortType = app.Type;
                else
                    curPortType = app.Type{i};
                end
                if strcmp(curPortType,'Inport')
                    PortName = InSigs{Iidx};
                    RealName = PortName;
                    isIn = true;
                    Iidx = Iidx + 1;
                else
                    PortName = OutSigs{Oidx};
                    if ~app.VSFlag
                        RealName = OutSigs{Oidx};
                    elseif isempty(VSSigs{Oidx})
                        VSSigs{Oidx} = OutSigs{Oidx};
                        RealName = OutSigs{Oidx};
                        app.VA_Param.Value = strjoin(VSSigs,';');
                    else
                        RealName = VSSigs{Oidx};
                    end
                    isIn = false;
                    Oidx = Oidx + 1;
                end
                set(app.curPorts(i),'Name',PortName);
                app.Message.Value = Extend(app.curPorts(i),app.Dist,PortName,isIn,RealName);
            end
            path = [app.Path,'/'];
            index = strfind(path,'/');
            for idx = length(index) : -1 : length(index) - app.Dist + 1
                SubHandle = getSimulinkBlockHandle(path(1 : index(idx)));
                switch app.ArrangeMethod
                    case '1'
                        PortArrRecur(SubHandle,200,200);
                    case '2'
                        PortArrNorm(SubHandle,200,200);
                end
            end
            
        end

        % Value changed function: DropDown
        function DropDownValueChanged(app, event)
            app.Message.Value = '';
            app.VA_Param.Value = '';
            app.Dist = app.Depth - app.DropDown.Value + 1;
            app.VA_Param.Editable = 'off';
            if app.DropDown.Value <= app.VsIndex
                app.VA_Param.Editable = 'on';
                app.Message.Value = ['路径中有变体子系统,输入变体输出信号名',newline, ...
                    '若未指定名称则使用当前线上名称'];
                app.VSFlag = true;
            end
        end

        % Value changed function: ArrMethod
        function ArrMethodValueChanged(app, event)
            app.ArrangeMethod = app.ArrMethod.Value;
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.Position = [100 100 736 237];
            app.UIFigure.Name = 'MATLAB App';

            % Create Label
            app.Label = uilabel(app.UIFigure);
            app.Label.HorizontalAlignment = 'right';
            app.Label.Position = [393 172 53 22];
            app.Label.Text = '目的层级';

            % Create DropDown
            app.DropDown = uidropdown(app.UIFigure);
            app.DropDown.Items = {};
            app.DropDown.ValueChangedFcn = createCallbackFcn(app, @DropDownValueChanged, true);
            app.DropDown.Position = [461 172 213 22];
            app.DropDown.Value = {};

            % Create ExtendButton
            app.ExtendButton = uibutton(app.UIFigure, 'push');
            app.ExtendButton.ButtonPushedFcn = createCallbackFcn(app, @ExtendButtonButtonPushed, true);
            app.ExtendButton.Enable = 'off';
            app.ExtendButton.Position = [582 33 100 23];
            app.ExtendButton.Text = 'Extend';

            % Create Label_2
            app.Label_2 = uilabel(app.UIFigure);
            app.Label_2.HorizontalAlignment = 'right';
            app.Label_2.FontWeight = 'bold';
            app.Label_2.Position = [31 172 65 22];
            app.Label_2.Text = '输入信号名';

            % Create GetPath
            app.GetPath = uibutton(app.UIFigure, 'push');
            app.GetPath.ButtonPushedFcn = createCallbackFcn(app, @GetPathPushed, true);
            app.GetPath.Position = [321 33 100 23];
            app.GetPath.Text = '获取路径';

            % Create Label_3
            app.Label_3 = uilabel(app.UIFigure);
            app.Label_3.HorizontalAlignment = 'right';
            app.Label_3.Position = [19 87 77 22];
            app.Label_3.Text = '变体模型输出';

            % Create VA_Param
            app.VA_Param = uieditfield(app.UIFigure, 'text');
            app.VA_Param.Editable = 'off';
            app.VA_Param.Position = [105 87 253 22];

            % Create Label_4
            app.Label_4 = uilabel(app.UIFigure);
            app.Label_4.HorizontalAlignment = 'right';
            app.Label_4.Position = [44 33 53 22];
            app.Label_4.Text = '接口整理';

            % Create ArrMethod
            app.ArrMethod = uidropdown(app.UIFigure);
            app.ArrMethod.Items = {'递归', '非递归'};
            app.ArrMethod.ItemsData = {'1', '2'};
            app.ArrMethod.ValueChangedFcn = createCallbackFcn(app, @ArrMethodValueChanged, true);
            app.ArrMethod.Position = [112 33 100 22];
            app.ArrMethod.Value = '2';

            % Create Label_5
            app.Label_5 = uilabel(app.UIFigure);
            app.Label_5.HorizontalAlignment = 'right';
            app.Label_5.FontWeight = 'bold';
            app.Label_5.Position = [31 131 65 22];
            app.Label_5.Text = '输出信号名';

            % Create Output
            app.Output = uieditfield(app.UIFigure, 'text');
            app.Output.Editable = 'off';
            app.Output.Position = [105 131 253 22];

            % Create Input
            app.Input = uieditfield(app.UIFigure, 'text');
            app.Input.Editable = 'off';
            app.Input.Position = [105 172 253 22];

            % Create Message
            app.Message = uitextarea(app.UIFigure);
            app.Message.Editable = 'off';
            app.Message.FontSize = 13;
            app.Message.FontWeight = 'bold';
            app.Message.Position = [393 87 281 66];

            % Show the figure after all components are created
            app.UIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = PortExtend

            % Create UIFigure and components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.UIFigure)

            % Execute the startup function
            runStartupFcn(app, @startupFcn)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值