Skip to content

Instantly share code, notes, and snippets.

@Krewn
Created February 3, 2025 11:12
Show Gist options
  • Save Krewn/075590154dadb76e833df67cf734958f to your computer and use it in GitHub Desktop.
Save Krewn/075590154dadb76e833df67cf734958f to your computer and use it in GitHub Desktop.
Pretty much just doodling
class pointXY:
def __init__(self,x,y):
self.x = x
self.y = y
def svgStr(self):
return f"{self.x},{self.y}"
class lines:
def __init__(self):
self.starts = []
self.ends = []
def addLine(self,start,end):
self.starts.append(start)
self.ends.append(end)
def strokes(self):
return "\n".join([ f"""<line stroke="rgb(10%,10%,16%)" x1="{start.x}" x2="{end.x}" y1="{start.y}" y2="{end.y}"/>""" for start,end in zip(self.starts, self.ends)])
class clipping:
count = 0
def __init__(self,width=512,height=512,href="blank.png",poly=[]):
self.poly = poly
for n,p in enumerate(poly):
if type(p) in [tuple,list]:
self.poly[n] = pointXY(p[0],p[1])
self.width = width
self.height = height
self.href = href
self.id = clipping.count
clipping.count+=1
def addPoint(self,p):
assert not (type(p) in [int,str,float])
if type(p) == list or type(p) == tuple:
self.poly.append(pointXY(p[0],p[1]))
else:
self.poly.append(p)
def clipPath(self):
return f"""<clipPath id="clipID-{self.id}"><polygon points="{" ".join([_.svgStr() for _ in self.poly])}"/></clipPath>"""
def image(self):
return f"""<image height="{self.height}" width="{self.width}" href="{self.href}" clip-path="url(#clipID-{self.id})"/>"""
def outLine(self):
l = lines()
for start,end in zip([self.poly[-1]]+self.poly[:-1],self.poly):
l.addLine(start,end)
return l.strokes()
class scrapBoard:
def __init__(self,width = 512, height = 512, xmlns="http://www.w3.org/2000/svg"):
self.clippings = []
self.width = width
self.height = height
self.xmlns = xmlns
def addClipping(self,c):
self.clippings.append(c)
def clips(self):
return "\n".join([_.clipPath() for _ in self.clippings])
def images(self):
return "\n".join([_.image() for _ in self.clippings]) + "\n" + "\n".join([_.outLine() for _ in self.clippings])
def write(self,fn):
with open(fn,"w") as out:
out.write(self.svg())
def svg(self):
return f"""<svg width="{self.width}" height="{self.height}" xmlns="{self.xmlns}">
<defs>
{self.clips()}
</defs>
{self.images()}
</svg>"""
sb = scrapBoard()
sb.addClipping( clipping(href="BRP2.png",poly=[(0,0), (400,0), (0,200)]) )
sb.write("testOp.svg")
from clipToSvg import clipping
from clipToSvg import scrapBoard
brp = "BRP.png"
polished = "polished.png"
other = "other.png"
"""
shapes:
box
boxWithMargin
slant
western
bevel
lineShapes:
Serp
Oval
Roof
book
"""
def vadd(*args):
return [sum(a) for a in zip(*args)]
def vscale(v,s):
return [_*s for _ in v]
class orthoPolyBuilder:
def __init__(self, right = (1,0), up = (0,-1), back = (-0.42,-0.42), location = [0,0]):
self.right = right
self.up = up
self.back = back
self.left = vscale(right,-1)
self.down = vscale(up,-1)
self.forth = vscale(back,-1)
self.points = []
self.location = location
def move(self,delta,addLocation=True):
self.location = vadd(delta,self.location)
if addLocation:
self.addLocation()
def relocate(self,newLocation):
self.location = location
def addLocation(self):
self.points.append(self.location)
def pointList(self):
return [_ for _ in self.points]
def clear(self):
self.points = []
def footinchToFloat(m):
if type m == float:
return m
fi = m.split("-")
foot = float(fi[0])
inch = float(fi[1])/12
return foot+inch
def box(length,width,height,topFinish="BRP2.png",frontFinish="other.png",sideFinish="BRP.png", outFile = "testOut.svg" ):
lengthDesciption = str(length)
widthDesciption = str(width)
heightDesciption = str(height)
length = footinchToFloat(length)
width = footinchToFloat(width)
height = footinchToFloat(height)
ob = orthoPolyBuilder(location = [256,256])
absWidth = 420
figureWidth = ob.right[0]*length+ob.up[0]*height+ob.forth[0]*width
figureHeight = ob.right[1]*length+ob.down[1]*height+ob.forth[1]*width
pxPerUnit = absWidth/( max(figureWidth , figureHeight) )
p = pxPerUnit
front = None
ob.move(vscale(vadd(vscale(ob.forth,width*p),vscale(ob.down,height*p),vscale(ob.right,length*p)),0.5)) # front bottom right corner
ob.move(vscale(ob.left,length*p)) # front bottom left corner
ob.move(vscale(ob.up,height*p)) # front top left corner
ob.move(vscale(ob.right,length*p)) # front top right corner
frontPoly = ob.pointList()
ob.clear()
ob.move(vscale(ob.left,length*p)) # front top left corner
ob.move(vscale(ob.back,width*p)) # back top left corner
ob.move(vscale(ob.right,length*p)) # back top right corner
ob.move(vscale(ob.forth,width*p)) # front top right corner
topPoly = ob.pointList()
ob.clear()
ob.move(vscale(ob.left,length*p)) # front top left corner
ob.move(vscale(ob.down,height*p)) # front bottom left corner
ob.move(vscale(ob.back,width*p)) # back bottom left corner
ob.move(vscale(ob.up,height*p)) # back top left corner
sidePoly = ob.pointList()
sb = scrapBoard()
sb.addClipping(clipping(poly = topPoly,href=topFinish))
sb.addClipping(clipping(poly = frontPoly,href=frontFinish))
sb.addClipping(clipping(poly = sidePoly,href=sideFinish))
sb.write(outFile)
def boxWithMargin(length,width,height,margin=.2,marginFinish="polished.png",topFinish="BRP2.png",frontFinish="other.png",sideFinish="BRP.png", outFile = "testOut.svg" ):
lengthDesciption = str(length)
widthDesciption = str(width)
heightDesciption = str(height)
marginDesciption = str(margin)
length = footinchToFloat(length)
width = footinchToFloat(width)
height = footinchToFloat(height)
margin = footinchToFloat(margin)
ob = orthoPolyBuilder(location = [256,256])
absWidth = 420
figureWidth = ob.right[0]*length+ob.up[0]*height+ob.forth[0]*width
figureHeight = ob.right[1]*length+ob.down[1]*height+ob.forth[1]*width
pxPerUnit = absWidth/( max(figureWidth , figureHeight) )
p = pxPerUnit
front = None
ob.move(vscale(vadd(vscale(ob.forth,width*p),vscale(ob.down,height*p),vscale(ob.right,length*p)),0.5)) # front bottom right corner
ob.move(vscale(ob.left,length*p)) # front bottom left corner
ob.move(vscale(ob.up,(height-margin)*p)) # front top left corner
ob.move(vscale(ob.right,length*p)) # front top right corner
frontPoly = ob.pointList()
ob.clear()
ob.move(vscale(ob.up,margin*p),addLocation=False)
ob.move(vscale(ob.left,length*p)) # front top left corner
ob.move(vscale(ob.back,width*p)) # back top left corner
ob.move(vscale(ob.right,length*p)) # back top right corner
ob.move(vscale(ob.forth,width*p)) # front top right corner
topPoly = ob.pointList()
ob.clear()
ob.move(vscale(ob.down,margin*p),addLocation=False)
ob.move(vscale(ob.left,length*p)) # front top left corner
ob.move(vscale(ob.down,(height-margin)*p)) # front bottom left corner
ob.move(vscale(ob.back,width*p)) # back bottom left corner
ob.move(vscale(ob.up,(height-margin)*p)) # back top left corner
sidePoly = ob.pointList()
ob.clear()
ob.move(vscale(ob.up,margin*p),addLocation=False)
ob.move(vscale(ob.forth,width*p),addLocation=True) # front top left corner
ob.move(vscale(ob.down,margin*p),addLocation=True) # front bottom left corner
ob.move(vscale(ob.back,width*p),addLocation=True) # back bottom left corner
ob.move(vscale(ob.up,margin*p),addLocation=True) # back top left corner
sideMarginPoly = ob.pointList()
ob.clear()
ob.move(vscale(ob.forth,width*p),addLocation=True) # front top left corner
ob.move(vscale(ob.right,length*p)) # front top right corner
ob.move(vscale(ob.down,margin*p)) # front bottom right corner
ob.move(vscale(ob.left,length*p)) # front bottom left corner
frontMarginPoly = ob.pointList()
sb = scrapBoard()
sb.addClipping(clipping(poly = topPoly,href=topFinish))
sb.addClipping(clipping(poly = frontPoly,href=frontFinish))
sb.addClipping(clipping(poly = sidePoly,href=sideFinish))
sb.addClipping(clipping(poly = sideMarginPoly,href=marginFinish))
sb.addClipping(clipping(poly = frontMarginPoly,href=marginFinish))
sb.write(outFile)
box(1,2,3,outFile = "testOut.svg")
boxWithMargin(3,3,3,outFile = "testOut2.svg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment