142 lines
5.7 KiB
Python
142 lines
5.7 KiB
Python
"""R05 visual revision over an immutable R04 source; dimensions are in metres.
|
|
|
|
Blender --background R04.blend --python refine_battery.py -- R05.blend
|
|
This is viewport geometry, not a fabrication or electrical-clearance approval.
|
|
"""
|
|
|
|
import json
|
|
import math
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import bmesh
|
|
import bpy
|
|
from mathutils import Matrix, Vector
|
|
|
|
output = Path(sys.argv[sys.argv.index("--") + 1])
|
|
if output.exists():
|
|
raise FileExistsError("Revision already exists")
|
|
source = bpy.data.filepath
|
|
outer = next(c for c in bpy.data.collections if c.name.startswith("OUT |"))
|
|
collection = bpy.data.collections.new("R05 | Welded angle frame 20x20x2")
|
|
outer.children.link(collection)
|
|
material = bpy.data.materials.new("FRAME | welded graphite steel")
|
|
material.diffuse_color = (0.055, 0.063, 0.07, 1)
|
|
material.use_nodes = True
|
|
principled = material.node_tree.nodes.get("Principled BSDF")
|
|
principled.inputs["Base Color"].default_value = material.diffuse_color
|
|
principled.inputs["Metallic"].default_value = 0.72
|
|
principled.inputs["Roughness"].default_value = 0.38
|
|
# Profile heels overlap the inner sheet surface by 0.2 mm to close the existing
|
|
# 0.5 mm sheet joints. Butt joints trim the cross rails/posts, with no coplanar
|
|
# overlapping end polygons or transparent sealing material.
|
|
xmin, xmax, ymin, ymax, zmin, zmax = -0.2777, 0.3077, -0.1177, 0.1177, -0.0097, 0.1757
|
|
leg, wall = 0.020, 0.002
|
|
profile = [(0, 0), (leg, 0), (leg, wall), (wall, wall), (wall, leg), (0, leg)]
|
|
created = []
|
|
|
|
|
|
def angle(name, axis, start, end, u, v, su, sv, cu, cv):
|
|
vertices = []
|
|
for longitudinal in (start, end):
|
|
for a, b in profile:
|
|
point = [0.0, 0.0, 0.0]
|
|
point[axis] = longitudinal
|
|
point[u] = cu + su * a
|
|
point[v] = cv + sv * b
|
|
vertices.append(point)
|
|
faces = [tuple(range(5, -1, -1)), tuple(range(6, 12))]
|
|
faces += [(i, (i + 1) % 6, (i + 1) % 6 + 6, i + 6) for i in range(6)]
|
|
mesh = bpy.data.meshes.new(name)
|
|
mesh.from_pydata(vertices, [], faces)
|
|
mesh.materials.append(material)
|
|
bm = bmesh.new()
|
|
bm.from_mesh(mesh)
|
|
bmesh.ops.recalc_face_normals(bm, faces=list(bm.faces))
|
|
bm.to_mesh(mesh)
|
|
bm.free()
|
|
obj = bpy.data.objects.new(name, mesh)
|
|
collection.objects.link(obj)
|
|
created.append(obj)
|
|
|
|
|
|
for y, sy, side in [(ymin, 1, "left"), (ymax, -1, "right")]:
|
|
for z, sz, level in [(zmin, 1, "bottom"), (zmax, -1, "top")]:
|
|
angle(f"FRAME | {level} longitudinal {side}", 0, xmin, xmax, 1, 2, sy, sz, y, z)
|
|
for x, sx, endname in [(xmin, 1, "rear"), (xmax, -1, "front")]:
|
|
for z, sz, level in [(zmin, 1, "bottom"), (zmax, -1, "top")]:
|
|
angle(f"FRAME | {level} cross {endname}", 1, ymin + leg, ymax - leg, 0, 2, sx, sz, x, z)
|
|
for y, sy, side in [(ymin, 1, "left"), (ymax, -1, "right")]:
|
|
angle(f"FRAME | post {endname} {side}", 2, zmin + leg, zmax - leg, 0, 1, sx, sy, x, y)
|
|
texts = []
|
|
for obj in bpy.data.objects:
|
|
if obj.type == "FONT" and re.search("cattle", obj.data.body, re.I):
|
|
obj.data.body = re.sub("cattle", "CATL", obj.data.body, flags=re.I)
|
|
texts.append(obj.name)
|
|
assert len(texts) == 5
|
|
# The circled attachment is the modelled NTC1 probe, not a busbar connector.
|
|
# Move its pad/probe together to a free top-lid area of C05; retain electrical
|
|
# endpoint identities and reroute only its two depicted leads.
|
|
pad = bpy.data.objects["NTC1 dielectric attachment"]
|
|
pivot = pad.matrix_world.translation.copy()
|
|
rotation = Matrix.Rotation(-math.pi / 2, 4, "X")
|
|
target = Vector((-0.072, -0.038, 0.1238))
|
|
transform = Matrix.Translation(target) @ rotation @ Matrix.Translation(-pivot)
|
|
for name in ["NTC1 dielectric attachment", "NTC1 insulated probe"]:
|
|
obj = bpy.data.objects[name]
|
|
obj.matrix_world = transform @ obj.matrix_world
|
|
for strand in (1, 2):
|
|
obj = bpy.data.objects[f"NTC1 | P3.{strand}"]
|
|
curve = obj.data
|
|
# Preserve the BMS connector end and the existing elevated harness route.
|
|
spline = curve.splines[0]
|
|
points = [
|
|
p.co.to_3d().copy()
|
|
for p in (spline.bezier_points if spline.type == "BEZIER" else spline.points)
|
|
]
|
|
assert (points[0] - Vector((-0.072, -0.0891, 0.087))).length < 0.004
|
|
dx = (strand - 1) * 0.0009
|
|
new = [
|
|
Vector((-0.072 + dx, -0.033, 0.1256)),
|
|
Vector((-0.072 + dx, -0.032, 0.143)),
|
|
Vector((-0.072 + dx, -0.085, 0.145)),
|
|
] + points[2:]
|
|
curve.splines.clear()
|
|
spline = curve.splines.new("BEZIER")
|
|
spline.bezier_points.add(len(new) - 1)
|
|
for p, co in zip(spline.bezier_points, new, strict=True):
|
|
p.co = co
|
|
p.handle_left_type = "AUTO"
|
|
p.handle_right_type = "AUTO"
|
|
bpy.context.view_layer.update()
|
|
# Every profile must be a closed solid and must stay clear of the cell envelopes.
|
|
for obj in created:
|
|
bm = bmesh.new()
|
|
bm.from_mesh(obj.data)
|
|
assert all(e.is_manifold for e in bm.edges)
|
|
bm.free()
|
|
bounds = [obj.matrix_world @ Vector(p) for p in obj.bound_box]
|
|
lo = [min(p[i] for p in bounds) for i in range(3)]
|
|
hi = [max(p[i] for p in bounds) for i in range(3)]
|
|
for cell in [o for o in bpy.data.objects if "Blue cell body" in o.name]:
|
|
b = [cell.matrix_world @ Vector(p) for p in cell.bound_box]
|
|
assert not all(
|
|
min(hi[i], max(p[i] for p in b)) - max(lo[i], min(p[i] for p in b)) > 1e-6
|
|
for i in range(3)
|
|
)
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
bpy.ops.wm.save_as_mainfile(filepath=str(output))
|
|
report = dict(
|
|
source=Path(source).name,
|
|
revision=output.name,
|
|
frame_profiles=len(created),
|
|
profile_mm=[20, 20, 2],
|
|
texts=texts,
|
|
attachment="NTC1 moved onto C05 top lid",
|
|
cell_geometry_unchanged=True,
|
|
source_preserved=True,
|
|
)
|
|
output.with_suffix(".json").write_text(json.dumps(report, indent=2))
|
|
print(json.dumps(report))
|