File: spread.py

package info (click to toggle)
sketch 0.6.13-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,284 kB
  • ctags: 8,453
  • sloc: python: 34,711; ansic: 16,543; makefile: 83; sh: 26
file content (203 lines) | stat: -rw-r--r-- 5,609 bytes parent folder | download
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# Sketch script for spreading selected objects ("distribute" in XFig)
# (c) 2000 Michael Loin
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# these are based on abut_*.py

from Sketch import Point
import Sketch.Scripting


# spread objects horizontally (cascade left)

def spread_h_casc_l(context):
    pos = []
    for obj in context.document.SelectedObjects():
	pos.append((obj.coord_rect.left, obj))
    l = len(pos) - 1
    if l > 1:
	pos.sort()
	left1, ob = pos[0]
	left2, ob = pos[-1]
	skip = (left2 - left1) / l
	next = left1 + skip
	for left, obj in pos[1:-1]:
	    obj.Translate(Point(next - left, 0))
	    next = next + skip

Sketch.Scripting.AddFunction('spread_h_casc_l',
	'Spread Horizontal (cascade left)', spread_h_casc_l, menu = 'Arrange')


# spread objects horizontally (cascade right)

def spread_h_casc_r(context):
    pos = []
    for obj in context.document.SelectedObjects():
	pos.append((obj.coord_rect.right, obj))
    l = len(pos) - 1
    if l > 1:
	pos.sort()
	right1, ob = pos[0]
	right2, ob = pos[-1]
	skip = (right2 - right1) / l
	next = right1 + skip
	for right, obj in pos[1:-1]:
	    obj.Translate(Point(next - right, 0))
	    next = next + skip

Sketch.Scripting.AddFunction('spread_h_casc_r',
	'Spread Horizontal (cascade right)', spread_h_casc_r, menu = 'Arrange')


# spread objects horizontally (equidistant centers)

def spread_h_center(context):
    pos = []
    for obj in context.document.SelectedObjects():
	rect = obj.coord_rect
	pos.append(((rect.left + rect.right) / 2, obj))
    l = len(pos) - 1
    if l > 1:
	pos.sort()
	center1, ob = pos[0]
	center2, ob = pos[-1]
	gap = (center2 - center1) / l
	next = center1 + gap
	for center, obj in pos[1:-1]:
	    obj.Translate(Point(next - center, 0))
	    next = next + gap

Sketch.Scripting.AddFunction('spread_h_center',
	'Spread Horizontal (center)', spread_h_center, menu = 'Arrange')


# spread objects horizontally (gaps/overlaps of equal width)

def spread_h_bbox(context):
    pos = []
    sum = 0
    for obj in context.document.SelectedObjects():
	rect = obj.coord_rect
	width = rect.right - rect.left
	pos.append((rect.left, width, obj))
	sum = sum + width
    l = len(pos) - 1
    if l > 1:
	pos.sort()
	start, width1, ob = pos[0]
	end, width2, ob = pos[-1]
	gap = (end + width2 - start - sum) / l
	next = start + width1 + gap
	for left, width, obj in pos[1:-1]:
	    obj.Translate(Point(next - left ,0))
	    next = next + width + gap

Sketch.Scripting.AddFunction('spread_h_bbox',
	'Spread Horizontal (bbox)', spread_h_bbox, menu = 'Arrange')


# spread objects vertically (cascade bottom)

def spread_v_casc_b(context):
    pos = []
    for obj in context.document.SelectedObjects():
	pos.append((obj.coord_rect.bottom, obj))
    l = len(pos) - 1
    if l > 1:
	pos.sort()
	pos.reverse()
	bottom1, ob = pos[0]
	bottom2, ob = pos[-1]
	skip = (bottom1 - bottom2) / l
	next = bottom1 - skip
	for bottom, obj in pos[1:-1]:
	    obj.Translate(Point(0, next - bottom))
	    next = next - skip

Sketch.Scripting.AddFunction('spread_v_casc_b',
	'Spread Vertical (cascade bottom)', spread_v_casc_b, menu = 'Arrange')


# spread objects vertically (cascade top)

def spread_v_casc_t(context):
    pos = []
    for obj in context.document.SelectedObjects():
	pos.append((obj.coord_rect.top, obj))
    l = len(pos) - 1
    if l > 1:
	pos.sort()
	pos.reverse()
	top1, ob = pos[0]
	top2, ob = pos[-1]
	skip = (top1 - top2) / l
	next = top1 - skip
	for top, obj in pos[1:-1]:
	    obj.Translate(Point(0, next - top))
	    next = next - skip

Sketch.Scripting.AddFunction('spread_v_casc_t',
	'Spread Vertical (cascade top)', spread_v_casc_t, menu = 'Arrange')


# spread objects vertically (equidistant centers)

def spread_v_center(context):
    pos = []
    for obj in context.document.SelectedObjects():
	rect = obj.coord_rect
	pos.append(((rect.top + rect.bottom) / 2, obj))
    l = len(pos) - 1
    if l > 1:
	pos.sort()
	pos.reverse()
	center1, ob = pos[0]
	center2, ob = pos[-1]
	gap = (center1 - center2) / l
	next = center1 - gap
	for center, obj in pos[1:-1]:
	    obj.Translate(Point(0, next - center))
	    next = next - gap

Sketch.Scripting.AddFunction('spread_v_center',
	'Spread Vertical (center)', spread_v_center, menu = 'Arrange')


# spread objects vertically (gaps/overlaps of equal height)

def spread_v_bbox(context):
    pos = []
    sum = 0
    for obj in context.document.SelectedObjects():
	rect = obj.coord_rect
	height = rect.top - rect.bottom
	pos.append((rect.top, height, obj))
	sum = sum + height
    l = len(pos) - 1
    if l > 1:
	pos.sort()
	pos.reverse()
	start, height1, ob = pos[0]
	end, height2, ob = pos[-1]
	gap = (start - end + height2 - sum) / l
	next = start - height1 - gap
	for top, height, obj in pos[1:-1]:
	    obj.Translate(Point(0, next - top))
	    next = next - height - gap

Sketch.Scripting.AddFunction('spread_v_bbox',
	'Spread Vertical (bbox)', spread_v_bbox, menu = 'Arrange')