Skip to content

Commit

Permalink
Update scaling.py
Browse files Browse the repository at this point in the history
Scale multiple shapes. Scale from the center of the shape (only for individual selection)
  • Loading branch information
MARIOBASZ committed Jan 29, 2019
1 parent 9381035 commit 63ab0b0
Showing 1 changed file with 110 additions and 60 deletions.
170 changes: 110 additions & 60 deletions bCNC/plugins/scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
# Author: Mario Basz
#mariob_1960@yahoo.com.ar
# Date: 20 may 2018
# A special thanks to Vasilis Viachoudis, Filippo Rivato and Buschhardt
# A special thanks to Vasilis Viachoudis, Filippo Rivato and Buschhardt
#This plugin is based on a variation
# of yours Driller plugin and My_Plugin example.

from __future__ import absolute_import
from __future__ import print_function

__author__ = "Mario S Basz"
__email__ = "mariob_1960@yaho.com.ar"

__name__ = _("Scaling")
__version__= "0.1"
__version__= "0.6"

import os.path
import re
Expand All @@ -27,47 +24,43 @@
from bmath import pi
from math import pi, sqrt, sin, cos, asin, acos, atan2, hypot, degrees, radians, copysign, fmod, tan


#==============================================================================
# Scaling selected block
#==============================================================================
class Tool(Plugin):
__doc__ = _("Scaling the selected block")

def __init__(self, master):
Plugin.__init__(self, master, "Scaling")
self.icon = "scale"
self.icon = "scaling"
self.group = "CAM"

self.variables = [
("name", "db", "", _("Name")),
("xscale" ,"float", "", _("X Scale")),
("yscale" ,"float", "", _("Y Scale")),
("zscale" ,"float", "", _("Z Scale")),
("feed" , "int", 1200 , _("Feed")),
("zfeed" , "int", "" , _("Plunge Feed")),
("rpm", "int" , 12000, _("RPM"))
("name" ,"db", "", _("Name")),
("xscale" ,"float", "", _("X Scale")),
("yscale" ,"float", "", _("Y Scale")),
("zscale" ,"float", "", _("Z Scale")),
("centered" ,"bool" , 0, _("X Y Center")),
("feed" , "int" , 1200, _("Feed")),
("zfeed" , "int" ,"" , _("Plunge Feed")),
("rpm" , "int" ,12000, _("RPM"))
]
self.buttons.append("exe")

# -----------------------------------------------------
def scaling(self,xyz):
xscale= self["xscale"]
if xscale=="":xscale=1
yscale= self["yscale"]
if yscale=="":yscale=1
zscale= self["zscale"]
if zscale=="":zscale=1
def scaling(self,xyz,center,xscale,yscale,zscale):
safe = CNC.vars["safe"]

A = xyz[0]
B = xyz[1]
# xlength = B[0]-A[0]
# ylength = B[1]-A[1]
# zlength = B[2]-A[2]
xnew=B[0]*xscale
ynew=B[1]*yscale
znew=B[2]*zscale
xnew=(B[0]-center[0])*xscale+center[0]
ynew=(B[1]-center[1])*yscale+center[1]
znew=min(B[2]*zscale,safe)

return xnew,ynew,znew
dxy=sqrt(((B[0]-A[0])*xscale)**2+((B[1]-A[1])*yscale)**2)
dz=(B[2]-A[2])*zscale
return xnew,ynew,znew,dxy,dz

# Calc subsegments -----------------------------------------------------
def calcSegmentLength(self, xyz):
Expand Down Expand Up @@ -103,16 +96,17 @@ def extractAllSegments(self, app,selectedBlock):
app.cnc.motionStart(cmd)
xyz = app.cnc.motionPath()
app.cnc.motionEnd()

if xyz:
#coment its?
#-----------------------------------------------------------------------------------------
#exclude if fast move or z only movement
G0 =('g0' in cmd) or ('G0' in cmd)
Zonly = (xyz[0][0] == xyz[1][0] and xyz[0][1] == xyz[1][1])
exclude = Zonly
#-----------------------------------------------------------------------------------------

#save length for later use
segLength = self.calcSegmentLength(xyz)

if len(xyz) < 3:
bidSegments.append([xyz[0],xyz[1],exclude,segLength])
else:
Expand All @@ -127,70 +121,126 @@ def extractAllSegments(self, app,selectedBlock):
if block.name() in ("Header", "Footer"): continue
block.enable = False

return allSegments
return allSegments,block.name()

# ----------------------------------------------------------------------
def execute(self, app):
# info =xnew,ynew,znew,dxy,dz

xscale= self["xscale"]
if xscale=="":xscale=1
yscale= self["yscale"]
if yscale=="":yscale=1
zscale= self["zscale"]
if zscale=="":zscale=1

surface = CNC.vars["surface"]
if zscale>0:
surface*=zscale

