Skip to content

Commit

Permalink
Very-very basic editing of tabs
Browse files Browse the repository at this point in the history
- Shortcut 't' in canvas will add a tab 
- Tools->Tabs will allow to modify a tab values
- Any subsequent Cut operation will use the tabs
  • Loading branch information
vlachoudis committed Nov 13, 2015
1 parent 7d54bdf commit a9aaa79
Show file tree
Hide file tree
Showing 4 changed files with 315 additions and 115 deletions.
42 changes: 41 additions & 1 deletion CNC.py
Original file line number Diff line number Diff line change
Expand Up @@ -1534,7 +1534,7 @@ def macroGroupG8X(self):
# a class holding tab information and necessary functions to break a segment
#==============================================================================
class Tab:
def __init__(self, xmin, xmax, ymin, ymax, z):
def __init__(self, xmin, ymin, xmax, ymax, z):
self.xmin = xmin # x,y limits of a square tab
self.xmax = xmax
self.ymin = ymin
Expand All @@ -1543,6 +1543,23 @@ def __init__(self, xmin, xmax, ymin, ymax, z):
# self.slope = 45 # cutting z-slope as entry/exit
# self.create()

#----------------------------------------------------------------------
def __str__(self):
return "Tab([%g, %g] .. [%g, %g] z=%g)" % \
(self.xmin, self.ymin,
self.xmax, self.ymax,
self.z)

#----------------------------------------------------------------------
# Correct tab for min/max
#----------------------------------------------------------------------
def correct(self):
if self.xmin > self.xmax:
self.xmin, self.xmax = self.xmax, self.xmin

if self.ymin > self.ymax:
self.ymin, self.ymax = self.ymax, self.ymin

#----------------------------------------------------------------------
# Create 4 line segment of the tab
#----------------------------------------------------------------------
Expand Down Expand Up @@ -1610,6 +1627,7 @@ def split(self, path):
if s._inside is None and self.inside(s.midPoint()):
s._inside = self


#==============================================================================
# Block of g-code commands. A gcode file is represented as a list of blocks
# - Commands are grouped as (non motion commands Mxxx)
Expand Down Expand Up @@ -1872,6 +1890,7 @@ def _addLine(self, line):
if line.startswith("(Tab:"):
items = map(float,line.replace("(Tab:","").replace(")","").split())
self.tabs.append(Tab(*items))
return

if line.startswith("(Block-name:"):
self._blocksExist = True
Expand Down Expand Up @@ -1935,6 +1954,10 @@ def save(self, filename=None):
f = open(self.filename,"w")
except:
return False

# write tabs if any
for tab in self.tabs:
f.write("(Tab:%g %g %g %g %g)\n"%(tab.xmin, tab.ymin, tab.xmax, tab.ymax, tab.z))
for block in self.blocks:
block.write(f)
f.close()
Expand Down Expand Up @@ -2241,6 +2264,23 @@ def _trim(self):
if len(self.blocks[-1])==0:
self.blocks.pop()

#----------------------------------------------------------------------
# Append a new tab
#----------------------------------------------------------------------
def addTabUndo(self, pos, tab):
undoinfo = (self.delTabUndo, len(self.tabs))
if pos<0 or pos>=len(self.tabs):
self.tabs.append(tab)
else:
self.tabs.insert(pos, tab)
return undoinfo

#----------------------------------------------------------------------
def delTabUndo(self, pos):
undoinfo = (self.addTabUndo, pos, self.tabs[pos])
del self.tabs[pos]
return undoinfo

#----------------------------------------------------------------------
# Change all lines in editor
#----------------------------------------------------------------------
Expand Down
107 changes: 79 additions & 28 deletions CNCCanvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import bmath
try:
from Tkinter import *
import Tkinter
except ImportError:
from tkinter import *
import tkinter as Tkinter

from CNC import CNC
from CNC import Tab, CNC
import tkExtra
import Utils

Expand Down Expand Up @@ -49,6 +51,7 @@
GRID_COLOR = "Gray"
BOX_SELECT = "Cyan"
TAB_COLOR = "DarkOrange"
WORK_COLOR = "Orange"

ENABLE_COLOR = "Black"
DISABLE_COLOR = "LightGray"
Expand All @@ -73,7 +76,7 @@

ACTION_RULER = 30

ACTION_TAB = 40
ACTION_ADDTAB = 40

SHIFT_MASK = 1
CONTROL_MASK = 4
Expand Down Expand Up @@ -102,7 +105,7 @@
# ACTION_VIEW_MOVE : "fleur",
# ACTION_VIEW_ROTATE : "exchange",

ACTION_TAB : "tcross",
ACTION_ADDTAB : "tcross",

ACTION_MOVE : "hand1",
ACTION_ROTATE : "exchange",
Expand Down Expand Up @@ -162,6 +165,7 @@ def __init__(self, master, app, *kw, **kwargs):
self.bind('<Key-o>', self.setActionOrigin)
self.bind('<Key-r>', self.setActionRuler)
self.bind('<Key-s>', self.setActionSelect)
self.bind('<Key-t>', self.setActionAddTab)

self.bind('<Control-Key-equal>',self.menuZoomIn)
self.bind('<Control-Key-minus>',self.menuZoomOut)
Expand Down Expand Up @@ -197,6 +201,8 @@ def __init__(self, master, app, *kw, **kwargs):
self._image = None
self._tkimage = None
self._probeImage = None
self._tab = None
self._tabRect = None

self.draw_axes = True # Drawing flags
self.draw_grid = True
Expand Down Expand Up @@ -269,8 +275,8 @@ def setActionRuler(self, event=None):
self.event_generate("<<Status>>",data="Drag a ruler to measure distances")

