Camlib

Geometry

class camlib.Geometry
bounds()

Returns coordinates of rectangular bounds of geometry: (xmin, ymin, xmax, ymax).

clear_polygon(polygon, tooldia, overlap=0.15)

Creates geometry inside a polygon for a tool to cover the whole area.

convert_units(units)

Converts the units of the object to units by scaling all the geometry appropriately. This call scale(). Don’t call it again in descendents.

Parameters:units (str) – “IN” or “MM”
Returns:Scaling factor resulting from unit change.
Return type:float
from_dict(d)

Sets object’s attributes from a dictionary. Attributes to include are listed in self.ser_attrs. This method will look only for only and all the attributes in self.ser_attrs. They must all be present. Use only for deserializing saved objects.

Parameters:d (dict) – Dictionary of attributes to set in the object.
Returns:None
get_empty_area(boundary=None)

Returns the complement of self.solid_geometry within the given boundary polygon. If not specified, it defaults to the rectangular bounding box of self.solid_geometry.

isolation_geometry(offset)

Creates contours around geometry at a given offset distance.

Parameters:offset (float) – Offset distance.
Returns:The buffered geometry.
Return type:Shapely.MultiPolygon or Shapely.Polygon
offset(vect)

Offset the geometry by the given vector. Override this method.

Parameters:vect (tuple) – (x, y) vector by which to offset the object.
Returns:None
scale(factor)

Scales all of the object’s geometry by a given factor. Override this method. :param factor: Number by which to scale. :type factor: float :return: None :rtype: None

size()

Returns (width, height) of rectangular bounds of geometry.

to_dict()

Returns a respresentation of the object as a dictionary. Attributes to include are listed in self.ser_attrs.

Returns:A dictionary-encoded copy of the object.
Return type:dict

Gerber

class camlib.Gerber(Geometry)

ATTRIBUTES

  • apertures (dict): The keys are names/identifiers of each aperture. The values are dictionaries key/value pairs which describe the aperture. The type key is always present and the rest depend on the key:
Key Value
type (str) “C”, “R”, “O”, “P”, or “AP”
others Depend on type
  • paths (list): A path is described by a line an aperture that follows that line. Each paths[i] is a dictionary:
Key Value
linestring (Shapely.LineString) The actual path.
aperture (str) The key for an aperture in apertures.
  • flashes (list): Flashes are single-point strokes of an aperture. Each is a dictionary:
Key Value
loc (Point) Shapely Point indicating location.
aperture (str) The key for an aperture in apertures.
  • regions (list): Are surfaces defined by a polygon (Shapely.Polygon), which have an exterior and zero or more interiors. An aperture is also associated with a region. Each is a dictionary:
Key Value
polygon (Shapely.Polygon) The polygon defining the region.
aperture (str) The key for an aperture in apertures.
  • aperture_macros (dictionary): Are predefined geometrical structures that can be instanciated with different parameters in an aperture definition. See apertures above. The key is the name of the macro, and the macro itself, the value, is a Aperture_Macro object.
  • flash_geometry (list): List of (Shapely) geometric object resulting from flashes. These are generated from flashes in do_flashes().
  • buffered_paths (list): List of (Shapely) polygons resulting from buffering (or thickening) the paths with the aperture. These are generated from paths in buffer_paths().

USAGE:

g = Gerber()
g.parse_file(filename)
g.create_geometry()
do_something(s.solid_geometry)
aperture_parse(apertureId, apertureType, apParameters)

Parse gerber aperture definition into dictionary of apertures. The following kinds and their attributes are supported:

  • Circular (C): size (float)
  • Rectangle (R): width (float), height (float)
  • Obround (O): width (float), height (float).
  • Polygon (P): diameter(float), vertices(int), [rotation(float)]
  • Aperture Macro (AM): macro (ApertureMacro), modifiers (list)
Parameters:
  • apertureId (str) – Id of the aperture being defined.
  • apertureType (str) – Type of the aperture.
  • apParameters (str) – Parameters of the aperture.
Returns:

Identifier of the aperture.

Return type:

str

buffer_paths()

This is part of the parsing process. “Thickens” the paths by their appertures. This will only work for circular appertures.

Returns:None
create_geometry()

Geometry from a Gerber file is made up entirely of polygons. Every stroke (linear or circular) has an aperture which gives it thickness. Additionally, aperture strokes have non-zero area, and regions naturally do as well.

:rtype : None :return: None

do_flashes()

Creates geometry for Gerber flashes (aperture on a single point).

fix_regions()

Overwrites the region polygons with fixed versions if found to be invalid (according to Shapely).

Returns:None
frac_digits = None

Number of fraction digits in Gerber numbers. Used during parsing.

get_bounding_box(margin=0.0, rounded=False)

