Пример #1
0
def compile_vitamin(v, dom):

    temp_name = "temp.scad"

    #
    # Make the target directories
    #
    target_dir = "../vitamins/stl"
    if not os.path.isdir(target_dir):
        os.makedirs(target_dir)

    view_dir = "../vitamins/images"
    if not os.path.isdir(view_dir):
        os.makedirs(view_dir)

    # Compile
    print("  " + v['title'])
    fn = '../' + v['file']
    if (os.path.isfile(fn)):

        print("    Checking csg hash")
        h = openscad.get_csg_hash(temp_name, v['call'], [fn])
        os.remove(temp_name)

        hashchanged = ('hash' in v and h != v['hash']) or (not 'hash' in v)

        # update hash in json
        v['hash'] = h

        # STL
        print("    STL Parts")
        for part in v['children']:
            if type(part) is DictType and part['type'] == 'part':
                stlpath = target_dir + '/' + openscad.stl_filename(
                    part['title'])
                if hashchanged or (not os.path.isfile(stlpath)):
                    print("      Rendering STL...")
                    openscad.render_stl(temp_name, stlpath, part['call'], [fn])
                else:
                    print("      STL up to date")

        # Views
        print("    Views")
        for view in v['children']:
            if type(view) is DictType and view['type'] == 'view':
                print("      " + view['title'])

                render_view(v['title'], v['call'], view_dir, view, hashchanged,
                            h, [fn], False)

                png_name = view_dir + '/' + view_filename(v['title'] + '_' +
                                                          view['title'])
                view['png_name'] = png_name

        node = add_vitamin(v, dom)

    else:
        print("    Error: scad file not found: " + v['file'])
def compile_vitamin(v, dom):

    temp_name =  "temp.scad"


    #
    # Make the target directories
    #
    target_dir = "../vitamins/stl"
    if not os.path.isdir(target_dir):
        os.makedirs(target_dir)

    view_dir = "../vitamins/images"
    if not os.path.isdir(view_dir):
        os.makedirs(view_dir)

    # Compile
    print("  "+v['title'])
    fn = '../' + v['file']
    if (os.path.isfile(fn)):

        print("    Checking csg hash")
        h = openscad.get_csg_hash(temp_name, v['call'], [fn]);
        os.remove(temp_name);

        hashchanged = ('hash' in v and h != v['hash']) or (not 'hash' in v)

        # update hash in json
        v['hash'] = h

        # STL
        print("    STL Parts")
        for part in v['children']:
            if type(part) is DictType and part['type'] == 'part':
                stlpath = target_dir + '/' + openscad.stl_filename(part['title'])
                if hashchanged or (not os.path.isfile(stlpath)):
                    print("      Rendering STL...")
                    openscad.render_stl(temp_name, stlpath, part['call'], [fn])
                else:
                    print("      STL up to date")

        # Views
        print("    Views")
        for view in v['children']:
            if type(view) is DictType and view['type'] == 'view':
                print("      "+view['title'])

                render_view(v['title'], v['call'], view_dir, view, hashchanged, h, [fn], False)

                png_name = view_dir + '/' + view_filename(v['title'] + '_' + view['title'])
                view['png_name'] = png_name


        node = add_vitamin(v, dom)


    else:
        print("    Error: scad file not found: "+v['file'])
def compile_vitamin(v):

    #
    # Make the target directories
    #
    if not os.path.isdir(config.paths['vitaminsstl']):
        os.makedirs(config.paths['vitaminsstl'])

    if not os.path.isdir(config.paths['vitaminsimages2']):
        os.makedirs(config.paths['vitaminsimages2'])

    # Compile
    print("  " + v['title'])
    fn = os.path.join('..', v['file'])
    if (os.path.isfile(fn)):

        print("    Checking csg hash")
        h = openscad.get_csg_hash(config.paths['tempscad'], v['call'])
        os.remove(config.paths['tempscad'])

        hashchanged = ('hash' in v and h != v['hash']) or (not 'hash' in v)

        # update hash in json
        v['hash'] = h

        # STL
        print("    STL Parts")
        if 'parts' in v:
            for part in v['parts']:
                stlpath = os.path.join(config.paths['vitaminsstl'],
                                       openscad.stl_filename(part['title']))
                if hashchanged or (not os.path.isfile(stlpath)):
                    print("      Rendering STL...")
                    openscad.render_stl(config.paths['tempscad'], stlpath,
                                        part['call'])
                else:
                    print("      STL up to date")

        # Views
        print("    Views")
        for view in v['views']:
            print("      " + view['title'])

            render_view(v['title'], v['call'], config.paths['vitaminsimages2'],
                        view, hashchanged, h)

    else:
        print("    Error: scad file not found: " + v['file'])
