pcb 4.1.1
An interactive printed circuit board layout editor.

gcode.c File Reference

GCODE export HID. More...

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include "global.h"
#include "error.h"
#include "data.h"
#include "misc.h"
#include "rats.h"
#include "hid.h"
#include "hid_draw.h"
#include "../hidint.h"
#include <gd.h>
#include "hid/common/hidnogui.h"
#include "hid/common/draw_helpers.h"
#include "bitmap.h"
#include "curve.h"
#include "potracelib.h"
#include "trace.h"
#include "decompose.h"
#include "pcb-printf.h"
#include "hid/common/hidinit.h"
Include dependency graph for gcode.c:

Go to the source code of this file.

Data Structures

struct  color_struct
struct  hid_gc_struct
struct  drill_hole
 Structure to represent a single hole. More...
struct  single_size_drills
 Structure to represent all holes of a given size. More...

Defines

#define CRASH   fprintf(stderr, "HID error: pcb called unimplemented GCODE function %s.\n", __FUNCTION__); abort()

Functions

 REGISTER_ATTRIBUTES (gcode_attribute_list)
 Convert from default PCB units to gcode units.
static void gcode_get_filename (char *filename, const char *layername)
 Fits the given layer name into basename, just before the suffix.
static void sort_drill (struct drill_hole *drill, int n_drill)
 Sorts drills to produce a short tool path.
static void gcode_parse_arguments (int *argc, char ***argv)
static HID_Attributegcode_get_export_options (int *n)
static void gcode_choose_groups ()
 Populates gcode_export_group array.
static void gcode_alloc_colors ()
 Allocate white and black.
static void gcode_start_png ()
static void gcode_finish_png (const char *layername)
static void gcode_start_png_export ()
static FILE * gcode_start_gcode (const char *layername, bool metric)
static void gcode_do_export (HID_Attr_Val *options)
static int gcode_set_layer (const char *name, int group, int empty)
static hidGC gcode_make_gc (void)
static void gcode_destroy_gc (hidGC gc)
static void gcode_use_mask (enum mask_mode mode)
static void gcode_set_color (hidGC gc, const char *name)
static void gcode_set_line_cap (hidGC gc, EndCapStyle style)
static void gcode_set_line_width (hidGC gc, Coord width)
static void gcode_set_draw_xor (hidGC gc, int xor_)
static void gcode_set_draw_faded (hidGC gc, int faded)
static void use_gc (hidGC gc)
static void gcode_draw_rect (hidGC gc, Coord x1, Coord y1, Coord x2, Coord y2)
static void gcode_fill_rect (hidGC gc, Coord x1, Coord y1, Coord x2, Coord y2)
static void gcode_draw_line (hidGC gc, Coord x1, Coord y1, Coord x2, Coord y2)
static void gcode_draw_arc (hidGC gc, Coord cx, Coord cy, Coord width, Coord height, Angle start_angle, Angle delta_angle)
static int _drill_size_comparator (const void *_size0, const void *_size1)
 Given a hole size, return the structure that currently holds the data for that hole size.
static struct single_size_drillsget_drill (double diameter_inches)
static void add_hole (struct single_size_drills *drill, double cx_inches, double cy_inches)
static void gcode_fill_circle (hidGC gc, Coord cx, Coord cy, Coord radius)
static void gcode_fill_polygon (hidGC gc, int n_coords, Coord *x, Coord *y)
static void gcode_calibrate (double xval, double yval)
static void gcode_set_crosshair (int x, int y, int a)
void hid_gcode_init ()

Variables

static HID gcode_hid
static HID_DRAW gcode_graphics
static struct color_structblack = NULL
static struct color_structwhite = NULL
static int linewidth = -1
static gdImagePtr lastbrush = (gdImagePtr)((void *) -1)
static gdImagePtr gcode_im = NULL
static FILE * gcode_f = NULL
static int is_mask
static int is_drill
static int is_bottom
static int gcode_export_group [MAX_GROUP]
static int gcode_cur_group
static const char * gcode_basename = NULL
static int gcode_dpi = -1
static double gcode_cutdepth = 0
static double gcode_isoplunge = 0
static double gcode_isofeedrate = 0
static char gcode_predrill
static double gcode_drilldepth = 0
static double gcode_drillfeedrate = 0
static double gcode_safeZ = 100
static int gcode_toolradius = 0
static char gcode_drillmill = 0
static double gcode_milldepth = 0
static double gcode_milltoolradius = 0
static double gcode_millplunge = 0
static double gcode_millfeedrate = 0
static char gcode_advanced = 0
static int save_drill = 0
static struct single_size_drillsdrills = NULL
static int n_drills = 0
static int n_drills_allocated = 0
HID_Attribute gcode_attribute_list []