Creates and returns a rectangular polygon bounding at a distance of margin from the object’s solid_geometry. If margin > 0, the polygon can optionally have rounded corners of radius equal to margin.

Parameters:
  • margin (float) – Distance to enlarge the rectangular bounding box in both positive and negative, x and y axes.
  • rounded (bool) – Wether or not to have rounded corners.
Returns:

The bounding box.

Return type:

Shapely.Polygon

int_digits = None

Number of integer digits in Gerber numbers. Used during parsing.

mirror(axis, point)

Mirrors the object around a specified axis passign through the given point. What is affected:

  • buffered_paths
  • flash_geometry
  • solid_geometry
  • regions

NOTE: Does not modify the data used to create these elements. If these are recreated, the scaling will be lost. This behavior was modified because of the complexity reached in this class.

Parameters:
  • axis (str) – “X” or “Y” indicates around which axis to mirror.
  • point (list) – [x, y] point belonging to the mirror axis.
Returns:

None

offset(vect)

Offsets the objects’ geometry on the XY plane by a given vector. These are:

  • buffered_paths
  • flash_geometry
  • solid_geometry
  • regions

NOTE: Does not modify the data used to create these elements. If these are recreated, the scaling will be lost. This behavior was modified because of the complexity reached in this class.

Parameters:vect (tuple) – (x, y) offset vector.
Returns:None
parse_file(filename)

Calls Gerber.parse_lines() with array of lines read from the given file.

Parameters:filename (str) – Gerber file to parse.
Returns:None
parse_lines(glines)

Main Gerber parser. Reads Gerber and populates self.paths, self.apertures, self.flashes, self.regions and self.units.

Parameters:glines (list) – Gerber code as list of strings, each element being one line of the source file.
Returns:None
Return type:None
scale(factor)

Scales the objects’ geometry on the XY plane by a given factor. These are:

  • buffered_paths
  • flash_geometry
  • solid_geometry
  • regions

NOTE: Does not modify the data used to create these elements. If these are recreated, the scaling will be lost. This behavior was modified because of the complexity reached in this class.

Parameters:factor (float) – Number by which to scale.

:rtype : None

ApertureMacro

class camlib.ApertureMacro(name=None)
append(data)

Appends a string to the raw macro.

Parameters:data (str) – Part of the macro.
Returns:None
static default2zero(n, mods)

Pads the mods list with zeros resulting in an list of length n.

Parameters:
  • n (int) – Length of the resulting list.
  • mods (list) – List to be padded.
Returns:

Zero-padded list.

Return type:

list

from_dict(d)

Populates the object from a serial representation created with self.to_dict().

Parameters:d – Serial representation of an ApertureMacro object.
Returns:None
static make_centerline(mods)
Parameters:mods – (Exposure 0/1, width >=0, height >=0, x-center, y-center, rotation angle around origin in degrees)
Returns:
static make_circle(mods)
Parameters:mods – (Exposure 0/1, Diameter >=0, X-coord, Y-coord)
Returns:
make_geometry(modifiers)

Runs the macro for the given modifiers and generates the corresponding geometry.

Parameters:modifiers (list) – Modifiers (parameters) for this macro
static make_lowerleftline(mods)
Parameters:mods – (exposure 0/1, width >=0, height >=0, x-lowerleft, y-lowerleft, rotation angle around origin in degrees)
Returns:
static make_moire(mods)

Note: Specs indicate that rotation is only allowed if the center (x, y) == (0, 0). I will tolerate breaking this rule.

Parameters:mods – (x-center, y-center, outer_dia_outer_ring, ring thickness, gap, max_rings, crosshair_thickness, crosshair_len, rotation angle around origin in degrees)
Returns:
static make_outline(mods)
Parameters:mods
Returns:
static make_polygon(mods)

Note: Specs indicate that rotation is only allowed if the center (x, y) == (0, 0). I will tolerate breaking this rule.

Parameters:mods – (exposure 0/1, n_verts 3<=n<=12, x-center, y-center, diameter of circumscribed circle >=0, rotation angle around origin)
Returns:
static make_thermal(mods)

Note: Specs indicate that rotation is only allowed if the center (x, y) == (0, 0). I will tolerate breaking this rule.

Parameters:mods – [x-center, y-center, diameter-outside, diameter-inside, gap-thickness, rotation angle around origin]
Returns:
static make_vectorline(mods)
Parameters:mods – (Exposure 0/1, Line width >= 0, X-start, Y-start, X-end, Y-end, rotation angle around origin in degrees)
Returns:
parse_content()

Creates numerical lists for all primitives in the aperture macro (in self.raw) by replacing all variables by their values iteratively and evaluating expressions. Results are stored in self.primitives.

Returns:None
to_dict()

