Files
dxfmakros/lib/export_planquadrat.py
T
m.stangl 193b0dea04 CSV-Export: Bounding-Box (Position/Boundingbox) und Planquadrat-Spalte ergaenzt
Nur EXPORTCSV betroffen, EXPORTSIVAS bleibt unveraendert:
- Position/Boundingbox: Bounding-Box je Block per vla-getboundingbox
  (Lisp/export.lsp, csv:get-bbox), Mittelpunkt und Ausdehnung x/y/z.
- Planquadrat: aus x/y-Koordinate berechnet nach cfg/export.cfg
  [Planquadrate] (Schrittweite, numerisch/alphabetisch je Achse,
  alphabetische Zaehlung mit Excel-Spalten-Umlauf Z->AA), siehe
  lib/export_planquadrat.py.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-22 12:52:33 +02:00

72 lines
2.6 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
export_planquadrat.py - Berechnet das Planquadrat eines Blocks aus x/y-Koordinate.
Liest die aktive Planquadrat-Konfiguration aus cfg/export.cfg
([Planquadrate] -> section=<Name der Sub-Sektion>, z.B. Planquadrate_AN) und
rundet die x/y-Koordinate eines Blocks (mm, wie im Roh-JSON von export.lsp)
je Achse auf die konfigurierte Schrittweite (in Metern) herunter.
Nur von export_csv.py genutzt (EXPORTCSV), nicht von export_sivas.py.
"""
import configparser
from export_blockpatterns import cfg_path_from_env
def load_planquadrat_config(cfg_path=None):
"""Liest die aktive Planquadrat-Konfiguration aus export.cfg.
Rueckgabe: dict mit x_step_mm, x_alpha, y_step_mm, y_alpha, format,
oder None, wenn [Planquadrate] bzw. die referenzierte Sub-Sektion fehlt.
"""
if cfg_path is None:
cfg_path = cfg_path_from_env()
# inline_comment_prefixes noetig, da export.cfg Werte wie
# "numerisch # zahlen, von links nach rechts, 2m pro Spalte" nutzt.
parser = configparser.ConfigParser(inline_comment_prefixes=("#",))
parser.read(cfg_path, encoding="utf-8")
if not parser.has_section("Planquadrate"):
return None
section_name = parser.get("Planquadrate", "section", fallback=None)
if not section_name or not parser.has_section(section_name):
return None
sec = parser[section_name]
return {
"x_step_mm": float(sec.get("x_step", "1")) * 1000.0,
"x_alpha": sec.get("x_description", "numerisch").strip().lower().startswith("alpha"),
"y_step_mm": float(sec.get("y_step", "1")) * 1000.0,
"y_alpha": sec.get("y_description", "numerisch").strip().lower().startswith("alpha"),
"format": sec.get("format", "{x}/{y}").strip(),
}
def _index_to_alpha(n):
"""Wandelt einen 1-basierten Index in eine Buchstaben-Bezeichnung um.
1=A, 26=Z, 27=AA, 28=AB, ... (wie Excel-Spaltennamen).
"""
result = ""
while n > 0:
n, remainder = divmod(n - 1, 26)
result = chr(65 + remainder) + result
return result
def _index_for(coord_mm, step_mm):
"""1-basierter Planquadrat-Index einer Koordinate fuer eine Schrittweite."""
return int(coord_mm // step_mm) + 1
def compute_planquadrat(x_mm, y_mm, cfg):
"""Berechnet die Planquadrat-Bezeichnung fuer eine x/y-Koordinate (mm)."""
x_idx = _index_for(x_mm, cfg["x_step_mm"])
y_idx = _index_for(y_mm, cfg["y_step_mm"])
x_label = _index_to_alpha(x_idx) if cfg["x_alpha"] else str(x_idx)
y_label = _index_to_alpha(y_idx) if cfg["y_alpha"] else str(y_idx)
return cfg["format"].format(x=x_label, y=y_label)