Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
def check_options(dir='.'):
    global options, options_mtime
    options = {"show_threads": str(os.getenv("SHOW_THREADS"))}
    options_fname = dir + '/options.json'
    try:
        with open(options_fname) as json_file:
            last_options = json.load(json_file)
    except:
        last_options = {}
    if last_options != options:
        with open(options_fname, 'w') as outfile:
            json.dump(options, outfile, indent=4)
    options_mtime = deps.mtime(options_fname)
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
def have_changed(changed, target):
    if not changed and deps.mtime(target) < options_mtime:
        return "command line options changed"
    return changed
Ejemplo n.º 5
0
def have_changed(changed, target):
    if not changed and deps.mtime(target) < options_mtime:
        return Fore.CYAN + "command line options changed" + Fore.WHITE
    return changed