Returns the object in a serializable form. Only the name and raw are required.

Returns:Dictionary representing the object. JSON ready.
Return type:dict

Excellon

class camlib.Excellon(Geometry)

ATTRIBUTES

  • tools (dict): The key is the tool name and the value is a dictionary specifying the tool:
Key Value
C Diameter of the tool
Others Not supported (Ignored).
  • drills (list): Each is a dictionary:
Key Value
point (Shapely.Point) Where to drill
tool (str) A key in tools
create_geometry()

Creates circles of the tool diameter at every point specified in self.drills.

Returns:None
mirror(axis, point)
Parameters:
  • axis (str) – “X” or “Y” indicates around which axis to mirror.
  • point (list) – [x, y] point belonging to the mirror axis.
Returns:

None

offset(vect)

Offsets geometry on the XY plane in the object by a given vector.

Parameters:vect (tuple) – (x, y) offset vector.
Returns:None
parse_file(filename)

Reads the specified file as array of lines as passes it to parse_lines().

Parameters:filename (str) – The file to be read and parsed.
Returns:None
parse_lines(elines)

Main Excellon parser.

Parameters:elines (list) – List of strings, each being a line of Excellon code.
Returns:None
scale(factor)

Scales geometry on the XY plane in the object by a given factor. Tool sizes, feedrates an Z-plane dimensions are untouched.

Parameters:factor (float) – Number by which to scale the object.
Returns:None
Return type:NOne

CNCJob

class camlib.CNCjob(Geometry)

Represents work to be done by a CNC machine.

ATTRIBUTES

  • gcode_parsed (list): Each is a dictionary:
Key Value
geom (Shapely.LineString) Tool path (XY plane)
kind (string) “AB”, A is “T” (travel) or “C” (cut). B is “F” (fast) or “S” (slow).
gcode_parse()

G-Code parser (from self.gcode). Generates dictionary with single-segment LineString’s and “kind” indicating cut or travel, fast or feedrate speed.

generate_from_excellon(exobj)

Generates G-code for drilling from Excellon object. self.gcode becomes a list, each element is a different job for each tool in the excellon code.

generate_from_excellon_by_tool(exobj, tools='all')

Creates gcode for this object from an Excellon object for the specified tools.

Parameters:
  • exobj (Excellon) – Excellon object to process
  • tools – Comma separated tool names
Type:

tools: str

Returns:

None

Return type:

None

generate_from_geometry(geometry, append=True, tooldia=None, tolerance=0)

Generates G-Code from a Geometry object. Stores in self.gcode.

Parameters:
  • geometry (Geometry) – Geometry defining the toolpath
  • append (bool) – Wether to append to self.gcode or re-write it.
  • tooldia (bool) – If given, sets the tooldia property but does not affect the process in any other way.
  • tolerance – All points in the simplified object will be within the tolerance distance of the original geometry.
Returns:

None

Return type:

None

linear2gcode(linear, tolerance=0)

Generates G-code to cut along the linear feature.

Parameters:
  • linear – The path to cut along.
  • tolerance (float) – All points in the simplified object will be within the tolerance distance of the original geometry.
Type:

Shapely.LinearRing or Shapely.Linear String

Returns:

G-code to cut alon the linear feature.

Return type:

str

offset(vect)

Offsets all the geometry on the XY plane in the object by the given vector.

Parameters:vect (tuple) – (x, y) offset vector.
Returns:None
plot2(axes, tooldia=None, dpi=75, margin=0.1, color={'C': ['#5E6CFF', '#4650BD'], 'T': ['#F0E24D', '#B5AB3A']}, alpha={'C': 1.0, 'T': 0.3}, tool_tolerance=0.0005)

Plots the G-code job onto the given axes.

Parameters:
  • axes – Matplotlib axes on which to plot.
  • tooldia – Tool diameter.
  • dpi – Not used!
  • margin – Not used!
  • color – Color specification.
  • alpha – Transparency specification.
  • tool_tolerance – Tolerance when drawing the toolshape.
Returns:

None

polygon2gcode(polygon, tolerance=0)

Creates G-Code for the exterior and all interior paths of a polygon.

Parameters:
  • polygon (Shapely.Polygon) – A Shapely.Polygon
  • tolerance (float) – All points in the simplified object will be within the tolerance distance of the original geometry.
Returns:

G-code to cut along polygon.

Return type:

str

pre_parse(gtext)

Separates parts of the G-Code text into a list of dictionaries. Used by self.gcode_parse().

Parameters:gtext – A single string with g-code
scale(factor)

Scales all the geometry on the XY plane in the object by the given factor. Tool sizes, feedrates, or Z-axis dimensions are not altered.

Parameters:factor (float) – Number by which to scale the object.
Returns:None
Return type:None