%matplotlib inline
import foldable_robotics
import foldable_robotics.dxf
import numpy
import shapely.geometry as sg
from foldable_robotics.layer import Layer
from foldable_robotics.laminate import Laminate
import foldable_robotics.manufacturing
import foldable_robotics.parts.castellated_hinge1
import idealab_tools.plot_tris
from math import pi, sin,cos,tan
import idealab_tools.text_to_polygons
foldable_robotics.display_height=200
foldable_robotics.line_width=.5
from foldable_robotics.layer import Layer
from foldable_robotics.laminate import Laminate
import foldable_robotics
import foldable_robotics.dxf
import foldable_robotics.manufacturing
import foldable_robotics.parts.castellated_hinge1
foldable_robotics.display_height=200
foldable_robotics.line_width=.5
#import workflow_support as ws
import os
import foldable_robotics.solidworks_support
def get_bodies(filename, layername, num_layers):
body = foldable_robotics.dxf.read_lwpolylines(filename,layer=layername, arc_approx = 10)
bodies = [Layer(sg.Polygon(item)) for item in body]
body = bodies.pop(0)
for item in bodies:
body ^= item
body = body.to_laminate(num_layers)
return body
def get_hinge_lines(filename,layername):
hinge_lines1 = foldable_robotics.dxf.read_lines(filename,layer=layername)
hinge_lines2 = foldable_robotics.dxf.read_lwpolylines(filename,layer=layername)
hinge_lines3 = []
for points in hinge_lines2:
hinge_lines3.append(points[:2])
hinge_lines = hinge_lines1 +hinge_lines3
return hinge_lines
def hinge_lines_to_hinges(hinge_lines,hinge):
lam = Layer().to_laminate(len(hinge))
all_hinges = []
for p3,p4 in hinge_lines:
all_hinges.append(hinge.map_line_stretch((0,0),(1,0),p3,p4))
all_hinges = lam.unary_union(*all_hinges)
return all_hinges
def get_cuts(filename,layername,thickness,num_layers):
cut_lines = foldable_robotics.dxf.read_lines(filename,layer=layername)
cut_lines += foldable_robotics.dxf.read_lwpolylines(filename,layer=layername, arc_approx = 10)
cuts = []
for item in cut_lines:
cuts.append(Layer(sg.LineString(item)))
cuts = Layer().unary_union(*cuts)
cuts<<=thickness/2
cuts = cuts.to_laminate(num_layers)
return cuts
def get_holes(filename, layername,num_layers):
holes = foldable_robotics.dxf.read_circles(filename,layer='holes')
holes2 = []
for center, radius in holes:
holes2.append(sg.Point(*center).buffer(radius))
holes_layer = Layer(*holes2)
holes_lam = holes_layer.to_laminate(num_layers)
return holes_lam
def hinge_width_calculator(desired_degrees,thickness):
theta = (180-desired_degrees)*pi/180
w=thickness/tan(theta)
return w
def polys_to_layer(l1):
l1 = [sg.Polygon(item) for item in l1]
l11 = Layer(l1.pop(0))
for item in l1:
l11 ^= Layer(item)
return l11
def output_pdf(filename,design2,x,y,layers_separate = True):
design2 = design2.translate(x,y)
design2=design2.scale(1/25.4,1/25.4)
design2=design2.scale(foldable_robotics.pdf.ppi,foldable_robotics.pdf.ppi)
if isinstance(design2,Laminate):
if not layers_separate:
p=foldable_robotics.pdf.Page(filename+'.pdf')
for d in design2:
# d = design2[0]
for item in d.exteriors()+d.interiors():
p.draw_poly(item)
p.close()
else:
for ii,d in enumerate(design2):
p=foldable_robotics.pdf.Page(filename+'{0:03f}.pdf'.format(ii))
for item in d.exteriors()+d.interiors():
p.draw_poly(item)
p.close()
elif isinstance(design2,Layer):
p=foldable_robotics.pdf.Page(filename+'.pdf')
for item in design2.exteriors()+design2.interiors():
p.draw_poly(item)
p.close()
def build_layer_numbers(num_layers, text_size = None, prop=None):
text_size = text_size or 1
prop = prop or {'family':'Arial','size':text_size}
layer_ids = []
for ii in range(num_layers):
l = idealab_tools.text_to_polygons.text_to_polygons('Layer '+str(ii),prop=prop)
layer_ids.append(l)
layer_ids = [polys_to_layer(item) for item in layer_ids]
layer_id = Laminate(*layer_ids)
return layer_id
def build_web(design2,keepout,support_width,jig_diameter,jig_hole_spacing,is_adhesive):
num_layers = len(design2)
layer_id = build_layer_numbers(num_layers,text_size=jig_diameter)
design_outer = foldable_robotics.manufacturing.unary_union(design2)
bb1= (design_outer<<jig_hole_spacing/2).bounding_box()
(x1,y1),p2 = bb1.bounding_box_coords()
w,h = bb1.get_dimensions()
w2 = round(w/jig_hole_spacing)*jig_hole_spacing
h2 = round(h/jig_hole_spacing)*jig_hole_spacing
points = []
points.append(sg.Point(x1,y1))
points.append(sg.Point(x1+w2,y1))
points.append(sg.Point(x1,y1+h2))
points.append(sg.Point(x1+w2,y1+h2))
layer_id = layer_id.translate(x1+jig_diameter,y1-jig_diameter/2)
placement_holes2 = Layer(*points)
placement_holes2<<=(jig_diameter/2)
sheet = (placement_holes2<<10).bounding_box()
placement_holes2=placement_holes2.to_laminate(num_layers)
sheet=sheet.to_laminate(num_layers)
removable_scrap = calculate_removable_scrap(design2,sheet,support_width,is_adhesive)
web = (removable_scrap-placement_holes2)-layer_id
return web,sheet
def calculate_removable_scrap(design,sheet,width,is_adhesive):
'''this computes all removable scrap given a sheet, a design, and a clearance width'''
all_scrap = sheet-design
ru = foldable_robotics.manufacturing.not_removable_up(design,is_adhesive)
rd = foldable_robotics.manufacturing.not_removable_down(design,is_adhesive)
removable_scrap_up = all_scrap-(ru<<width)
removable_scrap_down = all_scrap-(rd<<width)
removable_scrap = removable_scrap_up|removable_scrap_down
return removable_scrap
folder = 'C:/Users/codem/Desktop/Prac_assembly_-_Sheet1_Drawing_View1/'
input_filename = folder+'Middle_seg_yaml_-_Sheet1_Drawing_View1.yaml'
output_file_name = 'design.dxf'
round_digits = 3
prescale=1000
body_prebuffer=-.01
joint_tolerance = 1e-5
round_digits = 2
prescale=1000
jig_diameter = 5
support_width = 1
kerf = .05
jig_hole_spacing=20
is_adhesive = [False,True,False,True,False]
arc_approx = 10
foldable_robotics.solidworks_support.process(input_filename,output_file_name,prescale,round_digits,body_prebuffer,joint_tolerance)
(<foldable_robotics.layer.Layer at 0x2409ceb7580>, <foldable_robotics.layer.Layer at 0x2409ceb7d90>, [<foldable_robotics.solidworks_support.Component at 0x2409ce96040>, <foldable_robotics.solidworks_support.Component at 0x2409ce96520>, <foldable_robotics.solidworks_support.Component at 0x2409ce96c40>, <foldable_robotics.solidworks_support.Component at 0x2409ce96dc0>, <foldable_robotics.solidworks_support.Component at 0x2409ce96e80>, <foldable_robotics.solidworks_support.Component at 0x2409ce96af0>, <foldable_robotics.solidworks_support.Component at 0x2409ce96220>, <foldable_robotics.solidworks_support.Component at 0x2409ce96fa0>, <foldable_robotics.solidworks_support.Component at 0x2409ce96880>, <foldable_robotics.solidworks_support.Component at 0x2409ce96580>, <foldable_robotics.solidworks_support.Component at 0x2409ce96430>, <foldable_robotics.solidworks_support.Component at 0x2409cea4940>, <foldable_robotics.solidworks_support.Component at 0x2409cea4160>, <foldable_robotics.solidworks_support.Component at 0x2409cea4340>, <foldable_robotics.solidworks_support.Component at 0x2409cea44c0>, <foldable_robotics.solidworks_support.Component at 0x2409cea4700>, <foldable_robotics.solidworks_support.Component at 0x2409cea4040>])
hinge = foldable_robotics.parts.castellated_hinge1.generate()
w=hinge_width_calculator(150,1)
hinge = hinge.scale(1,w)
hinge.plot()
>>> import shapely
>>> import sys
>>> m=sys.modules['shapely']
>>> m.__version__
'1.7.1'
NUMLAYERS = len(hinge)
body = get_bodies(output_file_name,'body',NUMLAYERS)
body = foldable_robotics.manufacturing.cleanup(body,.02)
body.plot()
joint_lines= get_hinge_lines(output_file_name,'joints')
joint_lines
j = []
for k in joint_lines:
k[0][0] = round(k[0][0], 2)
k[0][1] = round(k[0][1], 2)
k[1][0] = round(k[1][0], 2)
k[1][1] = round(k[1][1], 2)
print(joint_lines[42])
print(joint_lines[41])
print(joint_lines[35])
print(joint_lines[34])
print(joint_lines[33])
print(joint_lines[15])
print(joint_lines[14])
print(joint_lines[13])
print(joint_lines[11])
print(joint_lines[10])
print(joint_lines[9])
joint_lines.remove(joint_lines[42]) #remove center hinges
joint_lines.remove(joint_lines[41])
joint_lines.remove(joint_lines[35])
# joint_lines.remove(joint_lines[34])
joint_lines.remove(joint_lines[33])
joint_lines.remove(joint_lines[15])
# joint_lines.remove(joint_lines[14])
joint_lines.remove(joint_lines[13])
joint_lines.remove(joint_lines[11])
# joint_lines.remove(joint_lines[10])
joint_lines.remove(joint_lines[9])
print(joint_lines[29])
print(joint_lines[11])
print(joint_lines[9])
print(joint_lines.pop(29))
print(joint_lines.pop(11))
print(joint_lines.pop(9))
# joint_lines.remove(joint_lines[29]) #can't remove, have to pop
# joint_lines.remove(joint_lines[11])
# joint_lines.remove(joint_lines[9])
joint_lines.remove(joint_lines[26]) #remove hinges on edge of body
joint_lines.remove(joint_lines[25])
joint_lines.remove(joint_lines[22])
joint_lines.remove(joint_lines[21])
joint_lines.remove(joint_lines[6])
joint_lines.remove(joint_lines[5])
joint_lines.remove(joint_lines[4])
joint_lines.remove(joint_lines[3])
joint_lines.remove(joint_lines[30]) #remove hinges on edge of body
joint_lines.remove(joint_lines[29])
joint_lines.remove(joint_lines[28])
joint_lines.remove(joint_lines[27])
joint_lines.remove(joint_lines[23])
joint_lines.remove(joint_lines[0])
joint_lines.remove(joint_lines[22]) #Anger
joint_lines.remove(joint_lines[14])
joint_lines.remove(joint_lines[13])
joint_lines.remove(joint_lines[4])
joint_lines.remove(joint_lines[3])
joints = hinge_lines_to_hinges(joint_lines,hinge)
joints = foldable_robotics.manufacturing.cleanup(joints,.02)
count = 0
for item in joint_lines:
tempjoint = hinge_lines_to_hinges([item],hinge)
print(item, count)
count = count +1
tempjoint.plot()
joints.plot()
[[0.0, -0.0], [19.74, 25.26]] [[-19.74, -25.26], [-0.0, -0.0]] [[-0.0, -0.0], [-18.0, -27.0]] [[0.0, -0.0], [-0.0, -0.0]] [[18.0, 27.0], [0.0, -0.0]] [[0.0, -0.0], [18.0, -27.0]] [[0.0, -0.0], [0.0, -0.0]] [[-18.0, 27.0], [0.0, -0.0]] [[-0.0, -0.0], [-19.74, 25.26]] [[0.0, -0.0], [-0.0, -0.0]] [[19.74, -25.26], [0.0, -0.0]] [[0.0, -0.0], [-0.0, -0.0]] [[0.0, -0.0], [0.0, -0.0]] [[0.0, -0.0], [-0.0, -0.0]] [[0.0, -0.0], [-0.0, -0.0]] [[0.0, -0.0], [0.0, -0.0]] [[0.0, -0.0], [-0.0, -0.0]] [[-18.0, 27.0], [18.0, 27.0]] 0 [[18.0, 33.55], [18.0, 47.0]] 1 [[-18.0, 47.0], [-18.0, 33.55]] 2 [[22.47, -31.47], [32.14, -41.14]] 3 [[45.0, -0.0], [19.74, -25.26]] 4 [[19.74, -25.26], [18.0, -27.0]] 5 [[-45.0, -0.0], [-19.74, 25.26]] 6 [[-19.74, 25.26], [-18.0, 27.0]] 7 [[-59.14, 14.14], [-50.66, 5.66]] 8 [[-22.47, 31.47], [-32.14, 41.14]] 9 [[59.14, 14.14], [50.66, 5.66]] 10 [[18.0, -27.0], [-18.0, -27.0]] 11 [[-18.0, -33.55], [-18.0, -47.0]] 12 [[18.0, -47.0], [18.0, -33.55]] 13 [[45.0, -0.0], [19.74, 25.26]] 14 [[19.74, 25.26], [18.0, 27.0]] 15 [[-45.0, -0.0], [-19.74, -25.26]] 16 [[-19.74, -25.26], [-18.0, -27.0]] 17 [[32.14, 41.14], [22.47, 31.47]] 18 [[-32.14, -41.14], [-22.47, -31.47]] 19
cuts = get_cuts(output_file_name,'cuts',.02,NUMLAYERS)
#cuts.plot()
holes = get_holes(output_file_name,'holes',NUMLAYERS)
#holes.plot()
hole,dummy = foldable_robotics.manufacturing.calc_hole(joint_lines,w)
hole = hole.to_laminate(NUMLAYERS)
hole<<=.2
hole.plot()
design2 = body- hole - joints - cuts - holes
design2.plot()
keepout = foldable_robotics.manufacturing.keepout_laser(design2)
keepout.plot()
web,sheet=build_web(design2,keepout,support_width,jig_diameter,jig_hole_spacing,is_adhesive)
web.plot()
sheet.plot()
second_pass_scrap = sheet-keepout
first_pass_scrap = sheet - design2-second_pass_scrap
first_pass_scrap = foldable_robotics.manufacturing.cleanup(first_pass_scrap,.00001)
first_pass_scrap.plot()
support = foldable_robotics.manufacturing.support(design2,foldable_robotics.manufacturing.keepout_laser,support_width,support_width/2)
support.plot()
#Calculate the web by using only the material which can be cut, minus a gap determined by the support width. Is that the only material you can use?
supported_design = web|design2|support
supported_design.plot()
#cut_line = keepout<<kerf
cut_material = (keepout<<kerf)-keepout
cut_material.plot()
final_cut = sheet - keepout
final_cut = final_cut[0]
final_cut.plot()
remaining_material = supported_design-cut_material
remaining_material.plot()
remaining_parts = foldable_robotics.manufacturing.find_connected(remaining_material,is_adhesive)
for item in remaining_parts:
item.plot(new=True)
d3=design2>>1
for item in remaining_parts:
if not (item&d3).is_null():
break
item.plot()
check = (item^design2)
check>>=1e-5
check.plot()
('zero-size array to reduction operation minimum which has no identity',)
w,h = supported_design.get_dimensions()
p0,p1 = supported_design.bounding_box_coords()
rigid_layer = supported_design[0] | (supported_design[-1].translate(w+10,0))
rigid_layer.plot()
l4 = supported_design[3].scale(-1,1)
p2,p3 = l4.bounding_box_coords()
l4 = l4.translate(p0[0]-p2[0]+10+w,p0[1]-p2[1])
adhesive_layer = supported_design[1] | l4
adhesive_layer.plot()
first_pass = Laminate(rigid_layer,adhesive_layer,supported_design[2])
# if check.is_null():
# first_pass.export_dxf('first_pass')
# final_cut.export_dxf('final_cut')
first_pass.export_dxf('first_pass')
final_cut.export_dxf('final_cut')