' ' FadalMacroTopOfFile.bas - Hook to save macro at top of file for Fadal style posts ' ' Author: Paul T. Shilton ' Organization: Engineering Geometry Systems ' Date: 7/17/02 ' Copyright (c) 2002, Engineering Geometry Systems ' ' 10/25/2004 Kyle Kershaw ' Edited to include renumbering sequence numbers. ' ' General Description ' ' - requires that the CNC file be set to save all macros in a single file ' - requires that the block increment be set to zero to suppress any sequence numbers. ' sequence numbers are added during the renumbering process. ' - when the post-NCGen hook fires, the nc output is scanned to find the program name. the ' contents of the macro file are inserted after the program name line. ' - if the program name is not found in the first 2 lines or there is not 1 macro file, ' the nc code file is not changed ' Private Sub Application_PostNCCreate(Doc As FeatureCAM.MFGDocument, ByVal nc_file_name As String, _ ByVal macro_file_cnt As Long, ByVal macro_file_names As Variant) Dim block_incr As Long Application.GetMillPostOptions(,,block_incr) If (block_incr <> 0) Then MsgBox "Please set block increment to zero in the Post Options to eliminate sequence numbers" + vbCrLf + _ "Sequence numbers will be re-created during the renumbering process",vbInformation, _ "Fadal Macro at Top Of Program" Exit Sub End If ' get a temp file name Dim tmp_name As String, ext_index As Integer nc_file_name = UCase( nc_file_name) ext_index = InStrRev( nc_file_name, ".TXT") tmp_name = Left$( nc_file_name, ext_index-1) + ".tmp" ' get the setup Dim setup As FMSetup Set setup = Doc.Setups.ActiveSetup ' get the part name Dim part_name As String part_name = "O" + setup.PartName part_name = UCase( part_name) ' open the nc code for reading and the tmp file for writing Open tmp_name For Output As #1 Open nc_file_name For Input As #2 ' check for the part name in the first 2 lines of the file Dim found_part_name As Boolean, buffer As String, i As Integer found_part_name = False i=1 Line Input #2, buffer Print #1, buffer If( InStrRev( buffer, part_name) <> 0) Then found_part_name = True Else Line Input #2, buffer Print #1, "N" + CStr(i) + " " + buffer i=i+1 If( InStrRev( buffer, part_name) <> 0) Then found_part_name = True End If End If ' if we found the part name and there is just 1 macro file, dump the macro file ' contents into tmp file after the part name If( found_part_name And macro_file_cnt = 1) Then Open macro_file_names(1) For Input As #3 While Not EOF(3) Line Input #3,buffer Print #1, "N" + CStr(i) + " " + buffer i=i+1 Wend Print #1, "N" + CStr(i) + " M30" i=i+1 Close #3 While Not EOF(2) Line Input #2,buffer If Not ( InStrRev( buffer, "%") <> 0) Then Print #1, "N" + CStr(i) + " " + buffer i=i+1 Else Print #1, buffer End If Wend End If Close #1 Close #2 ' delete the nc code and macro files and rename the tmp file so that it shows up in ' the nc code window If( found_part_name And macro_file_cnt = 1) Then Kill nc_file_name Kill macro_file_names(1) FileCopy( tmp_name, nc_file_name) Kill tmp_name Else Kill tmp_name End If End Sub