Detailed Description

GCODE export HID.

This HID exports a PCB layout into:

  • one layer mask file (PNG format) per copper layer,
  • one G-CODE CNC drill file per drill size
  • one G-CODE CNC file per copper layer.

The latter is used by a CNC milling machine to mill the pcb.


Copyright.


PCB, interactive printed circuit board design

Copyright (c) 2010 Alberto Maccioni

Copyright (c) 2012 Markus Hitter (mah@jump-ing.de)

This code is based on the NELMA export HID, the PNG export HID, and potrace, a tracing program by Peter Selinger

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

Definition in file gcode.c.


Define Documentation

#define CRASH   fprintf(stderr, "HID error: pcb called unimplemented GCODE function %s.\n", __FUNCTION__); abort()

Definition at line 82 of file gcode.c.

Referenced by gcode_calibrate().


Function Documentation

static int _drill_size_comparator ( const void *  _size0,
const void *  _size1 
) [static]

Given a hole size, return the structure that currently holds the data for that hole size.

If there isn't one, make it.

Definition at line 1600 of file gcode.c.

Referenced by get_drill().

static void add_hole ( struct single_size_drills drill,
double  cx_inches,
double  cy_inches 
) [static]

Definition at line 1659 of file gcode.c.

References single_size_drills::holes, single_size_drills::n_holes, single_size_drills::n_holes_allocated, realloc(), drill_hole::x, and drill_hole::y.

Referenced by gcode_fill_circle().

Here is the call graph for this function:

static void gcode_alloc_colors ( ) [static]

Allocate white and black.

The first color allocated becomes the background color.

Definition at line 566 of file gcode.c.

References color_struct::b, color_struct::c, color_struct::g, malloc(), color_struct::r, and white.

Referenced by gcode_start_png().

Here is the call graph for this function:

static void gcode_calibrate ( double  xval,
double  yval 
) [static]

Definition at line 1728 of file gcode.c.

References CRASH.

Referenced by hid_gcode_init().

static void gcode_choose_groups ( ) [static]

Populates gcode_export_group array.

Definition at line 527 of file gcode.c.

References LayerType::ArcN, PCBType::Data, GetLayerGroupNumberByNumber(), DataType::Layer, layer, LayerType::LineN, m, max_copper_layer, n, PCB, LayerType::PolygonN, SL_TYPE, and LayerType::TextN.

Referenced by gcode_do_export().

Here is the call graph for this function:

static void gcode_destroy_gc ( hidGC  gc) [static]

Definition at line 1344 of file gcode.c.

Referenced by hid_gcode_init().

static void gcode_draw_arc ( hidGC  gc,
Coord  cx,
Coord  cy,
Coord  width,
Coord  height,
Angle  start_angle,
Angle  delta_angle 
) [static]

Definition at line 1547 of file gcode.c.

References color_struct::c, hid_gc_struct::color, im, linewidth, NormalizeAngle(), SCALE, SCALE_X, SCALE_Y, start_angle, and use_gc().

Referenced by hid_gcode_init().

Here is the call graph for this function:

static void gcode_draw_line ( hidGC  gc,
Coord  x1,
Coord  y1,
Coord  x2,
Coord  y2 
) [static]

Definition at line 1524 of file gcode.c.

References gcode_fill_rect(), linewidth, use_gc(), and hid_gc_struct::width.

Referenced by hid_gcode_init().

Here is the call graph for this function:

static void gcode_draw_rect ( hidGC  gc,
Coord  x1,
Coord  y1,
Coord  x2,
Coord  y2 
) [static]

Definition at line 1496 of file gcode.c.

References color_struct::c, hid_gc_struct::color, and use_gc().

Referenced by hid_gcode_init().

Here is the call graph for this function:

static void gcode_fill_circle ( hidGC  gc,
Coord  cx,
Coord  cy,
Coord  radius 
) [static]

Definition at line 1677 of file gcode.c.

References add_hole(), color_struct::c, hid_gc_struct::color, COORD_TO_INCH, get_drill(), linewidth, PCBType::MaxHeight, PCBType::MaxWidth, PCB, and use_gc().

