Skip to content

Commit

Permalink
Added lasercutter support
Browse files Browse the repository at this point in the history
  • Loading branch information
vlachoudis committed Aug 26, 2015
1 parent 9998a39 commit 9295695
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 30 deletions.
78 changes: 58 additions & 20 deletions CNC.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def makeMatrix(self):
self.matrix.append([0.0]*(self.xn))

#----------------------------------------------------------------------
# Load level information from file
# Load autolevel information from file
#----------------------------------------------------------------------
def load(self, filename=None):
if filename is not None:
Expand Down Expand Up @@ -334,6 +334,7 @@ def splitLine(self, x1, y1, z1, x2, y2, z2):
#==============================================================================
class CNC:
inch = False
lasercutter = False
acceleration_x = 25.0 # mm/s^2
acceleration_y = 25.0 # mm/s^2
acceleration_z = 25.0 # mm/s^2
Expand Down Expand Up @@ -369,6 +370,7 @@ def __init__(self):
def loadConfig(config):
section = "CNC"
CNC.inch = bool(int(config.get(section, "units")))
CNC.lasercutter = bool(int(config.get(section, "lasercutter")))
CNC.acceleration_x = float(config.get(section, "acceleration_x"))
CNC.acceleration_y = float(config.get(section, "acceleration_y"))
CNC.acceleration_z = float(config.get(section, "acceleration_z"))
Expand Down Expand Up @@ -911,15 +913,27 @@ def pathMargins(self, xyz):
#==============================================================================
class Block(list):
def __init__(self, name=None):
# Copy constructor
if isinstance(name, Block):
self.copy(name)
return
self._name = name
self.enable = True # Enabled/Visible in drawing
self.expand = False # Expand in editor
# self.start = None # starting coordinates
# self.stop = None # exit coordinates

self._path = [] # canvas drawing paths
self.x = self.y = self.z = 0 # ending coordinates

#----------------------------------------------------------------------
def copy(self, src):
self._name = src._name
self.enable = src.enable
self.expand = src.expand
self[:] = src[:]
self._path = []
self.x = src.x
self.y = src.y
self.z = src.z

#----------------------------------------------------------------------
def name(self):
return self._name is None and "block" or self._name
Expand Down Expand Up @@ -1111,6 +1125,25 @@ def headerFooter(self):
self.addBlockFromString("Header",self.header)
self.addBlockFromString("Footer",self.footer)

#----------------------------------------------------------------------
# Enter to material or start the laser
#----------------------------------------------------------------------
def zenter(self, z):
if CNC.lasercutter:
return "m3"
else:
return "g1 %s %s"%(self.fmt("z",z), self.fmt("f",self.feedz))

#----------------------------------------------------------------------
# gcode to go to z-safe
# Exit from material or stop the laser
#----------------------------------------------------------------------
def zsafe(self):
if CNC.lasercutter:
return "m5"
else:
return "g0 %s"%(self.fmt("z",self.safe))

#----------------------------------------------------------------------
# Load DXF file into gcode
#----------------------------------------------------------------------
Expand All @@ -1119,8 +1152,7 @@ def importDXF(self, filename):
dxf = DXF(filename,"r")
except:
return False
name,ext = os.path.splitext(filename)
self.filename = "%s.ngc"%(name)
self.filename = ""

dxf.readFile()
dxf.close()
Expand Down Expand Up @@ -1224,9 +1256,8 @@ def importEntityPoints(self, pos, entities, name, enable=True):

x,y = entities[i].start()
block.append("g0 %s %s"%(self.fmt("x",x,7),self.fmt("y",y,7)))
block.append("g1 %s %s"%(self.fmt("z",self.surface),
self.fmt("f",self.feedz)))
block.append("g0 %s"%(self.fmt("z",self.safe)))
block.append(self.zenter(self.surface))
block.append(self.zsafe())
undoinfo.append(self.addBlockUndo(pos,block))
if pos is not None: pos += 1

