1
0
mirror of https://github.com/nophead/NopSCADlib.git synced 2025-08-14 11:24:00 +02:00

Renders of STLs are now the correct colour. Fixes #71

This commit is contained in:
Chris Palmer
2020-04-05 16:18:24 +01:00
parent bc4e18d788
commit 7e0c5fdb6e
29 changed files with 175 additions and 76 deletions

View File

@@ -46,6 +46,16 @@ def find_scad_file(mname):
return filename
return None
class Part:
def __init__(self, args):
self.count = 1
for arg in args:
arg = arg.replace('true', 'True').replace('false', 'False').replace('undef', 'None')
exec('self.' + arg)
def data(self):
return self.__dict__
class BOM:
def __init__(self, name):
self.name = name
@@ -65,12 +75,17 @@ class BOM:
"big" : self.big,
"count" : self.count,
"assemblies" : assemblies,
"vitamins" : self.vitamins,
"printed" : self.printed,
"routed" : self.routed
"vitamins" : {v : self.vitamins[v].data() for v in self.vitamins},
"printed" : {p : self.printed[p].data() for p in self.printed},
"routed" : {r : self.routed[r].data() for r in self.routed}
}
def add_part(self, s):
args = []
match = re.match(r'^(.*?\.stl)\((.*)\)$', s) #look for name.stl(...)
if match:
s = match.group(1)
args = [match.group(2)]
if s[-4:] == ".stl":
parts = self.printed
else:
@@ -79,9 +94,9 @@ class BOM:
else:
parts = self.vitamins
if s in parts:
parts[s] += 1
parts[s].count += 1
else:
parts[s] = 1
parts[s] = Part(args)
def add_assembly(self, ass, args = []):
if ass in self.assemblies:
@@ -126,10 +141,10 @@ class BOM:
for ass in sorted(self.assemblies):
bom = self.assemblies[ass]
if part in bom.vitamins:
file.write("%2d|" % bom.vitamins[part])
file.write("%2d|" % bom.vitamins[part].count)
else:
file.write(" |")
print("%3d" % self.vitamins[part], description, file=file)
print("%3d" % self.vitamins[part].count, description, file=file)
if self.printed:
if self.vitamins:
@@ -140,10 +155,10 @@ class BOM:
for ass in sorted(self.assemblies):
bom = self.assemblies[ass]
if part in bom.printed:
file.write("%2d|" % bom.printed[part])
file.write("%2d|" % bom.printed[part].count)
else:
file.write(" |")
print("%3d" % self.printed[part], part, file=file)
print("%3d" % self.printed[part].count, part, file=file)
if self.routed:
print(file=file)
@@ -153,10 +168,10 @@ class BOM:
for ass in sorted(self.assemblies):
bom = self.assemblies[ass]
if part in bom.routed:
file.write("%2d|" % bom.routed[part])
file.write("%2d|" % bom.routed[part].count)
else:
file.write(" |")
print("%3d" % self.routed[part], part, file=file)
print("%3d" % self.routed[part].count, part, file=file)
if self.assemblies:
print(file=file)

View File

@@ -29,6 +29,7 @@ import openscad
from tests import do_cmd, update_image, colour_scheme, background
from deps import mtime
from colorama import init
import json
def usage():
print("\nusage:\n\trender [target_config] - Render images of the stl and dxf files.");
@@ -48,6 +49,20 @@ def render(target, type):
#
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):
@@ -55,7 +70,9 @@ def render(target, type):
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'
@@ -64,8 +81,13 @@ def render(target, type):
#
if mtime(part_file) > mtime(png_name):
png_maker_name = "png.scad"
colour = [0, 146/255, 0]
if part in colours:
colour = colours[part]
if not '[' in colour:
colour = '"' + colour + '"'
with open(png_maker_name, "w") as f:
f.write('color([0, 146/255, 0]) import("%s");\n' % part_file)
f.write('color(%s) import("%s");\n' % (colour, 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'

View File

@@ -232,9 +232,10 @@ def tests(tests):
j = name.find(']]') + 2
name = name.replace(name[i : j], '[ ... ]')
desc = vit[1]
body += ['| %3d | %s | %s |' % (things[item], name, desc)]
body += ['| %3d | %s | %s |' % (things[item]["count"], name, desc)]
else:
body += ['| %3d | %s |' % (things[item], name)]
count = things[item] if thing == 'assemblies' else things[item]["count"]
body += ['| %3d | %s |' % (count, name)]
body += ['']
body += ['\n<a href="#top">Top</a>']

View File

@@ -248,9 +248,9 @@ def views(target, do_assemblies = None):
for t in types:
for thing in ass[t]:
if thing in things[t]:
things[t][thing] += ass[t][thing]
things[t][thing] += ass[t][thing]["count"]
else:
things[t][thing] = ass[t][thing]
things[t][thing] = ass[t][thing]["count"]
for ass in flat_bom:
name = titalise(ass["name"][:-9]).replace(' ','&nbsp;')
print('| <span style="writing-mode: vertical-rl; text-orientation: mixed;">%s</span> ' % name, file = doc_file, end = '')
@@ -265,7 +265,7 @@ def views(target, do_assemblies = None):
print(('| ' * len(flat_bom) + '| | **%s** |') % heading, file = doc_file)
for thing in sorted(things[t], key = lambda s: s.split(":")[-1]):
for ass in flat_bom:
count = ass[t][thing] if thing in ass[t] else 0
count = ass[t][thing]["count"] if thing in ass[t] else 0
print('| %s ' % pad(count if count else '.', 2, 1), file = doc_file, end = '')
name = ass["name"]
if name in totals:
@@ -301,7 +301,7 @@ def views(target, do_assemblies = None):
print("|Qty|Description|", file = doc_file)
print("|---:|:----------|", file = doc_file)
for v in sorted(vitamins, key = lambda s: s.split(":")[-1]):
print("|%d|%s|" % (vitamins[v], v.split(":")[1]), file = doc_file)
print("|%d|%s|" % (vitamins[v]["count"], v.split(":")[1]), file = doc_file)
print("\n", file = doc_file)
printed = ass["printed"]
@@ -310,7 +310,7 @@ def views(target, do_assemblies = None):
keys = sorted(list(printed.keys()))
for i in range(len(keys)):
p = keys[i]
print('%s %d x %s |' % ('\n|' if not (i % 3) else '', printed[p], p), file = doc_file, end = '')
print('%s %d x %s |' % ('\n|' if not (i % 3) else '', printed[p]["count"], p), file = doc_file, end = '')
if (i % 3) == 2 or i == len(printed) - 1:
n = (i % 3) + 1
print('\n|%s' % ('---|' * n), file = doc_file)
@@ -326,7 +326,7 @@ def views(target, do_assemblies = None):
keys = sorted(list(routed.keys()))
for i in range(len(keys)):
r = keys[i]
print('%s %d x %s |' % ('\n|' if not (i % 3) else '', routed[r], r), file = doc_file, end = '')
print('%s %d x %s |' % ('\n|' if not (i % 3) else '', routed[r]["count"], r), file = doc_file, end = '')
if (i % 3) == 2 or i == len(routed) - 1:
n = (i % 3) + 1
print('\n|%s' % ('---|' * n), file = doc_file)