feed = self["feed"]
zfeed = CNC.vars["cutfeedz"]
rpm = self["rpm"]
if self["zfeed"]:
zfeed = self["zfeed"]

zup = self["zup"]
#zup = self["zup"]


centered = self["centered"]

surface = CNC.vars["surface"]
# zbeforecontact=surface+CNC.vars["zretract"]
# hardcrust = surface - CNC.vars["hardcrust"]
# feedbeforecontact = CNC.vars["feedbeforecontact"]/100.0
# hardcrustfeed = CNC.vars["hardcrustfeed"]/100.0

# Get selected blocks from editor
selBlocks = app.editor.getSelectedBlocks()

selBlocks = app.editor.getSelectedBlocks()
if not selBlocks:
app.setStatus(_("Scaling abort: Please select some path"))
return
elements=len(app.editor.getSelectedBlocks())
print("elements",elements)
for bid in app.editor.getSelectedBlocks():
if len(app.gcode.toPath(bid)) < 1: continue
path = app.gcode.toPath(bid)[0]
if centered:
center = path.center()
else:
center=0,0
print "center",center[0],center[1]
# if elements>=2:
# center=0,0


#Get all segments from gcode
allSegments = self.extractAllSegments(app,selBlocks)
allSegments = self.extractAllSegments(app,selBlocks)[0]
name_block = self.extractAllSegments(app,selBlocks)[1]
# num_block = self.extractAllSegments(app,selBlocks)[2]

#Create holes locations
# allHoles=[]
all_blocks=[]
for bidSegment in allSegments:
if len(bidSegment)==0:
continue
blocks = []
n = self["name"]
# if not n or n=="default": n="Trochoidal_3D"
n="Scaling"
scal_block = Block(n)

for idx, segm in enumerate(bidSegment):
if idx >= 0:
if idx == 0:
scal_block.append("M03")
scal_block.append("S "+str(rpm))
scal_block.append(CNC.zsafe())
scal_block.append("F "+str(feed))
scal_block.append("(--------------------------------------------------)")

B=self.scaling(segm)
# all_blocks = []
n = self["name"]
# if not n or n=="default": n="Trochoidal_3D"
if elements>1:
n="scale "
else:
if centered:
n="center scale "+str(name_block)
else:
n="scale "+str(name_block)
bid_block = Block(n)

if segm[0][2]> surface and segm[1][2]>=surface:
scal_block.append("g0 x "+str(B[0])+" y "+str(B[1])+ " z "+str(B[2]))
for idx, segm in enumerate(bidSegment):
# if idx >= 0:
# bid_block.append("(idx "+str(idx)+" -------------- )")
info=self.scaling(segm,center,xscale,yscale,zscale)
if idx == 0:
bid_block.append("(---- Scale (x "+str(xscale)+" : 1.0),(y "+str(yscale)+" : 1.0),(z "+str(zscale)+" : 1.0) ---- )")
bid_block.append("(center "+str(center[0])+" ,"+str(center[1])+" )")
bid_block.append("M03")
bid_block.append("S "+str(rpm))
bid_block.append(CNC.zsafe())
bid_block.append("F "+str(zfeed))
bid_block.append("g0 x "+str(info[0])+" y "+str(info[1]))
currentfeed=oldfeed=zfeed
else:
scal_block.append("g1 x "+str(B[0])+" y "+str(B[1])+ " z "+str(B[2]))
scal_block.append(CNC.zsafe()) #<<< Move rapid Z axis to the safe height in Stock Material
blocks.append(scal_block)
self.finish_blocks(app, blocks)
# if B[5]>=0: #<< zsign
# currentfeed=feed
# else:
#relationship
if info[4]>=0:
currentfeed=feed
else:
rel=info[3]/(info[3]+abs(info[4]))
currentfeed=int(rel*feed+(1-rel)*zfeed)

if segm[0][2]> surface and segm[1][2]>=surface:
bid_block.append("g0 x "+str(info[0])+" y "+str(info[1])+ " z "+str(info[2]))
else:
if currentfeed!=oldfeed:
bid_block.append("F "+str(currentfeed))
bid_block.append("g1 x "+str(info[0])+" y "+str(info[1])+ " z "+str(info[2]))
oldfeed=currentfeed

bid_block.append(CNC.zsafe()) #<<< Move rapid Z axis to the safe height in Stock Material
all_blocks.append(bid_block)
# print "bid", bid_block.name(), bid_block,"*****************"
self.finish_blocks(app, all_blocks,elements)
#--------------------------------------------------------------


#Insert created blocks
def finish_blocks(self, app, blocks):
def finish_blocks(self, app, blocks,elements):
active = app.activeBlock()
if active==0: active=1
app.gcode.insBlocks(active, blocks, "scale")
# if active==0: active=1
if elements>1:
active=-2
app.gcode.insBlocks(active+1, blocks, "scale ")
app.refresh()
app.setStatus(_("Scaling Generated"))

0 comments on commit 63ab0b0

Please sign in to comment.