GCAdapter
1. Introduction
Overview
The GCAdapter is a highly customizable post that works inside of Mach 4 that changes GCode to Mach compatible GCode. This manual will describe how to use the GCAdapter.
File Locations
GCAdapter.lua
In the Mach4 directory, GCAdapter.lua is located in the Modules folder
"\Mach4_Directory\Modules\GCAdapter.lua"
GCAdapter_Common_Post.lua, GCAdapter_Machine_Post.lua, GCAdapter_User_Post.lua
In the Mach4 directory, the Post files are located in the Post\GCAdapter folder
Example Post file location:
"\Mach4_Directory\Post\GCAdapter\GCAdapter_Common_Post.lua"
GCAdapterConfig.lua
The config file is created when a file is loaded for the first time or the "Adapt GCode Settings" Mach command is called for the first time. It is located in the Mach4 directory, in the Profiles folder, in the specific profile of the machine folder, and then in the GCAdapterSettings folder.
Example GCAdapterConfig.lua location:
"\Mach4_Directory\Profiles\Mill\GCodeAdapterSettings\GCAdapterConfig.lua"
Word Parlance
block - whole single line of GCode EX: "G01 X12 Y32 Z1"
word - a GCode instruction EX: "G01", "M06", "X12"
code - the letter part of a word EX: "G","X","M"
value - the number part of a word EX: "23","01"
Order of Modifications
- CommonPreParse Functions
- MachinePreParse Functions
- UserPreParse Functions
- Table Modifiers
- InsertAt Function
- CommonMod Functions
- MachineMod Functions
- UserMod Functions
Post Setup
To setup the User Post file, all you need to do is alter POST_NAME. Change "Machine" in 'GCAdapter_Machine_Post' to the specific machines type.
Example: 'GCAdapter_Fanuc_Post'
local POST_NAME = 'GCAdapter_Machine_Post'
package.loaded[POST_NAME] = nil
local is_ok,ParentPostTable = pcall(require,POST_NAME)
if not is_ok then
print(string.format("Failed to load %s post\n%s",POST_NAME,ParentPostTable))
end
ParentPostTable.__index = ParentPostTable
local UserPost = setmetatable({},ParentPostTable)
local POST_NAME = 'GCAdapter_Machine_Post'
2. Setup & Configuration
Copy Machine and User .lua post files into the following directory: Mach4\Post\GCAdapter folder .
To use the GC Adapter, you must first enable the MachMotion parameter. Set Convert Files to Yes.
Then go to the Service screen, click on CMDS, and then GC Adapter Settings. Press Run Command.
GCAdapter Settings
Opens a settings menu which allows the user to change the settings of the GCAdapter
-
Select User Post Processor:
-
-
- Opens a drop down menu to select the Post file for the Adapter to use
- Pulls available posts from the PostFiles folder in the Mach directory
- If not using a default post processor, select the User Post processor.
-
Other Advanced Settings:
-
Default File Extensions:
- These extensions are what GCAdapter is allowed to convert
- Only files with these extensions will show when looking for a file to load
- If All Files(*) is present, all files are allowed to be converted
- If adding a new extension, make sure to follow the format of other extensions or add from Post
-
Default Output Directory:
- Click Browse to look through folders
- The selected folder is where converted files will be output
-
Max Length of File:
- The number here is the max number of lines a file is allowed to have
- Make sure there are no commas in the number
- Be careful allowing files much larger than 260,000
-
Number of digits after decimal point:
- Changes the number of decimal places in certain GCode words
- The number after the decimal point is how many digits will be in the decimals place of certain GCode words
- When changing make sure to leave decimal point and only change the number
-
Show modifications:
- If YES, when converting, will write modifications made by GCAdapter to the top of the converted file
- If NO, won't write modifications to the top of converted file
Other Options under CMDS
GCAdapter Adapt GCode
Opens a file dialog allowing the user to select a GCode file to run through GCAdapter. Loads GCode into Mach when done.
"Load" from the GCode screen automatically calls this command
GCAdapter Adapt Folder
Opens a dialogue to select a folder and then converts all GCode files in that folder. If the selected folder has other folders in it, will step into the folders to convert files in them as well.
GCAdapter Adapt Folder Showing Toolpaths
Same as Adapt Folder but will wait a few seconds after loading GCode into Mach to allow the user to view the tool path.
3. Block Functions
These functions either modify or compare a block of GCode.
TableToString()
- Returns a formatted string that can be printed to see the data of block
Comparator Functions
These functions are used to find specific data in a block. has, without, lessThan, greaterThan, withinRange return either true or false. getBlockBefore, getBlockAfter, getBlockAt return the corresponding block.
has( passed_word, close_to, thresh )
- Looks for passed_word in block, returning true if found or false if not found
- passed_word can be either full word, "X32", or just a code "G"
- close_to is an optional parameter and if passed looks within range of thresh
- If thresh isn't passed, set to default value 10E-6
- Examples:
- block:has("G53")
- block:has("G")
- block:has("M06" , "true" , .0006)
without( passed_word )
- Looks for passed_word in block, returning false if found or true if not found
- passed_word can be either full word, "Y21", or just a code "F"'
- Examples:
- block:without("T2")
- block:without("Z")
lessThan( passed_word )
- Looks for a matching word with values less than passed_word in block
- Return true if a matching word is found, otherwise returns false
- passed_word must be a full word
- Examples:
- block:lessThan("T100")
- block:lessThan("M7")
greaterThan( passed_word )
- Looks for a matching word with values greater than passed_word in block
- Return true if a matching word is found, otherwise returns false
- passed_word must be a full word
- Examples:
- block:greaterThan("G01")
- block:greaterThan("X10")
withinRange( code, low_val, high_val )
- Looks for a word that starts with code and is between low_val and high_val
- Returns true if a matching word is found otherwise returns false
- code is a code, low_val and high_val are values
- Examples:
- block:withinRange("T",1,99)
- block:withinRange("X",-23,23)
getBlockBefore()
- returns the block on the line above the current block
- Example: block:getBlockBefore()
getBlockAfter()
- returns the block on the line below the current block
- Example: block:getBlockAfter()
getBlockAt( line_number )
- returns the block at line_number
- Example: block:getBlockAt(23)
Modifier Functions
ModWord( self, old_word, new_word )
- Replaces old_word with new_word in current block
- self refers to block calling ModWord. Passed implicitly when called
- old_word and new_word can be either a word or a code (They must be the same)
- If they are a code, be careful because depending on where you call ModWord, it will replace all instances of that code
- Examples:
-
block:ModWord("M02","M30")
- block:ModWord("G43.4","G43")
- block:ModWord("R","P")
-
DeleteWord( self, target, codes )
- Deletes target along with codes (if provided) from current block
- self refers to block calling DeleteWord. Passed implicitly when called
- target is a word and codes is a list of parameters belonging to target that should be deleted with it
- Examples:
- block:DeleteWord("G53.1")
- block:DeleteWord("G5.1",{"Q","R"})
AddWord( self, new_word )
- Adds new_word onto the end of the current block
- self refers to block calling AddWord. Passed implicitly when called
- new_word needs to be a word
- Examples:
- block:AddWord("G83")
- block:AddWord("M06")
CommentWord( self, target )
- Comments out target in the current block
- self refers to block calling CommentWord. Passed implicitly when called
- target is a word
- Examples:
- block:CommentWord("M29")
- block:CommentWord("G53")
InsertBlockBefore( new_block )
- Inserts new_block on the line above the current block
- new_block is a block of GCode
- Examples:
- block:InsertBlockBefore("G00 X2 Y6.77 Z0")
- block:InsertBlockBefore("M6 T05")
InsertBlockAfter( new_block )
- Inserts new_block on the line after the current block
- new_block is a block of GCode
- Examples:
- block:InsertBlockAfter("G00 X2 Y6.77 Z0")
- block:InsertBlockAfter("M6 T05")
InsertBlockAtLine( new_block, new_index )
- Inserts new_block at new_index (Pushes previous block at index down)
- new_block is a block of GCode and new_index is the line number of where to insert
- Examples:
- block:InsertBlockAtLine("G83 P12 Q32", 32)
- block:InsertBlockAtLine("M6 T25",46)
DeleteBlock( self, str )
- Deletes entire block that has an exact match with str and match isn't a comment
- self refers to block calling DeleteBlock. Passed implicitly when called
- str is a string that will be looked for in the block
- Examples:
- block:DeleteBlock("G83 R54")
- block:DeleteBlock("L_Parkz")
DeleteCurrentBlock()
- Deletes the current block
- Example:
- block:DeleteCurrentBlock()
DeleteBlockAt( del_index )
- Deletes block at del_index
- del_index is the line number of the block to be deleted
- Example:
- block:DeleteBlockAt(3)
CommentBlock( self, str )
- Comments entire block that has an exact match with str and match isn't a comment
- self refers to block calling CommentBlock. Passed implicitly when called
- str is a string that will be looked for in the block
- Examples:
- block:CommentBlock("G02 X12")
- block:CommentBlock("M04")
CommentCurrentBlock()
- Comments out the current block
- Example:
- block:CommentCurrentBlock()
JoinBlock( loc )
- Takes block from either before, after or at an index, adds words in block to end of current block then deletes block at loc
- loc is either a string ("BEFORE" or "AFTER"), or a number corresponding to the line number of the block to pull from
- Example:
- block:JoinBlock("BEFORE")
- block:JoinBlock("AFTER")
- block:JoinBlock(54)
4. Post
The Post consists of 3 different post files but the user only needs to interact with the final user specific Post
Modifier Tables
These tables will automatically call the modifier functions and are a helpful way to make simple modifications to GCode files. These modifiers are ran on every block of GCode in the file. When adding to the tables, copy and paste the examples and replace what you want to easily keep the correct format.
Examples:
-------------------UserPost Varaiables----------------------
--Table of words to be replaced.
--If given just a code will replace all instances of that code
--{"ModWord","OldWord","NewWord"},
UserPost.WordsToReplace =
{
--EX: {"ModWord","G17","G18"},
--EX: {"ModWord","G","H"},
}
--Table of words to be deleted. If given parameters along with word will delete then as well
--{"DeleteWord","WordToDelete"}
--{"DeleteWord","WordToDelete",{"Param1","Param2","Param3"}}
UserPost.WordsToDelete =
{
--EX: {"DeleteWord","G80"},
--EX: {"DeleteWord","G99",{"X","Y","Z"}},
}
--Table of words to be commented out
--They are added to the end of line with a valid comment
--{"CommentWord","WordToComment"}
UserPost.WordsToComment =
{
--EX: {"CommentWord","G81"},
{"CommentWord","M29"}
}
--Looks for an exact string match in block and if found deletes whole block
--{"DeleteBlock","StringToFind"}
UserPost.BlocksToDelete =
{
--EX: {"DeleteBlock","X12 Y14"},
}
WordsToReplace
Uses ModWord to modify all matching words in the file
WordsToDelete
Uses DeleteWord to delete all matching words in the file
WordsToComment
Uses CommentWord to comment out all matching words in the file
BlocksToDelete
Uses DeleteBlock to delete all matching blocks in the file
BlockToComment
Uses CommentBlock to comment out all matching blocks in the file
CommentsToChange
Add invalid comments here to be converted to valid comments
DefaultExtensions
Add extensions here to add them to the extensions that are allowed to be converted. Copy and replace the examples to make sure the format is correct.
CommentEverythingBefore
Add a string here to comment out all blocks at and above a block that has a matching string
Modifier Functions
These functions are so the user can add their own custom functions. The user should write the function definition underneath "--Write custom functions here"(They can be written anywhere but keeping them here will help with organization). Then write function calls inside either PreParseFunctions or ModFunctions.
-------------------UserPost Modifier Function Calls--------------
--Custom functions to run before parsing
function UserPost.UserPreParseFunctions( file_list, index )
--ExampleFunctionCall(file_list,index)
end
--Call custom functions here
function UserPost.UserModFunctions( block )
--Don't remove this function
UserPost:InsertAtLocations(block)
end
------------------UserPost Insert Functions--------------------
--Inserts before or after a tool change("M6")
function UserPost.AtToolChange( block )
--block:InsertBlockBefore("G00 X2")
--block:InsertBlockAfter("G01 X5 Y23 Z12")
end
--Inserts before or after a spindle change("M3","M4","M5")
function UserPost.AtSpindleChange( block )
--block:InsertBlockBefore("G00 X2")
--block:InsertBlockAfter("G01 X5 Y23 Z12")
end
--Inserts before or after file start(Be careful inserting before)
function UserPost.AtStartOfFile( block )
--block:InsertBlockBefore("G00 X2")
--block:InsertBlockAfter("G01 X5 Y23 Z12")
end
--Inserts before or after M30,at end of file(Be careful inserting after M30)
function UserPost.AtM30( block )
--block:InsertBlockBefore("G00 X2")
--block:InsertBlockAfter("G01 X5 Y23 Z12")
end
----------------------UserPost Functions ----------------------
--Write custom functions here
PreParseFunctions
- Any functions here are ran before the GCAdapter parses the GCode blocks. So functions here are done on the raw string
- The best kind of functions to add here are string matching functions and modifications that need to be done before the blocks are parsed
- Functions called here will need file_list and index passed to them
--Custom functions to run before parsing
function UserPost.UserPreParseFunctions( file_list, index )
--ExampleFunctionCall(file_list,index)
end
ModFunctions
- Functions here are ran after the GCAdapter has parsed the blocks. So any functions here are done on parsed blocks
- The best kind of functions to add here are functions that the user will want to use the block functions in for easier block manipulation
- Any functions called here will need block passed to them
- It also comes with a prebuilt function that inserts blocks at specific, common places. For more details look at Insert Functions
--Call custom functions here
function UserPost.UserModFunctions( block )
--Don't remove this function
UserPost:InsertAtLocations(block)
end
Insert Functions
- These functions are easy ways to insert blocks at specific locations
- AtToolChange will insert blocks before or after a M6 call
- AtSpindleChange will insert blocks before or after a M3,M4 or a M5 call
- AtStartOfFile will insert blocks before or after the start of the file
- If before, the inserted block will go on line 1.
- If after, the inserted block will go after block on line 1
- Be careful what you insert before start of file.
- At M30 will insert blocks before or after a M30 call
- Be careful what you insert after M30
------------------UserPost Insert Functions--------------------
--Inserts before or after a tool change("M6")
function UserPost.AtToolChange( block )
--block:InsertBlockBefore("G00 X2")
--block:InsertBlockAfter("G01 X5 Y23 Z12")
end
--Inserts before or after a spindle change("M3","M4","M5")
function UserPost.AtSpindleChange( block )
--block:InsertBlockBefore("G00 X2")
--block:InsertBlockAfter("G01 X5 Y23 Z12")
end
--Inserts before or after file start(Be careful inserting before)
function UserPost.AtStartOfFile( block )
--block:InsertBlockBefore("G00 X2")
--block:InsertBlockAfter("G01 X5 Y23 Z12")
end
--Inserts before or after M30,at end of file(Be careful inserting after M30)
function UserPost.AtM30( block )
--block:InsertBlockBefore("G00 X2")
--block:InsertBlockAfter("G01 X5 Y23 Z12")
end
5. Developer Information
How It Works
The GCAdapter has 4 main operations and they happen in this order
- Reading in file
- Stores each line of file in a table, fixes invalid comments and runs any modifiers that happen before parsing
- Parsing lines in file to blocks
- Separates words and comments in lines. Storing them in a block
- Running post modifiers on blocks
- Modifies blocks based on modifiers in Post files
- Outputting modified blocks
- Formats blocks and outputs them to the output file