def render(target, type): # # Make the target directory # top_dir = set_config(target, usage) target_dir = top_dir + type + 's' bom_dir = top_dir + 'bom' if not os.path.isdir(target_dir): os.makedirs(target_dir) # # Find all the parts # parts = bom_to_parts(bom_dir, type) # # Remove unused png files # for file in os.listdir(target_dir): if file.endswith('.png'): if not file[:-4] + '.' + type in parts: print("Removing %s" % file) os.remove(target_dir + '/' + file) for part in parts: part_file = target_dir + '/' + part png_name = target_dir + '/' + part[:-4] + '.png' # # make a file to import the stl # if mtime(part_file) > mtime(png_name): png_maker_name = "png.scad" with open(png_maker_name, "w") as f: f.write('color([0, 146/255, 0]) import("%s");\n' % part_file) cam = "--camera=0,0,0,70,0,315,500" if type == 'stl' else "--camera=0,0,0,0,0,0,500" render = "--preview" if type == 'stl' else "--render" tmp_name = 'tmp.png' openscad.run(colour_scheme, "--projection=p", "--imgsize=4096,4096", cam, render, "--autocenter", "--viewall", "-o", tmp_name, png_maker_name) do_cmd(( "magick " + tmp_name + " -trim -resize 280x280 -background %s -gravity Center -extent 280x280 -bordercolor %s -border 10 %s" % (background, background, tmp_name)).split()) update_image(tmp_name, png_name) os.remove(png_maker_name)
def render(target, type): # # Make the target directory # top_dir = set_config(target, usage) tmp_dir = mktmpdir(top_dir) target_dir = top_dir + type + 's' bom_dir = top_dir + 'bom' if not os.path.isdir(target_dir): os.makedirs(target_dir) # # Find all the parts # parts = bom_to_parts(bom_dir, type) # # Read the json bom to get the colours # bom_file = bom_dir + "/bom.json" with open(bom_file) as json_file: flat_bom = json.load(json_file) things = {'stl': 'printed', 'dxf': 'routed'}[type] colours = {} for ass in flat_bom: for part in ass[things]: obj = ass[things][part] if "colour" in obj: colours[part] = obj["colour"] # # Remove unused png files # for file in os.listdir(target_dir): if file.endswith('.png'): if not file[:-4] + '.' + type in parts: print("Removing %s" % file) os.remove(target_dir + '/' + file) # # Render the parts # for part in parts: part_file = target_dir + '/' + part png_name = target_dir + '/' + part[:-4] + '.png' # # make a file to import the stl # if mtime(part_file) > mtime(png_name): png_maker_name = tmp_dir + "/png.scad" pp1 = [0, 146 / 255, 0] colour = pp1 if part in colours: colour = colours[part] if not '[' in colour: colour = '"' + colour + '"' with open(png_maker_name, "w") as f: f.write('color(%s) import("%s");\n' % (colour, reltmp(part_file, target))) cam = "--camera=0,0,0,70,0,315,500" if type == 'stl' else "--camera=0,0,0,0,0,0,500" render = "--preview" if type == 'stl' or colour != pp1 else "--render" tmp_name = tmp_dir + '/' + part[:-4] + '.png' openscad.run("-o", tmp_name, png_maker_name, colour_scheme, "--projection=p", "--imgsize=4096,4096", cam, render, "--autocenter", "--viewall") do_cmd(( "magick " + tmp_name + " -trim -resize 280x280 -background %s -gravity Center -extent 280x280 -bordercolor %s -border 10 %s" % (background, background, tmp_name)).split()) update_image(tmp_name, png_name) os.remove(png_maker_name) # # Remove tmp dir # rmtmpdir(tmp_dir)