Files

335 lines
15 KiB
Python
Raw Permalink 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.
"""Baut die CAD-Vorlagen von OPENBUREAU aus einer Quelle:
dist/openbureau-schraffuren.pat Schraffurmuster (metrisch, mm bei Massstab 1)
dist/openbureau-linientypen.lin Linientypen (metrisch)
dist/openbureau-cad-vorlagen.zip beides + Einzeldateien + Vorschauen + Lizenz
dist/einzeln/OB_*.pat|.lin jedes Muster / jeder Linientyp als eigene Datei
(AutoCAD verlangt für ein eigenes Muster eine
.pat-Datei mit genau dem Musternamen)
dist/vorlagen.json Verzeichnis für die Website (Name, Text, Dateien)
vorschau/*.svg eine Vorschau je Muster/Linientyp + Übersichten
python3 build.py (nur Standardbibliothek)
Format: AutoCAD .pat/.lin — gelesen von AutoCAD, BricsCAD, ZWCAD, DraftSight,
progeCAD, LibreCAD/QCAD (Schraffuren). Jede Linienfamilie:
winkel, x0,y0, dx,dy [, strich, lücke, …]
dx = Versatz entlang der Linie, dy = Abstand quer dazu (wie in acad.pat).
"""
import math
import zipfile
from pathlib import Path
ROOT = Path(__file__).parent
DIST = ROOT / "dist"
PREV = ROOT / "vorschau"
VERSION = "1.1"
# ---------------------------------------------------------------- Schraffuren
# (name, beschreibung, [familien], vorschau-massstab)
S = 1 / math.sqrt(5) # Zickzack-Hilfswerte (Steigung 2:1)
ZZ_L = 5 * math.sqrt(5) # Länge eines Zickzack-Schenkels bei Amplitude 10
PATTERNS = [
("OB_MAUERWERK", "Mauerwerk im Schnitt — Diagonale 45°",
[(45, 0, 0, 0, 2.5)], 1.0),
("OB_STAHLBETON", "Stahlbeton im Schnitt — Kreuzschraffur 45°/135°",
[(45, 0, 0, 0, 2.5), (135, 0, 0, 0, 2.5)], 1.0),
("OB_BETON", "Beton unbewehrt — Punkte und kurze Striche, unregelmässig",
[(0, 0, 0, 3.1, 4.3, 0, -6.2),
(60, 1.3, 2.1, 4.7, 5.9, 0.9, -8.5),
(135, 2.6, 0.7, 5.3, 6.7, 0.7, -10.1),
(20, 4.1, 3.3, 3.9, 7.3, 0, -7.9)], 1.0),
("OB_DAEMMUNG_WEICH", "Weiche Wärmedämmung — Zickzack (auf Dämmstärke skalieren: Massstab = Stärke/10)",
[(math.degrees(math.atan2(2, 1)), 0, 0, 10 * 2 * S, 10 * S, ZZ_L, -ZZ_L),
(180 - math.degrees(math.atan2(2, 1)), 0, 0, -10 * 2 * S, 10 * S, ZZ_L, -ZZ_L)], 0.35),
("OB_DAEMMUNG_HART", "Harte Wärmedämmung — weite Kreuzschraffur",
[(45, 0, 0, 0, 6), (135, 0, 0, 0, 6)], 1.0),
("OB_HOLZ_QUER", "Holz quer zur Faser — Kreuz mit Diagonale",
[(45, 0, 0, 0, 5), (135, 0, 0, 0, 5), (0, 0, 0, 0, 7.0711)], 0.8),
("OB_HOLZ_LAENGS", "Holz längs zur Faser — gestaffelte Maserung",
[(0, 0, 0, 7, 1.6, 9, -2, 4, -3),
(0, 3, 0.8, 5, 1.6, 6, -4, 11, -2)], 1.0),
("OB_ERDREICH", "Gewachsener Boden — Strichgruppen",
[(45, 0, 0, 6, 6, 3, -9),
(45, 1.0607, -1.0607, 6, 6, 3, -9),
(45, 2.1213, -2.1213, 6, 6, 3, -9),
(135, 4.5, 1.5, 6, 6, 2, -10)], 0.7),
("OB_KIES", "Kies / Schotter — versetzte Punkte und Striche",
[(0, 0, 0, 2.2, 2.9, 0, -4.1),
(0, 1.1, 1.4, 3.3, 3.7, 0.6, -5.9),
(75, 0.4, 0.9, 2.8, 4.6, 0.5, -6.3),
(150, 2.0, 0.3, 3.5, 5.2, 0.4, -7.1)], 1.0),
("OB_PUTZ", "Putz / Mörtel — feine Punkte",
[(0, 0, 0, 0.9, 1.1, 0, -1.8), (0, 0.45, 0.55, 0.9, 1.1, 0, -1.8)], 2.0),
("OB_BACKSTEIN_ANSICHT", "Backstein in Ansicht — Läuferverband 250×65, Fuge 10",
[(0, 0, 0, 0, 75), (90, 0, 0, 75, 130, 75, -75)], 0.06),
("OB_NATURSTEIN", "Naturstein in Ansicht — unregelmässiger Verband",
[(0, 0, 0, 0, 40),
(90, 0, 0, 40, 70, 40, -80),
(90, 25, 0, 40, 90, 40, -80)], 0.09),
("OB_SCHIFFSBODEN", "Parkett Schiffsboden — Dielen 100×1000, Drittelstoss",
[(0, 0, 0, 0, 100), (90, 0, 0, 100, 1000 / 3, 100, -200)], 0.025),
("OB_PLATTEN_30", "Platten 300×300 (Fugenraster)",
[(0, 0, 0, 0, 300), (90, 0, 0, 0, 300)], 0.03),
("OB_PLATTEN_30x60", "Platten 300×600, halber Versatz",
[(0, 0, 0, 0, 300), (90, 0, 0, 300, 300, 300, -300)], 0.02),
("OB_RASTER_100", "Raster 100 mm (z. B. Achs- oder Planraster)",
[(0, 0, 0, 0, 100), (90, 0, 0, 0, 100)], 0.03),
("OB_GLAS", "Glas im Schnitt — zwei feine Diagonalen in Gruppen",
[(45, 0, 0, 0, 6), (45, 0.6, -0.6, 0, 6)], 1.0),
("OB_WASSER", "Wasser / Gewässer — gestrichelte Wellenlinien",
[(0, 0, 0, 4, 3, 6, -2, 2, -2), (0, 2, 1.5, 4, 3, 3, -5)], 1.0),
("OB_GRUEN", "Grünfläche / Rasen — kurze Halme",
[(80, 0, 0, 3, 4, 1.2, -6), (100, 0.5, 0, 3, 4, 1.2, -6), (90, 2, 2, 3, 4, 1.0, -7)], 1.2),
]
# ---------------------------------------------------------------- Linientypen
# (name, beschreibung, definition) — Masse in mm bei Linientypfaktor 1
LINETYPES = [
("OB_ACHSE", "Achse / Mittellinie ____ . ____ . ____", "A,12,-2.5,0,-2.5"),
("OB_ACHSE_LANG", "Achse lang (grosse Massstäbe) ________ . ________", "A,24,-3,0,-3"),
("OB_VERDECKT", "Verdeckte Kante _ _ _ _ _", "A,4,-2"),
("OB_VERDECKT_FEIN", "Verdeckt fein - - - - - -", "A,2,-1"),
("OB_UEBER_SCHNITT", "Über der Schnittebene (z. B. Deckenrand) ___ . . ___ . . ___", "A,10,-2,0,-2,0,-2"),
("OB_PARZELLE", "Parzellengrenze _____ . . . _____", "A,16,-2,0,-2,0,-2,0,-2"),
("OB_BAULINIE", "Bau- und Niveaulinie ________ __ ________", "A,20,-2.5,4,-2.5"),
("OB_SCHNITT", "Schnittführung ______ _ ______", "A,14,-2.5,2,-2.5"),
("OB_PUNKT", "Punktiert . . . . . . .", "A,0,-1.5"),
("OB_STRICH_LANG", "Lang gestrichelt ______ ______", "A,10,-4"),
("OB_ABBRUCH", "Abbruch / Rückbau __ __ __ __", "A,6,-3"),
("OB_LEITUNG_W", "Leitung Wasser ---- W ---- W ----",
'A,10,-2,["W",STANDARD,S=2,R=0.0,X=-0.7,Y=-1],-2.5'),
("OB_LEITUNG_A", "Leitung Abwasser ---- A ---- A ----",
'A,10,-2,["A",STANDARD,S=2,R=0.0,X=-0.7,Y=-1],-2.5'),
("OB_LEITUNG_E", "Leitung Elektro ---- E ---- E ----",
'A,10,-2,["E",STANDARD,S=2,R=0.0,X=-0.7,Y=-1],-2.5'),
("OB_LEITUNG_G", "Leitung Gas ---- G ---- G ----",
'A,10,-2,["G",STANDARD,S=2,R=0.0,X=-0.7,Y=-1],-2.5'),
]
HEADER = f"""; OPENBUREAU — {{what}} {VERSION}
; https://openbureau.ch · https://git.openbureau.ch/karim/VORLAGEN
; Lizenz: CC0 1.0 — frei verwendbar, ohne Bedingungen.
; Metrisch: Masse in mm bei Massstab/Faktor 1 (Zeichnungseinheit mm).
; Bei Zeichnungseinheit m: Massstab 0.001.
"""
def ascii_(t):
"""CAD-Programme lesen .pat/.lin als ANSI — Umlaute und Sonderzeichen umschreiben."""
for a, b in (("—", "-"), ("–", "-"), ("×", "x"), ("°", " Grad"), ("ä", "ae"), ("ö", "oe"), ("ü", "ue"),
("Ä", "Ae"), ("Ö", "Oe"), ("Ü", "Ue"), ("ß", "ss"), ("·", "-"), ("…", "..."), ("é", "e")):
t = t.replace(a, b)
return t.encode("ascii", "replace").decode()
def fmt(v):
s = f"{v:.6f}".rstrip("0").rstrip(".")
return "0" if s in ("-0", "") else s
def write_pat():
out = [HEADER.format(what="Schraffuren"), ";%UNITS=MM", ""]
for name, desc, fams, _ in PATTERNS:
out.append(f"*{name},{desc.replace(',', ' -')}")
for f in fams:
out.append(",".join(fmt(v) for v in f))
out.append("")
(DIST / "openbureau-schraffuren.pat").write_text(ascii_("\r\n".join(out)) + "\r\n", encoding="ascii")
def write_lin():
out = [HEADER.format(what="Linientypen"), ""]
for name, desc, d in LINETYPES:
out += [f"*{name},{desc.replace(',', ' -')}", d, ""]
(DIST / "openbureau-linientypen.lin").write_text(ascii_("\r\n".join(out)) + "\r\n", encoding="ascii")
def write_single():
one = DIST / "einzeln"
one.mkdir(exist_ok=True)
for old in one.iterdir():
old.unlink()
for name, desc, fams, _ in PATTERNS:
out = [HEADER.format(what=f"Schraffur {name}"), ";%UNITS=MM", "", f"*{name},{desc.replace(',', ' -')}"]
out += [",".join(fmt(v) for v in f) for f in fams]
(one / f"{name}.pat").write_text(ascii_("\r\n".join(out)) + "\r\n", encoding="ascii")
for name, desc, d in LINETYPES:
out = [HEADER.format(what=f"Linientyp {name}"), "", f"*{name},{desc.replace(',', ' -')}", d]
(one / f"{name}.lin").write_text(ascii_("\r\n".join(out)) + "\r\n", encoding="ascii")
def write_manifest():
import json
data = {
"version": VERSION,
"lizenz": "CC0-1.0",
"schraffuren": [{"name": n, "text": d, "datei": f"dist/einzeln/{n}.pat", "vorschau": f"vorschau/{n}.svg"}
for n, d, _, _ in PATTERNS],
"linientypen": [{"name": n, "text": d.split(" ")[0], "datei": f"dist/einzeln/{n}.lin", "vorschau": f"vorschau/{n}.svg"}
for n, d, _ in LINETYPES],
}
(DIST / "vorlagen.json").write_text(json.dumps(data, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
# ---------------------------------------------------------------- Vorschau
INK = "#141414"
def clip(x1, y1, x2, y2, W, H):
"""Liang-Barsky auf [0,W]×[0,H]."""
dx, dy = x2 - x1, y2 - y1
t0, t1 = 0.0, 1.0
for p, q in ((-dx, x1), (dx, W - x1), (-dy, y1), (dy, H - y1)):
if p == 0:
if q < 0:
return None
continue
t = q / p
if p < 0:
if t > t1: return None
t0 = max(t0, t)
else:
if t < t0: return None
t1 = min(t1, t)
return x1 + t0 * dx, y1 + t0 * dy, x1 + t1 * dx, y1 + t1 * dy
def hatch_lines(fams, W, H, k):
"""Segmente (in Vorschau-Pixeln) für ein Muster im Feld W×H, Massstab k px/mm."""
segs, dots = [], []
R = math.hypot(W, H) / k * 1.2 + 50 # Reichweite in mm
for ang, x0, y0, dx, dy, *dash in fams:
a = math.radians(ang)
u = (math.cos(a), math.sin(a))
n = (-u[1], u[0])
if abs(dy) < 1e-9:
continue
nmax = int(R / abs(dy)) + 2
period = sum(abs(d) for d in dash) if dash else 0
for i in range(-nmax, nmax + 1):
ox = x0 + i * (dx * u[0] + dy * n[0])
oy = y0 + i * (dx * u[1] + dy * n[1])
if not dash:
p1 = (ox - u[0] * R, oy - u[1] * R)
p2 = (ox + u[0] * R, oy + u[1] * R)
c = clip(p1[0] * k, p1[1] * k, p2[0] * k, p2[1] * k, W, H)
if c: segs.append(c)
continue
# Startpunkt so weit zurück, dass das ganze Feld abgedeckt ist
m = math.floor(-R / period)
t = m * period
while t < R:
for d in dash:
L = abs(d)
if d >= 0:
sx, sy = ox + u[0] * t, oy + u[1] * t
if d == 0:
if 0 <= sx * k <= W and 0 <= sy * k <= H:
dots.append((sx * k, sy * k))
else:
ex, ey = sx + u[0] * L, sy + u[1] * L
c = clip(sx * k, sy * k, ex * k, ey * k, W, H)
if c: segs.append(c)
t += L
return segs, dots
def svg_pattern(fams, W, H, k, stroke=1.0):
segs, dots = hatch_lines(fams, W, H, k)
# y nach unten im SVG → spiegeln, damit 45° wie im CAD nach rechts oben steigt
d = "".join(f"M{a:.2f} {H - b:.2f}L{c:.2f} {H - e:.2f}" for a, b, c, e in segs)
dd = "".join(f"M{x:.2f} {H - y:.2f}h0" for x, y in dots)
return (f'<path d="{d}" fill="none" stroke="{INK}" stroke-width="{stroke}" stroke-linecap="butt"/>'
+ (f'<path d="{dd}" fill="none" stroke="{INK}" stroke-width="{stroke * 1.7}" stroke-linecap="round"/>' if dd else ""))
def svg_linetype(defn, W, y, k):
parts = defn.split(",", 1)[1]
# Text-Elemente [..] als Buchstabe zeichnen
items, buf, depth = [], "", 0
for ch in parts:
if ch == "[": depth += 1
if ch == "]": depth -= 1
if ch == "," and depth == 0:
items.append(buf); buf = ""
else:
buf += ch
items.append(buf)
out, x = [], 0.0
while x < W:
for it in items:
if it.startswith("["):
letter = it.split('"')[1]
out.append(f'<text x="{x + 0.7 * k:.1f}" y="{y + 0.33 * 2 * k:.1f}" font-family="sans-serif" font-size="{2 * k:.1f}" text-anchor="middle" fill="{INK}">{letter}</text>')
continue
v = float(it)
if v > 0:
out.append(f'<path d="M{x:.1f} {y}h{min(v * k, W - x):.1f}" stroke="{INK}" stroke-width="1.4"/>')
elif v == 0:
out.append(f'<circle cx="{x:.1f}" cy="{y}" r="1.1" fill="{INK}"/>')
x += abs(v) * k
if x >= W: break
return "".join(out)
def write_previews():
PREV.mkdir(exist_ok=True)
W = H = 240
for name, desc, fams, scale in PATTERNS:
body = svg_pattern(fams, W, H, 4 * scale)
(PREV / f"{name}.svg").write_text(
f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {W} {H}" width="{W}" height="{H}">'
f'<rect width="{W}" height="{H}" fill="#fff"/>{body}</svg>\n', encoding="utf-8")
# Übersicht Schraffuren: Raster 4 Spalten
cols, cell, gap, lab = 4, 200, 24, 34
rows = math.ceil(len(PATTERNS) / cols)
TW, TH = cols * cell + (cols - 1) * gap, rows * (cell + lab + gap)
g = []
for i, (name, desc, fams, scale) in enumerate(PATTERNS):
cx, cy = (i % cols) * (cell + gap), (i // cols) * (cell + lab + gap)
g.append(f'<g transform="translate({cx},{cy})"><rect width="{cell}" height="{cell}" fill="#fff" stroke="{INK}" stroke-width="1"/>'
f'<svg width="{cell}" height="{cell}" viewBox="0 0 {cell} {cell}">{svg_pattern(fams, cell, cell, 4 * scale, 0.9)}</svg>'
f'<text x="0" y="{cell + 20}" font-family="sans-serif" font-size="13" font-weight="600" fill="{INK}">{name}</text></g>')
(PREV / "uebersicht-schraffuren.svg").write_text(
f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="-2 -2 {TW + 4} {TH + 4}" width="{TW + 4}" height="{TH + 4}">{"".join(g)}</svg>\n', encoding="utf-8")
# Vorschau je Linientyp (Streifen)
for name, desc, defn in LINETYPES:
(PREV / f"{name}.svg").write_text(
f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 480 60" width="480" height="60">'
f'<rect width="480" height="60" fill="#fff"/><g transform="translate(16,0)">{svg_linetype(defn, 448, 30, 5)}</g></svg>\n', encoding="utf-8")
# Übersicht Linientypen
k, LW, rowh = 4, 520, 44
g = []
for i, (name, desc, defn) in enumerate(LINETYPES):
y = i * rowh + 16
g.append(f'<text x="0" y="{y + 5}" font-family="sans-serif" font-size="13" font-weight="600" fill="{INK}">{name}</text>')
g.append(f'<g transform="translate(200,0)">{svg_linetype(defn, LW, y, k)}</g>')
(PREV / "uebersicht-linientypen.svg").write_text(
f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {200 + LW} {len(LINETYPES) * rowh}" width="{200 + LW}" height="{len(LINETYPES) * rowh}">'
f'<rect width="100%" height="100%" fill="#fff"/>{"".join(g)}</svg>\n', encoding="utf-8")
def write_zip():
z = DIST / "openbureau-cad-vorlagen.zip"
with zipfile.ZipFile(z, "w", zipfile.ZIP_DEFLATED) as f:
for p in ("openbureau-schraffuren.pat", "openbureau-linientypen.lin"):
f.write(DIST / p, p)
for p in sorted((DIST / "einzeln").iterdir()):
f.write(p, f"einzeln/{p.name}")
for p in sorted(PREV.glob("*.svg")):
f.write(p, f"vorschau/{p.name}")
for p in ("README.md", "LICENSE"):
f.write(ROOT / p, p)
if __name__ == "__main__":
DIST.mkdir(exist_ok=True)
write_pat()
write_lin()
write_single()
write_previews()
write_manifest()
write_zip()
print(f"{len(PATTERNS)} Schraffuren, {len(LINETYPES)} Linientypen → {DIST}")