#!/usr/bin/env python3
"""DXFs do pacote de execução da Oficina — PR-02, PR-04 a PR-09 (PR-01 é o
KML; PR-03 já tem `canteiro-radier.dxf`).

Unidades em metros, layers por categoria, cotas com entidades DIMENSION
reais. Gera `oficina-*.dxf` em `referencias-drive/desenhos/`.
"""

import ezdxf
from ezdxf.enums import TextEntityAlignment

OUT = "/home/paulo/Documentos/Paulo/Sítio/referencias-drive/desenhos"

PILARES = [
    (0, 0),
    (1.5, 0),
    (4.5, 0),
    (6, 0),
    (0, 3),
    (6, 3),
    (0, 6),
    (6, 6),
    (0, 9),
    (6, 9),
    (0, 12),
    (1.5, 12),
    (4.5, 12),
    (6, 12),
    (0, -4),
    (3, -4),
    (6, -4),
]


def novo(nome):
    doc = ezdxf.new("R2010")
    doc.header["$INSUNITS"] = 6  # metros
    for layer, color, ltype in [
        ("PAREDES", 7, "Continuous"),
        ("PILAR", 7, "Continuous"),
        ("ZONAS", 8, "Continuous"),
        ("ESQUADRIAS", 3, "Continuous"),
        ("ELETRICA", 1, "Continuous"),
        ("HIDRAULICA", 5, "Continuous"),
        ("COBERTURA", 4, "Continuous"),
        ("COTAS", 3, "Continuous"),
        ("TEXTOS", 7, "Continuous"),
    ]:
        try:
            doc.layers.add(layer, color=color, linetype=ltype)
        except Exception:
            pass
    return doc


def rect(msp, x0, y0, w, h, layer):
    msp.add_lwpolyline(
        [(x0, y0), (x0 + w, y0), (x0 + w, y0 + h), (x0, y0 + h)],
        close=True,
        dxfattribs={"layer": layer},
    )


def texto(msp, x, y, s, h=0.25):
    t = msp.add_text(s, dxfattribs={"layer": "TEXTOS", "height": h})
    t.set_placement((x, y), align=TextEntityAlignment.CENTER)


def cota_h(msp, x0, x1, y, off=-0.8):
    msp.add_linear_dim(
        base=(0, y + off),
        p1=(x0, y),
        p2=(x1, y),
        dxfattribs={"layer": "COTAS"},
    ).render()


def cota_v(msp, x, y0, y1, off=-0.8):
    msp.add_linear_dim(
        base=(x + off, 0),
        p1=(x, y0),
        p2=(x, y1),
        angle=90,
        dxfattribs={"layer": "COTAS"},
    ).render()


def salva(doc, nome):
    doc.saveas(f"{OUT}/{nome}")
    print(f"{nome} OK")


# ================= PR-02 planta baixa =================
doc = novo("oficina-planta-baixa")
m = doc.modelspace()
rect(m, 0, 0, 6, 12, "PAREDES")  # oficina
rect(m, 0, -4, 6, 4, "PAREDES")  # anexo
rect(m, -3.8, 13.6, 3.8, 4.3, "PAREDES")  # depósito
for z in [(0, 0, 6, 3), (0, 3, 2.5, 5), (2.5, 3, 3.5, 5), (0, 8, 3, 4), (3, 8, 3, 4)]:
    rect(m, *z, "ZONAS")
for px, py in PILARES:
    rect(m, px - 0.075, py - 0.075, 0.15, 0.15, "PILAR")
m.add_line((2, 0), (4, 0), dxfattribs={"layer": "ESQUADRIAS"})  # portão
m.add_line((6, -3.25), (6, -0.75), dxfattribs={"layer": "ESQUADRIAS"})
m.add_line((-2.4, 13.6), (-1.4, 13.6), dxfattribs={"layer": "ESQUADRIAS"})
texto(m, 3, 6, "OFICINA 6x12 - ver PR-02", 0.3)
cota_h(m, 0, 6, 0)
cota_v(m, 0, -4, 12)
salva(doc, "oficina-planta-baixa.dxf")

# ================= PR-04 cobertura =================
doc = novo("oficina-cobertura")
m = doc.modelspace()
rect(m, -0.5, -0.5, 7, 13, "COBERTURA")  # beiral
rect(m, 0, -4, 6, 16, "COBERTURA")  # projeção c/ anexo
m.add_line((3, 0), (3, 12), dxfattribs={"layer": "COBERTURA"})  # cumeeira
for xt in (1.5, 4.5):
    m.add_line((xt, 0), (xt, 12), dxfattribs={"layer": "COBERTURA"})
m.add_line((3, -4), (3, 0), dxfattribs={"layer": "COBERTURA"})  # anexo 1 água
texto(m, 3, 6, "CUMEIRA N-S - aguas ~3 m", 0.3)
texto(m, 3, -2, "ANEXO uma agua p/ sul 30%", 0.3)
cota_h(m, 0, 6, -4)
cota_v(m, 0, -4, 12)
salva(doc, "oficina-cobertura.dxf")

# ================= PR-05 elevações =================
doc = novo("oficina-elevacoes")
m = doc.modelspace()


