-
Notifications
You must be signed in to change notification settings - Fork 0
/
Laser_Box.m
299 lines (266 loc) · 11.6 KB
/
Laser_Box.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
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
classdef Laser_Box
%LASER_BOX Class
%create each face of the box on the SVG file, and create the SVG file
properties
X %length
Y %width
Z %height
T %material thickness
lid %if lid exist
divider %0 for no divider, 1/3, 1/2, 2/3 for divider position
side_text
lid_text
end
methods
function obj = Laser_Box(X,Y,Z,T,lid,divider,side_text,lid_text)
%LASER_BOX Construct an instance of this class
obj.X = X;
obj.Y = Y;
obj.Z = Z;
obj.T = T;
obj.lid = lid; % Y or N
obj.divider = divider; % 0, 1/3, 1/2, 2/3
obj.side_text = side_text;
obj.lid_text = lid_text;
end
function shape = screw_slot(obj,x,y,o)
%SCREW_SLOT Create a screw slot
% o: orientation U(up),D(down),L(left),R(right)
% x,y: position of screw slot
t=obj.T;
shape = [1 0; 1 6-t; 2.3 6-t; 2.3 7.5-t; 1 7.5-t; 1 10.5-t;
-1 10.5-t; -1 7.5-t; -2.3 7.5-t; -2.3 6-t; -1 6-t; -1 0];%upward
if o == 'D'
shape = shape*[-1 0;0 -1];
end
if o == 'R'
shape = shape*[0 -1;1 0];
end
if o == 'L'
shape = shape*[0 1;-1 0];
end
shape(:,1) = shape(:,1)+x;
shape(:,2) = shape(:,2)+y;
end
function hole = screw_hole(~,x,y)
%SCREW_HOLE Create a screw hole
% x,y: position of screw hole
cx = x;
cy = y;
r = 1; %mm
hole = [cx,cy,r];
end
function base = base_face(obj)
%BASE_FACE Create the base face of the box
% output an array of points that making the shape of the base face
x = obj.X; %horizontal length
y = obj.Y; %vertical length downward
t = obj.T; %material thickness
base=[0 0;x/4 0;x/4 t;flip(screw_slot(obj,x/2,t,'U'));3*x/4 t;3*x/4,0;x 0;x y/4;x-t y/4;flip(screw_slot(obj,x-t,y/2,'L'));x-t 3*y/4;
x 3*y/4;x y;3*x/4 y;3*x/4 y-t;flip(screw_slot(obj,x/2,y-t,'D'));x/4 y-t;x/4 y;0 y;0 3*y/4;t 3*y/4;flip(screw_slot(obj,t,y/2,'R'));
t y/4;0 y/4;0 0];
end
function x_face=x_face(obj)
%X_FACE Create the x orientation face of the box
% output an array of points that making the shape of the x face
x = obj.X;
z = obj.Z;
t = obj.T;
x_face=[t 0;x-t 0;x-t z/4;x z/4;x 3*z/4;x-t 3*z/4;x-t z-t;3*x/4 z-t;3*x/4,z;x/4 z;x/4 z-t;t z-t;t 3*z/4;0 3*z/4;
0 z/4;t z/4;t 0];
end
function holes = x_face_hole(obj)
%X_FACE_HOLE Create screw holes on the x face
% output an array of screw hole positions on the x face
z=obj.Z;
t=obj.T;
x=obj.X;
holes = [obj.screw_hole(0,z-t/2);obj.screw_hole(x/2-t/2,z/2);obj.screw_hole(-x/2+t/2,z/2)];
holes(:,1) = holes(:,1)+x/2;
end
function holes = y_face_hole(obj)
%Y_FACE_HOLE Create screw holes on the y face
% output an array of screw hole positions on the y face
holes = obj.screw_hole(obj.Y/2,obj.Z-obj.T/2);
if obj.lid == true
holes(2,:) = [7.5,-1.5,4.5/2];
end
end
function y_face=y_face(obj)
%Y_FACE Create the x orientation face of the box
% output an array of points that making the shape of the y face
y = obj.Y;
z = obj.Z;
t = obj.T;
if obj.lid == true
% lid holder dimention
r=7.5; %% mm
theta=0:-pi/10:-pi;
xc = (r * cos(theta) + r)';
yc = (r * sin(theta) + 0)';
circle=flip([xc yc]);
y_face=[circle;y 0;y z/4;y-t z/4;flip(obj.screw_slot(y-t,z/2,'L'));y-t 3*z/4;y 3*z/4;y z-t;3*y/4 z-t;3*y/4,z;y/4 z;y/4 z-t;0 z-t;0 3*z/4;t 3*z/4;
flip(obj.screw_slot(t,z/2,'R'));t z/4;0 z/4;0 0];
else
y_face=[0 0;y 0;y z/4;y-t z/4;flip(obj.screw_slot(y-t,z/2,'L'));y-t 3*z/4;y 3*z/4;y z-t;3*y/4 z-t;3*y/4,z;y/4 z;y/4 z-t;0 z-t;0 3*z/4;t 3*z/4;
flip(obj.screw_slot(t,z/2,'R'));t z/4;0 z/4;0 0];
end
end
function lid_face = lid_face(obj)
%LID_HOLE Create screw holes on the y face
% output an array of screw hole positions on the lid face
x=obj.X;
y=obj.Y;
t=obj.T;
lo=4; %mm length offset
wo=10; %mm width offset
lid_face=[0 0;x 0;x t;x-lo t;x-lo t+wo;x t+wo;x y;0 y;0 t+wo;lo t+wo;lo t;0 t;0 0];
end
function divider_face = divider_face(obj)
%DIVIDER_HOLE Create screw holes on the y face
% output an array of screw hole positions on the divider face
x=obj.X;
z=obj.Z;
t=obj.T;
divider_face=[t 0;x-t 0;x-t (z-t)/4;x (z-t)/4;x 3*(z-t)/4;x-t 3*(z-t)/4;x-t z-t;t z-t;t 3*(z-t)/4;0 3*(z-t)/4;0 (z-t)/4;t (z-t)/4;t 0];
end
function slot = divider_slot(obj)
%DIVIDER_SLOT Create slot for divider on the y face
% output an array of points for the shape of the divider slot
t=obj.T;
z=obj.Z;
y=obj.Y;
slot=[0 0;t 0;t (z-t)/2;0 (z-t)/2;0 0];
slot(:,2)=slot(:,2)+(z-t)/4;
if obj.divider == 1/3
slot(:,1)=slot(:,1)+y/3;
elseif obj.divider == 1/2
slot(:,1)=slot(:,1)+y/2;
elseif obj.divider == 2/3
slot(:,1)=slot(:,1)+2*y/3; %3/4?
end
end
function pattern = decoration(obj)
%DIVIDER_SLOT Create a decoration pattern
% output an array of points for the decoration pattern
x=obj.X;
z=obj.Z;
r0=min(z,x)/4; %% mm
theta=linspace(0,360,180);
x0 = (r0 * cos(theta) + x/2)';
y0 = (r0 * sin(theta) + z/2)';
r1_2=r0/2;
r3_4=r1_2/2;
x1 = (r1_2 * cos(theta) + x/2+r0)';
y1 = (r1_2 * sin(theta) + z/2)';
x2 = (r1_2 * cos(theta) + x/2-r0)';
y2 = (r1_2 * sin(theta) + z/2)';
x3 = (r3_4 * cos(theta) + x/2+r0+r1_2)';
y3 = (r3_4 * sin(theta) + z/2)';
x4 = (r3_4 * cos(theta) + x/2-r0-r1_2)';
y4 = (r3_4 * sin(theta) + z/2)';
pattern=zeros(180,2,5);
pattern(:,1,1)=x0;
pattern(:,2,1)=y0;
pattern(:,1,2)=x1;
pattern(:,2,2)=y1;
pattern(:,1,3)=x2;
pattern(:,2,3)=y2;
pattern(:,1,4)=x3;
pattern(:,2,4)=y3;
pattern(:,1,5)=x4;
pattern(:,2,5)=y4;
end
function fprintf_pattern(~,file,offset,pattern)
% output the SVG text for the decoration pattern
fprintf(file,'\n<!--pattern--> \n');
pattern = pattern*1; %scale
for n = 1:size(pattern,3)
pattern(:,1,n) = pattern(:,1,n)+offset(1);
pattern(:,2,n) = pattern(:,2,n)+offset(2);
fprintf(file,' <polygon points="');
for i = 1:size(pattern,1)
fprintf(file,'%8.3f,%8.3f ',pattern(i,:,n));
end
fprintf(file,'" \n fill="none" stroke="blue" />\n');
end
end
function fprintf_text(obj,file,offset,text)
% output the SVG text for the text on the faces
size = 6;
fprintf(file,'\n<!--text--> \n');
fprintf(file,' <style>\n');
fprintf(file,' .small { font: bold %dpx sans-serif; fill: red;}\n',size);
fprintf(file,' </style>\n');
fprintf(file,' <text x="%d" y="%d" text-anchor="middle" class="small"> ',offset);
fprintf(file,text);
fprintf(file,' </text>\n');
end
function fprintf_image(obj,file,offset,image)
% output the SVG text for image
xy = [0,0];
size = [10 10];
currentFolder = pwd;
fprintf(file,'\n<!--image--> \n');
fprintf(file,' <image xlink:href="');
fprintf(file,image);
fprintf(file,'" x="%8.3f" y="%8.3f"',xy+offset);
fprintf(file,'" width="%8.3f" height="%8.3f"',size);
fprintf(file,' />\n');
end
function fprintf_face(~,file,offset,verteces,holes)
% output the SVG text for faces
%if no hole put [] for holes
fprintf(file,'\n<!--face--> \n');
fprintf(file,' <polygon points="');
nVert = size(verteces);
nVert = nVert(1);
verteces(:,1) = verteces(:,1)+offset(1);
verteces(:,2) = verteces(:,2)+offset(2);
for i = 1:nVert
fprintf(file,'%8.3f,%8.3f ',verteces(i,:));
end
fprintf(file,'" \n fill="none" stroke="black" />\n');
nHole = size(holes);
nHole = nHole(1);
if nHole>0
holes(:,1) = holes(:,1)+offset(1);
holes(:,2) = holes(:,2)+offset(2);
for i = 1:nHole
fprintf(file,' <circle cx="%8.3f" cy="%8.3f" r="%8.3f" fill="none" stroke="black" />\n',holes(i,:));
end
end
end
function create_svg(obj,file_name)
%CREATE_SVG Create the SVG file for the box
file = fopen(file_name,'w');
fprintf(file,'<?xml version ="1.0" encoding="UTF-8" ?>\n');
fprintf(file,'<svg xmlns="http://www.w3.org/2000/svg" version="1.1">\n');
base_offset = [0,0];
xf_offset1 = [0,obj.Y+obj.T];
xf_offset2 = [0,obj.Y+2*obj.T+obj.Z];
yf_offset1 = [obj.X+obj.T,6];
yf_offset2 = [obj.X+obj.T,12+obj.Z];
obj.fprintf_face(file,base_offset,obj.base_face(),[]);
obj.fprintf_face(file,xf_offset1,obj.x_face(),obj.x_face_hole());
obj.fprintf_text(file,xf_offset1+[obj.X/2,obj.Z/2],obj.side_text);
obj.fprintf_face(file,xf_offset2,obj.x_face(),obj.x_face_hole());
obj.fprintf_face(file,yf_offset1,obj.y_face(),obj.y_face_hole());
obj.fprintf_face(file,yf_offset1,obj.divider_slot(),[]);
obj.fprintf_face(file,yf_offset2,obj.y_face(),obj.y_face_hole());
obj.fprintf_face(file,yf_offset2,obj.divider_slot(),[]);
obj.fprintf_pattern(file,xf_offset2,obj.decoration());
if obj.lid == true
lf_offset = [obj.X+2*obj.T+obj.Y,6];
obj.fprintf_face(file,lf_offset,obj.lid_face(),[]);
obj.fprintf_text(file,lf_offset+[obj.X/2,obj.Y/2],obj.lid_text);
end
if obj.divider ~= 0
df_offset = [obj.X+obj.T,12+2*obj.Z+obj.T];
obj.fprintf_face(file,df_offset,obj.divider_face(),[]);
end
fprintf(file,'</svg>');
fclose(file);
end
end
end