forked from mcneel/MOVED-rhinoscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoxFrame.rvb
82 lines (63 loc) · 2.81 KB
/
BoxFrame.rvb
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
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' BoxFrame.rvb -- November 2009
' If this code works, it was written by Dale Fugier.
' If not, I don't know who wrote it.
' Works with Rhino 4.0.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' http://wiki.mcneel.com/developer/scriptsamples/boxframe
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub BoxFrame()
' Local constants
Const X = 0
Const Y = 1
Const Z = 2
Const TOL = 0.01
' Local variables
Dim box_points, box_object, box_center, box_plane
Dim offset(2), dist(2), pfact(2), nfact(2)
Dim xform(2), boxes(2)
' Prompt the user to define a box
box_points = Rhino.GetBox()
If IsNull(box_points) Then Exit Sub
' Calculate half distances between corners
dist(X) = Rhino.Distance(box_points(0), box_points(1)) * 0.5
dist(Y) = Rhino.Distance(box_points(0), box_points(3)) * 0.5
dist(Z) = Rhino.Distance(box_points(0), box_points(4)) * 0.5
' Define axes offsets
offset(X) = Rhino.GetReal("Thickness in X direction", 1.0, TOL, dist(X) - TOL)
If IsNull(offset(X)) Then Exit Sub
offset(Y) = Rhino.GetReal("Thickness in Y direction", 1.0, TOL, dist(Y) - TOL)
If IsNull(offset(Y)) Then Exit Sub
offset(Z) = Rhino.GetReal("Thickness in Z direction", 1.0, TOL, dist(Z) - TOL)
If IsNull(offset(Z)) Then Exit Sub
' Do this for performance
Call Rhino.EnableRedraw(False)
' Calculate increasing scale factors
pfact(X) = (dist(X) + offset(X)) / dist(X)
pfact(Y) = (dist(Y) + offset(Y)) / dist(Y)
pfact(Z) = (dist(Z) + offset(Z)) / dist(Z)
' Calculate decreasing scale factors
nfact(X) = (dist(X) - offset(X)) / dist(X)
nfact(Y) = (dist(Y) - offset(Y)) / dist(Y)
nfact(Z) = (dist(Z) - offset(Z)) / dist(Z)
' Some stuff need for the scale transformation
box_center = Rhino.PointDivide(Rhino.PointAdd(box_points(0), box_points(6)), 2.0)
box_plane = Rhino.PlaneFromPoints(box_points(0), box_points(1), box_points(3))
box_plane = Rhino.MovePlane(box_plane, box_center)
' Define scale transformations
xform(0) = Rhino.XformScale(box_plane, pfact(0), nfact(1), nfact(2))
xform(1) = Rhino.XformScale(box_plane, nfact(0), pfact(1), nfact(2))
xform(2) = Rhino.XformScale(box_plane, nfact(0), nfact(1), pfact(2))
' Create the box
box_object = Rhino.AddBox(box_points)
' Create the boxes to difference
boxes(0) = Rhino.TransformObject(box_object, xform(0), True)
boxes(1) = Rhino.TransformObject(box_object, xform(1), True)
boxes(2) = Rhino.TransformObject(box_object, xform(2), True)
' Do the difference
Call Rhino.BooleanDifference(Array(box_object), boxes, True)
' Enable redraw
Call Rhino.EnableRedraw(True)
End Sub