looping struggle with index/match

L

Lumpy

I'm struggling with a looping structure. I've got a spreadsheet with a
couple of sheets like this:

Boards!
Board# Pc# Length
1 1 750
2 2 750
2 3 750
2 4 750

Pieces!
Piece# Length Orientation Angle
1 650 Right -30
1 650 Left 30
2 300 Right -45
2 300 Left -45
3 200 Right 90
3 200 Left 30
4 225 Right 20
4 225 Left 30

Here's a graphic that represents the data and my problem.
http://picasaweb.google.com/kurtaframe/CncSaw/photo#5176694552076241586

The Boards sheet is a list raw boards we need to make something and the
pieces that each board contains. For this example, the Boards sheet lists 2
boards beign cut into 4 pieces. The Pieces sheet lists each piece twice,
once for the right side cut angle information and once for the left side cut.

We've used other software to nest and optimize the pieces so they can be cut
most efficiently out of the 750mm stock length of our raw material.

I need to generate some simple text commands to control automated cutting of
the pieces out of the boards.

The steps for this example would be:
-----(Board 1)-----
1. Make sure push block is in home position (800mm from saw) wait for
operator to load board.
2. Move the red push block (and board) so that we make the cut on the right
end initial cut off with a small scrap piece. (moveX = PushblockHome -
BoardLenght + 15*Tan RAngle)
3. Adjust the saw angle for the right cut of piece 1 (RotM = 90 - RAngle)
(wait for the operator to make cut)
4. Move the push block for left cut (PieceLength + 15*Tan LAngle)
5. Adjust the saw angle for the left cut of piece 1 (RotM = 90 - LAngle).
Wait for cut.

------(Board 2)-----
6. Push block to home. Wait (same as step 1)
7. Move the push block for initial right cutoff of piece 2 . (same as step
2)
8. Adjust saw angle for initial right cutoff of piece 2. Wait for cut.
(same as step 3)
9. Move the push block for the left cut of piece 2. (same as step 4)
10. Adjust the saw angle for the left cut of piece 2 (LastLeftAngle - ( 90
- ThisPieceRAngle) Wait for cut.
11. if the last piece left cut angle is not he same as this piece right cut
angle, ajust the saw miter and bevel for the right cut of piece 3
12. move the push block just enough to make the right cut of piece 3
(PieceLenght+LastLeftMiter+10) Wait for cut.
13. Move the push block for the left cut of piece 3. (same as step 4)
14. Repeat steps 11-13 for piece 4.

So, alooping description would be:


For
If this is the first piece in a board
Generate gcode line "G28 M6"
Generate gcode line for initial right end cut-off. "G1 X-- Y-- Z-- M6"
Generate the gcode line for the left cut. "G1 X-- Y-- Z-- M6"
Else
Compare last piece left cut with this piece right cut.
If angle and bevel are not the same
Generate the gcode for this piece right cut "G1 X-- Y-- Z-- M6"
Else
Genreate the gcode for this piece left cut "G1 X-- Y-- Z--
M6"
End If
End If
Next

I've got this (thanks Bob Phillips!) but it's not quite right. The looping
structure is just one level and doesn't do a separate calculation for the
first piece in a board and the syntax of the index/match doesn't work. I'm
having trouble finding anything about index/match using a looping variable in
help or on-line.

Sub WriteToText()
'The file name that will be used
Dim sFileName As String
sFileName = "C:\temp\ExampleOuput3.txt"

'File System Objects - Set a reference to "Microsoft Scripting Runtime"
'Under Tools - References
Dim fso As Scripting.FileSystemObject
Dim f As Scripting.TextStream

'Create the File System and Text Stream Objects
Set fso = New Scripting.FileSystemObject
Set f = fso.CreateTextFile(sFileName, True, False)

'Data Range Information to Process
Dim iRow As Long, iCol As Long
Dim iLoop As Long
Dim iBoardNum As Variant
Dim wsWorkSheet As Worksheet
Dim wsRange As Range
Dim val1 As Variant, val2 As Variant

Set wsWorkSheet = ThisWorkbook.Sheets("Boards")
Set wsRange = wsWorkSheet.Cells(1, 1).CurrentRegion ' Get the range of
Data

f.WriteLine "G28" 'Start the file with a G28 command

For iLoop = 2 To wsRange.Rows.Count 'skilp the header row thus iLoop = 2
iBoardNum = wsRange(iLoop, 1)
RAngle = ActiveSheet.Evaluate("=INDEX(Pieces!D1:D2000," & _
"MATCH(1,(Pieces!A1:A2000=Boards!B" &
iLoop & ")*" & _
"(Pieces!C1:C2000=""Left""),0))")
LAngle = ActiveSheet.Evaluate("=INDEX(Pieces!D1:D2000," & _
"MATCH(1,(Pieces!A1:A2000=Boards!B" &
iLoop & ")*" & _
"(Pieces!C1:C2000=""Right""),0))")
f.WriteLine "G1 X" & wsRange(iLoop, 3) & "Y " & RAngle
If iBoardNum <> wsRange(iLoop + 1, 1) Then
f.WriteLine "G28"
End If
f.WriteLine val1 & val2 & " M6"
Next

f.Close

Set f = Nothing
Set fso = Nothing

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top