Expand Down Expand Up @@ -1286,8 +1317,7 @@ def fromPath(self, path, block=None, z=None, entry=True, exit=True):
if z is None: z = self.surface
if entry:
block.append("g0 %s %s"%(self.fmt("x",x,7),self.fmt("y",y,7)))
block.append("g1 %s %s"%(self.fmt("z",z), self.fmt("f",self.feedz)))

block.append(self.zenter(z))
first = True
for segment in path:
x,y = segment.end
Expand All @@ -1306,7 +1336,7 @@ def fromPath(self, path, block=None, z=None, entry=True, exit=True):
block[-1] += " %s"%(self.fmt("f",self.feed))
first = False
if exit:
block.append("g0 %s"%(self.fmt("z",self.safe)))
block.append(self.zsafe())
else:
for p in path:
self.fromPath(p, block)
Expand Down Expand Up @@ -1436,6 +1466,12 @@ def insLineUndo(self, bid, lid, line):
block.insert(lid, line)
return undoinfo

#----------------------------------------------------------------------
# Clone line inside a block
#----------------------------------------------------------------------
def cloneLineUndo(self, bid, lid):
return self.insLineUndo(bid, lid, self.blocks[bid][lid])

#----------------------------------------------------------------------
# Delete line from block
#----------------------------------------------------------------------
Expand All @@ -1457,6 +1493,12 @@ def addBlockUndo(self, bid, block):
self.blocks.insert(bid, block)
return undoinfo

#----------------------------------------------------------------------
# Clone a block
#----------------------------------------------------------------------
def cloneBlockUndo(self, bid):
return self.addBlockUndo(bid, Block(self.blocks[bid]))

#----------------------------------------------------------------------
# Delete a whole block
#----------------------------------------------------------------------
Expand Down Expand Up @@ -1799,18 +1841,14 @@ def drill(self, items, depth=None, peck=None, dwell=None):
if self.cnc.dz<0.0:
# drill point
if peck is None:
lines.append("g1 %s %s"%(
self.fmt("z",depth),
self.fmt("f",self.feedz)))
lines.append("g0 %s"%(self.fmt("z",self.safe)))
lines.append(self.zenter(depth))
lines.append(self.zsafe())
else:
z = self.surface
while z>depth:
z = max(z-peck, depth)
lines.append("g1 %s %s"%(
self.fmt("z",z),
self.fmt("f",self.feedz)))
lines.append("g0 %s"%(self.fmt("z",self.safe)))
lines.append(self.zenter(z))
lines.append(self.zsafe())
if dwell:
lines.append("g4 %s"%(self.fmt("p",dwell)))

Expand Down
21 changes: 21 additions & 0 deletions CNCList.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,27 @@ def deleteLine(self, event=None):
self.see(ACTIVE)
self.event_generate("<<Modified>>")

# ----------------------------------------------------------------------
def clone(self, event=None):
sel = list(map(int,self.curselection()))
if not sel: return

ypos = self.yview()[0]
undoinfo = []
for i in reversed(sel):
bid, lid = self._items[i]
if lid is None:
undoinfo.append(self.gcode.cloneBlockUndo(bid))
else:
undoinfo.append(self.gcode.cloneLineUndo(bid, lid))
self.gcode.addUndo(undoinfo)
self.selection_clear(0,END)
self.fill()
self.yview_moveto(ypos)
self.selection_set(ACTIVE)
self.see(ACTIVE)
self.event_generate("<<Modified>>")

# ----------------------------------------------------------------------
# Button1 clicked
# ----------------------------------------------------------------------
Expand Down
1 change: 0 additions & 1 deletion EditorPage.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def __init__(self, master, app):
text="Clone",
compound=LEFT,
anchor=W,
state=DISABLED,
background=Ribbon._BACKGROUND)
b.grid(row=row, column=col, padx=0, pady=0, sticky=NSEW)
tkExtra.Balloon.set(b, "Clone selected lines or blocks [Ctrl-D]")
Expand Down
10 changes: 6 additions & 4 deletions Sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,11 @@ def _loadRecent8(self,event): self.loadRecent(8)
def _loadRecent9(self,event): self.loadRecent(9)