Referenced by hid_gcode_init().

Here is the call graph for this function:

static void gcode_fill_polygon ( hidGC  gc,
int  n_coords,
Coord x,
Coord y 
) [static]

Definition at line 1703 of file gcode.c.

References color_struct::c, hid_gc_struct::color, linewidth, malloc(), and use_gc().

Referenced by hid_gcode_init().

Here is the call graph for this function:

static void gcode_fill_rect ( hidGC  gc,
Coord  x1,
Coord  y1,
Coord  x2,
Coord  y2 
) [static]

Definition at line 1509 of file gcode.c.

References color_struct::c, hid_gc_struct::color, linewidth, and use_gc().

Referenced by gcode_draw_line(), and hid_gcode_init().

Here is the call graph for this function:

static void gcode_finish_png ( const char *  layername) [static]

Definition at line 586 of file gcode.c.

References filename, gcode_get_filename(), malloc(), Message(), and white.

Referenced by gcode_do_export().

Here is the call graph for this function:

static HID_Attribute* gcode_get_export_options ( int *  n) [static]
static void gcode_get_filename ( char *  filename,
const char *  layername 
) [static]

Fits the given layer name into basename, just before the suffix.

Definition at line 410 of file gcode.c.

References gcode_basename.

Referenced by gcode_finish_png(), and gcode_start_gcode().

static hidGC gcode_make_gc ( void  ) [static]
static void gcode_parse_arguments ( int *  argc,
char ***  argv 
) [static]

Definition at line 487 of file gcode.c.

References hid_parse_command_line(), and hid_register_attributes().

Referenced by hid_gcode_init().

Here is the call graph for this function:

static void gcode_set_color ( hidGC  gc,
const char *  name 
) [static]

Definition at line 1356 of file gcode.c.

References black, hid_gc_struct::color, hid_gc_struct::erase, and white.

Referenced by hid_gcode_init().

static void gcode_set_crosshair ( int  x,
int  y,
int  a 
) [static]

Definition at line 1734 of file gcode.c.

Referenced by hid_gcode_init().

static void gcode_set_draw_faded ( hidGC  gc,
int  faded 
) [static]

Definition at line 1403 of file gcode.c.

Referenced by hid_gcode_init().

static void gcode_set_draw_xor ( hidGC  gc,
int  xor_ 
) [static]

Definition at line 1397 of file gcode.c.

Referenced by hid_gcode_init().

static int gcode_set_layer ( const char *  name,
int  group,
int  empty 
) [static]
static void gcode_set_line_cap ( hidGC  gc,
EndCapStyle  style 
) [static]

Definition at line 1385 of file gcode.c.

References hid_gc_struct::cap, and style.

Referenced by hid_gcode_init().

static void gcode_set_line_width ( hidGC  gc,
Coord  width 
) [static]

Definition at line 1391 of file gcode.c.

References hid_gc_struct::width.

Referenced by hid_gcode_init().

static FILE* gcode_start_gcode ( const char *  layername,
bool  metric 
) [static]

Definition at line 629 of file gcode.c.

References buffer, gcode_get_filename(), PCBType::MaxHeight, PCBType::MaxWidth, Message(), PCB, and pcb_fprintf().

Referenced by gcode_do_export().

Here is the call graph for this function:

static void gcode_start_png ( ) [static]

Definition at line 578 of file gcode.c.

References gcode_alloc_colors(), PCBType::MaxHeight, PCBType::MaxWidth, and PCB.

Referenced by gcode_do_export().

Here is the call graph for this function:

static void gcode_start_png_export ( ) [static]

Definition at line 613 of file gcode.c.

References hid_expose_callback(), lastbrush, linewidth, PCBType::MaxHeight, PCBType::MaxWidth, PCB, region, BoxType::X1, BoxType::X2, BoxType::Y1, and BoxType::Y2.

Referenced by gcode_do_export().

Here is the call graph for this function:

static void gcode_use_mask ( enum mask_mode  mode) [static]

Definition at line 1350 of file gcode.c.

Referenced by hid_gcode_init().

static struct single_size_drills* get_drill ( double  diameter_inches) [static, read]
REGISTER_ATTRIBUTES ( gcode_attribute_list  )

Convert from default PCB units to gcode units.

Definition at line 392 of file gcode.c.