def fachada(x0, y0, larg, titulo, portao=0.0):
    m.add_line((x0 - 0.5, y0), (x0 + larg + 0.5, y0), dxfattribs={"layer": "PAREDES"})
    rect(m, x0, y0, larg, 1.2, "PAREDES")
    rect(m, x0, y0 + 1.2, larg, 1.8, "PAREDES")
    m.add_line(
        (x0 - 0.4, y0 + 3.0),
        (x0 + larg / 2, y0 + 3.9),
        dxfattribs={"layer": "COBERTURA"},
    )
    m.add_line(
        (x0 + larg / 2, y0 + 3.9),
        (x0 + larg + 0.4, y0 + 3.0),
        dxfattribs={"layer": "COBERTURA"},
    )
    if portao:
        m.add_line(
            (x0 + larg / 2 - portao / 2, y0),
            (x0 + larg / 2 + portao / 2, y0),
            dxfattribs={"layer": "ESQUADRIAS"},
        )
    texto(m, x0 + larg / 2, y0 - 0.9, titulo, 0.3)


fachada(0, 8, 6, "SUL - portao 2 m", portao=2.0)
fachada(8, 8, 6, "NORTE")
fachada(0, 0, 12, "LESTE")
fachada(14, 0, 12, "OESTE")
salva(doc, "oficina-elevacoes.dxf")

# ================= PR-06 elétrica =================
doc = novo("oficina-eletrica")
m = doc.modelspace()
rect(m, 0, 0, 6, 12, "PAREDES")
pts = {
    "QD": (6.0, 6.4),
    "T1": (1.5, 10.5),
    "T2": (4.3, 5.1),
    "T3": (4.5, 10.5),
    "FORCA": (1.25, 5.6),
    "T4": (5.0, 1.5),
}
for lab, (x, y) in pts.items():
    m.add_point((x, y), dxfattribs={"layer": "ELETRICA"})
    texto(m, x, y + 0.5, lab, 0.2)
    if lab != "QD":
        m.add_line((6.0, 6.4), (x, y), dxfattribs={"layer": "ELETRICA"})
for lx, ly in ((1.5, 3), (4.5, 3), (1.5, 9), (4.5, 9)):
    m.add_circle((lx, ly), 0.2, dxfattribs={"layer": "ELETRICA"})
texto(m, 3, 12.8, "QD + T1-T4 + FORCA + 4x LED", 0.3)
cota_h(m, 0, 6, 0)
cota_v(m, 0, 0, 12)
salva(doc, "oficina-eletrica.dxf")

# ================= PR-07 hidráulica =================
doc = novo("oficina-hidraulica")
m = doc.modelspace()
rect(m, 0, 0, 6, 12, "PAREDES")
m.add_line((6.0, 1.5), (12.0, 8.0), dxfattribs={"layer": "HIDRAULICA"})
rect(m, 4.3, 0.4, 1.4, 1.1, "HIDRAULICA")
m.add_point((6.0, 1.5), dxfattribs={"layer": "HIDRAULICA"})
texto(m, 3, 6, "PVC 25 - entrada ~14,5 m - torneira + lava-pecas", 0.3)
cota_h(m, 0, 6, 0)
cota_v(m, 0, 0, 12)
salva(doc, "oficina-hidraulica.dxf")

# ================= PR-08 detalhes (posições reais) =================
doc = novo("oficina-detalhes")
m = doc.modelspace()
rect(m, 0, 0, 0.15, 3.0, "PAREDES")  # D1 pilar
rect(m, -0.175, -0.3, 0.5, 0.3, "PAREDES")  # D1 sapata
rect(m, 2, 0, 5, 5, "PAREDES")  # D2 parede depósito
rect(m, 4, 0, 1, 4.4, "ESQUADRIAS")  # D2 porta 1 m
rect(m, 9, 0, 6, 3.4, "PAREDES")  # D4 portão (vão 2 m em x=12)
rect(m, 12, 0, 2, 2.6, "ESQUADRIAS")
rect(m, 17, 0, 12, 3.4, "PAREDES")  # D5 porta 2,5 m em x=21.5
rect(m, 21.5, 0, 2.5, 2.8, "ESQUADRIAS")
texto(m, 0, 3.6, "D1 pilar/sapata", 0.3)
texto(m, 4.5, 5.6, "D2 porta 1 m", 0.3)
texto(m, 12, 4.0, "D4 portao 2 m", 0.3)
texto(m, 23, 4.0, "D5 porta 2,5 m", 0.3)
salva(doc, "oficina-detalhes.dxf")

# ================= PR-09 solar =================
doc = novo("oficina-solar")
m = doc.modelspace()
rect(m, -15, -70, 30, 130, "PAREDES")  # faixa do lote (croqui)
rect(m, -3, -46, 6, 16, "PAREDES")  # oficina
rect(m, -4, -20, 8, 3, "PAREDES")  # estrutura solar
m.add_line((0, -20), (3, -34), dxfattribs={"layer": "ELETRICA"})
texto(m, 0, -38, "OFICINA", 1.0)
texto(m, 0, -18.5, "SOLAR 5x330Wp - N 10-15o", 0.8)
salva(doc, "oficina-solar.dxf")

print("todos os DXFs OK")
