aboutsummaryrefslogtreecommitdiff
path: root/mplek.py
blob: 91ca0b256666241e41f336f4ef939569568f14b9 (plain)
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
# Copyright (c) 2014 Hanspeter Portner (dev@open-music-kontrollers.ch)
# 
# This documentation describes Open Hardware and is licensed under the
# CERN OHL v.1.2. You may redistribute and modify this documentation
# under the terms of the CERN OHL v.1.2. (http://ohwr.org/cernohl). This
# documentation is distributed WITHOUT ANY EXPRESS OR IMPLIED WARRANTY,
# INCLUDING OF MERCHANTABILITY, SATISFACTORY QUALITY AND FITNESS FOR A
# PARTICULAR PURPOSE. Please see the CERN OHL v.1.2 for applicable
# conditions.

import sys
import bpy
from math import pi, sin, cos

D = float(sys.argv[4]) # diameter of 2nd phalanx
T = 1 # wall thickness
B = 1 # bevel thickness
N = 12 # number of edges of polygonal prism
P = pi/N # edge angle of polygonal prism

prec = 1.01

W = 4 # width of bar magnet
H = 3 # height of bar magnet
L = 20 # length of bar magnet

R0 = D/2 # radius of 2nd phalanx
R1 = R0+1.5*T+H/2 # minimal radius of polygonal prism
R2 = R1/cos(P) # maximal radius of polygonal prism

cen = (0, 0, 0) # origin

# clear scene
for item in bpy.data.objects:
	item.select = True
bpy.ops.object.delete()

# create inner cyclinder, aka virtual finger
bpy.ops.mesh.primitive_cylinder_add(vertices=64, radius=R0, depth=L, location=(-(H+T)/2,0,0))
inner = bpy.context.active_object
inner.name = "inner"

# create bar magnet
bpy.ops.mesh.primitive_cube_add(location=(R1-T-H/2, 0, 0))
bar = bpy.context.active_object
bar.name = "bar"
bpy.ops.transform.resize(value=(H/2*prec, W/2*prec, L/2*prec))

# create partially cut polygonal prism
P2 = P*2
coords=[]
for i in range(0, N):
	phi = P2*i + P
	coords.append( (sin(phi)*R2, cos(phi)*R2, -L/2) )
for i in range(0, N):
	phi = P2*i + P
	coords.append( (sin(phi)*R2, cos(phi)*R2, L/4*(1+sin(phi))) )
faces = []
tmp1 = []
tmp2 = []
for i in range(0, N):
	if i+1 < N:
		faces.append( (i+1, i, N+i, N+i+1) )
	else:
		faces.append( (0, i, N+i, N) )
	tmp1.append(i)
	tmp2.append(N*2-1-i)
faces.append(tmp1)
faces.append(tmp2)
 
# create a new mesh  
mesh = bpy.data.meshes.new("polygonal_prism_mesh")
mesh.from_pydata(coords,[],faces)
mesh.update(calc_edges=True)
 
# create an object with that mesh
outer = bpy.data.objects.new("outer", mesh)
scn = bpy.context.scene
scn.objects.link(outer)
scn.objects.active = outer
outer.select = True

# add bevel modifier
bpy.ops.object.modifier_add(type="BEVEL")
bevel = outer.modifiers["Bevel"]
bevel.name = "bevel"
bevel.width = B
bevel.segments = 1

# subtract inner from outer cylinder
bpy.ops.object.modifier_add(type="BOOLEAN")
sub_inner = outer.modifiers["Boolean"]
sub_inner.name = "sub_inner"
sub_inner.operation = "DIFFERENCE"
sub_inner.object = inner

# subtract bar from outer cylinder
bpy.ops.object.modifier_add(type="BOOLEAN")
sub_bar = outer.modifiers["Boolean"]
sub_bar.name = "sub_bar"
sub_bar.operation = "DIFFERENCE"
sub_bar.object = bar

# rotate 45°
outer.select = True
inner.select = True
bar.select = True
bpy.ops.transform.rotate(value=pi/4, axis=(0, 0, 1))
inner.select = False
bar.select = False

# export to Collada
bpy.ops.wm.collada_export(filepath=sys.argv[5], selected=True, apply_modifiers=True)

# export to STL
#bpy.ops.export_mesh.stl(filepath=sys.argv[5], check_existing=False, ascii=True, use_mesh_modifiers=True)

bpy.ops.wm.quit_blender()