fix(blocks): block_define refuses a non-finite base point and a missing layer unless create_layers

base_x/base_y were only float()-cast, so a NaN base point was written into
the BLOCK record; a primitive on a layer the drawing lacks was written as a
dangling reference doc.audit() never flags (and on COM would fail mid-loop
after Blocks.Add). validate_base_point names the key; the layer table is
probed before the first write and the missing names are refused, or created
and reported as layers_created when create_layers=true (new optional kwarg,
contract + both engines + tool).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FKFStjEFzpGyLq5bMpWxTd
This commit is contained in:
Umut CAN
2026-09-18 00:11:38 +03:00
co-authored by Claude Opus 5
parent 7f79497928
commit 375c9f4c70
7 changed files with 165 additions and 17 deletions
+16 -1
View File
@@ -33,7 +33,10 @@ _POSITIVE = {"r", "height"}
def _scalar(value, where: str, *, positive: bool = False) -> float:
if isinstance(value, bool) or not isinstance(value, (int, float)):
raise TypeError(f"{where} must be a number, got {value!r}")
number = float(value)
try:
number = float(value)
except OverflowError as exc: # a 400-digit JSON integer is an int, not a float
raise TypeError(f"{where} must be finite") from exc
if not math.isfinite(number):
raise TypeError(f"{where} must be finite")
if positive and number <= 0:
@@ -204,6 +207,18 @@ def validate_attdef_specs(attdefs) -> list[dict]:
return out
def validate_base_point(base_x, base_y) -> tuple[float, float]:
"""``block_define``'s base point as finite floats; ``TypeError`` names the key.
Only ``float()`` was applied before, so ``float("nan")`` was written into
the BLOCK record headlessly and would reach AutoCAD as a NaN VARIANT.
"""
return (
_scalar(base_x, "block_define: 'base_x'"),
_scalar(base_y, "block_define: 'base_y'"),
)
def validate_attribute_values(attributes, where: str = "attributes") -> dict[str, str]:
"""``{TAG: value}`` for ``block_insert`` / ``block_set_attributes`` as the
strings that will be written, refused before any write.