Files
vibemake-pcb-studio/codex_pcb/step_colors.py
T
2026-11-11 19:27:49 +08:00

78 lines
3.5 KiB
Python

"""Check that STEP surface styles retain a non-uniform source palette.
This is an appearance check, not a geometry or engineering validator. It follows
style references instead of accepting unused COLOUR_RGB declarations.
"""
import re
STYLE_TYPES = {
'COLOUR_RGB', 'DRAUGHTING_PRE_DEFINED_COLOUR', 'FILL_AREA_STYLE_COLOUR',
'FILL_AREA_STYLE', 'SURFACE_STYLE_FILL_AREA', 'SURFACE_SIDE_STYLE',
'SURFACE_STYLE_USAGE', 'SURFACE_STYLE_RENDERING',
'SURFACE_STYLE_RENDERING_WITH_PROPERTIES', 'PRESENTATION_STYLE_ASSIGNMENT',
'PRESENTATION_STYLE_BY_CONTEXT', 'STYLED_ITEM', 'OVER_RIDING_STYLED_ITEM',
}
NAMED_COLORS = {
'black': (0., 0., 0.), 'white': (1., 1., 1.), 'red': (1., 0., 0.),
'green': (0., 1., 0.), 'blue': (0., 0., 1.), 'yellow': (1., 1., 0.),
'magenta': (1., 0., 1.), 'cyan': (0., 1., 1.),
}
def validate_step_colors(data):
text = re.sub(r'/\*.*?\*/', '', data.decode('latin-1'), flags=re.S)
pattern = r'#(\d+)\s*=\s*(' + '|'.join(sorted(STYLE_TYPES, key=len, reverse=True)) + r')\s*\((.*?)\)\s*;'
records = {m[1]: (m[2], m[3]) for m in re.finditer(pattern, text, re.S | re.I)}
colors = {}
for key, (kind, body) in records.items():
if kind.upper() == 'COLOUR_RGB':
values = body.rsplit(',', 3)
try:
rgb = tuple(float(v.strip()) for v in values[-3:]) if len(values) == 4 else ()
if len(rgb) == 3 and all(0 <= v <= 1 for v in rgb):
colors[key] = tuple(round(v, 6) for v in rgb)
except ValueError:
pass
elif kind.upper() == 'DRAUGHTING_PRE_DEFINED_COLOUR':
rgb = NAMED_COLORS.get(body.strip().strip("'").lower())
if rgb is not None:
colors[key] = rgb
roots = []
for kind, body in records.values():
if kind.upper() not in {'STYLED_ITEM', 'OVER_RIDING_STYLED_ITEM'}:
continue
# name, (presentation styles), actual representation item [, overridden style]
match = re.match(r"\s*'(?:[^']|'')*'\s*,\s*\(([^)]*)\)\s*,\s*#(\d+)", body, re.S)
if match:
roots.append((match[1], match[2]))
candidates = {target for _, target in roots}
defined = {m[1] for m in re.finditer(r'#(\d+)\s*=', text) if m[1] in candidates}
palette, targets = set(), set()
for styles, target in roots:
if target not in defined or target in records:
continue
todo, seen, used = re.findall(r'#(\d+)', styles), set(), set()
while todo:
ref = todo.pop()
if ref in seen:
continue
seen.add(ref)
if ref in colors:
used.add(colors[ref])
elif ref in records:
todo.extend(re.findall(r'#(\d+)', records[ref][1]))
if used:
targets.add(target)
palette.update(used)
if len(palette) < 2 or not targets:
raise ValueError(
'STEP appearance check failed: the delivered assembly has no usable multi-color surface styles. '
'Repair the STEP in this same session using the actual native model colors. '
'When rebuilding an XCAF assembly, copy document/shape/face and instance color attributes '
'with their transformed geometry; copying TopoDS shapes alone loses colors. '
'Enable STEPCAF color export and reopen the exact final file to verify it. '
'A colored PNG or OBJ does not replace a colored STEP. Do not invent colors to pass.'
)
return {'distinct_colors': len(palette), 'styled_items': len(targets)}