HCL CAMWorks
Published

Computer Programming-Related Features of Custom Macro

Custom macro is an interpreter-based language, meaning that all CNC G code and custom macro commands are executed as the CNC comes across them.

Share

Leaders-In background
Operator hand pushing a button

In last month’s column, we described five application categories for custom macro. Here, we dig into custom macro features.

The two categories of custom macro features are computer programming-related features and CNC-related features. This article examines the following computer programming-related features:

  • Variables
  • Arithmetic
  • Program flow control (logic)
  • Looping

These features can be found in any computer programming language. While most current computer programming languages are compiler-based, custom macro is an interpreter-based language. This means that all CNC G code and custom macro commands are executed as the CNC comes across them. This gives a custom macro the ability to make decisions in real time.

Variables are storage registers for values. They can be used to represent just about anything, including — among many other things — component dimensions, spindle speeds, feed rates, calculated values or “flags” that tell the custom macro how to behave. You can use variables in place of hard-and-fixed values or to help with calculations needed within the CNC program.

There are five kinds of variables in custom macro:

  1. Letter address arguments – One way to get data into the custom macro is to use a G65 or G66 command and specify a series of letter address arguments (input data). They resemble the various values placed in a hole machining canned cycle, like X, Y, R, Z and so on.
  2. Local variables – Ranging from #1-#33, the primary application for local variables is to represent the letter address arguments from the calling G65 or G66 command. Local variable #1, for instance, is used in the custom macro to represent the value of letter address “A” specified in the calling command. Local variables are reset to null at the completion of the custom macro’s execution (at M99).
  3. Common variables – Ranging from #100 through #149, these variables can be shared among programs, including multiple custom macros. They are set back to null at the completion of the program (at M30 or M02) or when the power is turned off (determined by parameter setting).
  4. Permanent common variables – Ranging from #500 through at least #549, these variables are retained after the power is turned off. In this regard, they are much like tool offset registers.
  5. System variables – Ranging over #1000, these variables provide access to many of the CNC-related features of custom macro.

Example that drills a hole:

Main program:

  • O0001
  • N005 T01 M06 (Drill)
  • N010 G54 G90 S500 M03
  • N015 G00 X0 Y0
  • N020 G43 H01 Z1.0 M08
  • N025 G65 P1000 X3.0 Y2.5 R0.1 Z-0.8 F10.0 (local variable assignments: X:#24, Y#25, R#18, Z#26, F:#9)
  • N030 G91 G28 Z0

Custom macro:

  • O1000
  • G00 X#24 Y#25
  • Z#18
  • G01 Z#26 F#9
  • G00 Z#18
  • M99

Arithmetic provides the ability to make calculations. Just about anything that can be done on a scientific calculator can be done with custom macro. This includes basic functions like add (+), subtract (-), multiply (*), and divide (/), as well as higher level functions like trig functions including sine (SIN), cosine (COS) and tangent (TAN), as well as other functions like square root (SQRT) and rounding (ROUND, FIX and FUP).

Program flow control (logic) provides decision-making capabilities. An IF statement is used to specify the test. If the test result is true, the custom macro will do what is to the right of the IF statement. If the test is false, the custom macro will execute the next command. Logical operators provide what it is you want to test against.

  • Equal to (EQ)
  • Not equal to (NE)
  • Greater than (GT)
  • Greater than or equal to (GE)
  • Less than (LT)
  • Less than or equal to (LE)

A GOTO statement is used to branch from one point in the custom macro to another. Sequence numbers are used as statement labels, branching points to which program execution will be sent.

Example that decides whether to use coolant:

  • O0001
  • N005 T01 M06 (Drill)
  • N010 G54 G90 S500 M03
  • N015 G00 X0 Y0
  • N020 G43 H01 Z1.0
  • N025 G65 P1000 X3.0 Y2.5 R0.1 Z-0.8 F10.0 C1.0 (C: #3)
  • N030 G91 G28 Z0

If letter address C (local variable #3) is set to 1.0, coolant will be turned on. If it is set to 0, coolant will be turned off.

  • .
  • IF [#3 EQ 0] GOTO 15 (Test)
  • M08 (Coolant comes on)
  • GOTO 20
  • N15 M09 (Coolant stays off)
  • N20 (Program continues)
  • .

Looping provides the ability to repeat a group of commands, while enabling certain things to change between executions. This can be a very powerful feature, generating many actions (like CNC motions or offset settings) based on only a few commands.

A WHILE statement can be used to specify a loop. As long as the condition within brackets is true, the loop will continue executing. When the condition is no longer true, the loop will end.

Example that counts to ten:

  • .
  • #100 = 1.0 (Counter)
  • WHILE [#100 LE 10.0] DO 1 (Test)
  • (Perform the action/s for each execution of the loop here)
  • #100 = #100+1 (Step counter)
  • END 1 (Go back to test)
  • N15 (Program continues)
  • .
HCL CAMWorks
World Machine Tool Survey
WENZEL America
FLEXARM
One-Touch Clamps
Techspex
Demo Fulcrum
Muratec

Related Content

Aerospace

5 Tips for Running a Profitable Aerospace Shop

Aerospace machining is a demanding and competitive sector of manufacturing, but this shop demonstrates five ways to find aerospace success.

Read More
CAD/CAM

Cutting Part Programming Times Through AI

CAM Assist cuts repetition from part programming — early users say it cuts tribal knowledge and could be a useful tool for training new programmers.

Read More

Can AI Replace Programmers? Writers Face a Similar Question

The answer is the same in both cases. Artificial intelligence performs sophisticated tasks, but falls short of delivering on the fullness of what the work entails.

Read More
Basics

Tips for Designing CNC Programs That Help Operators

The way a G-code program is formatted directly affects the productivity of the CNC people who use them. Design CNC programs that make CNC setup people and operators’ jobs easier.

Read More

Read Next

CNC Tech Talks

5 Reasons Why You Should Know How to Write Custom Macros

Custom macros enhance what can be done in G-code programs, giving users the ability to code operations that were previously not possible.

Read More
View From My Shop

Shop Tour Video: You've Never Seen a Manufacturing Facility Like This

In the latest installment of our “View From My Shop” series, explore Marathon Precision’s multi-process approach to manufacturing, where blacksmiths and hand-forged dies meet state-of-the-art CNC machining. Discover how restoring classic muscle cars and building custom art projects creates a dynamic shop culture — and draws top talent to this unique and innovative metalworking facility. 

Read More
Top Shops

2025 Top Shops Benchmarking Survey Now Open Through April 30

Modern Machine Shop's Top Shops Benchmarking Survey is now open, offering metalworking and machining operations actionable feedback across several shopfloor and business metrics. 

Read More
HCL CAMWorks