def compile_vitamin(v, dom):

    #
    # Make the target directories
    #
    if not os.path.isdir(config.paths['vitaminsimages']):
        os.makedirs(config.paths['vitaminsimages'])

    # Compile
    print("  " + v['title'])
    fn = '../' + v['file']
    if (os.path.isfile(fn)):

        print("    Checking csg hash")
        h = openscad.get_csg_hash(config.paths['tempscad'], v['call'], [fn])
        os.remove(config.paths['tempscad'])

        hashchanged = ('hash' in v and h != v['hash']) or (not 'hash' in v)

        # update hash in json
        v['hash'] = h

        # Views
        print("    Views")
        for view in v['children']:
            if isinstance(view, dict) and view['type'] == 'view':
                print("      " + view['title'])

                render_view(v['title'],
                            v['call'],
                            config.paths['vitaminsimages'],
                            view,
                            hashchanged,
                            h, [fn],
                            False,
                            useVitaminSTL=False)

                png_name = os.path.join(
                    config.paths['vitaminsimages'],
                    view_filename(v['title'] + '_' + view['title']))
                view['png_name'] = png_name

        node = add_vitamin(v, dom)

    else:
        print("    Error: scad file not found: " + v['file'])
Пример #5
0
def printed():
    print("Printed Parts")
    print("-------------")

    temp_name = "temp.scad"

    #
    # Make the target directories
    #
    target_dir = "../printedparts/stl"
    if not os.path.isdir(target_dir):
        os.makedirs(target_dir)

    # store a list of valid STLs to aid cleanup
    stlList = []

    view_dir = "../printedparts/images"
    if not os.path.isdir(view_dir):
        os.makedirs(view_dir)

    # load hardware.json
    jf = open("hardware.json", "r")
    jso = json.load(jf)
    jf.close()

    # for each machine
    for m in jso:
        if type(m) is DictType and m['type'] == 'machine':
            print(m['title'])

            pl = m['printed']

            for p in pl:
                print("  " + p['title'])
                fn = '../' + p['file']
                if (os.path.isfile(fn)):

                    stlpath = os.path.join(target_dir,
                                           openscad.stl_filename(p['title']))
                    md5path = os.path.join(
                        target_dir,
                        openscad.stl_filename(p['title']) + '.md5')

                    print("    Checking csg hash")
                    # Get csg hash
                    h = openscad.get_csg_hash(temp_name, p['call'])
                    os.remove(temp_name)

                    # Get old csg hash
                    oldh = ""
                    if os.path.isfile(md5path):
                        with open(md5path, 'r') as f:
                            oldh = f.read()

                    hashchanged = h != oldh

                    # update hash in json
                    p['hash'] = h

                    # save new hash
                    with open(md5path, 'w') as f:
                        f.write(h)

                    # STL
                    print("    STL")
                    if hashchanged or (not os.path.isfile(stlpath)):
                        print("      Rendering STL...")
                        info = openscad.render_stl(temp_name, stlpath,
                                                   p['call'])
                        jsontools.json_merge_missing_keys(p, info)

                    # Slice for weight and volume
                    print("    Slice")
                    if hashchanged or ('plasticWeight' not in p):
                        # Slice part and track volume of plastic required
                        # Estimate KG of plastic from density range: 1.23-1.25 g/cm3
                        plasticInfo = slic3r.calc_plastic_required(stlpath)
                        jsontools.json_merge_missing_keys(p, plasticInfo)

                    else:
                        print("      GCode up to date")

                    print("    views")
                    # Views
                    for view in p['views']:
                        print("      " + view['title'])

                        render_view(p['title'], p['call'], view_dir, view,
                                    hashchanged, h)

                    # Add to stlList
                    stlList.append(stlpath)
                    stlList.append(md5path)

                else:
                    print("    Error: scad file not found: " + p['file'])

    # Save changes to json
    with open('hardware.json', 'w') as f:
        f.write(
            json.dumps(jso, sort_keys=False, indent=4, separators=(',', ': ')))

    # clean-up orphaned stls and checksums
    print "Checking for outdated STLs..."
    for f in os.listdir(target_dir):
        fp = os.path.join(target_dir, f)
        try:
            if os.path.isfile(fp) and (fp not in stlList):
                print "Removing: " + fp
                os.remove(fp)
        except Exception, e:
            print e
