"""An example of creating a parameterized assembly and using variables to make theScad output file more readable."""importsys,osfrompy2scadimport*# Setup the path to the Scad executablescad=''# Default to Falseifsys.platform=='darwin':scad="/Applications/OpenSCAD.app/Contents/MacOS/OpenSCAD"elifsys.platform=="win32":scad=r'"D:\Program files\OpenSCAD\OpenSCAD.exe"'outfilename="assembly.scad"# This is just an example, so we are going to make pointless mounting plate!########################### Variables ############################ Assume the user of the script might want to play with w/h/d and edge offsetsvars=Variables(width=50.0,height=25.0,depth=2.0,inset=3.0)# Hole sizes are fixed thoughbolt_dia=3.0hole_dia=10.0tol=0.25# Gap tolerance########################### Assembly ############################# Make a faux m3 socket head bolt assemblym3_bolt=Assembly(name="m3bolt",parameters=["length"],obj=Union([# socket cylinderTranslate(Cylinder(h=bolt_dia,r1=bolt_dia),[0,0,bolt_dia/2]),# threaded shaftTranslate(Cylinder(h="length+1",r1=bolt_dia/2),[0,0,"-length/2"])]),comment="Generate an m3 bolt at the specific length.")############################# Part ############################### The part is just a cube with some holes, and transparent bolts in the holespart=Difference([Cube(size=[vars.width,vars.height,vars.depth]),# Bolt holesTranslate(Cylinder(h='2+'+vars.depth,r1=(bolt_dia/2)+tol),["({0.width}/2)-{0.inset}".format(vars),"({0.height}/2)-{0.inset}".format(vars),0]),Translate(Cylinder(h='2+'+vars.depth,r1=(bolt_dia/2)+tol),["(-{0.width}/2)+{0.inset}".format(vars),"({0.height}/2)-{0.inset}".format(vars),0]),Translate(Cylinder(h='2+'+vars.depth,r1=(bolt_dia/2)+tol),["(-{0.width}/2)+{0.inset}".format(vars),"(-{0.height}/2)+{0.inset}".format(vars),0]),Translate(Cylinder(h='2+'+vars.depth,r1=(bolt_dia/2)+tol),["({0.width}/2)-{0.inset}".format(vars),"(-{0.height}/2)+{0.inset}".format(vars),0]),# BoltsUnion([Translate(m3_bolt(16),["({0.width}/2)-{0.inset}".format(vars),"({0.height}/2)-{0.inset}".format(vars),0]),Translate(m3_bolt(16),["(-{0.width}/2)+{0.inset}".format(vars),"({0.height}/2)-{0.inset}".format(vars),0]),Translate(m3_bolt(16),["(-{0.width}/2)+{0.inset}".format(vars),"(-{0.height}/2)+{0.inset}".format(vars),0]),Translate(m3_bolt(16),["({0.width}/2)-{0.inset}".format(vars),"(-{0.height}/2)+{0.inset}".format(vars),0])],mod='%'),# Center holeCylinder(h=vars['depth']+2,r1=hole_dia/2)])design=SCAD_Prog()design.add([vars,m3_bolt,part])design.write(outfilename)ifscad:# If we can find scad# Generate a stl file in the same dir with the same namd as the scad fileos.system('{scad} -s {stl} {file}'.format(scad=scad,stl=outfilename.replace('.scad','.stl'),file=outfilename))