You question wasn't very specific, so the answer isn't either. You didn't specify your CNC controller, your computer, your operating system, etc.
Various options:
- Write a program in a real programming language to output G code
- Use loops
- Use conditionals to simulate loops
- Use subroutines
These last few aren't terribly standardized across controllers. One machine might use M94 to call a subroutine and another might use O100. One machine might use O101 for a loop and another might require you to simulate a loop with "IF" statements.
In the case of a program writing a program, you can modify the program to support multiple controllers ideosyncratic g-code. You can also add things like text engraving which many controllers don't do (of course, you will need a vector font and code to handle it).
For a ruler, you probably don't want to make all your lines equal size, so simply repeating an operation 1000 times isn't your best bet. A subroutine
may be more appropriate.
Incremental moves can be used to continue where the last tick mark left off, or you can use absolute moves and a variable, which you offset each time. Or you can move the cordinate space as suggested by previous poster.
This psuedocode shows using subroutines with no loops. Eliminating tencm and replacing onemeter with a loop that calls onecm 100 times would generally be more useful.
Code:
onecm()
{
tick();
tick();
tick();
tick();
mediumtick();
tick();
tick();
tick();
tick();
longtick();
}
tencm()
{
onecm();
onecm();
onecm();
onecm();
onecm();
onecm();
onecm();
onecm();
onecm();
onecm();
onecm();
}
onemeter()
{
longtick();
tencm();
tencm();
tencm();
tencm();
tencm();
tencm();
tencm();
tencm();
tencm();
tencm();
}
Of course, this is uglier and machine specific in g-code.
Or a 31 line minimalist C program to generate the g-code. Doesn't do any of the initial setup (feed rates, select metric, etc.), just the repetitive part:
Code:
// C language program to draw ruler ticks
// Compile using, for example: cc -lm -oticks ticks.c
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
main()
{
// declare variables
int i;
float ticklength;
float x;
float y;
y=0.0;
for(i=0; i<=1000; i+=1) {
if((i%10)==0) { // i divisible by 10, % is modulo operator
ticklength=6.0;
} else if( (i%5)==0) { // i divisible by 5
ticklength=4.0;
} else {
ticklength=2.0;
}
x=i;
// % is beginning of parameter substitution in format string
// %.3f is a floating point number with 3 digits after decimal point
// \n is a newline (carriage return/line feed)
printf("G01 X%.3f Y%.3f\n", x, y);
printf("G01 Z%.3f\n", -0.1);
printf("G01 X%.3f Y%.3f\n", x, y-ticklength);
printf("G01 Z%.3f\n", 1.0); // traverse height
}
}
This generates 4004 lines of g-code. Replacing the printf() calls for each line of G01 with a subroutine that does the printing allows you to adjust for different formatting for different controllers (some need different number of decimals or omitted decimal point, etc.).
Another approach is to use a CAD package (even a free 2D one will work), and a gcode converter. Or CAM software.