Пример #6
0
def cut():
    print("Cut Parts")
    print("---------")

    temp_name = "temp.scad"

    # load hardware.json
    jf = open("hardware.json", "r")
    jso = json.load(jf)
    jf.close()

    # for each machine
    for m in jso:
        if type(m) is DictType and m['type'] == 'machine':
            print(m['title'])

            al = m['cut']

            # make target directory
            view_dir = "../cutparts/images"
            if not os.path.isdir(view_dir):
                os.makedirs(view_dir)

            for a in al:
                print("  " + a['title'])
                fn = '../' + a['file']
                if (os.path.isfile(fn)):

                    print("    Checking csg hash")
                    h = openscad.get_csg_hash(temp_name, a['call'])
                    os.remove(temp_name)

                    hashchanged = ('hash' in a
                                   and h != a['hash']) or (not 'hash' in a)

                    # update hash in json
                    a['hash'] = h

                    # Generate completed part file
                    f = open(temp_name, "w")
                    f.write("include <../config/config.scad>\n")
                    f.write("DebugConnectors = false;\n")
                    f.write("DebugCoordinateFrames = false;\n")
                    f.write(a['completeCall'] + ";\n")
                    f.close()

                    # Completed views
                    for view in a['views']:
                        render_view_using_file(a['title'], temp_name, view_dir,
                                               view, hashchanged, h)

                    # Fabrication Steps
                    for step in a['steps']:
                        # Generate step file
                        f = open(temp_name, "w")
                        f.write("include <../config/config.scad>\n")
                        f.write("DebugConnectors = false;\n")
                        f.write("DebugCoordinateFrames = false;\n")
                        f.write("$Explode = true;\n")
                        f.write("$ShowStep = " + str(step['num']) + ";\n")
                        f.write(a['call'] + ";\n")
                        f.close()

                        # Views
                        print("      Step " + str(step['num']))
                        for view in step['views']:
                            render_view_using_file(
                                a['title'] + '_step' + str(step['num']),
                                temp_name, view_dir, view, hashchanged, h)

                else:
                    print("    Error: scad file not found: " + a['file'])

    # Save changes to json
    with open('hardware.json', 'w') as f:
        f.write(
            json.dumps(jso, sort_keys=False, indent=4, separators=(',', ': ')))

    return 0
Пример #7
0
def assemblies():
    print("Assemblies")
    print("----------")

    temp_name =  "temp.scad"

    # load hardware.json
    jf = open("hardware.json","r")
    jso = json.load(jf)
    jf.close()

    # for each machine
    for m in jso:
        if type(m) is DictType and m['type'] == 'machine':
            print(m['title'])

            al = m['assemblies']

            # make target directory
            view_dir = "../assemblies/"+machine_dir(m['title'])
            if not os.path.isdir(view_dir):
                os.makedirs(view_dir)

            # for each assembly
            for a in al:
                print("  "+a['title'])
                fn = '../' + a['file']
                if (os.path.isfile(fn)):

                    print("    Checking csg hash")
                    h = openscad.get_csg_hash(temp_name, a['call']);
                    os.remove(temp_name);

                    hashchanged = ('hash' in a and h != a['hash']) or (not 'hash' in a)

                    # update hash in json
                    a['hash'] = h

                    # Steps
                    for step in a['steps']:
                        # Generate step file
                        f = open(temp_name, "w")
                        f.write("include <../config/config.scad>\n")
                        f.write("DebugConnectors = false;\n");
                        f.write("DebugCoordinateFrames = false;\n");
                        f.write("UseSTL=true;\n");
                        f.write("$Explode = true;\n");
                        f.write("$ShowStep = "+ str(step['num']) +";\n");
                        f.write(a['call'] + ";\n");
                        f.close()

                        # Views
                        print("      Step "+str(step['num']))
                        for view in step['views']:
                            render_view_using_file(a['title']+'_step'+str(step['num']), temp_name, view_dir, view, hashchanged, h)

                    # for each animation
                    for anim in a['animations']:
                        print("    Animation: "+anim['title']+", framesPerStep="+str(anim['framesPerStep']))

                        animateAssembly(m['title'], a['title'], anim['title'], anim['framesPerStep'])

                else:
                    print("    Error: scad file not found: "+a['file'])


    # Save changes to json
    with open('hardware.json', 'w') as f:
        f.write(json.dumps(jso, sort_keys=False, indent=4, separators=(',', ': ')))

    return 0
