View, delete, re-plot items
This commit is contained in:
63
camlib.py
63
camlib.py
@@ -1,4 +1,4 @@
|
||||
import cairo
|
||||
#import cairo
|
||||
|
||||
#from string import *
|
||||
#from math import *
|
||||
@@ -11,7 +11,7 @@ from numpy import arctan2, Inf, array, sqrt, pi, ceil, sin, cos
|
||||
from matplotlib.figure import Figure
|
||||
|
||||
# See: http://toblerity.org/shapely/manual.html
|
||||
from shapely.geometry import Polygon, LineString, Point
|
||||
from shapely.geometry import Polygon, LineString, Point, LinearRing
|
||||
from shapely.geometry import MultiPoint, MultiPolygon
|
||||
from shapely.geometry import box as shply_box
|
||||
from shapely.ops import cascaded_union
|
||||
@@ -41,7 +41,11 @@ class Geometry:
|
||||
if self.solid_geometry == None:
|
||||
print "Warning: solid_geometry not computed yet."
|
||||
return (0,0,0,0)
|
||||
return self.solid_geometry.bounds
|
||||
|
||||
if type(self.solid_geometry) == list:
|
||||
return cascaded_union(self.solid_geometry).bounds
|
||||
else:
|
||||
return self.solid_geometry.bounds
|
||||
|
||||
def size(self):
|
||||
'''
|
||||
@@ -62,7 +66,7 @@ class Geometry:
|
||||
'''
|
||||
if boundary == None:
|
||||
boundary = self.solid_geometry.envelope
|
||||
return boundary.difference(g.solid_geometry)
|
||||
return boundary.difference(self.solid_geometry)
|
||||
|
||||
def clear_polygon(self, polygon, tooldia, overlap = 0.15):
|
||||
'''
|
||||
@@ -340,9 +344,10 @@ class Excellon(Geometry):
|
||||
self.solid_geometry.append(poly)
|
||||
self.solid_geometry = cascaded_union(self.solid_geometry)
|
||||
|
||||
class CNCjob:
|
||||
class CNCjob(Geometry):
|
||||
def __init__(self, units="in", kind="generic", z_move = 0.1,
|
||||
feedrate = 3.0, z_cut = -0.002):
|
||||
|
||||
# Options
|
||||
self.kind = kind
|
||||
self.units = units
|
||||
@@ -366,7 +371,8 @@ class CNCjob:
|
||||
self.tooldia = 0
|
||||
|
||||
# Output generated by CNCjob.create_gcode_geometry()
|
||||
self.G_geometry = None
|
||||
#self.G_geometry = None
|
||||
self.gcode_parsed = None
|
||||
|
||||
def generate_from_excellon(self, exobj):
|
||||
'''
|
||||
@@ -447,7 +453,7 @@ class CNCjob:
|
||||
self.gcode += "G00 Z%.4f\n"%self.z_move # Stop cutting
|
||||
continue
|
||||
|
||||
if type(geo) == LineString or type(geo) == LineRing:
|
||||
if type(geo) == LineString or type(geo) == LinearRing:
|
||||
path = list(geo.coords)
|
||||
self.gcode += t%(0, path[0][0], path[0][1]) # Move to first point
|
||||
self.gcode += "G01 Z%.4f\n"%self.z_cut # Start cutting
|
||||
@@ -469,7 +475,7 @@ class CNCjob:
|
||||
self.gcode += "G00 X0Y0\n"
|
||||
self.gcode += "M05\n" # Spindle stop
|
||||
|
||||
def create_gcode_geometry(self):
|
||||
def gcode_parse(self):
|
||||
steps_per_circ = 20
|
||||
'''
|
||||
G-Code parser (from self.gcode). Generates dictionary with
|
||||
@@ -534,7 +540,8 @@ class CNCjob:
|
||||
for code in gobj:
|
||||
current[code] = gobj[code]
|
||||
|
||||
self.G_geometry = geometry
|
||||
#self.G_geometry = geometry
|
||||
self.gcode_parsed = geometry
|
||||
return geometry
|
||||
|
||||
def plot(self, tooldia=None, dpi=75, margin=0.1,
|
||||
@@ -555,7 +562,7 @@ class CNCjob:
|
||||
ax.set_ylim(ymin-margin, ymax+margin)
|
||||
|
||||
if tooldia == 0:
|
||||
for geo in self.G_geometry:
|
||||
for geo in self.gcode_parsed:
|
||||
linespec = '--'
|
||||
linecolor = color[geo['kind'][0]][1]
|
||||
if geo['kind'][0] == 'C':
|
||||
@@ -563,7 +570,7 @@ class CNCjob:
|
||||
x, y = geo['geom'].coords.xy
|
||||
ax.plot(x, y, linespec, color=linecolor)
|
||||
else:
|
||||
for geo in self.G_geometry:
|
||||
for geo in self.gcode_parsed:
|
||||
poly = geo['geom'].buffer(tooldia/2.0)
|
||||
patch = PolygonPatch(poly, facecolor=color[geo['kind'][0]][0],
|
||||
edgecolor=color[geo['kind'][0]][1],
|
||||
@@ -576,20 +583,13 @@ class CNCjob:
|
||||
color={"T":["#F0E24D", "#B5AB3A"], "C":["#5E6CFF", "#4650BD"]},
|
||||
alpha={"T":0.3, "C":1.0}):
|
||||
'''
|
||||
Plots the G-code job onto the given axes
|
||||
Plots the G-code job onto the given axes.
|
||||
'''
|
||||
if tooldia == None:
|
||||
tooldia = self.tooldia
|
||||
|
||||
#fig = Figure(dpi=dpi)
|
||||
#ax = fig.add_subplot(111)
|
||||
#ax.set_aspect(1)
|
||||
#xmin, ymin, xmax, ymax = self.input_geometry_bounds
|
||||
#ax.set_xlim(xmin-margin, xmax+margin)
|
||||
#ax.set_ylim(ymin-margin, ymax+margin)
|
||||
|
||||
if tooldia == 0:
|
||||
for geo in self.G_geometry:
|
||||
for geo in self.gcode_parsed:
|
||||
linespec = '--'
|
||||
linecolor = color[geo['kind'][0]][1]
|
||||
if geo['kind'][0] == 'C':
|
||||
@@ -597,13 +597,17 @@ class CNCjob:
|
||||
x, y = geo['geom'].coords.xy
|
||||
axes.plot(x, y, linespec, color=linecolor)
|
||||
else:
|
||||
for geo in self.G_geometry:
|
||||
for geo in self.gcode_parsed:
|
||||
poly = geo['geom'].buffer(tooldia/2.0)
|
||||
patch = PolygonPatch(poly, facecolor=color[geo['kind'][0]][0],
|
||||
edgecolor=color[geo['kind'][0]][1],
|
||||
alpha=alpha[geo['kind'][0]], zorder=2)
|
||||
axes.add_patch(patch)
|
||||
|
||||
def create_geometry(self):
|
||||
self.solid_geometry = cascaded_union([geo['geom'] for geo in self.gcode_parsed])
|
||||
|
||||
|
||||
|
||||
def gparse1b(gtext):
|
||||
'''
|
||||
@@ -650,21 +654,18 @@ def gparse1b(gtext):
|
||||
gcmds.append(cmds)
|
||||
return gcmds
|
||||
|
||||
def get_bounds(geometry_sets):
|
||||
def get_bounds(geometry_set):
|
||||
xmin = Inf
|
||||
ymin = Inf
|
||||
xmax = -Inf
|
||||
ymax = -Inf
|
||||
|
||||
#geometry_sets = [self.gerbers, self.excellons]
|
||||
|
||||
for gs in geometry_sets:
|
||||
for g in gs:
|
||||
gxmin, gymin, gxmax, gymax = g.solid_geometry.bounds
|
||||
xmin = min([xmin, gxmin])
|
||||
ymin = min([ymin, gymin])
|
||||
xmax = max([xmax, gxmax])
|
||||
ymax = max([ymax, gymax])
|
||||
for gs in geometry_set:
|
||||
gxmin, gymin, gxmax, gymax = geometry_set[gs].bounds()
|
||||
xmin = min([xmin, gxmin])
|
||||
ymin = min([ymin, gymin])
|
||||
xmax = max([xmax, gxmax])
|
||||
ymax = max([ymax, gymax])
|
||||
|
||||
return [xmin, ymin, xmax, ymax]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user