1
2
3
4
5
6
7
8
9
10
11Sub PocketRectangular(P_X As Float, P_Y As Float, P_W As Float, P_H As Float, P_D As Float, P_R As Float)
12Dim currentZ As Float
13Dim startX As Float
14Dim startY As Float
15Dim endX As Float
16Dim endY As Float
17Dim toolRadius As Float
18
19' --- Input Validation ---
20If P_W <= 0 Or P_H <= 0 Then
21MsgBox "Error: Pocket width and height must be positive."
22Return
23End If
24If P_D >= 0 Then
25MsgBox "Error: Pocket depth must be negative."
26Return
27End If
28If P_R < 0 Then
29MsgBox "Error: Corner radius cannot be negative."
30Return
31End If
32
33' --- Determine Tool Radius (assuming a standard tool for simplicity) ---
34' In a real scenario, this would be read from tool offset or a parameter.
35' For this example, we'll assume a tool radius of 0.5 units.
36toolRadius = 0.5
37
38' Adjust radius if it's larger than half the smallest dimension minus tool radius
39If P_R > (Min(P_W, P_H) / 2.0) - toolRadius Then
40MsgBox "Warning: Corner radius too large for pocket dimensions and tool. Adjusting."
41P_R = (Min(P_W, P_H) / 2.0) - toolRadius
42If P_R < 0 Then P_R = 0 ' Ensure radius doesn't become negative after adjustment
43End If
44
45' --- Calculate Pocket Boundaries ---
46startX = P_X - (P_W / 2.0) + P_R
47startY = P_Y - (P_H / 2.0) + P_R
48endX = P_X + (P_W / 2.0) - P_R
49endY = P_Y + (P_H / 2.0) - P_R
50
51' --- Machining Sequence ---
52' Move to safe Z height before starting
53currentZ = 0.0 ' Assuming Z=0 is safe plane
54MoveTo(0, 0, currentZ + 5.0) ' Move to a safe height above the part
55
56' Position to the starting point of the pocket contour
57MoveTo(startX, startY, currentZ + 5.0)
58
59' Plunge to depth
60currentZ = P_D
61PlungeTo(currentZ)
62
63' Machine the pocket contour
64' First pass: straight edge
65MoveTo(endX, startY, currentZ)
66
67' Second pass: corner arc
68If P_R > 0 Then
69ArcTo(endX, endY, P_R, 90) ' Arc to endX, endY with radius P_R, 90 degrees
70Else
71MoveTo(endX, endY, currentZ)
72End If
73
74' Third pass: straight edge
75MoveTo(startX, endY, currentZ)
76
77' Fourth pass: corner arc
78If P_R > 0 Then
79ArcTo(startX, startY, P_R, 90) ' Arc to startX, startY with radius P_R, 90 degrees
80Else
81MoveTo(startX, startY, currentZ)
82End If
83
84' Return to safe Z height
85currentZ = 0.0 ' Reset to safe plane
86MoveTo(startX, startY, currentZ + 5.0)
87
88' Return control to the main program
89Return
90End Sub
91
92
93
94
95
96Sub MoveTo(x As Float, y As Float, z As Float)
97Print "G00 X" + Format(x, "#.###") + " Y" + Format(y, "#.###") + " Z" + Format(z, "#.###")
98End Sub
99
100
101
102
103
104Sub PlungeTo(z As Float)
105Print "G01 Z" + Format(z, "#.###") ' Assuming G01 for controlled plunge
106End Sub
107
108
109
110
111
112Sub ArcTo(x As Float, y As Float, radius As Float, angle As Integer)
113Print "G02 X" + Format(x, "#.###") + " Y" + Format(y, "#.###") + " R" + Format(radius, "#.###") ' G02 for clockwise arc
114' Actual Fanuc arc commands can be more complex (e.g., I, J, K parameters).
115End Sub
116
117
118
119
120Sub MsgBox(message As String)
121Print "[MESSAGE] " + message
122End Sub
123
124
125
126
127Function Format(value As Float, formatString As String) As String
128Format = "" + value
129End Function
130
131
132
133
134Function Min(a As Float, b As Float) As Float
135If a < b Then
136Min = a
137Else
138Min = b
139End If
140End Function
141
142
143
144
145Sub Main()
146Print "--- Machining Pocket 1 ---"
147PocketRectangular(50.0, 50.0, 40.0, 30.0, -5.0, 5.0) ' Pocket with radius
148
149Print "\n--- Machining Pocket 2 (Sharp Corners) ---"
150PocketRectangular(100.0, 100.0, 20.0, 20.0, -3.0, 0.0) ' Pocket with sharp corners
151
152Print "\n--- Testing Error Cases ---"
153PocketRectangular(50.0, 50.0, -10.0, 30.0, -5.0, 5.0) ' Negative width
154PocketRectangular(50.0, 50.0, 40.0, 30.0, 5.0, 5.0) ' Positive depth
155PocketRectangular(50.0, 50.0, 40.0, 30.0, -5.0, -2.0) ' Negative radius
156
157Print "\n--- Testing Radius Adjustment ---"
158' Pocket where radius would be too large for tool
159PocketRectangular(150.0, 150.0, 10.0, 10.0, -2.0, 6.0)
160
161End Sub