Пример #8
0
def animateAssembly(mname, aname, prefix, framesPerStep):
    print("Animate Assembly")
    print("----------------")

    print("NB: Make sure you've run parse.py first!")
    print("")

    temp_name = "temp.scad"

    # load hardware.json
    jf = open("hardware.json", "r")
    jso = json.load(jf)
    jf.close()

    # locate required machine
    for m in jso:
        if type(m) is DictType and m['type'] == 'machine' and m[
                'title'] == mname:
            print("Found machine: " + m['title'])

            al = m['assemblies']

            # make target directory
            view_dir = "../assemblies/" + machine_dir(m['title'])
            if not os.path.isdir(view_dir):
                os.makedirs(view_dir)

            # locate required assembly
            for a in al:
                if a['title'] == aname:
                    print("Found assembly: " + a['title'])
                    fn = '../' + a['file']
                    if (os.path.isfile(fn)):

                        print("  Checking csg hash")
                        h = openscad.get_csg_hash(temp_name, a['call'])
                        os.remove(temp_name)

                        hashchanged = ('hash' in a
                                       and h != a['hash']) or (not 'hash' in a)

                        numSteps = 0
                        frameNum = 0
                        lfn = ""  # last frame filename

                        # Calc number of steps, and grab first view
                        view = {
                            'size': [400, 300],
                            'dist': 140,
                            'rotate': [0, 0, 0],
                            'translate': [0, 0, 0],
                            'title': 'view'
                        }
                        firstView = True
                        for step in a['steps']:
                            print("Step: " + str(step['num']))

                            # generate a transition move?
                            if len(step['views']) > 0:
                                # see if new view is diff from current
                                nv = step['views'][0]

                                if firstView:
                                    view['size'] = nv['size']

                                if ((nv['size'] != view['size']) or
                                    (nv['dist'] != view['dist']) or
                                    (nv['rotate'] != view['rotate']) or
                                    (nv['translate'] !=
                                     view['translate'])) and (not firstView):
                                    print("Generating transition move...")

                                    # prep tween view
                                    tv = copy.deepcopy(view)

                                    # iterate over frames
                                    for frame in range(0, framesPerStep):
                                        t = easeInOut(frame /
                                                      (framesPerStep - 1.0))
                                        # show previous step during transition
                                        ShowStep = step['num'] - 1

                                        # tween between view and nv
                                        tv['dist'] = mapRange(
                                            t, 0, 1.0, view['dist'],
                                            nv['dist'])
                                        tv['translate'][0] = mapRange(
                                            t, 0, 1.0, view['translate'][0],
                                            nv['translate'][0])
                                        tv['translate'][1] = mapRange(
                                            t, 0, 1.0, view['translate'][1],
                                            nv['translate'][1])
                                        tv['translate'][2] = mapRange(
                                            t, 0, 1.0, view['translate'][2],
                                            nv['translate'][2])
                                        tv['rotate'][0] = mapRange(
                                            t, 0, 1.0, view['rotate'][0],
                                            nv['rotate'][0])
                                        tv['rotate'][1] = mapRange(
                                            t, 0, 1.0, view['rotate'][1],
                                            nv['rotate'][1])
                                        tv['rotate'][2] = mapRange(
                                            t, 0, 1.0, view['rotate'][2],
                                            nv['rotate'][2])

                                        #print("t: "+str(t) +", s: "+str(ShowStep)+", a:"+str(AnimateExplodeT))

                                        # Generate step file
                                        f = open(temp_name, "w")
                                        f.write(
                                            "include <../config/config.scad>\n"
                                        )
                                        f.write("DebugConnectors = false;\n")
                                        f.write(
                                            "DebugCoordinateFrames = false;\n")
                                        f.write("$Explode = false;\n")
                                        f.write("UseSTL=true;\n")
                                        f.write("$AnimateExplode = false;\n")
                                        f.write("$ShowStep = " +
                                                str(ShowStep) + ";\n")
                                        f.write(a['call'] + ";\n")
                                        f.close()

                                        # Views
                                        views.PolishTransparentBackground = False
                                        views.PolishCrop = False
                                        lfn = view_dir + "/" + prefix + format(
                                            frameNum, '03'
                                        ) + "_" + view['title'] + ".png"
                                        views.render_view_using_file(
                                            prefix + format(frameNum, '03'),
                                            temp_name, view_dir, tv,
                                            hashchanged, h)
                                        frameNum = frameNum + 1

                                view['dist'] = nv['dist']
                                view['translate'] = nv['translate']
                                view['rotate'] = nv['rotate']
                                firstView = False

                            # iterate over frames to animate the assembly step
                            for frame in range(0, framesPerStep):

                                t = easeInOut(frame / (framesPerStep - 1.0))
                                ShowStep = step['num']
                                AnimateExplodeT = t

                                #print("t: "+str(t) +", s: "+str(ShowStep)+", a:"+str(AnimateExplodeT))

                                # Generate step file
                                f = open(temp_name, "w")
                                f.write("include <../config/config.scad>\n")
                                f.write("DebugConnectors = false;\n")
                                f.write("DebugCoordinateFrames = false;\n")
                                f.write("$Explode = true;\n")
                                f.write("UseSTL=true;\n")
                                f.write("$AnimateExplode = true;\n")
                                f.write("$ShowStep = " + str(ShowStep) + ";\n")
                                f.write("$AnimateExplodeT = " +
                                        str(AnimateExplodeT) + ";\n")
                                f.write(a['call'] + ";\n")
                                f.close()

                                # Views
                                views.PolishTransparentBackground = False
                                views.PolishCrop = False
                                fn = view_dir + "/" + prefix + format(
                                    frameNum,
                                    '03') + "_" + view['title'] + ".png"
                                print("Rendering: " + fn)
                                views.render_view_using_file(
                                    prefix + format(frameNum, '03'), temp_name,
                                    view_dir, view, hashchanged, h)
                                frameNum = frameNum + 1

                                if frame == 0:
                                    # load previous image for cross-fade
                                    if lfn != "":
                                        imgPrev = Image.open(lfn)

                                    # Annotate and extend first frame with step info
                                    img = Image.open(fn)
                                    imgNew = img.copy()

                                    # go back one to overwrite first frame
                                    frameNum = frameNum - 1

                                    # extend a bit
                                    for extra in range(0, 2 * framesPerStep):
                                        if lfn != "":
                                            alpha = extra / (1.0 *
                                                             framesPerStep)
                                            if (alpha > 1.0):
                                                alpha = 1.0
                                            print("Blending: " + str(alpha) +
                                                  ", " + str(frameNum))
                                            imgNew = Image.blend(
                                                imgPrev, img, alpha)

                                        draw = ImageDraw.Draw(imgNew, "RGBA")
                                        font = ImageFont.truetype(
                                            "Tahoma.ttf", 12)
                                        draw.rectangle(
                                            (0, 0, view['size'][0], 30),
                                            fill=(240, 240, 240))
                                        margin = 30
                                        offset = 10
                                        for line in textwrap.wrap(
                                                step['desc'],
                                                width=((view['size'][0] -
                                                        2 * margin) /
                                                       (draw.textsize(
                                                           "abcdefghij")[0] /
                                                        10))):
                                            draw.rectangle(
                                                (0, offset, view['size'][0],
                                                 offset + 20),
                                                fill=(240, 240, 240))
                                            draw.rectangle(
                                                (0, offset + 20,
                                                 view['size'][0], offset + 22),
                                                fill=(200, 200, 200))
                                            draw.text((margin, offset),
                                                      line, (0, 0, 0),
                                                      font=font)
                                            offset += font.getsize(line)[1]

                                        # draw step number
                                        draw.ellipse((5, 5, 25, 25),
                                                     fill=(255, 140, 50),
                                                     outline=(255, 140, 50))
                                        draw.text((11, 8),
                                                  str(step['num']),
                                                  (255, 255, 255),
                                                  font=font)

                                        fn = view_dir + "/" + prefix + format(
                                            frameNum, '03'
                                        ) + "_" + view['title'] + ".png"
                                        frameNum = frameNum + 1
                                        print("Saving: " + fn)
                                        imgNew.save(fn)

                                lfn = fn

                        # final turntable, using last view as starting point
                        print("Generating final turntable")
                        tv = copy.deepcopy(view)
                        for frame in range(0, framesPerStep * 2):
                            t = easeInOut(frame /
                                          ((framesPerStep * 2.0) - 1.0))
                            ShowStep = 100

                            # tween between view and nv
                            r = mapRange(t, 0, 1.0, 0, 360)
                            tv['rotate'][2] = view['rotate'][2] + r
                            if tv['rotate'][2] > 360:
                                tv['rotate'][2] = tv['rotate'][2] - 360

                            #print("t: "+str(t) +", r:"+str(r))

                            # Generate step file
                            f = open(temp_name, "w")
                            f.write("include <../config/config.scad>\n")
                            f.write("DebugConnectors = false;\n")
                            f.write("DebugCoordinateFrames = false;\n")
                            f.write("UseSTL=true;\n")
                            f.write("$Explode = false;\n")
                            f.write("$ShowStep = " + str(ShowStep) + ";\n")
                            f.write(a['call'] + ";\n")
                            f.close()

                            # Views
                            views.PolishTransparentBackground = False
                            views.PolishCrop = False
                            views.render_view_using_file(
                                prefix + format(frameNum, '03'), temp_name,
                                view_dir, tv, hashchanged, h)
                            frameNum = frameNum + 1

                        numFrames = frameNum

                        # build video
                        cmd = "ffmpeg -r " + str(
                            framesPerStep
                        ) + " -y -i " + view_dir + "/" + prefix + "%03d_" + view[
                            'title'] + ".png -vcodec libx264 -pix_fmt yuv420p " + view_dir + "/" + prefix + ".mp4"
                        print("Encoding video with: " + cmd)
                        os.system(cmd)

                        # build animated gif
                        cmd = "convert " + view_dir + "/" + prefix + ".mp4 " + view_dir + "/" + prefix + ".gif"
                        print("Creating GIF with: " + cmd)
                        os.system(cmd)

                        # clean up temporary images
                        #for frame in range(0, numFrames):
                        #    os.remove(view_dir + "/" +prefix + format(frame, '03') + "_" +view['title']+".png");

                        print("Done")

                        try:
                            if sys.platform == "darwin":
                                subprocess.check_output([
                                    'osascript', '-e',
                                    'display notification "Animation Complete" with title "Animation"'
                                ])
                        except:
                            print("Exception running osascript")

                    else:
                        print("    Error: scad file not found: " + a['file'])

    return 0