# ----------------------------------------------------------------------
def setActionTab(self, event=None):
self.setAction(ACTION_TAB)
def setActionAddTab(self, event=None):
self.setAction(ACTION_ADDTAB)
self.event_generate("<<Status>>",data="Draw a square tab")

# ----------------------------------------------------------------------
Expand Down Expand Up @@ -370,12 +376,21 @@ def click(self, event):
self.setActionSelect()

# Add tab
#elif self.action == ACTION_TAB:
# i = self.canvasx(event.x)
# j = self.canvasy(event.y)
# x,y,z = self.canvas2xyz(i,j)
# self.app.insertCommand("origin %g %g %g"%(x,y,z),True)
# self.setActionSelect()
elif self.action == ACTION_ADDTAB:
i = self.canvasx(event.x)
j = self.canvasy(event.y)
x,y,z = self.canvas2xyz(i,j)
x = round(x,CNC.digits)
y = round(y,CNC.digits)
z = round(z,CNC.digits)
# use the same z as the last tab added in gcode
if self.gcode.tabs: z = self.gcode.tabs[-1].z
self._tab = Tab(x,y,x,y,z)
self._tabRect = self._drawRect(
self._tab.xmin, self._tab.ymin,
self._tab.xmax, self._tab.ymax,
fill=TAB_COLOR)
self._mouseAction = self.action

# ----------------------------------------------------------------------
# Canvas motion button 1
Expand Down Expand Up @@ -420,6 +435,19 @@ def buttonMotion(self, event):
% (dx,dy,dz,math.sqrt(dx**2+dy**2+dz**2),
math.degrees(math.atan2(dy,dx))))

# Resize tab
elif self._mouseAction == ACTION_ADDTAB:
i = self.canvasx(event.x)
j = self.canvasy(event.y)
x,y,z = self.canvas2xyz(i,j)
x = round(x,CNC.digits)
y = round(y,CNC.digits)
self._tab.xmax = x
self._tab.ymax = y
self._rectCoords(self._tabRect,
self._tab.xmin, self._tab.ymin,
self._tab.xmax, self._tab.ymax)

self.setStatus(event)

# ----------------------------------------------------------------------
Expand Down Expand Up @@ -482,6 +510,15 @@ def release(self, event):
self.event_generate("<<Status>>", data="Move by %g, %g, %g"%(dx,dy,dz))
self.app.insertCommand("move %g %g %g"%(dx,dy,dz),True)

# Finalize tab
elif self._mouseAction == ACTION_ADDTAB:
self._tab.correct()
self.gcode.addUndo(self.gcode.addTabUndo(-1,self._tab))
self._tab = None
self._tabRect = None
self.setActionSelect()
self.event_generate("<<TabAdded>>")

# ----------------------------------------------------------------------
def double(self, event):
#self.app.selectBlocks()
Expand Down Expand Up @@ -895,19 +932,38 @@ def drawMargin(self):
fill=MARGIN_COLOR)
self.tag_lower(self._amargin)

#----------------------------------------------------------------------
# Change rectangle coordinates
#----------------------------------------------------------------------
def _rectCoords(self, rect, xmin, ymin, xmax, ymax, z=0.0):
self.coords(rect, Tkinter._flatten(self.plotCoords(
[(xmin, ymin, z),
(xmax, ymin, z),
(xmax, ymax, z),
(xmin, ymax, z),
(xmin, ymin, z)]
)))

#----------------------------------------------------------------------
# Draw a 3D rectangle
#----------------------------------------------------------------------
def _drawRect(self, xmin, ymin, xmax, ymax, z=0.0, **kwargs):
xyz = [(xmin, ymin, z),
(xmax, ymin, z),
(xmax, ymax, z),
(xmin, ymax, z),
(xmin, ymin, z)]
rect = self.create_line(
self.plotCoords(xyz),
**kwargs),
return rect

#----------------------------------------------------------------------
def drawTabs(self):
if not self.draw_margin: return
for tab in self.gcode.tabs:
xyz = [(tab.xmin, tab.ymin, 0.), #tab.z),
(tab.xmax, tab.ymin, 0.), #tab.z),
(tab.xmax, tab.ymax, 0.), #tab.z),
(tab.xmin, tab.ymax, 0.), #tab.z),
(tab.xmin, tab.ymin, 0.)] #tab.z)]
tabLine = self.create_line(
self.plotCoords(xyz),
fill=TAB_COLOR)
self.tag_lower(tabLine)
item = self._drawRect(tab.xmin, tab.ymin, tab.xmax, tab.ymax, 0., fill=TAB_COLOR)
self.tag_lower(item)

#----------------------------------------------------------------------
def drawWorkarea(self):
Expand All @@ -921,14 +977,7 @@ def drawWorkarea(self):
ymax = self._dy
zmax = self._dz

xyz = [(xmin, ymin, 0.),
(xmax, ymin, 0.),
(xmax, ymax, 0.),
(xmin, ymax, 0.),
(xmin, ymin, 0.)]
self._workarea = self.create_line(
self.plotCoords(xyz),
fill="Orange", dash=(3,2))
self._workarea = self._drawRect(xmin, ymin, xmax, ymax, 0., fill=WORK_COLOR, dash=(3,2))
self.tag_lower(self._workarea)

#----------------------------------------------------------------------
Expand Down Expand Up @@ -1174,6 +1223,8 @@ def drawPath(self, block, cmds):

#----------------------------------------------------------------------
# Return plotting coordinates for a 3d xyz path
#
# NOTE: Use the Tkinter._flatten() to pass to self.coords() function
#----------------------------------------------------------------------
def plotCoords(self, xyz):
coords = None
Expand Down
Loading

0 comments on commit a9aaa79

Please sign in to comment.