'#Reference {420B2830-E718-11CF-893D-00A0C9054228}#1.0#0#C:\WINDOWS\system32\scrrun.dll#Microsoft Scripting Runtime
' DisplayMultiTurretNCCodeInHTMLFormat.bas
'
' Author: Polina Milyavskaya
' Organization: Delcam USA
' Date: 10/10/07
' Copyright (c) 2008, Delcam USA
'
' General Description: Add-In allows user to view nc code for multiple turrets in parallel.
' The nc code for multiple turrets is aligned by sync codes
' (lower and upper bounds of sync numbers are integer values specified
' by the user) and displayed in the resulting html file.
' Remark: the document must be opened and cnc file has to be loaded
' prior to the script execution.
'
' Works in FeatureCAM v14.2.0.12 and above
'
' Revised on 07/23/08-07/30/08. Polina:
' Default upper and lower bounds of the sync numbers are set to
' 100 and 200 correspondingly.
' Added ability to "remember" entered values of the upper and lower
' bounds of the sync numbers (until the macro is unloaded or
' FeatureCAM is restarted)
' Added sync number prefix ("M", "P", etc.) field and its default value ("M")
' Revised on 09/22/08. Polina:
' Changed 3 and more turrets functionality. Now we can "sync" 2 turrets
' at a time and specify which turrets are synced.
' Revised on 09/04/09. Polina:
' Added support for Okuma style posts. In Okuma style post case nc code
' for all turrets is saved to one file. Okuma style posts should output comments
' "( BEGIN TURRET ... PROGRAM )" and "( END TURRET ... PROGRAM )" for the
' macro to work properly, since it relies on the comments in order to
' separate/identify beginning and end of the program for each turret.
' Revised on 03/10/10. Polina:
' Files (nc code, html) are now saved to Windows Temp folder. This had to be
' done because many Vista/Windows 7 users don't have permissions to save files
' to C drive.
' Revised on 03/25/10. Polina:
' Addins: DisplayMultiTurretNCCodeInHTMLFormat.bas: macro doesn't align sync points
' properly if Okuma style post calls P.
' Solution (offered by Beth): If there is a Q on the same line, P is not sync point
' abbreviation in this case.
' Change position variable type to long
' Revised on 04/20/10. Polina:
' Fixed problems introduced on 03/25/10.
' Revised on 04/21/10. Mayank:
' Added support for specifying input file(s) from disk. To use Fanuc-style files,
' select the turret 1 file. Note: The macro presumes that the files are named in FeatureCAM
' style, i.e. Turret 1: output.txt, Turret 2: output.t2.txt, etc.
' For Okuma-style code, just select the file and change the radio button to 'Okuma'.
Option Explicit
Dim sync_num_lower_bound As Integer
Dim sync_num_upper_bound As Integer
Dim sync_num_prefix As String
Dim prog_style As String
Dim input_file_name As String
Dim input_file_check As Integer
Dim output_file_name_original As String
Sub DisplayMultiTurretNCCodeInHTMLFormat
Dim doc As FMDocument
Dim save_nc_result As String
Dim res_fnames() As String
Dim file_content() As String
Dim i As Integer
Dim fso 'As Scripting.FileSystemObject
Dim file_tmp 'As Scripting.TextStream
Dim output_file_name As String
Dim output_fpath As String
Dim num_of_turrets As Integer
Dim html_file_generated As Boolean
Dim input_file_dir As String
Dim input_file_ext As String
Dim input_file_partial_name As String
'Initialize variable
'sync_num_lower_bound = -1 'don't initialize now (to keep old value)
'sync_num_upper_bound = -1 'don't initialize now (to keep old value)
output_file_name = ""
If input_file_check <= 0 Then
input_file_check = 0
End If
If (sync_num_lower_bound <= 0 And sync_num_upper_bound <= 0) Then
sync_num_lower_bound = 100 'temporary value, common for many machines
sync_num_upper_bound = 200 'temporary value, common for many machines
End If
If (sync_num_prefix = "") Then sync_num_prefix = "M"
If (prog_style = "") Then prog_style = "Fanuc"
Set fso = CreateObject("Scripting.FileSystemObject")
'Get document handle
Set doc = Application.ActiveDocument
If (doc Is Nothing) Then
MsgBox "Error accessing file. Application.ActiveDocument returns 'Nothing'", vbExclamation, "DisplayMultiTurretNCCodeInHTMLFormat macro error"
Exit Sub
End If
output_fpath = fso.GetSpecialFolder(2) 'Temporary folder
If (InStrRev(output_fpath, "\") <> 1) Then
output_fpath = output_fpath & "\"
End If
'Construct output file name (html), which will be displayed in the dialog box
Dim pos As Integer
pos = InStr(UCase(doc.Name), ".FM")
If (pos > 0) Then
output_file_name = output_fpath & Left(doc.Name, pos - 1) & ".html"
Else
output_file_name = output_fpath & doc.Name & ".html"
End If
'Get user input and verify it
If (Not GetUserInput(sync_num_lower_bound, sync_num_upper_bound, output_file_name, input_file_name)) Then Exit Sub
If (sync_num_lower_bound = -1 Or sync_num_upper_bound = -1 Or output_file_name = "") Then
MsgBox "Required parameters were not set properly", vbExclamation, "DisplayMultiTurretNCCodeInHTMLFormat macro error"
Exit Sub
End If
' If an input file has been specified, use that instead
If input_file_name <> "" Then
FindDirectoryAndFilename input_file_name, input_file_dir, input_file_ext, input_file_partial_name', isPartial
If prog_style = "Okuma" Then
ReDim res_fnames(1)
res_fnames(1) = input_file_name
Else
res_fnames = GetAllFilesInDirectory(input_file_dir, input_file_ext, input_file_partial_name)
End If
If IsEmpty(res_fnames) Or UBound(res_fnames) > 4 Then
MsgBox "Input file path not set properly", vbExclamation, "DisplayMultiTurretNCCodeInHTMLFormat macro error"
Exit Sub
End If
Else
'Save nc code and get the names of the generated files
doc.SaveNC("temporaryNCCode.txt", output_fpath,,eNCFT_NCCode, False, save_nc_result)
res_fnames = Split(save_nc_result, vbLf & vbTab)
End If
'Compute the number of turrets based on the number of files generated
num_of_turrets = 0
' If an input file has been specified and code is Fanuc style, set the number of turrets to number of files
' Else, find the number of turrets based on how many 'BEGIN TURRET i PROGRAM' lines are there.
If input_file_name <> "" Then
If prog_style = "Fanuc" Then
num_of_turrets = UBound(res_fnames)
Else
FindOkumaNumTurrets res_fnames(1), num_of_turrets
If num_of_turrets > 4 Then
MsgBox "Input file path not set properly", vbExclamation, "DisplayMultiTurretNCCodeInHTMLFormat macro error"
Exit Sub
End If
End If
Else
Dim available As Boolean
Dim turret_i As Integer
For turret_i = 0 To 3
Application.GetTurnTurretInfo(turret_i, available)
num_of_turrets = num_of_turrets + IIf(available, 1, 0)
Next turret_i
End If
'Verify that files with nc code exist
If (prog_style = "Fanuc") Then
If (UBound(res_fnames) < num_of_turrets) Then
MsgBox "Only 1 file with nc code was created instead of " & num_of_turrets & "." & _
vbCrLf & _
"Is there posting error or you selected wrong multi-turret programming style?", _
vbExclamation, "DisplayMultiTurretNCCodeInHTMLFormat macro error"
Exit Sub
End If
For i = 1 To num_of_turrets
If (Not fso.FileExists(res_fnames(i))) Then
MsgBox "File " & res_fnames(i) & " doesn't exist", vbExclamation, "DisplayMultiTurretNCCodeInHTMLFormat macro error"
Exit Sub
End If
Next i
ElseIf (prog_style = "Okuma") Then
If (Not fso.FileExists(res_fnames(1))) Then
MsgBox "File " & res_fnames(1) & " doesn't exist", vbExclamation, "DisplayMultiTurretNCCodeInHTMLFormat macro error"
Exit Sub
End If
End If
'Read nc code
ReDim file_content(num_of_turrets - 1)
If (prog_style = "Fanuc") Then
For i = 1 To num_of_turrets
Set file_tmp = fso.OpenTextFile(res_fnames(i), 1)
file_content(i-1) = file_tmp.ReadAll
file_tmp.Close
Next i
ElseIf (prog_style = "Okuma") Then
'All turret programs are saved to 1 file in Okuma style posts
Dim tmp_file_content As String
Dim pos_start As Long, pos_end As Long, pos_start1 As Long, pos_end1 As Long
Set file_tmp = fso.OpenTextFile(res_fnames(1), 1)
tmp_file_content = file_tmp.ReadAll
file_tmp.Close
For i = 1 To num_of_turrets
pos_start = InStr(UCase(tmp_file_content), "BEGIN TURRET " & (i + 1) & " PROGRAM")
If (pos_start <> 0) Then
pos_start1 = InStrRev(tmp_file_content, vbCr, pos_start)
file_content(i-1) = Mid(tmp_file_content, 1, pos_start1)
tmp_file_content = Trim(Mid(tmp_file_content, pos_start1 + 1))
Else
file_content(i-1) = tmp_file_content 'last turret program
End If
'pos_start1 = InStrRev(tmp_file_content, vbCr, pos_start)
'pos_end = InStr(UCase(tmp_file_content), "END TURRET " & i & " PROGRAM")
'pos_end1 = InStr(pos_end, tmp_file_content, vbCr)
'file_content(i-1) = Mid(tmp_file_content, pos_start1, pos_end - pos_start1 + Len("BEGIN TURRET " & i & " PROGRAM"))
Next i
End If
Dim t_num As Integer
Dim sync_codes() As String
Dim nc_code_fragments() As String
FindAllSyncCodes file_content, sync_codes, t_num
FragmentNcCode file_content, sync_codes, t_num, nc_code_fragments
html_file_generated = SaveHTMLFile(output_file_name, t_num, num_of_turrets - 1, nc_code_fragments)
If (html_file_generated) Then
'View result (open html file)
OpenHTMLFile output_file_name
End If
If input_file_name = "" Then
'Delete temporary nc code files
If (prog_style = "Fanuc") Then
For i = 1 To num_of_turrets
If (fso.FileExists(res_fnames(i))) Then fso.DeleteFile(res_fnames(i))
Next i
ElseIf (prog_style = "Okuma") Then
If (fso.FileExists(res_fnames(1))) Then fso.DeleteFile(res_fnames(1))
End If
End If
End Sub
'Opens a dialog box and reads user data
Private Function GetUserInput(ByRef sync_num_lower_bound As Integer, _
ByRef sync_num_upper_bound As Integer, _
ByRef output_file_name As String, _
ByRef input_file_name As String) As Boolean
Dim result As Long
Dim pos As Integer
Dim input_file_dir As String, input_file_ext As String, input_file_partial_name As String
GetUserInput = False
output_file_name_original = output_file_name
On Error GoTo reportError
Begin Dialog UserDialog 580,224,"Display nc code for multiple turrets",.DialogFunc ' %GRID:10,7,1,1
' %GRID:10,7,1,1
OKButton 480,42,90,21
CancelButton 480,70,90,21
GroupBox 10,0,450,217,"",.GroupBox1
OptionGroup .options
OptionButton 50,28,90,14,"Fanuc",.OptionButton1
OptionButton 170,28,90,14,"Okuma",.OptionButton2
Text 55,65,90,21,"Prefix:",.Text6
TextBox 170,62,90,15,.prefix
Text 55,87,90,21,"Lower bound:",.Text1
TextBox 170,83,90,15,.lower_bound
Text 55,109,100,21,"Upper bound:",.Text2
TextBox 170,106,90,15,.upper_bound
Text 55,149,180,21,"Enter full path:",.Text3
TextBox 170,147,190,15,.output_file
Text 30,47,100,14,"Sync code",.Text4
Text 30,14,320,14,"Multi-turret programming style:",.Text7
Text 30,130,110,14,"Output file (html)",.Text5
Text 55,187,180,21,"Enter full path:",.text9
TextBox 170,187,190,15,.input_file
PushButton 370,182,70,21,"Browse...",.Button1
CheckBox 30,168,80,14,"Input file",.CheckBox1
End Dialog
Dim dlg As UserDialog
dlg.output_file = output_file_name
'if bound values are > 0, display them
If (sync_num_upper_bound > 0) Then
dlg.upper_bound = CStr(sync_num_upper_bound)
End If
If (sync_num_lower_bound > 0) Then
dlg.lower_bound = CStr(sync_num_lower_bound)
End If
dlg.prefix = sync_num_prefix
dlg.options = IIf(prog_style = "Fanuc", 0, 1)
dlg.input_file = input_file_name
dlg.CheckBox1 = input_file_check
If input_file_name <> "" Then
'Construct output file name (html), which will be displayed in the dialog box
FindDirectoryAndFilename dlg.input_file, input_file_dir, input_file_ext, input_file_partial_name
pos = InStrRev(UCase(dlg.output_file), "\")
If (pos > 0) Then
dlg.output_file = Left(dlg.output_file, pos) & input_file_partial_name & ".html"
End If
End If
result = Dialog(dlg)
If result = 0 Then
Exit Function
End If
If (dlg.prefix = "") Then
MsgBox "Sync code prefix wasn't specified. Defaulting to 'M'", vbExclamation, "DisplayMultiTurretNCCodeInHTMLFormat macro error"
sync_num_prefix = "M"
Else
sync_num_prefix = UCase(dlg.prefix)
End If
If (Not IsNumeric(dlg.lower_bound)) Then
MsgBox "Sync code lower bound must be a number", vbExclamation, "DisplayMultiTurretNCCodeInHTMLFormat macro error"
Exit Function
End If
If (Not IsNumeric(dlg.upper_bound)) Then
MsgBox "Sync code upper bound must be a number", vbExclamation, "DisplayMultiTurretNCCodeInHTMLFormat macro error"
Exit Function
End If
If (dlg.output_file = "") Then
MsgBox "Output file must be specified", vbExclamation, "DisplayMultiTurretNCCodeInHTMLFormat macro error"
Exit Function
End If
sync_num_lower_bound = CInt(dlg.lower_bound)
sync_num_upper_bound = CInt(dlg.upper_bound)
output_file_name = dlg.output_file
input_file_check = dlg.CheckBox1
If input_file_check = 0 Then
input_file_name = ""
Else
input_file_name = dlg.input_file
End If
prog_style = IIf(dlg.options = 0, "Fanuc", "Okuma")
GetUserInput = True
Exit Function
'If error occured
reportError:
MsgBox "Failed to get or process user input. Error details: " & Err.Description, vbExclamation, "DisplayMultiTurretNCCodeInHTMLFormat macro error"
Err.Clear
Exit Function
End Function
' This function is called whenever the dialog box is changed
Function DialogFunc%(DlgItem$, Action%, SuppValue%)
Dim input_file_dir As String, input_file_ext As String, input_file_partial_name As String, input_file_name As String
Dim output_file_name As String, pos As Integer
Select Case Action%
Case 1 ' Dialog box initialization
If DlgValue("CheckBox1") = 1 Then
DlgEnable "Button1",True
DlgEnable "input_file",True
Else
DlgEnable "Button1",False
DlgEnable "input_file",False
End If
DialogFunc% = False 'do not exit the dialog
Case 2 ' Value changing or button pressed
Select Case DlgItem$
Case "CheckBox1"
If SuppValue = 1 Then
DlgEnable "Button1",True
DlgEnable "input_file",True
Else
DlgEnable "Button1",False
DlgEnable "input_file",False
DlgText "output_file", output_file_name_original
End If
DialogFunc% = True 'do not exit the dialog
Case "Button1"
input_file_name = GetFilePath(,"*.*",,"Open",)
If input_file_name <> "" Then
DlgText "input_file", input_file_name
'Construct output file name (html), which will be displayed in the dialog box
FindDirectoryAndFilename input_file_name, input_file_dir, input_file_ext, input_file_partial_name
output_file_name = DlgText("output_file")
pos = InStrRev(UCase(output_file_name), "\")
If (pos > 0) Then
output_file_name = Left(output_file_name, pos) & input_file_partial_name & ".html"
DlgText "output_file", output_file_name
End If
End If
DialogFunc% = True 'do not exit the dialog
End Select
End Select
End Function
Private Sub FindAllSyncCodes(ByRef nc_code() As String, ByRef sync_codes() As String, _
ByRef total_num_sync_nums As Integer)
Dim found_sync_num As Boolean
Dim i As Integer
Dim sn_i As Integer
Dim sn_pos As Long, q_pos As Long, newline_pos As Long
total_num_sync_nums = 0
For i = LBound(nc_code) To UBound(nc_code)
nc_code(i) = UCase(nc_code(i))
Next i
For sn_i = sync_num_lower_bound To sync_num_upper_bound
i = 0
found_sync_num = False
While ((i <= UBound(nc_code)) And (Not found_sync_num))
sn_pos = InStr(nc_code(i), sync_num_prefix & sn_i)
If (sn_pos > 0 And Not IsNumeric(Mid(nc_code(i), sn_pos + Len(sync_num_prefix & sn_i), 1))) Then
q_pos = InStr(sn_pos, nc_code(i), "Q")
newline_pos = InStr(sn_pos, nc_code(i), vbCr)
'it's not a sync code if there is a "Q" on the same line
If ((sync_num_prefix = "M") Or ((sync_num_prefix = "P") And (q_pos <= 0 Or q_pos > newline_pos))) Then
found_sync_num = True
ReDim Preserve sync_codes(total_num_sync_nums)
sync_codes(total_num_sync_nums) = sync_num_prefix & sn_i
total_num_sync_nums = total_num_sync_nums + 1
End If
End If
i = i + 1
Wend
Next sn_i
End Sub
Private Sub FragmentNcCode(ByRef nc_code() As String, ByRef sync_codes() As String, _
ByVal total_num_sync_nums As Integer, ByRef nc_code_fragments() As String)
Dim found_sync_num As Boolean
Dim pos As Long, q_pos As Long, newline_pos As Long
Dim tmp_nc_code As String
Dim i As Integer
Dim sn_i As Integer
Dim file_i As Integer
If (total_num_sync_nums <= 0) Then
ReDim Preserve nc_code_fragments(UBound(nc_code()), 0)
For file_i = LBound(nc_code) To UBound(nc_code)
nc_code_fragments(file_i, 0) = Replace(nc_code(file_i), vbCrLf, "
")
Next file_i
Exit Sub
End If
ReDim Preserve nc_code_fragments(UBound(nc_code), 2 * total_num_sync_nums)
For file_i = LBound(nc_code) To UBound(nc_code)
tmp_nc_code = nc_code(file_i)
For sn_i = LBound(sync_codes) To UBound(sync_codes)
pos = CLng(InStr(UCase(tmp_nc_code), sync_codes(sn_i)))
If (pos > 0) Then
q_pos = CLng(InStr(pos, UCase(tmp_nc_code), "Q"))
newline_pos = CLng(InStr(pos, UCase(tmp_nc_code), vbCr))
'it's not a sync code if there is a "Q" on the same line
If ((sync_num_prefix = "M") Or ((sync_num_prefix = "P") And (q_pos <= 0 Or q_pos > newline_pos))) Then
nc_code_fragments(file_i,2*sn_i) = Replace(Mid(tmp_nc_code, 1, pos-1), vbCrLf, "
")
tmp_nc_code = Mid(tmp_nc_code, pos)
pos = CLng(InStr(UCase(tmp_nc_code), vbLf))
If (pos > 0) Then
nc_code_fragments(file_i,2*sn_i + 1) = Replace(Mid(tmp_nc_code, 1, pos), vbCrLf, "
")
tmp_nc_code = Mid(tmp_nc_code, pos+Len(vbLf))
End If
End If
End If
Next sn_i
nc_code_fragments(file_i,2*sn_i) = Replace(tmp_nc_code, vbCrLf, "
")
Next file_i
End Sub
Private Function SaveHTMLFile(ByVal output_file_name As String, _
ByVal total_num_sync_nums As Integer, _
ByVal num_of_turrets As Integer, _
ByRef nc_code_fragments() As String) As Boolean
Dim html_file
Dim fso
Dim tmp As String
Dim sync_code_found As Boolean
Dim sync_code_found_each_file() As Boolean
Dim search_sync_num As Integer
Dim i As Integer, sn_i As Integer, prev_row_span_i As Integer, temp_i As Integer
Dim row_span() As Integer
Dim jump_to_row() As Integer
Dim num_of_lines_to_combine As Integer
Dim prev_non_empty_line_ind As Integer
Dim next_non_empty_line_ind As Integer
Dim sync_code_line As String
SaveHTMLFile = False
Set fso = CreateObject("Scripting.FileSystemObject")
ReDim row_span(num_of_turrets, 2 * total_num_sync_nums)
ReDim jump_to_row(num_of_turrets, 2 * total_num_sync_nums)
For i = 0 To num_of_turrets
For sn_i = 0 To total_num_sync_nums*2
jump_to_row(i, sn_i) = sn_i
row_span(i, sn_i) = 0
Next sn_i
Next i
For i = 0 To num_of_turrets
prev_row_span_i = 0
For sn_i = 0 To total_num_sync_nums*2
If (nc_code_fragments(i, sn_i) = "") Then
prev_non_empty_line_ind = sn_i - 1
While ((sn_i <= total_num_sync_nums*2) And (nc_code_fragments(i, sn_i) = ""))
sn_i += 1
Wend
If (nc_code_fragments(i, sn_i) <> "") Then
next_non_empty_line_ind = sn_i
End If
If (sn_i Mod 2 <> 0) Then 'next line contains sync code, so we shouldn't include it into spanned row
next_non_empty_line_ind -= 1
End If
For temp_i = prev_non_empty_line_ind + 1 To next_non_empty_line_ind
jump_to_row(i, temp_i) = next_non_empty_line_ind + 1
Next
row_span(i, prev_non_empty_line_ind + 1) = next_non_empty_line_ind - prev_non_empty_line_ind
'row_span(i, prev_row_span_i) = row_span(i, prev_row_span_i) + 1
'If (row_span(i, prev_row_span_i) > 1) Then
' For temp_i = prev_row_span_i To Min(total_num_sync_nums*2, prev_row_span_i + row_span(i, prev_row_span_i) + 1)
' jump_to_row(i, temp_i) = prev_row_span_i + row_span(i, prev_row_span_i) + 1
' Next temp_i
'End If
'Else
' prev_row_span_i = sn_i + 1
End If
Next sn_i
Next i
'Create html file
On Error Resume Next
Set html_file = fso.OpenTextFile(output_file_name, 2, True)
If (Not fso.FileExists(output_file_name)) Then
MsgBox "Failed to create file " & output_file_name
Exit Function
End If
If (html_file Is Empty) Then Exit Function
On Error GoTo reportError
html_file.WriteLine("")
html_file.WriteLine("