Пример #9
0
def vitamins():
    print("Vitamins")
    print("--------")

    temp_name = "temp.scad"

    #
    # Make the target directories
    #
    target_dir = "../vitamins/stl"
    if not os.path.isdir(target_dir):
        os.makedirs(target_dir)

    view_dir = "../vitamins/images"
    if not os.path.isdir(view_dir):
        os.makedirs(view_dir)

    # load hardware.json
    jf = open("hardware.json", "r")
    jso = json.load(jf)
    jf.close()

    # for each machine
    for m in jso:
        if type(m) is DictType and m['type'] == 'machine':
            print(m['title'])

            vl = m['vitamins']

            for v in vl:
                print("  " + v['title'])
                fn = '../' + v['file']
                if (os.path.isfile(fn)):

                    print("    Checking csg hash")
                    h = openscad.get_csg_hash(temp_name, v['call'])
                    os.remove(temp_name)

                    hashchanged = ('hash' in v
                                   and h != v['hash']) or (not 'hash' in v)

                    # update hash in json
                    v['hash'] = h

                    # STL
                    print("    STL Parts")
                    if 'parts' in v:
                        for part in v['parts']:
                            stlpath = target_dir + '/' + openscad.stl_filename(
                                part['title'])
                            if hashchanged or (not os.path.isfile(stlpath)):
                                print("      Rendering STL...")
                                openscad.render_stl(temp_name, stlpath,
                                                    part['call'])
                            else:
                                print("      STL up to date")

                    # Views
                    print("    Views")
                    for view in v['views']:
                        print("      " + view['title'])

                        render_view(v['title'], v['call'], view_dir, view,
                                    hashchanged, h)

                else:
                    print("    Error: scad file not found: " + v['file'])

    # Save changes to json
    with open('hardware.json', 'w') as f:
        f.write(
            json.dumps(jso, sort_keys=False, indent=4, separators=(',', ': ')))

    return 0
