build3d / app /quickqa.py
dellweb3's picture
部署 2026-10-28 18:31 · 图纸对话与知识库
4367adf
Raw History Blame Contribute Delete
13.5 kB
# -*- coding: utf-8 -*-
"""没接大模型时的「直答」:从图纸参数表取字段,先用一句话回答问题。
为什么要有这个模块:
线上演示(HF Space)如果没配百炼 Key,老实现会退化成「知识库直查」——
把 BM25 命中的原文片段整段摆给客户看。客户问「这栋楼几层」,
屏幕上却是一堆资料标题加字段 dump,等于绕过了问题、只给引用。
这里改成规则化问答:先认出问的是哪个属性,再去参数表取数,直接给结论。
规则覆盖不到的,仍然明说「这条规则答不了,需要接模型」,绝不拿片段充答案。
"""
import json
import os
from paths import OUTPUT, PLANS
def _load(path, default=None):
try:
with open(path, encoding="utf-8") as f:
return json.load(f)
except Exception:
return default if default is not None else {}
def _plan(pid):
return _load(os.path.join(PLANS, pid + ".json"))
def _info(pid):
return _load(os.path.join(OUTPUT, pid, "model_info.json"))
def _openings(plan, kind=None, lvl=None):
out = []
for o in plan.get("openings") or []:
if len(o) < 6:
continue
x1, y1, x2, y2, k, l = o
if kind and k != kind:
continue
if lvl is not None and l != lvl:
continue
out.append((x1, y1, x2, y2, k, l))
return out
def _len(o):
x1, y1, x2, y2 = o[0], o[1], o[2], o[3]
return round(abs(x2 - x1) if abs(x2 - x1) > 1e-6 else abs(y2 - y1), 2)
def _rooms(plan):
out = []
for r in plan.get("rooms") or []:
if len(r) >= 5:
out.append(r[4])
return out
def _has(q, *words):
return any(w in q for w in words)
def _f(v, n=2):
"""数字好看一点:3.0 而不是 3.00"""
s = ("%." + str(n) + "f") % float(v)
return s.rstrip("0").rstrip(".") if "." in s else s
def answer(question, pid=None):
"""返回直接作答的字符串;规则覆盖不到时返回 None。"""
q = (question or "").strip()
if not q:
return None
# 跨楼栋对比:哪栋最大 / 最高 / 层数最多
if _has(q, "哪栋", "哪个楼", "哪一栋") and _has(q, "大", "高", "层", "小", "多"):
rows = []
for i in range(1, 7):
p = _plan("BLD-%03d" % i)
if not p:
continue
w, d = p["footprint"]
rows.append((p["id"], p["name"], float(w) * float(d), int(p["floors"])))
if not rows:
return None
if _has(q, "层", "高"):
pick = max(rows, key=lambda r: (r[3], r[2]))
return ("层数最多的是 %s(%s),%d 层,层高 %s m;占地 %s ㎡。"
% (pick[0], pick[1], pick[3],
_f(_plan(pick[0]).get("floor_h", 3)), _f(pick[2])))
pick = max(rows, key=lambda r: r[2]) if not _has(q, "小") else min(rows, key=lambda r: r[2])
return ("占地最大的是 %s(%s),轮廓 %s × %s m,占地 %s ㎡,%d 层。"
% (pick[0], pick[1], _f(_plan(pick[0])["footprint"][0]),
_f(_plan(pick[0])["footprint"][1]), _f(pick[2]), pick[3]))
if not pid:
return None
plan = _plan(pid)
if not plan:
return None
info = _info(pid)
st = (info.get("stats") or {}) if info else {}
W, D = [float(v) for v in plan.get("footprint") or [0, 0]]
nf = int(plan.get("floors") or 0)
fh = float(plan.get("floor_h") or 0)
t = float(plan.get("wall_t") or 0)
roof = plan.get("roof") or {}
inner_y = float(plan.get("inner_y") or 0)
ridge = float(roof.get("ridge_y") or 0)
over = float(roof.get("overhang") or 0)
gable = float(roof.get("gable_h") or 0)
ztop = nf * fh
zroof = ztop + 0.16 + gable
name = plan.get("name") or ""
# ---- 口径类问题优先:客户问「能改吗」「能导 IFC 吗」,别被属性名抢走 ----
if _has(q, "改参数", "改层高", "改墙厚", "能否改", "能不能改", "能改", "可以改", "支持改",
"能改吗", "改吗", "调参数", "重新生成", "改得动", "改不了"):
return ("可以改。层高、墙厚、内隔墙位置(inner_y)、屋脊高(ridge_y)、山墙高(gable_h)、"
"出檐(overhang)这六项支持一句话改参:勾上「改参数并重建」,说出目标值"
"(例如「层高改成 3.6 米」),系统解析成参数补丁后由 Blender 重建;原图纸不动,"
"新模型另存一版用于前后对比。六个字段以外的不开放改参,避免几何失稳。")
if _has(q, "ifc", "格式", "导出", "二次编辑", "revit", "能不能编辑", "能编辑"):
return ("交付格式是 glTF(.glb)+ Blender 源文件(.blend),不含 IFC。"
".blend 可以二次编辑;glb 里每个构件都带编号、类型、材质、尺寸、楼层属性,"
"可以按编号做二次开发或对接其他引擎。")
# ---- 层数 / 层高 / 总高 ----
if _has(q, "几层", "多少层", "层数", "楼层数"):
return ("%s(%s)共 %d 层,层高 %s m,檐口标高 %s m;屋脊最高点 %s m。"
% (pid, name, nf, _f(fh), _f(ztop), _f(zroof)))
if _has(q, "层高", "每层多高", "净高"):
return ("层高 %s m。%d 层累计 %s m,加屋面山墙后脊高约 %s m;楼板厚 0.16 m。"
% (_f(fh), nf, _f(ztop), _f(zroof)))
if _has(q, "总高", "多高", "高度", "标高"):
return ("%s 共 %d 层、层高 %s m,檐口 %s m;双坡屋面山墙高 %s m,屋脊最高 %s m。"
% (pid, nf, _f(fh), _f(ztop), _f(gable), _f(zroof)))
# ---- 墙厚 / 轮廓 / 面积 ----
if _has(q, "墙厚", "墙多厚", "墙体厚度", "厚度"):
return ("墙厚 %s m,内外墙同厚;山墙厚 0.12 m。墙段共 %s 段(1F 四面外墙 + 内隔墙,逐层叠加)。"
% (_f(t, 3), st.get("墙段", "若干")))
if _has(q, "轮廓", "尺寸", "长宽", "多大", "面积", "占地", "开间", "进深"):
area = W * D
return ("轮廓 %s × %s m,占地 %s ㎡;%d 层总建筑面积约 %s ㎡。屋面含出檐 %s m,"
"投影约 %s × %s m。"
% (_f(W), _f(D), _f(area), nf, _f(area * nf), _f(over),
_f(W + 2 * over), _f(ridge * 2 + 2 * over)))
# ---- 房间 / 分隔 ----
if _has(q, "房间", "分隔", "户型", "几室", "功能"):
rooms = _rooms(plan)
txt = "、".join(rooms) if rooms else "图纸未标注房间名"
return ("一层由内隔墙分成 %d 个房间:%s。竖向内墙在 x = %s m(贯通进深 %s m),"
"横向内墙在 y = %s m,从竖墙一直拉到右侧外墙。"
% (len(rooms), txt, _f(_inner_x(plan)), _f(D), _f(inner_y)))
# ---- 洞口按立面分布(问「开在哪个面上、各多少个」) ----
if _has(q, "面") and _has(q, "洞", "窗", "门"):
return _facade_answer(plan, pid, W, D, nf)
# ---- 洞口:内墙门洞 / 门 / 窗 ----
if _has(q, "门洞", "通道", "内墙的洞", "洞里", "过道", "洞"):
ps = _openings(plan, "passage", 0)
if ps:
parts = []
for o in ps:
horiz = abs(o[1] - o[3]) < 1e-6
pos = ("x %s~%s m" % (_f(o[0]), _f(o[2]))) if horiz else ("y %s~%s m" % (_f(o[1]), _f(o[3])))
parts.append("%s上一处宽 %s m(%s)" % ("横墙" if horiz else "竖墙", _f(_len(o)), pos))
return ("一层内墙留了 %d 处门洞,按 2.2 m 高开洞、洞口不通顶:%s。"
"透视模式下外墙半透明、内墙保持实体,可以直接看到这些洞。"
% (len(ps), ";".join(parts)))
if _has(q, "门", "窗"):
doors = len(_openings(plan, "door"))
wins = len(_openings(plan, "window"))
ps = len(_openings(plan, "passage"))
seg = ["门 %d 樘(入口 1.2 m 宽、2.2 m 高)" % doors,
"窗 %d 樘(窗台 0.9 m,洞顶 2.3 m,洞口高 1.4 m)" % wins]
if ps:
seg.append("内墙门洞 %d 处(不装门扇,直接通房间)" % ps)
return "%s 共:%s。" % (pid, "、".join(seg))
# ---- 屋面 ----
if _has(q, "屋脊", "屋面", "屋顶", "坡度", "出檐", "山墙", "檐口"):
half = ridge + over
import math
ang = math.degrees(math.atan2(gable, half)) if half else 0
return ("双坡屋面:屋脊在 y = %s m,脊高 %s m;两侧坡长 %s m、坡度 %s°;"
"出檐 %s m,屋面板厚 0.14 m;两端山墙高 %s m。"
% (_f(ridge), _f(zroof), _f(math.hypot(half, gable)), _f(ang, 1), _f(over), _f(gable)))
# ---- 构件总览 ----
if _has(q, "构件", "多少个部件", "部件", "模型里"):
if st:
return ("%s 共 %s 个构件:墙段 %s、门窗 %s、楼板 %s。每个构件都带编号、类型、材质、"
"尺寸、楼层五项属性,右侧清单点一条就能把镜头对过去。"
% (pid, st.get("构件数"), st.get("墙段"), st.get("门窗"), st.get("楼板")))
# ---- 材料 ----
if _has(q, "材质", "材料", "涂什么", "什么漆"):
comps = (info.get("components") or []) if info else []
by = {}
for c in comps:
m = c.get("mat")
if m:
by.setdefault(m, []).append(c.get("type"))
if by:
seg = ["%s 用于 %s" % (m, "、".join(sorted(set(v))[:4])) for m, v in by.items()]
return "%s 的材质口径:%s。" % (pid, ";".join(seg))
# ---- 图纸 / 约束点 ----
if _has(q, "几张", "几个视角", "约束点", "图纸数量", "多少张"):
pts = plan.get("points") or {}
views = info.get("views") or []
return ("每栋出 5 张图纸(平面、标准层平面、正立面、侧立面、A-A 剖面),"
"本栋五视角约束点合计 %s 个:%s。"
% (sum(int(v) for v in pts.values()) if pts else "—",
"、".join("%s %s" % (k, v) for k, v in pts.items())))
if _has(q, "耗时", "多久", "多快", "时间"):
return ("单栋从图纸到 3D 模型是本机 Blender 无头执行,确定性重建,秒级完成;"
"运行时不调用大模型,几何全部由 bpy 计算,所以同一份图纸每次结果一致。")
return None
FACES = [("前墙(y=0,入口面)", "front"), ("后墙(y=D)", "back"),
("左墙(x=0)", "left"), ("右墙(x=W)", "right")]
def _face_of(o, W, D):
x1, y1, x2, y2 = o[0], o[1], o[2], o[3]
if abs(y1) < 1e-6 and abs(y2) < 1e-6:
return "front"
if abs(y1 - D) < 1e-6 and abs(y2 - D) < 1e-6:
return "back"
if abs(x1) < 1e-6 and abs(x2) < 1e-6:
return "left"
if abs(x1 - W) < 1e-6 and abs(x2 - W) < 1e-6:
return "right"
return None # 内墙上的洞
def _facade_answer(plan, pid, W, D, nf):
"""按立面条目统计门窗洞口:客户问「开在哪几个面上、各多少个」就照这个答。"""
stat = {}
inner = {"passage": 0, "door": 0}
for o in plan.get("openings") or []:
if len(o) < 6:
continue
f = _face_of(o, W, D)
if f is None:
if o[4] in inner:
inner[o[4]] += 1
continue
d = stat.setdefault(f, {"door": 0, "window": 0})
if o[4] in d:
d[o[4]] += 1
total_door = sum(v["door"] for v in stat.values())
total_win = sum(v["window"] for v in stat.values())
seg = []
for cn, key in FACES:
d = stat.get(key) or {"door": 0, "window": 0}
bits = []
if d["door"]:
bits.append("门 %d" % d["door"])
if d["window"]:
bits.append("窗 %d" % d["window"])
seg.append("%s %s" % (cn, "、".join(bits) if bits else "无洞口"))
inner_txt = ""
if inner["passage"] or inner["door"]:
inner_txt = "另有内隔墙上 %d 处门洞(不在外立面上,透视后才能看到)。" % (inner["passage"] + inner["door"])
return ("%s 的洞口分布(%d 层合计):%s。四个立面共 %d 个洞口——门 %d 樘、窗 %d 樘;%s"
"各层洞口位置相同,往上逐层叠加。"
% (pid, nf, ";".join(seg), total_door + total_win, total_door, total_win,
inner_txt))
def _inner_x(plan):
"""竖向内墙的 x 坐标:walls 里 x1==x2 且不在外墙位置的那一段"""
W = float((plan.get("footprint") or [0, 0])[0])
for w in plan.get("walls") or []:
if len(w) == 5 and abs(w[0] - w[2]) < 1e-6 and 1e-6 < w[0] < W - 1e-6:
return w[0]
return 0.0
if __name__ == "__main__":
tests = ["这栋楼几层", "层高多少", "墙厚", "轮廓多大,占地面积多少", "房间怎么分",
"内墙的门洞在哪", "有几个门几扇窗", "屋面坡度多少", "一共多少构件",
"墙是什么材质", "出几张图纸", "层高能改吗", "能导出 IFC 吗", "哪栋楼最大"]
for q in tests:
a = answer(q, "BLD-001")
print(("Q: " + q).ljust(28), "→", a or "(未覆盖)")