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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
| import FreeCAD as App
import Part
doc = App.newDocument("DeskRiser_Half_165x230x80_RoundedCableHoles_TieHoles_2x2")
# =====================
# 1個分の外寸
# =====================
outer_w = 165.0
outer_d = 230.0
outer_h = 80.0
# =====================
# 板厚
# =====================
top_t = 12.0
side_t = 12.0
back_t = 10.0
inner_h = outer_h - top_t # 68mm
# =====================
# 角丸ケーブル穴寸法
# =====================
side_hole_w = 60.0 # 横支え穴の幅(Y方向)
side_hole_h = 28.0 # 横支え穴の高さ(Z方向)
back_hole_w = 70.0 # 後ろ穴の幅(X方向)
back_hole_h = 28.0 # 後ろ穴の高さ(Z方向)
hole_r = 8.0 # 穴そのものの角丸半径
entry_fillet_r = 2.0 # 大きいケーブル穴入口のフチの丸み
# =====================
# 結束バンド用の小穴
# =====================
tie_hole_d = 5.5
tie_hole_r = tie_hole_d / 2.0
tie_hole_entry_fillet_r = 0.8
# 前後位置
tie_hole_front_y = 28.0
tie_hole_back_y = outer_d - 28.0 # 202mm
# 縦に2穴
tie_hole_zs = [18.0, 32.0]
# =====================
# 穴位置
# =====================
side_hole_center_y = outer_d / 2.0
side_hole_center_z = inner_h / 2.0
back_hole_center_x = outer_w / 2.0
back_hole_center_z = inner_h / 2.0
tol = 0.3
# =====================
# 本体
# =====================
top = Part.makeBox(
outer_w,
outer_d,
top_t,
App.Vector(0, 0, inner_h)
)
left_side = Part.makeBox(
side_t,
outer_d,
inner_h,
App.Vector(0, 0, 0)
)
right_side = Part.makeBox(
side_t,
outer_d,
inner_h,
App.Vector(outer_w - side_t, 0, 0)
)
back_wall = Part.makeBox(
outer_w,
back_t,
inner_h,
App.Vector(0, outer_d - back_t, 0)
)
shape = top.fuse(left_side).fuse(right_side).fuse(back_wall)
# =====================
# 角丸長方形カット:横支え用
# Y-Z平面の穴をX方向に貫通
# =====================
def make_side_rounded_cut(x_start, cut_depth, center_y, center_z, width, height, r):
box1 = Part.makeBox(
cut_depth,
width - 2 * r,
height,
App.Vector(
x_start,
center_y - (width - 2 * r) / 2,
center_z - height / 2
)
)
box2 = Part.makeBox(
cut_depth,
width,
height - 2 * r,
App.Vector(
x_start,
center_y - width / 2,
center_z - (height - 2 * r) / 2
)
)
cut = box1.fuse(box2)
corner_centers = [
(center_y - width / 2 + r, center_z - height / 2 + r),
(center_y + width / 2 - r, center_z - height / 2 + r),
(center_y - width / 2 + r, center_z + height / 2 - r),
(center_y + width / 2 - r, center_z + height / 2 - r),
]
for y, z in corner_centers:
cyl = Part.makeCylinder(
r,
cut_depth,
App.Vector(x_start, y, z),
App.Vector(1, 0, 0)
)
cut = cut.fuse(cyl)
return cut
# =====================
# 角丸長方形カット:後ろ補強用
# X-Z平面の穴をY方向に貫通
# =====================
def make_back_rounded_cut(y_start, cut_depth, center_x, center_z, width, height, r):
box1 = Part.makeBox(
width - 2 * r,
cut_depth,
height,
App.Vector(
center_x - (width - 2 * r) / 2,
y_start,
center_z - height / 2
)
)
box2 = Part.makeBox(
width,
cut_depth,
height - 2 * r,
App.Vector(
center_x - width / 2,
y_start,
center_z - (height - 2 * r) / 2
)
)
cut = box1.fuse(box2)
corner_centers = [
(center_x - width / 2 + r, center_z - height / 2 + r),
(center_x + width / 2 - r, center_z - height / 2 + r),
(center_x - width / 2 + r, center_z + height / 2 - r),
(center_x + width / 2 - r, center_z + height / 2 - r),
]
for x, z in corner_centers:
cyl = Part.makeCylinder(
r,
cut_depth,
App.Vector(x, y_start, z),
App.Vector(0, 1, 0)
)
cut = cut.fuse(cyl)
return cut
# =====================
# 結束バンド用の丸穴
# 側板をX方向に貫通
# =====================
def make_tie_hole_x(x_start, cut_depth, center_y, center_z, radius):
return Part.makeCylinder(
radius,
cut_depth,
App.Vector(x_start, center_y, center_z),
App.Vector(1, 0, 0)
)
# =====================
# 大きいケーブル穴を開ける
# =====================
left_cut = make_side_rounded_cut(
-1.0,
side_t + 2.0,
side_hole_center_y,
side_hole_center_z,
side_hole_w,
side_hole_h,
hole_r
)
right_cut = make_side_rounded_cut(
outer_w - side_t - 1.0,
side_t + 2.0,
side_hole_center_y,
side_hole_center_z,
side_hole_w,
side_hole_h,
hole_r
)
back_cut = make_back_rounded_cut(
outer_d - back_t - 1.0,
back_t + 2.0,
back_hole_center_x,
back_hole_center_z,
back_hole_w,
back_hole_h,
hole_r
)
shape = shape.cut(left_cut)
shape = shape.cut(right_cut)
shape = shape.cut(back_cut)
# =====================
# 結束バンド用の小穴を開ける
# 前後2か所 × 縦2穴
# 左右側板にも同じ穴を開ける
# =====================
tie_positions_y = [tie_hole_front_y, tie_hole_back_y]
for y in tie_positions_y:
for z in tie_hole_zs:
left_tie = make_tie_hole_x(
-1.0,
side_t + 2.0,
y,
z,
tie_hole_r
)
right_tie = make_tie_hole_x(
outer_w - side_t - 1.0,
side_t + 2.0,
y,
z,
tie_hole_r
)
shape = shape.cut(left_tie)
shape = shape.cut(right_tie)
# =====================
# フィレット用ヘルパー
# =====================
def is_close(a, b, tol=0.3):
return abs(a - b) <= tol
def in_range(v, vmin, vmax, tol=0.3):
return (v >= vmin - tol) and (v <= vmax + tol)
# =====================
# 大きいケーブル穴の入口フィレット
# =====================
def collect_large_cable_hole_edges(shape):
edges = []
side_ymin = side_hole_center_y - side_hole_w / 2.0
side_ymax = side_hole_center_y + side_hole_w / 2.0
side_zmin = side_hole_center_z - side_hole_h / 2.0
side_zmax = side_hole_center_z + side_hole_h / 2.0
back_xmin = back_hole_center_x - back_hole_w / 2.0
back_xmax = back_hole_center_x + back_hole_w / 2.0
back_zmin = back_hole_center_z - back_hole_h / 2.0
back_zmax = back_hole_center_z + back_hole_h / 2.0
x_planes = [0.0, side_t, outer_w - side_t, outer_w]
y_planes = [outer_d - back_t, outer_d]
for edge in shape.Edges:
bb = edge.BoundBox
# 横穴の開口部まわり
if bb.XLength <= tol:
x_plane = bb.XMin
if any(is_close(x_plane, p, tol) for p in x_planes):
cy = (bb.YMin + bb.YMax) / 2.0
cz = (bb.ZMin + bb.ZMax) / 2.0
if in_range(cy, side_ymin, side_ymax, tol) and in_range(cz, side_zmin, side_zmax, tol):
edges.append(edge)
continue
# 後ろ穴の開口部まわり
if bb.YLength <= tol:
y_plane = bb.YMin
if any(is_close(y_plane, p, tol) for p in y_planes):
cx = (bb.XMin + bb.XMax) / 2.0
cz = (bb.ZMin + bb.ZMax) / 2.0
if in_range(cx, back_xmin, back_xmax, tol) and in_range(cz, back_zmin, back_zmax, tol):
edges.append(edge)
continue
return edges
large_edges = collect_large_cable_hole_edges(shape)
if large_edges:
try:
shape = shape.makeFillet(entry_fillet_r, large_edges)
except Exception as e:
print("Large cable-hole fillet failed. Try entry_fillet_r = 1.0")
print(e)
# =====================
# 結束バンド用の小穴にも入口フィレット
# =====================
def collect_tie_hole_edges(shape):
edges = []
x_planes = [0.0, side_t, outer_w - side_t, outer_w]
tie_ys = [tie_hole_front_y, tie_hole_back_y]
for edge in shape.Edges:
bb = edge.BoundBox
# 小穴の開口エッジはX面上に出る円形エッジ
if bb.XLength <= tol:
x_plane = bb.XMin
if any(is_close(x_plane, p, tol) for p in x_planes):
cy = (bb.YMin + bb.YMax) / 2.0
cz = (bb.ZMin + bb.ZMax) / 2.0
if any(is_close(cy, y, 1.0) for y in tie_ys) and any(is_close(cz, z, 1.0) for z in tie_hole_zs):
# 直径5.5mm穴周辺だけ拾う
if bb.YLength <= tie_hole_d + 1.5 and bb.ZLength <= tie_hole_d + 1.5:
edges.append(edge)
return edges
tie_edges = collect_tie_hole_edges(shape)
if tie_edges:
try:
shape = shape.makeFillet(tie_hole_entry_fillet_r, tie_edges)
except Exception as e:
print("Tie-hole fillet failed. Try tie_hole_entry_fillet_r = 0.5")
print(e)
# =====================
# FreeCADに表示
# =====================
obj = doc.addObject(
"Part::Feature",
"DeskRiser_Half_165x230x80_RoundedCableHoles_TieHoles_2x2"
)
obj.Shape = shape
doc.recompute()
try:
App.Gui.activeDocument().activeView().viewAxometric()
App.Gui.SendMsgToActiveView("ViewFit")
except Exception:
pass
|