Пример #10
0
def printed():
    print("Printed Parts")
    print("-------------")

    temp_name =  "temp.scad"

    #
    # Make the target directories
    #
    target_dir = "../printedparts/stl"
    if not os.path.isdir(target_dir):
        os.makedirs(target_dir)

    view_dir = "../printedparts/images"
    if not os.path.isdir(view_dir):
        os.makedirs(view_dir)

    # load hardware.json
    jf = open("hardware.json","r")
    jso = json.load(jf)
    jf.close()
    
    # for each machine
    for m in jso:
        if type(m) is DictType and m['type'] == 'machine':
            print(m['title'])
            
            pl = m['printed']
            
            for p in pl:
                print("  "+p['title'])
                fn = '../' + p['file']
                if (os.path.isfile(fn)):
                    
                    stlpath = target_dir + '/' + openscad.stl_filename(p['title'])
                    md5path = target_dir + '/' + openscad.stl_filename(p['title']) + '.md5'
                
                    print("    Checking csg hash")
                    # Get csg hash
                    h = openscad.get_csg_hash(temp_name, p['call']);
                    os.remove(temp_name);
                    
                    # Get old csg hash
                    oldh = ""
                    if os.path.isfile(md5path):
                        with open(md5path,'r') as f:
                            oldh = f.read()
                    
                    hashchanged = h != oldh
                    
                    # update hash in json
                    p['hash'] = h
                    
                    # save new hash
                    with open(md5path,'w') as f:
                        f.write(h)
                        
                    # STL
                    print("    STL")
                    if hashchanged or (not os.path.isfile(stlpath)):
                        print("      Rendering STL...")
                        info = openscad.render_stl(temp_name, stlpath, p['call'])
                        
                        jsontools.json_merge_missing_keys(p, info) 
                        
                    else:
                        print("      STL up to date")
                    
                    print("    views")
                    # Views
                    for view in p['views']:
                        print("      "+view['title'])
                        
                        render_view(p['title'], p['call'], view_dir, view, hashchanged, h)
                        
                else:
                    print("    Error: scad file not found: "+p['file'])
                        
            
    # Save changes to json
    with open('hardware.json', 'w') as f:
        f.write(json.dumps(jso, sort_keys=False, indent=4, separators=(',', ': ')))
        
    return 0
