Spaces:
Sleeping
Rotate All Shapes button; Keep Proportions no longer rewrites the typed value
Browse files- Rotate All Shapes: applies the same absolute angle to every uploaded
shape at once, each carrying its own configured dimensions through the
turn (split pieces are skipped with a note). The per-shape rotation
core is factored into _apply_rotation_to_record, shared by the
single-shape and rotate-all paths.
- Keep Proportions fix: entering a dimension (e.g. 25) no longer snaps
back (e.g. to 25.1). A fresh edit now anchors on the edited axis, full
stop - the odd-one-out ratio heuristic could misfire on rounded rows
(equal X/Y originals agree with each other while a small axis whose
rounded value lands back on its original reads as ratio 1.0) and
rewrite the value the user just typed on the echo round. Echoes now
count as converged when the row recomputes to ITSELF from its last
anchor, which is the rounding-aware replacement for exact-ratio
equality; the heuristic remains only as a fallback for rows with no
edit history.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
- README.md +1 -1
- app.py +219 -55
- tests/test_nozzle_spacing.py +92 -0
|
@@ -51,7 +51,7 @@ Then open the local Gradio URL in your browser, upload STL files or load the bun
|
|
| 51 |
- Port groups are marked too: shapes sharing a Port get a matching underline on their Pressure and Port cells and a summary line ("Port 1: A + B share one pressure regulator (25 psi)") — one regulator per serial port is why their pressures stay in sync
|
| 52 |
- Automatically unions the sliced shapes into a combined reference layer set whenever shapes are sliced
|
| 53 |
- Shows a sliced-layer preview in the Selected Shape Preview accordion (layer slider through the shape's polygon outlines, drawn in its print color; assembly parts sharing the nozzle are drawn together so multi-material slicing can be checked before generating G-code)
|
| 54 |
-
- **Rotate a shape to print in any direction**: the Selected Shape Preview accordion has a Rotate (°) input that spins the selected shape on the bed (about Z, around its center) before slicing — the table dimensions
|
| 55 |
- Bundled sample sets include a **Multi-Material Demo** (checkerboard cube, wrapped egg, space helmet — two STLs each) that loads with the parts of each model already grouped onto shared nozzles, forming three assemblies in one click
|
| 56 |
- **Save / Load Settings**: exports every table setting plus the generation options as a small JSON keyed by STL filename; re-upload the same STLs later (or after a Space restart) and import to restore the whole setup — files in the export that aren't loaded yet are listed so they can be added. Split pieces are derived geometry and don't round-trip
|
| 57 |
- Generate G-Code reports live progress (slicing, reference building, then shape-by-shape generation)
|
|
|
|
| 51 |
- Port groups are marked too: shapes sharing a Port get a matching underline on their Pressure and Port cells and a summary line ("Port 1: A + B share one pressure regulator (25 psi)") — one regulator per serial port is why their pressures stay in sync
|
| 52 |
- Automatically unions the sliced shapes into a combined reference layer set whenever shapes are sliced
|
| 53 |
- Shows a sliced-layer preview in the Selected Shape Preview accordion (layer slider through the shape's polygon outlines, drawn in its print color; assembly parts sharing the nozzle are drawn together so multi-material slicing can be checked before generating G-code)
|
| 54 |
+
- **Rotate a shape to print in any direction**: the Selected Shape Preview accordion has a Rotate (°) input that spins the selected shape on the bed (about Z, around its center) before slicing — the configured table dimensions follow the rotation (a spun 20 × 8 shape becomes 8 × 20, keeping any custom scaling), and the 3D/layer previews show the rotated shape. **Rotate All Shapes** applies the same angle to every uploaded shape at once (each carrying its own dimensions through the turn; split pieces are skipped). Assembly parts sharing a nozzle rotate about the group's combined center, so equal rotations turn the whole assembly as one unit. The rotation exports/imports with the other settings. (The engine also supports X/Y tilts, not yet exposed in the UI)
|
| 55 |
- Bundled sample sets include a **Multi-Material Demo** (checkerboard cube, wrapped egg, space helmet — two STLs each) that loads with the parts of each model already grouped onto shared nozzles, forming three assemblies in one click
|
| 56 |
- **Save / Load Settings**: exports every table setting plus the generation options as a small JSON keyed by STL filename; re-upload the same STLs later (or after a Space restart) and import to restore the whole setup — files in the export that aren't loaded yet are listed so they can be added. Split pieces are derived geometry and don't round-trip
|
| 57 |
- Generate G-Code reports live progress (slicing, reference building, then shape-by-shape generation)
|
|
@@ -1998,6 +1998,21 @@ def _record_rotation(record: dict) -> tuple[float, float, float]:
|
|
| 1998 |
return tuple(round(value, 1) for value in values)
|
| 1999 |
|
| 2000 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2001 |
def _rotated_display_stl(stl_path: str, rotation: tuple[float, float, float]) -> str:
|
| 2002 |
"""A temp STL of the rotated mesh, so the 3D preview shows the shape the
|
| 2003 |
way it will actually print."""
|
|
@@ -3394,46 +3409,69 @@ def normalize_shape_dimensions_for_mode(
|
|
| 3394 |
continue
|
| 3395 |
idx = int(copy.get("idx", 0))
|
| 3396 |
|
| 3397 |
-
|
| 3398 |
-
|
| 3399 |
-
|
| 3400 |
-
|
| 3401 |
-
|
| 3402 |
-
|
| 3403 |
-
|
| 3404 |
-
|
| 3405 |
-
|
| 3406 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3407 |
|
| 3408 |
-
|
| 3409 |
-
|
| 3410 |
-
#
|
| 3411 |
-
|
| 3412 |
-
|
| 3413 |
-
|
| 3414 |
-
|
| 3415 |
-
|
| 3416 |
-
others_agree = [
|
| 3417 |
-
_close(ratios[1], ratios[2]),
|
| 3418 |
-
_close(ratios[0], ratios[2]),
|
| 3419 |
-
_close(ratios[0], ratios[1]),
|
| 3420 |
-
]
|
| 3421 |
-
if sum(others_agree) == 1:
|
| 3422 |
-
anchor_index = others_agree.index(True)
|
| 3423 |
else:
|
| 3424 |
-
#
|
| 3425 |
-
#
|
| 3426 |
-
|
| 3427 |
-
|
| 3428 |
-
|
| 3429 |
-
|
| 3430 |
-
|
| 3431 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3432 |
copy["last_scaled_axis"] = TARGET_DIMENSION_KEYS[anchor_index]
|
| 3433 |
-
|
| 3434 |
-
copy["target_x"] =
|
| 3435 |
-
copy["target_y"] =
|
| 3436 |
-
copy["target_z"] =
|
| 3437 |
recomputed_idx.add(idx)
|
| 3438 |
normalized.append(copy)
|
| 3439 |
|
|
@@ -3522,39 +3560,143 @@ def apply_shape_rotation(
|
|
| 3522 |
"shape, then split again.",
|
| 3523 |
)
|
| 3524 |
|
| 3525 |
-
rotation =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3526 |
round(_coerce_float(value, 0.0) % 360.0, 1)
|
| 3527 |
for value in (rotate_x, rotate_y, rotate_z)
|
| 3528 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3529 |
try:
|
| 3530 |
-
|
| 3531 |
-
|
| 3532 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3533 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3534 |
except Exception as exc:
|
| 3535 |
-
return
|
| 3536 |
|
| 3537 |
record["rotation"] = rotation if any(rotation) else None
|
| 3538 |
-
for axis,
|
| 3539 |
-
record[f"original_{axis}"] = round(
|
| 3540 |
-
record[f"target_{axis}"] = round(
|
|
|
|
| 3541 |
|
| 3542 |
-
|
| 3543 |
-
|
| 3544 |
-
|
| 3545 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3546 |
else:
|
| 3547 |
-
|
|
|
|
|
|
|
| 3548 |
status = (
|
| 3549 |
-
f"Rotated {
|
| 3550 |
-
|
| 3551 |
-
"
|
| 3552 |
)
|
| 3553 |
else:
|
| 3554 |
-
status = f"Rotation cleared for {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3555 |
return records, _shape_settings_rows(records), status
|
| 3556 |
|
| 3557 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3558 |
def apply_shape_z_rotation(
|
| 3559 |
records: list[dict] | None,
|
| 3560 |
selected: str | None,
|
|
@@ -5420,7 +5562,11 @@ def build_dynamic_demo() -> gr.Blocks:
|
|
| 5420 |
"Apply Rotation", variant="secondary", size="sm",
|
| 5421 |
min_width=140, scale=1,
|
| 5422 |
)
|
| 5423 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5424 |
rotation_status = gr.Markdown("")
|
| 5425 |
|
| 5426 |
with gr.Row():
|
|
@@ -5956,6 +6102,24 @@ def build_dynamic_demo() -> gr.Blocks:
|
|
| 5956 |
outputs=[gcode_stale_banner],
|
| 5957 |
queue=False,
|
| 5958 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5959 |
# Selecting a shape shows its stored rotation in the input.
|
| 5960 |
selected_shape.change(
|
| 5961 |
fn=selected_shape_z_rotation,
|
|
|
|
| 1998 |
return tuple(round(value, 1) for value in values)
|
| 1999 |
|
| 2000 |
|
| 2001 |
+
def _rotation_matrix(rotation: tuple[float, float, float]) -> np.ndarray:
|
| 2002 |
+
"""4x4 matrix of a shape rotation (X, then Y, then Z degrees, about the
|
| 2003 |
+
origin) — matching `rotate_mesh`'s composition order. Rotation matrices
|
| 2004 |
+
invert by transposition, which is how the rotation DELTA between two
|
| 2005 |
+
stored rotations is computed."""
|
| 2006 |
+
matrix = np.eye(4)
|
| 2007 |
+
for angle, axis in zip(rotation, ((1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0))):
|
| 2008 |
+
if abs(angle) < 1e-9:
|
| 2009 |
+
continue
|
| 2010 |
+
matrix = (
|
| 2011 |
+
trimesh.transformations.rotation_matrix(math.radians(angle), axis) @ matrix
|
| 2012 |
+
)
|
| 2013 |
+
return matrix
|
| 2014 |
+
|
| 2015 |
+
|
| 2016 |
def _rotated_display_stl(stl_path: str, rotation: tuple[float, float, float]) -> str:
|
| 2017 |
"""A temp STL of the rotated mesh, so the 3D preview shows the shape the
|
| 2018 |
way it will actually print."""
|
|
|
|
| 3409 |
continue
|
| 3410 |
idx = int(copy.get("idx", 0))
|
| 3411 |
|
| 3412 |
+
def _recomputed_from(anchor_index: int) -> tuple[float, float, float]:
|
| 3413 |
+
scale = float(targets[anchor_index] / originals[anchor_index])
|
| 3414 |
+
scaled = originals * scale
|
| 3415 |
+
values = [round(float(value), 1) for value in scaled]
|
| 3416 |
+
# The anchor keeps the ENTERED value exactly (float noise in the
|
| 3417 |
+
# round trip must never nudge it).
|
| 3418 |
+
values[anchor_index] = round(float(targets[anchor_index]), 1)
|
| 3419 |
+
return tuple(values)
|
| 3420 |
+
|
| 3421 |
+
def _self_consistent(anchor_index: int) -> bool:
|
| 3422 |
+
return all(
|
| 3423 |
+
math.isclose(value, float(targets[axis]), abs_tol=1e-9)
|
| 3424 |
+
for axis, value in enumerate(_recomputed_from(anchor_index))
|
| 3425 |
+
)
|
| 3426 |
|
| 3427 |
+
edited_key = edited_axes.get(idx)
|
| 3428 |
+
if edited_key in TARGET_DIMENSION_KEYS:
|
| 3429 |
+
# A FRESH user edit anchors on the edited axis, full stop. The
|
| 3430 |
+
# ratio heuristic below can misfire on rounded rows — e.g. equal
|
| 3431 |
+
# X/Y originals agree with each other, and a small axis whose
|
| 3432 |
+
# rounded value lands back on its original reads as ratio 1.0 —
|
| 3433 |
+
# and would rewrite the value the user just typed.
|
| 3434 |
+
anchor_index = TARGET_DIMENSION_KEYS.index(edited_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3435 |
else:
|
| 3436 |
+
# No fresh edit: an echo of our own write-back, or a stale
|
| 3437 |
+
# table. Tenths rounding makes exact proportionality impossible,
|
| 3438 |
+
# so "converged" means the row RECOMPUTES TO ITSELF from its
|
| 3439 |
+
# last anchor — skip the write entirely. This is
|
| 3440 |
+
# ORDER-INDEPENDENT: stale/echoed tables are self-consistent
|
| 3441 |
+
# and never get mis-anchored against fresher records.
|
| 3442 |
+
last_key = str(copy.get("last_scaled_axis") or "target_x")
|
| 3443 |
+
last_index = (
|
| 3444 |
+
TARGET_DIMENSION_KEYS.index(last_key)
|
| 3445 |
+
if last_key in TARGET_DIMENSION_KEYS
|
| 3446 |
+
else 0
|
| 3447 |
+
)
|
| 3448 |
+
if _self_consistent(last_index):
|
| 3449 |
+
normalized.append(copy)
|
| 3450 |
+
continue
|
| 3451 |
+
|
| 3452 |
+
# Not self-consistent (e.g. dims carried over from Independent
|
| 3453 |
+
# mode, or an import): anchor on the row's own evidence — the
|
| 3454 |
+
# axis whose OTHER two ratios agree is the edited one.
|
| 3455 |
+
ratios = targets / originals
|
| 3456 |
+
|
| 3457 |
+
def _close(a: float, b: float) -> bool:
|
| 3458 |
+
return math.isclose(float(a), float(b), rel_tol=1e-6, abs_tol=1e-9)
|
| 3459 |
+
|
| 3460 |
+
others_agree = [
|
| 3461 |
+
_close(ratios[1], ratios[2]),
|
| 3462 |
+
_close(ratios[0], ratios[2]),
|
| 3463 |
+
_close(ratios[0], ratios[1]),
|
| 3464 |
+
]
|
| 3465 |
+
if sum(others_agree) == 1:
|
| 3466 |
+
anchor_index = others_agree.index(True)
|
| 3467 |
+
else:
|
| 3468 |
+
anchor_index = last_index
|
| 3469 |
+
|
| 3470 |
copy["last_scaled_axis"] = TARGET_DIMENSION_KEYS[anchor_index]
|
| 3471 |
+
new_x, new_y, new_z = _recomputed_from(anchor_index)
|
| 3472 |
+
copy["target_x"] = new_x
|
| 3473 |
+
copy["target_y"] = new_y
|
| 3474 |
+
copy["target_z"] = new_z
|
| 3475 |
recomputed_idx.add(idx)
|
| 3476 |
normalized.append(copy)
|
| 3477 |
|
|
|
|
| 3560 |
"shape, then split again.",
|
| 3561 |
)
|
| 3562 |
|
| 3563 |
+
rotation = _normalize_rotation_input(rotate_x, rotate_y, rotate_z)
|
| 3564 |
+
error = _apply_rotation_to_record(record, rotation)
|
| 3565 |
+
if error:
|
| 3566 |
+
return records, _shape_settings_rows(records), f"Rotation failed: {error}"
|
| 3567 |
+
|
| 3568 |
+
dims = _record_dims_text(record)
|
| 3569 |
+
if any(rotation):
|
| 3570 |
+
status = (
|
| 3571 |
+
f"Rotated {name} to {_rotation_angle_text(rotation)}. "
|
| 3572 |
+
f"Dimensions follow the rotation ({dims} mm) — edit them as needed; "
|
| 3573 |
+
"the next Generate G-Code (or split) slices the rotated shape."
|
| 3574 |
+
)
|
| 3575 |
+
else:
|
| 3576 |
+
status = f"Rotation cleared for {name}; dimensions follow back to {dims} mm."
|
| 3577 |
+
return records, _shape_settings_rows(records), status
|
| 3578 |
+
|
| 3579 |
+
|
| 3580 |
+
def _normalize_rotation_input(
|
| 3581 |
+
rotate_x: Any, rotate_y: Any, rotate_z: Any
|
| 3582 |
+
) -> tuple[float, float, float]:
|
| 3583 |
+
return tuple(
|
| 3584 |
round(_coerce_float(value, 0.0) % 360.0, 1)
|
| 3585 |
for value in (rotate_x, rotate_y, rotate_z)
|
| 3586 |
)
|
| 3587 |
+
|
| 3588 |
+
|
| 3589 |
+
def _rotation_angle_text(rotation: tuple[float, float, float]) -> str:
|
| 3590 |
+
if rotation[0] or rotation[1]:
|
| 3591 |
+
return f"X {rotation[0]:g}°, Y {rotation[1]:g}°, Z {rotation[2]:g}°"
|
| 3592 |
+
return f"{rotation[2]:g}°"
|
| 3593 |
+
|
| 3594 |
+
|
| 3595 |
+
def _record_dims_text(record: dict) -> str:
|
| 3596 |
+
return " x ".join(
|
| 3597 |
+
f"{round(_coerce_float(record.get(f'target_{axis}'), 0.0), 1):g}"
|
| 3598 |
+
for axis in ("x", "y", "z")
|
| 3599 |
+
)
|
| 3600 |
+
|
| 3601 |
+
|
| 3602 |
+
def _apply_rotation_to_record(
|
| 3603 |
+
record: dict, rotation: tuple[float, float, float]
|
| 3604 |
+
) -> str | None:
|
| 3605 |
+
"""Set one shape's rotation in place; returns an error message or None.
|
| 3606 |
+
|
| 3607 |
+
Carries the CONFIGURED dimensions through the rotation: the shape as it
|
| 3608 |
+
is set up today is reoriented by the rotation DELTA and its new bounding
|
| 3609 |
+
box becomes the targets — a spun 20 x 8 shape becomes 8 x 20, it does
|
| 3610 |
+
not snap back to its natural size. Originals stay the NATURAL rotated
|
| 3611 |
+
size (the ratio basis for Keep Proportions).
|
| 3612 |
+
"""
|
| 3613 |
+
old_rotation = _record_rotation(record)
|
| 3614 |
try:
|
| 3615 |
+
raw_mesh = load_mesh(record["stl_path"])
|
| 3616 |
+
old_rotated = rotate_mesh(raw_mesh, old_rotation)
|
| 3617 |
+
old_extents = tuple(float(value) for value in old_rotated.extents)
|
| 3618 |
+
natural = tuple(
|
| 3619 |
+
float(value) for value in rotate_mesh(raw_mesh, rotation).extents
|
| 3620 |
+
)
|
| 3621 |
+
factors = tuple(
|
| 3622 |
+
(
|
| 3623 |
+
_coerce_float(record.get(f"target_{axis}"), extent) / extent
|
| 3624 |
+
if extent > 1e-9
|
| 3625 |
+
else 1.0
|
| 3626 |
+
)
|
| 3627 |
+
for axis, extent in zip(("x", "y", "z"), old_extents)
|
| 3628 |
)
|
| 3629 |
+
carried_mesh = scale_mesh(old_rotated, factors)
|
| 3630 |
+
delta = _rotation_matrix(rotation) @ _rotation_matrix(old_rotation).T
|
| 3631 |
+
carried_mesh.apply_transform(delta)
|
| 3632 |
+
carried = tuple(float(value) for value in carried_mesh.extents)
|
| 3633 |
except Exception as exc:
|
| 3634 |
+
return str(exc)
|
| 3635 |
|
| 3636 |
record["rotation"] = rotation if any(rotation) else None
|
| 3637 |
+
for axis, natural_extent, carried_extent in zip(("x", "y", "z"), natural, carried):
|
| 3638 |
+
record[f"original_{axis}"] = round(natural_extent, 1)
|
| 3639 |
+
record[f"target_{axis}"] = round(carried_extent, 1)
|
| 3640 |
+
return None
|
| 3641 |
|
| 3642 |
+
|
| 3643 |
+
def apply_all_shapes_rotation(
|
| 3644 |
+
records: list[dict] | None,
|
| 3645 |
+
settings_table: Any,
|
| 3646 |
+
rotate_x: Any,
|
| 3647 |
+
rotate_y: Any,
|
| 3648 |
+
rotate_z: Any,
|
| 3649 |
+
) -> tuple:
|
| 3650 |
+
"""Set the SAME rotation on every uploaded shape at once.
|
| 3651 |
+
|
| 3652 |
+
The angle is absolute (like the single-shape apply): every shape's
|
| 3653 |
+
rotation becomes exactly the entered value, each carrying its own
|
| 3654 |
+
configured dimensions through the turn. Split pieces are derived
|
| 3655 |
+
geometry and are skipped with a note.
|
| 3656 |
+
"""
|
| 3657 |
+
records = _apply_shape_settings(records or [], settings_table)
|
| 3658 |
+
if not records:
|
| 3659 |
+
return records, _shape_settings_rows(records), "Load shapes before rotating."
|
| 3660 |
+
|
| 3661 |
+
rotation = _normalize_rotation_input(rotate_x, rotate_y, rotate_z)
|
| 3662 |
+
rotated_count = 0
|
| 3663 |
+
skipped_pieces = 0
|
| 3664 |
+
errors: list[str] = []
|
| 3665 |
+
for record in records:
|
| 3666 |
+
if not record.get("stl_path"):
|
| 3667 |
+
skipped_pieces += 1
|
| 3668 |
+
continue
|
| 3669 |
+
error = _apply_rotation_to_record(record, rotation)
|
| 3670 |
+
if error:
|
| 3671 |
+
errors.append(f"Shape {record.get('idx')}: {error}")
|
| 3672 |
else:
|
| 3673 |
+
rotated_count += 1
|
| 3674 |
+
|
| 3675 |
+
if any(rotation):
|
| 3676 |
status = (
|
| 3677 |
+
f"Rotated {rotated_count} shape(s) to {_rotation_angle_text(rotation)}. "
|
| 3678 |
+
"Every shape's dimensions follow its rotation; the next Generate "
|
| 3679 |
+
"G-Code (or split) slices the rotated shapes."
|
| 3680 |
)
|
| 3681 |
else:
|
| 3682 |
+
status = f"Rotation cleared for {rotated_count} shape(s)."
|
| 3683 |
+
if skipped_pieces:
|
| 3684 |
+
status += (
|
| 3685 |
+
f" \n{skipped_pieces} split piece(s) skipped — rotate before "
|
| 3686 |
+
"splitting, or re-split after rotating."
|
| 3687 |
+
)
|
| 3688 |
+
if errors:
|
| 3689 |
+
status += " \n" + " \n".join(errors)
|
| 3690 |
return records, _shape_settings_rows(records), status
|
| 3691 |
|
| 3692 |
|
| 3693 |
+
def apply_all_shapes_z_rotation(
|
| 3694 |
+
records: list[dict] | None, settings_table: Any, angle: Any
|
| 3695 |
+
) -> tuple:
|
| 3696 |
+
"""UI entry point: spin EVERY shape on the bed by the same angle."""
|
| 3697 |
+
return apply_all_shapes_rotation(records, settings_table, 0.0, 0.0, angle)
|
| 3698 |
+
|
| 3699 |
+
|
| 3700 |
def apply_shape_z_rotation(
|
| 3701 |
records: list[dict] | None,
|
| 3702 |
selected: str | None,
|
|
|
|
| 5562 |
"Apply Rotation", variant="secondary", size="sm",
|
| 5563 |
min_width=140, scale=1,
|
| 5564 |
)
|
| 5565 |
+
rotate_all_button = gr.Button(
|
| 5566 |
+
"Rotate All Shapes", variant="secondary", size="sm",
|
| 5567 |
+
min_width=150, scale=1,
|
| 5568 |
+
)
|
| 5569 |
+
gr.HTML("", scale=2)
|
| 5570 |
rotation_status = gr.Markdown("")
|
| 5571 |
|
| 5572 |
with gr.Row():
|
|
|
|
| 6102 |
outputs=[gcode_stale_banner],
|
| 6103 |
queue=False,
|
| 6104 |
)
|
| 6105 |
+
rotate_all_button.click(
|
| 6106 |
+
fn=apply_all_shapes_z_rotation,
|
| 6107 |
+
inputs=[shape_records, shape_settings, rotate_input],
|
| 6108 |
+
outputs=[shape_records, shape_settings, rotation_status],
|
| 6109 |
+
).then(
|
| 6110 |
+
fn=show_selected_model,
|
| 6111 |
+
inputs=preview_inputs,
|
| 6112 |
+
outputs=[model_viewer, model_details],
|
| 6113 |
+
).then(
|
| 6114 |
+
fn=update_layer_preview,
|
| 6115 |
+
inputs=layer_preview_inputs,
|
| 6116 |
+
outputs=layer_preview_outputs,
|
| 6117 |
+
).then(
|
| 6118 |
+
fn=check_gcode_staleness,
|
| 6119 |
+
inputs=stale_inputs,
|
| 6120 |
+
outputs=[gcode_stale_banner],
|
| 6121 |
+
queue=False,
|
| 6122 |
+
)
|
| 6123 |
# Selecting a shape shows its stored rotation in the input.
|
| 6124 |
selected_shape.change(
|
| 6125 |
fn=selected_shape_z_rotation,
|
|
@@ -1249,6 +1249,46 @@ def test_keep_proportions_is_stale_echo_proof() -> None:
|
|
| 1249 |
assert updated3[0]["target_z"] == 43.3
|
| 1250 |
|
| 1251 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1252 |
def _mm_member(idx: int, nozzle: int, ox: float, oy: float, oz: float) -> dict:
|
| 1253 |
return {
|
| 1254 |
"idx": idx, "name": f"part{idx}", "stl_path": f"part{idx}.stl",
|
|
@@ -1807,6 +1847,58 @@ def test_apply_shape_rotation_updates_dims_and_reslices(tmp_path) -> None:
|
|
| 1807 |
assert (spun[0]["target_x"], spun[0]["target_y"], spun[0]["target_z"]) == (4.0, 10.0, 2.0)
|
| 1808 |
assert selected_shape_z_rotation(spun, None) == 90.0
|
| 1809 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1810 |
|
| 1811 |
def test_project_settings_export_import_round_trip(tmp_path) -> None:
|
| 1812 |
from app import export_project_settings, import_project_settings
|
|
|
|
| 1249 |
assert updated3[0]["target_z"] == 43.3
|
| 1250 |
|
| 1251 |
|
| 1252 |
+
def test_keep_proportions_preserves_the_entered_value_despite_rounding() -> None:
|
| 1253 |
+
# User scenario: a round shape whose X/Y originals are EQUAL and whose
|
| 1254 |
+
# small axis rounds back onto its own original (12.4 * 25/25.1 -> 12.4).
|
| 1255 |
+
# On the echo round the odd-one-out heuristic then saw X/Y agreeing and
|
| 1256 |
+
# anchored on the small axis (ratio 1.0), rewriting the typed 25 back to
|
| 1257 |
+
# 25.1. A fresh edit must anchor on the edited axis, and the echo must
|
| 1258 |
+
# recognize the row as converged (recomputes to itself from its anchor).
|
| 1259 |
+
def _record() -> dict:
|
| 1260 |
+
return {
|
| 1261 |
+
"idx": 1, "name": "sphere", "stl_path": "sphere.stl",
|
| 1262 |
+
"original_x": 25.1, "original_y": 25.1, "original_z": 12.4,
|
| 1263 |
+
"target_x": 25.1, "target_y": 25.1, "target_z": 12.4,
|
| 1264 |
+
"pressure": 25.0, "valve": 4, "nozzle": 1, "port": 1,
|
| 1265 |
+
"color": "#111111", "last_scaled_axis": "target_x",
|
| 1266 |
+
}
|
| 1267 |
+
|
| 1268 |
+
records = [_record()]
|
| 1269 |
+
rows = _shape_settings_rows(records)
|
| 1270 |
+
rows[0][2] = 25.0 # the user types 25 into X
|
| 1271 |
+
updated, table_out = normalize_shape_dimensions_for_mode(
|
| 1272 |
+
records, rows, SCALE_MODE_UNIFORM_FACTOR
|
| 1273 |
+
)
|
| 1274 |
+
assert (updated[0]["target_x"], updated[0]["target_y"], updated[0]["target_z"]) == (
|
| 1275 |
+
25.0, 25.0, 12.4,
|
| 1276 |
+
)
|
| 1277 |
+
assert isinstance(table_out, list)
|
| 1278 |
+
|
| 1279 |
+
# The echo of our own write-back must be a converged no-op — the typed
|
| 1280 |
+
# value stays 25.0 forever, on every echo.
|
| 1281 |
+
for _ in range(3):
|
| 1282 |
+
echo_rows = _shape_settings_rows(updated)
|
| 1283 |
+
updated, table_out = normalize_shape_dimensions_for_mode(
|
| 1284 |
+
updated, echo_rows, SCALE_MODE_UNIFORM_FACTOR
|
| 1285 |
+
)
|
| 1286 |
+
assert (updated[0]["target_x"], updated[0]["target_y"], updated[0]["target_z"]) == (
|
| 1287 |
+
25.0, 25.0, 12.4,
|
| 1288 |
+
)
|
| 1289 |
+
assert not isinstance(table_out, list)
|
| 1290 |
+
|
| 1291 |
+
|
| 1292 |
def _mm_member(idx: int, nozzle: int, ox: float, oy: float, oz: float) -> dict:
|
| 1293 |
return {
|
| 1294 |
"idx": idx, "name": f"part{idx}", "stl_path": f"part{idx}.stl",
|
|
|
|
| 1847 |
assert (spun[0]["target_x"], spun[0]["target_y"], spun[0]["target_z"]) == (4.0, 10.0, 2.0)
|
| 1848 |
assert selected_shape_z_rotation(spun, None) == 90.0
|
| 1849 |
|
| 1850 |
+
# CUSTOM dimensions survive rotation: scale the spun bar to 8 x 20 x 4,
|
| 1851 |
+
# rotate back to 0° — the dims follow the spin to 20 x 8 x 4 instead of
|
| 1852 |
+
# snapping back to the natural 10 x 4 x 2 (originals stay natural, they
|
| 1853 |
+
# are the Keep Proportions ratio basis).
|
| 1854 |
+
spun[0]["target_x"], spun[0]["target_y"], spun[0]["target_z"] = 8.0, 20.0, 4.0
|
| 1855 |
+
unspun, _rows, _unspun_status = apply_shape_z_rotation(spun, None, None, 0)
|
| 1856 |
+
assert (unspun[0]["target_x"], unspun[0]["target_y"], unspun[0]["target_z"]) == (20.0, 8.0, 4.0)
|
| 1857 |
+
assert (unspun[0]["original_x"], unspun[0]["original_y"], unspun[0]["original_z"]) == (10.0, 4.0, 2.0)
|
| 1858 |
+
respun, _rows, _respun_status = apply_shape_z_rotation(unspun, None, None, 90)
|
| 1859 |
+
assert (respun[0]["target_x"], respun[0]["target_y"], respun[0]["target_z"]) == (8.0, 20.0, 4.0)
|
| 1860 |
+
assert (respun[0]["original_x"], respun[0]["original_y"], respun[0]["original_z"]) == (4.0, 10.0, 2.0)
|
| 1861 |
+
|
| 1862 |
+
|
| 1863 |
+
def test_rotate_all_shapes_at_once(tmp_path) -> None:
|
| 1864 |
+
import trimesh
|
| 1865 |
+
|
| 1866 |
+
from app import apply_all_shapes_z_rotation
|
| 1867 |
+
|
| 1868 |
+
for name, extents in (("bar", (10.0, 4.0, 2.0)), ("slab", (6.0, 2.0, 1.0))):
|
| 1869 |
+
trimesh.creation.box(extents=extents).export(tmp_path / f"{name}.stl")
|
| 1870 |
+
|
| 1871 |
+
records = _records_from_files(
|
| 1872 |
+
[str(tmp_path / "bar.stl"), str(tmp_path / "slab.stl")], None
|
| 1873 |
+
)
|
| 1874 |
+
# The bar carries custom dimensions; the slab stays natural. A split
|
| 1875 |
+
# piece (no stl_path) rides along and must be skipped.
|
| 1876 |
+
records[0]["target_x"], records[0]["target_y"] = 20.0, 8.0
|
| 1877 |
+
records.append({
|
| 1878 |
+
"idx": 3, "name": "piece", "stl_path": None, "nozzle": 3,
|
| 1879 |
+
"split_group_id": "split-a", "split_index": 0,
|
| 1880 |
+
"target_x": 5.0, "target_y": 5.0, "target_z": 2.0,
|
| 1881 |
+
})
|
| 1882 |
+
|
| 1883 |
+
updated, rows, status = apply_all_shapes_z_rotation(records, None, 90)
|
| 1884 |
+
|
| 1885 |
+
assert "Rotated 2 shape(s) to 90°" in status
|
| 1886 |
+
assert "1 split piece(s) skipped" in status
|
| 1887 |
+
assert updated[0]["rotation"] == (0.0, 0.0, 90.0)
|
| 1888 |
+
assert updated[1]["rotation"] == (0.0, 0.0, 90.0)
|
| 1889 |
+
# Custom dims follow the spin; natural dims swap to the rotated bbox.
|
| 1890 |
+
assert (updated[0]["target_x"], updated[0]["target_y"], updated[0]["target_z"]) == (8.0, 20.0, 2.0)
|
| 1891 |
+
assert (updated[1]["target_x"], updated[1]["target_y"], updated[1]["target_z"]) == (2.0, 6.0, 1.0)
|
| 1892 |
+
# The split piece is untouched.
|
| 1893 |
+
assert updated[2].get("rotation") is None
|
| 1894 |
+
assert updated[2]["target_x"] == 5.0
|
| 1895 |
+
|
| 1896 |
+
# Clearing restores every shape's configured dims.
|
| 1897 |
+
cleared, _rows, cleared_status = apply_all_shapes_z_rotation(updated, None, 0)
|
| 1898 |
+
assert "Rotation cleared for 2 shape(s)" in cleared_status
|
| 1899 |
+
assert (cleared[0]["target_x"], cleared[0]["target_y"]) == (20.0, 8.0)
|
| 1900 |
+
assert (cleared[1]["target_x"], cleared[1]["target_y"]) == (6.0, 2.0)
|
| 1901 |
+
|
| 1902 |
|
| 1903 |
def test_project_settings_export_import_round_trip(tmp_path) -> None:
|
| 1904 |
from app import export_project_settings, import_project_settings
|