Simulink输入输出模块与端口对齐 您所在的位置:网站首页 simulink电气端口 Simulink输入输出模块与端口对齐

Simulink输入输出模块与端口对齐

2023-09-05 00:02| 来源: 网络整理| 查看: 265

Simulink输入输出模块与端口对齐 1. 获取当前模块输入输出端口的句柄1.1 获取当前模块的端口句柄1.2 分别存储输入端口句柄和输出端口句柄 2. 输入模块左对齐2.1 获取输入端口所连接的源输入模块句柄2.2 更新源输入模块位置 3. 输出模块右对齐3.1 获取输出端口所连接的目标模块句柄3.2 更新目标模块位置 4. 如何使用 本文介绍如何使用matlab脚本语言实现对Simulink中子系统或者模块的端口对齐功能。

1. 获取当前模块输入输出端口的句柄 port_handles=get_param(gcbh,'PortHandles'); %gcbh获取当前模块句柄 Inport_handles=port_handles.Inport; outport_handles=port_handles.Outport;

在这里插入图片描述

1.1 获取当前模块的端口句柄 port_handles=get_param(gcbh,'PortHandles');

gcbh 返回当前系统中当前模块的句柄。 获取当前选择模块的输入输出端口句柄;

1.2 分别存储输入端口句柄和输出端口句柄 Inport_handles=port_handles.Inport; outport_handles=port_handles.Outport;

将输入输出端口句柄,放在Inport_handles和outport_handles中;

2. 输入模块左对齐 for i=1:length(Inport_handles) line_handles=get_param(Inport_handles(i),'Line'); src_handles=get_param(line_handles,'SrcBlockHandle'); port_pos=get(Inport_handles(i),'Position'); src_pos=get_param(src_handles,'Position'); src_len=src_pos(3)-src_pos(1); src_width=src_pos(4)-src_pos(2); new_pos(1)=port_pos(1)-100; new_pos(3)=new_pos(1)+src_len; new_pos(2)=port_pos(2)-fix(src_width/2); new_pos(4)=port_pos(2)+fix(src_width/2); set_param(src_handles,'Position',new_pos); end

在这里插入图片描述

2.1 获取输入端口所连接的源输入模块句柄 line_handles=get_param(Inport_handles(i),'Line'); src_handles=get_param(line_handles,'SrcBlockHandle');

根据输入端口Inport_handles的句柄,获取与输入端口相连接的Line的句柄; 再依据Line的句柄,获取与该Line相连接的源模块句柄,并存放在src_handles中;

2.2 更新源输入模块位置 port_pos=get(Inport_handles(i),'Position'); src_pos=get_param(src_handles,'Position'); src_len=src_pos(3)-src_pos(1); src_width=src_pos(4)-src_pos(2); new_pos(1)=port_pos(1)-100; new_pos(3)=new_pos(1)+src_len; new_pos(2)=port_pos(2)-fix(src_width/2); new_pos(4)=port_pos(2)+fix(src_width/2); set_param(src_handles,'Position',new_pos);

port_pos选中子系统输入端口的位置; src_pos源输入模块的初始位置,用来确认输入模块的大小; src_len源输入模块长度; src_width源输入模块高度; 根据上面的相关数据,计算并更新源输入模块的位置;

3. 输出模块右对齐 for i=1:length(outport_handles) line_handles = get_param(outport_handles(i),'Line'); src_handles =get_param(line_handles,'DstBlockHandle'); port_pos=get(outport_handles(i),'Position'); src_pos=get_param(src_handles,'Position'); src_len=src_pos(3)-src_pos(1); src_width=src_pos(4)-src_pos(2); new_pos(3)=port_pos(1)+200; new_pos(1)=new_pos(3)-src_len; new_pos(2)=port_pos(2)-fix(src_width/2); new_pos(4)=port_pos(2)+fix(src_width/2); set_param(src_handles,'Position',new_pos); end 3.1 获取输出端口所连接的目标模块句柄 line_handles = get_param(outport_handles(i),'Line'); src_handles =get_param(line_handles,'DstBlockHandle');

根据输出端口outport_handles的句柄,获取与输入端口相连接的Line的句柄; 再依据Line的句柄,获取与该Line相连接的目标模块句柄,并存放在src_handles 中; 这里的src_handles与上面源输入模块句柄存放位置一致;

3.2 更新目标模块位置 port_pos=get(outport_handles(i),'Position'); src_pos=get_param(src_handles,'Position'); src_len=src_pos(3)-src_pos(1); src_width=src_pos(4)-src_pos(2); new_pos(3)=port_pos(1)+200; new_pos(1)=new_pos(3)-src_len; new_pos(2)=port_pos(2)-fix(src_width/2); new_pos(4)=port_pos(2)+fix(src_width/2); set_param(src_handles,'Position',new_pos);

与上面更新源输入模块同理,这里对于输出模块进行右对齐;

4. 如何使用

使用该脚本时,选中一个子系统或者stateflow模型,运行脚本就可以将该模块相连接的输入输出模块与端口对齐了;

源码

%% 自定义菜单功能PortAlignment % 2021.05.04 % Author: LL %% 自定义菜单功能PortAlignment function PortAlignment(~) clc;clear; port_handles=get_param(gcbh,'PortHandles'); %gcbh获取当前模块句柄 Inport_handles=port_handles.Inport; for i=1:length(Inport_handles) line_handles=get_param(Inport_handles(i),'Line'); src_handles=get_param(line_handles,'SrcBlockHandle'); port_pos=get(Inport_handles(i),'Position'); src_pos=get_param(src_handles,'Position'); src_len=src_pos(3)-src_pos(1); src_width=src_pos(4)-src_pos(2); new_pos(1)=port_pos(1)-100; new_pos(3)=new_pos(1)+src_len; new_pos(2)=port_pos(2)-fix(src_width/2); new_pos(4)=port_pos(2)+fix(src_width/2); set_param(src_handles,'Position',new_pos); end outport_handles=port_handles.Outport; for i=1:length(outport_handles) line_handles = get_param(outport_handles(i),'Line'); src_handles =get_param(line_handles,'DstBlockHandle'); port_pos=get(outport_handles(i),'Position'); src_pos=get_param(src_handles,'Position'); src_len=src_pos(3)-src_pos(1); src_width=src_pos(4)-src_pos(2); new_pos(3)=port_pos(1)+200; new_pos(1)=new_pos(3)-src_len; new_pos(2)=port_pos(2)-fix(src_width/2); new_pos(4)=port_pos(2)+fix(src_width/2); set_param(src_handles,'Position',new_pos); end clear; end


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有