#----------------------------------------------------------------------
def _saveConfigFile(self):
Utils.setStr("File", "dir", os.path.dirname(os.path.abspath(self.gcode.filename)))
Utils.setStr("File", "file", os.path.basename(self.gcode.filename))
def _saveConfigFile(self, filename=None):
if filename is None:
filename = self.gcode.filename
Utils.setStr("File", "dir", os.path.dirname(os.path.abspath(filename)))
Utils.setStr("File", "file", os.path.basename(filename))
Utils.setStr("File", "probe", os.path.basename(self.gcode.probe.filename))

#----------------------------------------------------------------------
Expand All @@ -246,7 +248,7 @@ def load(self, filename):
elif ext==".dxf":
self.gcode.init()
self.gcode.importDXF(filename)
self._saveConfigFile()
self._saveConfigFile(filename)
else:
self.gcode.load(filename)
self._saveConfigFile()
Expand Down
1 change: 1 addition & 0 deletions ToolsPage.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ def __init__(self, master):
self.name = "CNC"
self.variables = [
("units" , "bool", 0 , "Units (inches)") ,
("lasercutter" , "bool", 0 , "Lasercutter") ,
("acceleration_x", "mm" , 25.0 , "Acceleration x") ,
("acceleration_y", "mm" , 25.0 , "Acceleration y") ,
("acceleration_z", "mm" , 5.0 , "Acceleration z") ,
Expand Down
1 change: 1 addition & 0 deletions Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ def addRecent(filename):
except UnicodeEncodeError:
sfn = filename.encode("utf-8")

last = _maxRecent-1
for i in range(_maxRecent):
rfn = getRecent(i)
if rfn is None:
Expand Down
4 changes: 3 additions & 1 deletion bCNC.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[bCNC]
tool = Box
tool = CNC
page = File

ribbon = File Control Probe Tools Editor Terminal>

Expand Down Expand Up @@ -76,6 +77,7 @@ command.0 = G90G0X0Y0Z0

[CNC]
units = 0
lasercutter = 0
acceleration_x = 25
acceleration_y = 25
acceleration_z = 50
Expand Down
8 changes: 5 additions & 3 deletions bCNC.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,11 @@ def __init__(self, master, **kw):
side = LEFT
self.ribbon.addPage(pages[name],side)

self.ribbon.changePage("File")
# Restore last page
self.ribbon.changePage(Utils.getStr(Utils.__prg__,"page", "File"))

self.bind("<<Add>>", self.editor.insertLine)
# self.bind("<<Clone>>", self.editor.insertLine)
self.bind("<<Add>>", self.editor.insertItem)
self.bind("<<Clone>>", self.editor.clone)
self.bind("<<Delete>>", self.editor.deleteLine)

# Canvas X-bindings
Expand Down Expand Up @@ -440,6 +441,7 @@ def saveConfig(self):

#save windowState
Utils.config.set(Utils.__prg__, "windowstate", str(self.wm_state()))
Utils.config.set(Utils.__prg__, "page", str(self.ribbon.getActivePage().name))
# Utils.config.set(Utils.__prg__, "tool", self.toolFrame.get())

# Connection
Expand Down
6 changes: 5 additions & 1 deletion lib/dxf.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,11 @@ def read(self):

line = self._f.readline()
if not line: return None, None
tag = int(line.strip())
try:
tag = int(line.strip())
except:
sys.stderr.write("Error reading line %s\n"%(line))
return None,None
value = self._f.readline().strip()
try:
value = int(value)
Expand Down

0 comments on commit 9295695

Please sign in to comment.