Пример #11
0
def assemblies():
    print("Assemblies")
    print("----------")

    # load hardware.json
    jf = open(config.paths['json'], "r")
    jso = json.load(jf)
    jf.close()

    # for each machine
    for m in jso:
        if type(m) is DictType and m['type'] == 'machine':
            print(m['title'])

            al = m['assemblies']

            # make target directory
            view_dir = config.paths['assemblies'] + "/" + machine_dir(
                m['title'])
            if not os.path.isdir(view_dir):
                os.makedirs(view_dir)

            # for each assembly
            for a in al:
                print("  " + a['title'])
                fn = config.paths['root'] + a['file']
                if (os.path.isfile(fn)):

                    print("    Checking csg hash")
                    h = openscad.get_csg_hash(config.paths['tempscad'],
                                              a['call'])
                    os.remove(config.paths['tempscad'])

                    hashchanged = ('hash' in a
                                   and h != a['hash']) or (not 'hash' in a)

                    # update hash in json
                    a['hash'] = h

                    # Steps
                    for step in a['steps']:
                        # Generate step file
                        f = open(config.paths['tempscad'], "w")
                        f.write("include <../config/config.scad>\n")
                        f.write("DebugConnectors = false;\n")
                        f.write("DebugCoordinateFrames = false;\n")
                        f.write("UseSTL=true;\n")
                        f.write("$Explode = true;\n")
                        f.write("$ShowStep = " + str(step['num']) + ";\n")
                        f.write(a['call'] + ";\n")
                        f.close()

                        # Views
                        print("      Step " + str(step['num']))
                        for view in step['views']:
                            render_view_using_file(
                                a['title'] + '_step' + str(step['num']),
                                config.paths['tempscad'], view_dir, view,
                                hashchanged, h)

                    # for each animation
                    for anim in a['animations']:
                        print("    Animation: " + anim['title'] +
                              ", framesPerStep=" + str(anim['framesPerStep']))

                        animateAssembly(m['title'], a['title'], anim['title'],
                                        anim['framesPerStep'])

                else:
                    print("    Error: scad file not found: " + a['file'])

    # Save changes to json
    with open(config.paths['json'], 'w') as f:
        f.write(
            json.dumps(jso, sort_keys=False, indent=4, separators=(',', ': ')))

    return 0
Пример #12
0
def printed():
    print("Printed Parts")
    print("-------------")

    temp_name = "temp.scad"

    #
    # Make the target directories
    #
    target_dir = "../printedparts/stl"
    if not os.path.isdir(target_dir):
        os.makedirs(target_dir)

    view_dir = "../printedparts/images"
    if not os.path.isdir(view_dir):
        os.makedirs(view_dir)

    # load hardware.json
    jf = open("hardware.json", "r")
    jso = json.load(jf)
    jf.close()

    # for each machine
    for m in jso:
        if type(m) is DictType and m['type'] == 'machine':
            print(m['title'])

            pl = m['printed']

            for p in pl:
                print("  " + p['title'])
                fn = '../' + p['file']
                if (os.path.isfile(fn)):

                    stlpath = target_dir + '/' + openscad.stl_filename(
                        p['title'])
                    md5path = target_dir + '/' + openscad.stl_filename(
                        p['title']) + '.md5'

                    print("    Checking csg hash")
                    # Get csg hash
                    h = openscad.get_csg_hash(temp_name, p['call'])
                    os.remove(temp_name)

                    # Get old csg hash
                    oldh = ""
                    if os.path.isfile(md5path):
                        with open(md5path, 'r') as f:
                            oldh = f.read()

                    hashchanged = h != oldh

                    # update hash in json
                    p['hash'] = h

                    # save new hash
                    with open(md5path, 'w') as f:
                        f.write(h)

                    # STL
                    print("    STL")
                    if hashchanged or (not os.path.isfile(stlpath)):
                        print("      Rendering STL...")
                        info = openscad.render_stl(temp_name, stlpath,
                                                   p['call'])

                        jsontools.json_merge_missing_keys(p, info)

                    else:
                        print("      STL up to date")

                    print("    views")
                    # Views
                    for view in p['views']:
                        print("      " + view['title'])

                        render_view(p['title'], p['call'], view_dir, view,
                                    hashchanged, h)

                else:
                    print("    Error: scad file not found: " + p['file'])

    # Save changes to json
    with open('hardware.json', 'w') as f:
        f.write(
            json.dumps(jso, sort_keys=False, indent=4, separators=(',', ': ')))

    return 0