References COORD_TO_INCH.

static void sort_drill ( struct drill_hole drill,
int  n_drill 
) [static]

Sorts drills to produce a short tool path.

I start with the hole nearest (0,0) and for each subsequent one, find the hole nearest to the previous.

This isn't guaranteed to find the shortest path, but should be good enough.

Note:
This is O(N^2). We can't use the O(N logN) sort, since our shortest-distance origin changes with every point.

Definition at line 446 of file gcode.c.

References x, drill_hole::x, y, and drill_hole::y.

Referenced by gcode_do_export().


Variable Documentation

struct color_struct* black = NULL [static]

Definition at line 106 of file gcode.c.

Referenced by gcode_set_color().

struct single_size_drills* drills = NULL [static]

At the start we have no drills at all

Definition at line 165 of file gcode.c.

char gcode_advanced = 0 [static]

Definition at line 141 of file gcode.c.

Definition at line 170 of file gcode.c.

Referenced by gcode_get_export_options().

const char* gcode_basename = NULL [static]

Filename prefix and suffix that will be used when saving files.

Definition at line 123 of file gcode.c.

Referenced by gcode_get_filename().

int gcode_cur_group [static]

Group that is currently exported.

Definition at line 121 of file gcode.c.

double gcode_cutdepth = 0 [static]

Milling depth (inch).

Definition at line 128 of file gcode.c.

int gcode_dpi = -1 [static]

Horizontal DPI (grid points per inch).

Definition at line 126 of file gcode.c.

Referenced by gcode_do_export().

double gcode_drilldepth = 0 [static]

Drilling depth (mm or in).

Definition at line 132 of file gcode.c.

double gcode_drillfeedrate = 0 [static]

Drilling feedrate.

Definition at line 133 of file gcode.c.

char gcode_drillmill = 0 [static]

Whether to drill with the mill tool.

Definition at line 136 of file gcode.c.

int gcode_export_group[MAX_GROUP] [static]

Which groups of layers to export into PNG layer masks.
1 means export, 0 means do not export.

Definition at line 118 of file gcode.c.

FILE* gcode_f = NULL [static]

Definition at line 111 of file gcode.c.

Definition at line 85 of file gcode.c.

Referenced by hid_gcode_init().

HID gcode_hid [static]

Definition at line 84 of file gcode.c.

Referenced by gcode_make_gc().

gdImagePtr gcode_im = NULL [static]

gd image and file for PNG export

Definition at line 110 of file gcode.c.

double gcode_isofeedrate = 0 [static]

Isolation milling feedrate.

Definition at line 130 of file gcode.c.

double gcode_isoplunge = 0 [static]

Isolation milling plunge feedrate.

Definition at line 129 of file gcode.c.

double gcode_milldepth = 0 [static]

Outline milling depth (mm or in).

Definition at line 137 of file gcode.c.

double gcode_millfeedrate = 0 [static]

Outline-milling feedrate.

Definition at line 140 of file gcode.c.

double gcode_millplunge = 0 [static]

Outline-milling plunge feedrate.

Definition at line 139 of file gcode.c.

double gcode_milltoolradius = 0 [static]

Outline-mill tool radius (mm or in).

Definition at line 138 of file gcode.c.

Referenced by gcode_do_export().

char gcode_predrill [static]

Definition at line 131 of file gcode.c.

double gcode_safeZ = 100 [static]

Safe Z (mm or in).

Definition at line 134 of file gcode.c.

int gcode_toolradius = 0 [static]

Iso-mill tool radius (1/100 mil).

Definition at line 135 of file gcode.c.

int is_bottom [static]

Definition at line 115 of file gcode.c.

Referenced by gcode_do_export().

int is_drill [static]

Definition at line 114 of file gcode.c.

Referenced by REGISTER_ATTRIBUTES().

int is_mask [static]

Definition at line 113 of file gcode.c.

Referenced by REGISTER_ATTRIBUTES().

gdImagePtr lastbrush = (gdImagePtr)((void *) -1) [static]

Definition at line 108 of file gcode.c.

Referenced by gcode_start_png_export(), and use_gc().

int n_drills = 0 [static]

Definition at line 167 of file gcode.c.

Referenced by gcode_do_export(), and get_drill().

int n_drills_allocated = 0 [static]

Definition at line 168 of file gcode.c.

int save_drill = 0 [static]

Definition at line 142 of file gcode.c.