def render_stl(scadname, stlpath, obj_call): f = open(scadname, "w") f.write("include <../config/config.scad>\n") f.write("UseSTL=false;\n") f.write("UseVitaminSTL=false;\n") f.write("DebugConnectors=false;\n") f.write("DebugCoordinateFrames=false;\n") f.write(obj_call + ";\n") f.close() run("-o", stlpath, scadname) return canonicalise(stlpath)
def render_stl(scadname, stlpath, obj_call): f = open(scadname, "w") f.write("include <../config/config.scad>\n") f.write("UseSTL=false;\n"); f.write("UseVitaminSTL=false;\n"); f.write("DebugConnectors=false;\n"); f.write("DebugCoordinateFrames=false;\n"); f.write(obj_call + ";\n"); f.close() run("-o", stlpath, scadname) return canonicalise(stlpath)
def stls(machine, parts=None): # # Make the target directory # target_dir = machine + "/stls" if os.path.isdir(target_dir): if not parts: shutil.rmtree( target_dir) #if making the BOM clear the directory first os.makedirs(target_dir) else: os.makedirs(target_dir) # # Set the target machine # f = open(source_dir + "/conf/machine.scad", "wt") f.write("include <%s_config.scad>\n" % machine) f.close() # # Decide which files to make # if parts: targets = list( parts) #copy the list so we dont modify the list passed in else: targets = bom_to_stls(machine) # # Find all the scad files # used = [] for filename in os.listdir(source_dir): if filename[-5:] == ".scad": # # find any modules ending in _stl # for line in open(source_dir + "/" + filename, "r").readlines(): words = line.split() if (len(words) and words[0] == "module"): module = words[1].split('(')[0] stl = module.replace("_stl", ".stl") if stl in targets: # # make a file to use the module # stl_maker_name = source_dir + "/stl.scad" f = open(stl_maker_name, "w") f.write("use <%s>\n" % filename) f.write("%s();\n" % module) f.close() # # Run openscad on the created file # stl_name = target_dir + "/" + module[:-4] + ".stl" openscad.run("-D$bom=1", "-o", stl_name, stl_maker_name) c14n_stl.canonicalise(stl_name) targets.remove(stl) # # Add the files on the BOM to the used list for plates.py # for line in open("openscad.log"): if line[:7] == 'ECHO: "' and line[-6:] == '.stl"\n': used.append(line[7:-2]) # # List the ones we didn't find # for module in targets: print("Could not find", module) return used
def make_parts(target, part_type, parts=None): # # Make the target directory # top_dir = set_config(target) target_dir = top_dir + part_type + 's' deps_dir = top_dir + "deps" if not os.path.isdir(target_dir): os.makedirs(target_dir) if not os.path.isdir(deps_dir): os.makedirs(deps_dir) times.read_times(target_dir) # # Decide which files to make # if parts: targets = list( parts) #copy the list so we dont modify the list passed in else: targets = bom_to_parts(target_dir, part_type) for file in os.listdir(target_dir): if file.endswith('.' + part_type): if not file in targets: print("Removing %s" % file) os.remove(target_dir + '/' + file) # # Read existing STL bounds # if part_type == 'stl': bounds_fname = target_dir + '/bounds.json' try: with open(bounds_fname) as json_file: bounds_map = json.load(json_file) except: bounds_map = {} # # Find all the scad files # lib_dir = os.environ['OPENSCADPATH'] + '/NopSCADlib/printed' module_suffix = '_dxf' if part_type == 'svg' else '_' + part_type for dir in [source_dir, lib_dir]: for filename in os.listdir(dir): if filename[-5:] == ".scad": # # find any modules ending in _<part_type> # with open(dir + "/" + filename, "r") as f: for line in f.readlines(): words = line.split() if (len(words) and words[0] == "module"): module = words[1].split('(')[0] if module.endswith(module_suffix): base_name = module[:-4] part = base_name + '.' + part_type if part in targets: # # make a file to use the module # part_maker_name = part_type + ".scad" with open(part_maker_name, "w") as f: f.write("use <%s/%s>\n" % (dir, filename)) f.write("%s();\n" % module) # # Run openscad on the created file # part_file = target_dir + "/" + part dname = deps_name(deps_dir, filename) changed = check_deps(part_file, dname) changed = times.check_have_time( changed, part) if part_type == 'stl' and not changed and not part in bounds_map: changed = "No bounds" if changed: print(changed) t = time.time() openscad.run("-D$bom=1", "-d", dname, "-o", part_file, part_maker_name) times.add_time(part, t) if part_type == 'stl': bounds = c14n_stl.canonicalise( part_file) bounds_map[part] = bounds targets.remove(part) os.remove(part_maker_name) # # Write new bounds file # if part_type == 'stl': with open(bounds_fname, 'w') as outfile: json.dump(bounds_map, outfile, indent=4) # # List the ones we didn't find # if targets: for part in targets: if part[-4:] != '.' + part_type: print(part, "is not a", part_type, "file") else: print("Could not find a module called", part[:-4] + module_suffix, "to make", part) sys.exit(1) times.print_times()
def stls(machine, parts = None): # # Make the target directory # target_dir = machine + "/stls" if os.path.isdir(target_dir): if not parts: shutil.rmtree(target_dir) #if making the BOM clear the directory first os.makedirs(target_dir) else: os.makedirs(target_dir) # # Set the target machine # f = open("scad/conf/machine.scad","wt") f. write("include <%s_config.scad>\n" % machine); f.close() # # Decide which files to make # if parts: targets = list(parts) #copy the list so we dont modify the list passed in else: targets = bom_to_stls(machine) # # Find all the scad files # used = [] for filename in os.listdir(source_dir): if filename[-5:] == ".scad": # # find any modules ending in _stl # for line in open(source_dir + "/" + filename, "r").readlines(): words = line.split() if(len(words) and words[0] == "module"): module = words[1].split('(')[0] stl = module.replace("_stl", ".stl") if stl in targets: # # make a file to use the module # stl_maker_name = source_dir + "/stl.scad" f = open(stl_maker_name, "w") f.write("use <%s>\n" % filename) f.write("%s();\n" % module); f.close() # # Run openscad on the created file # stl_name = target_dir + "/" + module[:-4] + ".stl" openscad.run("-o", stl_name, stl_maker_name) c14n_stl.canonicalise(stl_name) targets.remove(stl) # # Add the files on the BOM to the used list for plates.py # for line in open("openscad.log"): if line[:7] == 'ECHO: "' and line[-6:] == '.stl"\n': used.append(line[7:-2]) # # List the ones we didn't find # for module in targets: print "Could not find", module return used
def make_parts(target, part_type, parts=None): # # Check list of parts is the correct type # if parts: for p in parts: if not p.endswith('.' + part_type): usage(part_type) # # Make the target directory # top_dir = set_config(target, lambda: usage(part_type)) target_dir = top_dir + part_type + 's' deps_dir = target_dir + "/deps" bom_dir = top_dir + "bom" tmp_dir = mktmpdir(top_dir) if not os.path.isdir(target_dir): os.makedirs(target_dir) if not os.path.isdir(deps_dir): os.makedirs(deps_dir) if os.path.isdir(top_dir + '/deps'): #old location shutil.rmtree(top_dir + '/deps') times.read_times(target_dir) # # Decide which files to make # all_parts = bom_to_parts(bom_dir, part_type) if parts: targets = list( parts) #copy the list so we dont modify the list passed in else: targets = list(all_parts) for file in os.listdir(target_dir): if file.endswith('.' + part_type): if not file in targets: print("Removing %s" % file) os.remove(target_dir + '/' + file) # # Read existing STL bounds # if part_type == 'stl': bounds_fname = target_dir + '/bounds.json' try: with open(bounds_fname) as json_file: bounds_map = json.load(json_file) except: bounds_map = {} # # Find all the scad files # module_suffix = '_dxf' if part_type == 'svg' else '_' + part_type for dir in source_dirs(bom_dir): if targets and os.path.isdir(dir): for filename in os.listdir(dir): if targets and filename[-5:] == ".scad": # # find any modules ending in _<part_type> # with open(dir + "/" + filename, "r") as f: for line in f.readlines(): words = line.split() if (len(words) and words[0] == "module"): module = words[1].split('(')[0] if module.endswith(module_suffix): base_name = module[:-4] part = base_name + '.' + part_type if part in targets: # # Run openscad on the created file # part_file = target_dir + "/" + part dname = deps_name(deps_dir, filename) changed = check_deps(part_file, dname) changed = times.check_have_time( changed, part) if part_type == 'stl' and not changed and not part in bounds_map: changed = "No bounds" if changed: print(changed) # # make a file to use the module # part_maker_name = tmp_dir + '/' + part_type + ".scad" with open(part_maker_name, "w") as f: f.write("use <%s/%s>\n" % (reltmp(dir, target), filename)) f.write("%s();\n" % module) t = time.time() openscad.run( "-D$bom=1", "-d", dname, "-o", part_file, part_maker_name) times.add_time(part, t) if part_type == 'stl': bounds = c14n_stl.canonicalise( part_file) bounds_map[part] = bounds os.remove(part_maker_name) targets.remove(part) # # Write new bounds file # if part_type == 'stl': with open(bounds_fname, 'w') as outfile: json.dump(bounds_map, outfile, indent=4) # # Remove tmp dir # rmtmpdir(tmp_dir) # # List the ones we didn't find # if targets: for part in targets: print("Could not find a module called", part[:-4] + module_suffix, "to make", part) usage(part_type) times.print_times(all_parts)
def plateup(target, part_type, usage = None): # # Make the target directory # top_dir = set_config(target, usage) parts_dir = top_dir + part_type + 's' target_dir = parts_dir + '/' + target_dirs[part_type] source_dir1 = source_dirs[part_type] source_dir2 = top_dir + source_dirs[part_type] # # Loop through source directories # all_used = [] all_sources = [] all_parts = [] read_times = False for dir in [source_dir1, source_dir2]: if not os.path.isdir(dir): continue if not os.path.isdir(target_dir): os.makedirs(target_dir) if not read_times: times.read_times(target_dir) read_times = True # # Make the deps dir # deps_dir = parts_dir + "/deps" if not os.path.isdir(deps_dir): os.makedirs(deps_dir) if os.path.isdir(dir + '/deps'): #old deps shutil.rmtree(dir + '/deps') # # Decide which files to make # sources = [file for file in os.listdir(dir) if file.endswith('.scad')] all_sources += sources # # Run OpenSCAD on the source files to make the targets # target_def = ['-D$target="%s"' % target] if target else [] cwd_def = ['-D$cwd="%s"' % os.getcwd().replace('\\', '/')] for src in sources: src_file = dir + '/' + src part = src[:-4] + part_type all_parts.append(part) part_file = target_dir + '/' + part uses_file = deps_dir + '/' + src[:-4] + 'txt' dname = deps_name(deps_dir, src) oldest = part_file if mtime(part_file) < mtime(uses_file) else uses_file changed = check_deps(oldest, dname) used = [] if changed: print(changed) t = time.time() openscad.run_list(["-D$bom=1"] + target_def + cwd_def + ["-d", dname, "-o", part_file, src_file]) if part_type == 'stl': c14n_stl.canonicalise(part_file) times.add_time(part, t) log_name = 'openscad.log' # # Add the files on the BOM to the used list # with open(log_name) as file: for line in file.readlines(): match = re.match(r'^ECHO: "~(.*?\.' + part_type + r').*"$', line) if match: used.append(match.group(1)) with open(uses_file, "wt") as file: for part in used: print(part, file = file) else: with open(uses_file, "rt") as file: for line in file: used.append(line[:-1]) all_used += used copied = [] if all_sources: # # Copy files that are not included # for file in os.listdir(parts_dir): if file.endswith('.' + part_type) and not file in all_used: src = parts_dir + '/' + file dst = target_dir + '/' + file if mtime(src) > mtime(dst): print("Copying %s to %s" % (src, dst)) shutil.copyfile(src, dst) copied.append(file) # # Remove any cruft # targets = [file[:-4] + part_type for file in all_sources] for file in os.listdir(target_dir): if file.endswith('.' + part_type): if not file in targets and not file in copied: print("Removing %s" % file) os.remove(target_dir + '/' + file) targets = [file[:-4] + 'txt' for file in all_sources] for file in os.listdir(deps_dir): if file.endswith('.' + 'txt'): if not file in targets: print("Removing %s" % file) os.remove(deps_dir + '/' + file) times.print_times(all_parts)
def plateup(target, part_type, usage=None): # # Make the target directory # top_dir = set_config(target, usage) parts_dir = top_dir + part_type + 's' target_dir = parts_dir + '/' + target_dirs[part_type] source_dir = top_dir + source_dirs[part_type] deps_dir = source_dir + "/deps" if not os.path.isdir(source_dir): return if not os.path.isdir(target_dir): os.makedirs(target_dir) if not os.path.isdir(deps_dir): os.makedirs(deps_dir) # # Decide which files to make # sources = [ file for file in os.listdir(source_dir) if file.endswith('.scad') ] # # Run OpenSCAD on the source files to make the targets # used = [] for src in sources: src_file = source_dir + '/' + src part_file = target_dir + '/' + src[:-4] + part_type dname = deps_name(deps_dir, src) changed = check_deps(part_file, dname) if changed: print(changed) openscad.run("-D$bom=1", "-d", dname, "-o", part_file, src_file) if part_type == 'stl': c14n_stl.canonicalise(part_file) log_name = 'openscad.log' else: log_name = 'openscad.echo' openscad.run_silent("-D$bom=1", "-o", log_name, src_file) # # Add the files on the BOM to the used list # with open(log_name) as file: for line in file.readlines(): if line.startswith('ECHO: "~') and line.endswith('.' + part_type + '"\n'): used.append(line[8:-2]) # # Copy file that are not included # copied = [] for file in os.listdir(parts_dir): if file.endswith('.' + part_type) and not file in used: src = parts_dir + '/' + file dst = target_dir + '/' + file if mtime(src) > mtime(dst): print("Copying %s to %s" % (src, dst)) copyfile(src, dst) copied.append(file) # # Remove any cruft # targets = [file[:-4] + part_type for file in sources] for file in os.listdir(target_dir): if file.endswith('.' + part_type): if not file in targets and not file in copied: print("Removing %s" % file) os.remove(target_dir + '/' + file)
def plateup(target, part_type, usage=None): # # Make the target directory # top_dir = set_config(target, usage) parts_dir = top_dir + part_type + 's' target_dir = parts_dir + '/' + target_dirs[part_type] source_dir1 = source_dirs[part_type] source_dir2 = top_dir + source_dirs[part_type] # # Loop through source directories # used = [] all_sources = [] for dir in [source_dir1, source_dir2]: if not os.path.isdir(dir): continue if not os.path.isdir(target_dir): os.makedirs(target_dir) # # Make the deps dir # deps_dir = dir + "/deps" if not os.path.isdir(deps_dir): os.makedirs(deps_dir) # # Decide which files to make # sources = [file for file in os.listdir(dir) if file.endswith('.scad')] all_sources += sources # # Run OpenSCAD on the source files to make the targets # for src in sources: src_file = dir + '/' + src part_file = target_dir + '/' + src[:-4] + part_type dname = deps_name(deps_dir, src) changed = check_deps(part_file, dname) if changed: print(changed) openscad.run("-D$bom=1", "-d", dname, "-o", part_file, src_file) if part_type == 'stl': c14n_stl.canonicalise(part_file) log_name = 'openscad.log' else: log_name = 'openscad.echo' openscad.run_silent("-D$bom=1", "-o", log_name, src_file) # # Add the files on the BOM to the used list # with open(log_name) as file: for line in file.readlines(): match = re.match(r'^ECHO: "~(.*?\.' + part_type + r').*"$', line) if match: used.append(match.group(1)) # # Copy file that are not included # copied = [] for file in os.listdir(parts_dir): if file.endswith('.' + part_type) and not file in used: src = parts_dir + '/' + file dst = target_dir + '/' + file if mtime(src) > mtime(dst): print("Copying %s to %s" % (src, dst)) copyfile(src, dst) copied.append(file) # # Remove any cruft # targets = [file[:-4] + part_type for file in all_sources] for file in os.listdir(target_dir): if file.endswith('.' + part_type): if not file in targets and not file in copied: print("Removing %s" % file) os.remove(target_dir + '/' + file)