Пример #13
0
def vitamins():
    print("Vitamins")
    print("--------")

    temp_name =  "temp.scad"

    #
    # Make the target directories
    #
    target_dir = "../vitamins/stl"
    if not os.path.isdir(target_dir):
        os.makedirs(target_dir)

    view_dir = "../vitamins/images"
    if not os.path.isdir(view_dir):
        os.makedirs(view_dir)

    # load hardware.json
    jf = open("hardware.json","r")
    jso = json.load(jf)
    jf.close()
    
    # for each machine
    for m in jso:
        if type(m) is DictType and m['type'] == 'machine':
            print(m['title'])
            
            vl = m['vitamins']
            
            for v in vl:
                print("  "+v['title'])
                fn = '../' + v['file']
                if (os.path.isfile(fn)):
                
                    print("    Checking csg hash")
                    h = openscad.get_csg_hash(temp_name, v['call']);
                    os.remove(temp_name);
                    
                    hashchanged = ('hash' in v and h != v['hash']) or (not 'hash' in v)
                    
                    # update hash in json
                    v['hash'] = h
                        
                    # STL
                    print("    STL Parts")
                    if 'parts' in v:
                        for part in v['parts']:
                            stlpath = target_dir + '/' + openscad.stl_filename(part['title'])
                            if hashchanged or (not os.path.isfile(stlpath)):
                                print("      Rendering STL...")
                                openscad.render_stl(temp_name, stlpath, part['call'])
                            else:
                                print("      STL up to date")
                    
                    # Views
                    print("    Views")
                    for view in v['views']:
                        print("      "+view['title'])
                        
                        render_view(v['title'], v['call'], view_dir, view, hashchanged, h)
                        
                        
                else:
                    print("    Error: scad file not found: "+v['file'])
                        
            
    # Save changes to json
    with open('hardware.json', 'w') as f:
        f.write(json.dumps(jso, sort_keys=False, indent=4, separators=(',', ': ')))
        
    return 0
Пример #14
0
def printed():
    print("Printed Parts")
    print("-------------")

    temp_name =  "temp.scad"

    #
    # Make the target directories
    #
    target_dir = "../printedparts/stl"
    if not os.path.isdir(target_dir):
        os.makedirs(target_dir)

    # store a list of valid STLs to aid cleanup
    stlList = []

    view_dir = "../printedparts/images"
    if not os.path.isdir(view_dir):
        os.makedirs(view_dir)

    # load hardware.json
    jf = open("hardware.json","r")
    jso = json.load(jf)
    jf.close()

    # for each machine
    for m in jso:
        if type(m) is DictType and m['type'] == 'machine':
            print(m['title'])

            pl = m['printed']

            for p in pl:
                print("  "+p['title'])
                fn = '../' + p['file']
                if (os.path.isfile(fn)):

                    stlpath = os.path.join(target_dir, openscad.stl_filename(p['title']))
                    md5path = os.path.join(target_dir, openscad.stl_filename(p['title']) + '.md5')

                    print("    Checking csg hash")
                    # Get csg hash
                    h = openscad.get_csg_hash(temp_name, p['call']);
                    os.remove(temp_name);

                    # Get old csg hash
                    oldh = ""
                    if os.path.isfile(md5path):
                        with open(md5path,'r') as f:
                            oldh = f.read()

                    hashchanged = h != oldh

                    # update hash in json
                    p['hash'] = h

                    # save new hash
                    with open(md5path,'w') as f:
                        f.write(h)

                    # STL
                    print("    STL")
                    if hashchanged or (not os.path.isfile(stlpath)):
                        print("      Rendering STL...")
                        info = openscad.render_stl(temp_name, stlpath, p['call'])
                        jsontools.json_merge_missing_keys(p, info)

                    # Slice for weight and volume
                    print("    Slice")
                    if hashchanged or ('plasticWeight' not in p):
                        # Slice part and track volume of plastic required
                        # Estimate KG of plastic from density range: 1.23-1.25 g/cm3
                        plasticInfo = slic3r.calc_plastic_required(stlpath)
                        jsontools.json_merge_missing_keys(p, plasticInfo)

                    else:
                        print("      GCode up to date")

                    print("    views")
                    # Views
                    for view in p['views']:
                        print("      "+view['title'])

                        render_view(p['title'], p['call'], view_dir, view, hashchanged, h)

                    # Add to stlList
                    stlList.append(stlpath)
                    stlList.append(md5path)

                else:
                    print("    Error: scad file not found: "+p['file'])


    # Save changes to json
    with open('hardware.json', 'w') as f:
        f.write(json.dumps(jso, sort_keys=False, indent=4, separators=(',', ': ')))

    # clean-up orphaned stls and checksums
    print "Checking for outdated STLs..."
    for f in os.listdir(target_dir):
        fp = os.path.join(target_dir, f)
        try:
            if os.path.isfile(fp) and (fp not in stlList):
                print "Removing: "+fp
                os.remove(fp)
        except Exception, e:
            print e