/
attach.scad
150 lines (121 loc) · 4.66 KB
/
attach.scad
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
//---------------------------------------------------------------
//-- Openscad Attachment library
//-- Attach parts easily. Make your designs more reusable and clean
//---------------------------------------------------------------
//-- This is a component of the obiscad opescad tools by Obijuan
//-- (C) Juan Gonzalez-Gomez (Obijuan)
//-- Sep-2012
//---------------------------------------------------------------
//-- Released under the GPL license
//---------------------------------------------------------------
use <vector.scad>
//--------------------------------------------------------------------
//-- Draw a connector
//-- A connector is defined a 3-tuple that consist of a point
//--- (the attachment point), and axis (the attachment axis) and
//--- an angle the connected part should be rotate around the
//-- attachment axis
//--
//--- Input parameters:
//--
//-- Connector c = [p , v, ang] where:
//--
//-- p : The attachment point
//-- v : The attachment axis
//-- ang : the angle
//--------------------------------------------------------------------
module connector(c)
{
//-- Get the three components from the connector
p = c[0];
v = c[1];
ang = c[2];
//-- Draw the attachment poing
color("Gray") point(p);
//-- Draw the attachment axis vector (with a mark)
translate(p)
rotate(a=ang, v=v)
color("Gray") vector(unitv(v)*6, l_arrow=2, mark=true);
}
//-------------------------------------------------------------------------
//-- ATTACH OPERATOR
//-- This operator applies the necesary transformations to the
//-- child (attachable part) so that it is attached to the main part
//--
//-- Parameters
//-- a -> Connector of the main part
//-- b -> Connector of the attachable part
//-------------------------------------------------------------------------
module attach(a,b)
{
//-- Get the data from the connectors
pos1 = a[0]; //-- Attachment point. Main part
v = a[1]; //-- Attachment axis. Main part
roll = a[2]; //-- Rolling angle
pos2 = b[0]; //-- Attachment point. Attachable part
vref = b[1]; //-- Atachment axis. Attachable part
//-- The rolling angle of the attachable part is not used
//-------- Calculations for the "orientate operator"------
//-- Calculate the rotation axis
cref = cross(vref,v);
raxis = norm(cref) == 0 ? ortho2(vref) : cref;
//-- Calculate the angle between the vectors
ang = anglev(vref,v);
//--------------------------------------------------------.-
//-- Apply the transformations to the child ---------------------------
//-- Place the attachable part on the main part attachment point
translate(pos1)
//-- Orientate operator. Apply the orientation so that
//-- both attachment axis are paralell. Also apply the roll angle
rotate(a=roll, v=v) rotate(a=ang, v=raxis)
//-- Attachable part to the origin
translate(-pos2)
children([0:$children-1]);
}
//--------------------------------------------------------------------
//--- An example of the attach operator
//---
//--- There are two parts: the main body and an arm
//--- They both are cubes (for simplicity)
//---
//-- In the main body there are 2 connectors defined, so that
//-- the arm can be attached to any of them (or both if you like)
//------------------------------------------------------------------
//-- In the debug mode the connectors and additional information
//-- are shown
debug=true;
//-- Define the Main part: A cube
//-- Two attachment points are defined: one on the top, another in the
//--- left side
size = [10,10,10];
//-- Connectors defined:
//-- Att. point Att. Axis Roll
c1 = [ [0,0,size[2]/2], [0,0,1], 20]; //-- Connector on the top
c2 = [ [-size[0]/2,0,0], [-1,0,0], 90]; //-- Connector on the left
c3 = [ [0,0,-size[0]/2], [0,0,-1], 90]; //-- Connector on the bottom, degenerate case !
//-- Draw the main part along with the connectors (for debugging)
cube(size,center=true);
//-- In debug mode: Draw the main part connectors!
if (debug) {
connector(c1);
connector(c2);
connector(c3);
}
//-- Define the Attachable part. It is another cube, with one connector
asize = [5,20,3];
a = [ [0,asize[1]/2-3,-asize[2]/2], [0,0,1], 0 ];
//-- Do the attach!
//-- Just change c1 by c2 to attach the part to the other
//-- connector. Super-easy!! :-)
//-- Modify the c1 and c2 roll angle for rotating the attachable
//-- part to the desired orientation
attach(c1,a)
//-- This is the attachable part!
union() {
cube(asize,center=true); //-- The part
//-- In debug mode show additional info:
if (debug) {
frame(l=10); //-- The part frame of reference.
connector(a); //-- Show the part connector
}
};