gendesign/backend/app/services/generative/exporters/dxf.py
Light1YT e0fff9cfb0
Some checks failed
CI / changes (push) Successful in 7s
CI / frontend-tests (push) Has been skipped
CI / changes (pull_request) Successful in 10s
CI / frontend-tests (pull_request) Has been skipped
CI / openapi-codegen-check (push) Successful in 1m45s
CI / openapi-codegen-check (pull_request) Successful in 1m43s
CI / backend-tests (push) Failing after 8m36s
CI / backend-tests (pull_request) Failing after 8m36s
fix(week-review): backend-аудит v2 — 82 issue (#1560-1656)
Имплементация фиксов 2-го аудита backend/app/** (после merge #1543).
Воркер на файл, точечные правки. Верификация: py_compile 58/58 .py.

Полностью исправлено (82).
Оставлены открытыми (13): partial/needs-cross-file/needs-leha — #1569, #1590, #1593, #1606, #1609, #1617, #1633, #1635, #1637, #1638, #1640, #1642, #1650.

Closes #1560
Closes #1561
Closes #1562
Closes #1563
Closes #1564
Closes #1565
Closes #1566
Closes #1567
Closes #1570
Closes #1571
Closes #1572
Closes #1573
Closes #1574
Closes #1576
Closes #1577
Closes #1578
Closes #1579
Closes #1580
Closes #1581
Closes #1582
Closes #1583
Closes #1584
Closes #1585
Closes #1586
Closes #1587
Closes #1588
Closes #1589
Closes #1591
Closes #1592
Closes #1594
Closes #1595
Closes #1596
Closes #1597
Closes #1598
Closes #1599
Closes #1600
Closes #1601
Closes #1602
Closes #1603
Closes #1604
Closes #1605
Closes #1607
Closes #1608
Closes #1610
Closes #1611
Closes #1612
Closes #1613
Closes #1614
Closes #1615
Closes #1616
Closes #1618
Closes #1619
Closes #1620
Closes #1621
Closes #1622
Closes #1623
Closes #1624
Closes #1625
Closes #1626
Closes #1627
Closes #1628
Closes #1629
Closes #1630
Closes #1631
Closes #1632
Closes #1634
Closes #1636
Closes #1639
Closes #1641
Closes #1643
Closes #1644
Closes #1645
Closes #1646
Closes #1647
Closes #1648
Closes #1649
Closes #1651
Closes #1652
Closes #1653
Closes #1654
Closes #1655
Closes #1656
2026-06-17 01:30:52 +05:00

175 lines
7.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Generative Design — Stage 1c DXF export via ezdxf.
Renders the parcel context (boundary + buildable area) and one variant's placed
building footprints into a binary DXF for hand-off to architects. Geometry is drawn
in the parcel's *metric* CRS (metres) — architects work in metres, and DXF has no
geographic CRS concept, so emitting WGS84 degrees would be unusable.
Layers:
* ``PARCEL`` — границы участка (синий).
* ``BUILDABLE`` — пятно застройки после отступов (зелёный, пунктир-цвет).
* ``BUILDINGS`` — секции варианта (красный), с текстовой подписью номера секции.
Returns ``bytes`` (binary DXF, R2010) ready for an HTTP response / file write.
Deterministic, no DB / no network. ``ezdxf`` is a light import, so it stays at
module level (unlike WeasyPrint in :mod:`pdf`).
"""
from __future__ import annotations
import io
import logging
# ezdxf.new живёт в ezdxf.filemanagement и не реэкспортируется через ezdxf.__all__;
# импорт из модуля удовлетворяет strict no-implicit-reexport.
from ezdxf.enums import TextEntityAlignment
from ezdxf.filemanagement import new as ezdxf_new
from shapely.geometry import Polygon
from app.schemas.concept import ConceptVariant
from app.services.generative.geometry import Parcel
logger = logging.getLogger(__name__)
# AutoCAD Color Index (ACI) per layer.
_ACI_PARCEL = 5 # blue
_ACI_BUILDABLE = 3 # green
_ACI_BUILDINGS = 1 # red
_LAYER_PARCEL = "PARCEL"
_LAYER_BUILDABLE = "BUILDABLE"
_LAYER_BUILDINGS = "BUILDINGS"
# Высота текста подписи секции (метры в модельном пространстве).
_LABEL_HEIGHT_M = 2.0
def _ring_points(coords: object) -> list[tuple[float, float]]:
"""Кольцо (exterior/interior) как список (x, y) для LWPolyline (без замыкающей точки)."""
pts = list(coords)
# Shapely дублирует первую точку в конце; close=True у ezdxf замкнёт сам.
if len(pts) > 1 and pts[0] == pts[-1]:
pts = pts[:-1]
return [(float(x), float(y)) for x, y in pts]
def _add_polygon(msp: object, poly: Polygon, layer: str) -> None:
"""Нарисовать полигон на слое: внешнее кольцо + каждое внутреннее (отверстие).
LWPolyline не умеет дырки, поэтому каждое interior-кольцо эмитируется отдельной
замкнутой полилинией на том же слое — иначе легальные вырезы (двор, сервитут,
охранная зона) терялись бы и заливались сплошняком.
"""
msp.add_lwpolyline(
_ring_points(poly.exterior.coords),
close=True,
dxfattribs={"layer": layer},
)
for interior in poly.interiors:
msp.add_lwpolyline(
_ring_points(interior.coords),
close=True,
dxfattribs={"layer": layer},
)
def export_concept_dxf(parcel: Parcel, variant: ConceptVariant) -> bytes:
"""Собрать binary DXF: участок + пятно застройки + секции одного варианта.
Args:
parcel: Stage 1a участок (метрическая геометрия parcel/buildable).
variant: вариант, чьи секции рисуем (footprints берём из его geojson —
но геометрию рисуем из метрического parcel-space через свежий парсинг
geojson обратно нельзя без CRS, поэтому секции восстанавливаем ниже).
Returns:
bytes: бинарный DXF R2010.
"""
doc = ezdxf_new("R2010")
doc.layers.add(_LAYER_PARCEL, color=_ACI_PARCEL)
doc.layers.add(_LAYER_BUILDABLE, color=_ACI_BUILDABLE)
doc.layers.add(_LAYER_BUILDINGS, color=_ACI_BUILDINGS)
msp = doc.modelspace()
# Участок и пятно застройки — из метрической геометрии Parcel.
_add_polygon(msp, parcel.polygon_m, _LAYER_PARCEL)
_add_polygon(msp, parcel.buildable_m, _LAYER_BUILDABLE)
# Секции: восстанавливаем метрические footprints из WGS84-geojson варианта.
features = variant.buildings_geojson.get("features", [])
section_count = 0
if isinstance(features, list):
for feature in features:
footprint = _feature_to_metric_polygon(parcel, feature)
if footprint is None:
continue
section_count += 1
_add_polygon(msp, footprint, _LAYER_BUILDINGS)
centroid = footprint.centroid
label = str(_feature_section_id(feature, section_count))
text = msp.add_text(
label,
dxfattribs={"layer": _LAYER_BUILDINGS, "height": _LABEL_HEIGHT_M},
)
text.set_placement(
(float(centroid.x), float(centroid.y)),
align=TextEntityAlignment.MIDDLE_CENTER,
)
stream = io.BytesIO()
doc.write(stream, fmt="bin")
data = stream.getvalue()
logger.info(
"DXF export: strategy=%s sections=%d bytes=%d",
variant.strategy,
section_count,
len(data),
)
return data
def _feature_section_id(feature: object, fallback: int) -> int:
"""Достать section_id из properties Feature, иначе fallback-счётчик."""
if isinstance(feature, dict):
props = feature.get("properties")
if isinstance(props, dict):
sid = props.get("section_id")
if isinstance(sid, int):
return sid
return fallback
def _feature_to_metric_polygon(parcel: Parcel, feature: object) -> Polygon | None:
"""WGS84 GeoJSON Feature -> метрический Shapely Polygon (через обратный трансформер).
Возвращает None для невалидных/непригодных фич (graceful — экспорт не падает).
"""
if not isinstance(feature, dict):
return None
geometry = feature.get("geometry")
if not isinstance(geometry, dict) or geometry.get("type") != "Polygon":
return None
coords = geometry.get("coordinates")
# Shapely mapping() emits nested tuples; accept both tuple and list.
if not isinstance(coords, (list, tuple)) or not coords:
return None
ring = coords[0]
if not isinstance(ring, (list, tuple)) or len(ring) < 4:
return None
metric_pts: list[tuple[float, float]] = []
for pt in ring:
if not isinstance(pt, (list, tuple)) or len(pt) < 2:
return None
lon, lat = float(pt[0]), float(pt[1])
x, y = parcel.wgs84_to_metric(lon, lat)
metric_pts.append((x, y))
try:
poly = Polygon(metric_pts)
except (ValueError, TypeError):
return None
return poly if (poly.is_valid and not poly.is_empty) else None
__all__ = ["export_concept_dxf"]