forked from petercorke/robotics-toolbox-matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distributeblocks.m
83 lines (68 loc) · 2.63 KB
/
distributeblocks.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
%DISTRIBUTEBLOCKS Distribute blocks in Simulink block library
%
% distributeBlocks(MODEL) equidistantly distributes blocks in a Simulink
% block library named MODEL.
%
% Notes::
% - The MATLAB functions to create Simulink blocks from symbolic
% expresssions actually place all blocks on top of each other. This
% function scans a simulink model and rearranges the blocks on an
% equidistantly spaced grid.
% - The Simulink model must already be opened before running this
% function!
%
% Author::
% Joern Malzahn, ([email protected])
%
% See also symexpr2slblock, doesblockexist.
% Copyright (C) 2012-2013, by Joern Malzahn
%
% This file is part of The Robotics Toolbox for Matlab (RTB).
%
% RTB is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% RTB is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU Lesser General Public License for more details.
%
% You should have received a copy of the GNU Leser General Public License
% along with RTB. If not, see <http://www.gnu.org/licenses/>.
%
% http://www.petercorke.com
%
% The code generation module emerged during the work on a project funded by
% the German Research Foundation (DFG, BE1569/7-1). The authors gratefully
% acknowledge the financial support.
function [ ] = distributeblocks( mdlName )
%% Get a list of all first level blocks:
blockNames = find_system(mdlName,'SearchDepth',1);
blockNames = blockNames(2:end); % First cell contains the model name
%% Determine the maximum width and height of a block
allPosC = get_param(blockNames,'Position');
allPosM = cell2mat(allPosC); % [left top right bottom] The maximum value for a coordinate is 32767
widths = allPosM(:,3)-allPosM(:,1);
heights = allPosM(:,4)-allPosM(:,2);
maxWidth = max(widths);
maxHeight = max(heights);
%% Set grid spacing
wBase = 2*maxWidth;
hBase = 2*maxHeight;
%% Start rearranging blocks
nBlocks = size(allPosM,1);
nColRow = ceil(sqrt(nBlocks));
for iBlocks = 1:nBlocks
% block location on grid
[row,col] = ind2sub([nColRow, nColRow],iBlocks);
% compute block coordinates
left = col*wBase;
right = left+maxWidth;
top = row*hBase;
bottom = top+maxHeight;
% apply new coordinates
set_param(blockNames{iBlocks},'Position',[left top right bottom])
end
end