#! python3 # A: finish the C-1 bars the corner method could not do (Ungok P3 72, Okseong C9/C13 18, Ramp-A C12 12) # C: Ungok B2 leg ends 2072.5 -> 2100 (outer top line, same reading as Sanoe / Okseong) # Sources: Ungok ..._수정6 -> ..._수정7_20261026 ; Okseong ..._수정2 -> ..._수정4_20261026 ; Ramp-A ..._수정_20261023 -> ..._수정2_20261026 import sys, os, json, math, collections, traceback sys.path.insert(0, r"C:\Users\User\AppData\Local\PierApp\jobs\c1_work") import c1_core import Rhino from Rhino.Geometry import Transform, Vector3d, PolyCurve, Point3d, PipeCapMode, Brep from c1_core import BASE, plan_curve, required_rc, _retarget DATE_TXT = "2026-10-26" OUT = r"C:\Users\User\AppData\Local\PierApp\jobs\c1_work\c1_fix_remaining.json" JOBS = { "ungok": {"src": os.path.join(BASE, "철근 모델랑 운곡교P1_바이브코딩", "운곡교p1_수정6_20261026.3dm"), "dst": os.path.join(BASE, "철근 모델랑 운곡교P1_바이브코딩", "운곡교p1_수정7_20261026.3dm"), "marks": {"P3": "scale"}, "b2": True, "note": "C-1 finish: P3 hook arcs R42.93 (sequential scale); footing B2 leg ends 2100"}, "okseong": {"src": os.path.join(BASE, "검토_옥성교P1", "옥성교P1_G5_수정2_20261026.3dm"), "dst": os.path.join(BASE, "검토_옥성교P1", "옥성교P1_G5_수정4_20261026.3dm"), "marks": {"C9": "corner", "C13": "corner"}, "b2": False, "note": "C-1 finish: C9, C13 (H25) R56.95 with robust pipe settings"}, "rampa": {"src": os.path.join(BASE, "Ramp-A교P1", "ramp-A G5_수정_20261023.3dm"), "dst": os.path.join(BASE, "Ramp-A교P1", "ramp-A G5_수정2_20261026.3dm"), "marks": {"C12": "corner"}, "b2": False, "note": "C-1 finish: C12 (H19) R42.93 with robust pipe settings"}, } res = {} def robust_pipe(curve, d, tol): """first variant that gives one closed solid whose volume matches pi r^2 L within 0.5 %""" for fit, atol in ((True, tol), (False, tol), (True, 0.01), (True, 0.1)): rr = Brep.CreatePipe(curve, d / 2.0, False, PipeCapMode.Flat, fit, atol, Rhino.RhinoMath.ToRadians(1.0)) if rr and len(rr) == 1 and rr[0].IsSolid: vol = Rhino.Geometry.VolumeMassProperties.Compute(rr[0]).Volume if abs(vol / (math.pi * (d / 2.0) ** 2 * curve.GetLength()) - 1.0) <= 0.005: return rr[0], "fit=%s atol=%g" % (fit, atol) return None, None def scale_chain(c, req): """arcs below req are scaled about their start point; everything after is translated, so start point and start tangent are kept""" n = c.SegmentCount off = Vector3d(0, 0, 0) pc = PolyCurve() changed = 0 for i in range(n): s = c.SegmentCurve(i).DuplicateCurve() ok, arc = s.TryGetArc() S, E = s.PointAtStart, s.PointAtEnd if ok and not s.IsLinear() and arc.Radius < req - 1e-6: k = req / arc.Radius s.Transform(Transform.Scale(S, k)) s.Transform(Transform.Translation(off)) off = off + (E - S) * (k - 1.0) changed += 1 else: s.Transform(Transform.Translation(off)) pc.Append(s) return (pc if changed else None), off.Length, changed def short_left(doc, lay, st): cnt = collections.Counter() for o in doc.Objects.GetObjectList(st): g = o.Geometry if isinstance(g, PolyCurve) and "중심선" in lay.get(o.Attributes.LayerIndex, ""): req, d = required_rc(o.Attributes.GetUserString("dia")) for i in range(g.SegmentCount): s = g.SegmentCurve(i) ok, arc = s.TryGetArc() if ok and not s.IsLinear() and arc.Radius < req - 1e-6: cnt[o.Attributes.GetUserString("mark")] += 1 return dict(cnt) def run_one(key, job): r = {} doc = Rhino.RhinoDoc.OpenHeadless(job["src"]) tol = doc.ModelAbsoluteTolerance st = Rhino.DocObjects.ObjectEnumeratorSettings() st.HiddenObjects = True; st.LockedObjects = True; st.NormalObjects = True lay = {l.Index: l.FullPath for l in doc.Layers} r["objects_before"] = doc.Objects.Count r["short_before"] = short_left(doc, lay, st) ft = [o for o in doc.Objects.GetObjectList(st) if o.Attributes.Name == "P1-FT"] fbot = ft[0].Geometry.GetBoundingBox(True).Min.Z if ft else 0.0 # pile foundation (Ramp-A) has no footing solid breps = collections.defaultdict(list) curves = [] for o in doc.Objects.GetObjectList(st): g = o.Geometry L = lay.get(o.Attributes.LayerIndex, "") if isinstance(g, Brep) and "철근" in L: breps[o.Attributes.Name].append(o) elif isinstance(g, Rhino.Geometry.Curve) and "중심선" in L: curves.append(o) stat = collections.Counter() drift = [] pipe_how = collections.Counter() for o in curves: a = o.Attributes m = a.GetUserString("mark") mode = job["marks"].get(m) isb2 = job["b2"] and m == "B2" and a.GetUserString("host") == "P1-FT" if not mode and not isb2: continue dia = a.GetUserString("dia") req, d = required_rc(dia) c = o.Geometry nb = breps.get(a.Name.replace("-RC-", "-R-"), []) if len(nb) != 1 or not isinstance(c, PolyCurve): stat[m + "_unexpected"] += 1 continue did = [] newc = c.Duplicate() if isb2: tz = fbot + 2100.0 n = newc.SegmentCount s0, sN = newc.SegmentCurve(0), newc.SegmentCurve(n - 1) if abs(s0.TangentAtStart.Z) < 0.999 or abs(sN.TangentAtEnd.Z) < 0.999 or abs(s0.PointAtStart.Z - (fbot + 2072.5)) > 0.6 or abs(sN.PointAtEnd.Z - (fbot + 2072.5)) > 0.6: stat["B2_unexpected_ends"] += 1 continue segs = [newc.SegmentCurve(i).DuplicateCurve() for i in range(n)] p0, pN = segs[0].PointAtStart, segs[-1].PointAtEnd segs[0] = _retarget(segs[0], start=Point3d(p0.X, p0.Y, tz)) segs[-1] = _retarget(segs[-1], end=Point3d(pN.X, pN.Y, tz)) newc = PolyCurve() for s in segs: newc.Append(s) did.append("legs end z 2100") elif mode == "scale": pc, dist, k = scale_chain(newc, req) if pc is None: stat[m + "_nothing_to_do"] += 1 continue drift.append(dist) newc = pc did.append("R %.2f (scale chain, end shift %.2f mm)" % (req, dist)) else: pc, info = plan_curve(newc, dia) if info["status"] != "ok": stat[m + "_plan_" + info["status"]] += 1 continue newc = pc did.append("R %.2f" % req) pipe, how = robust_pipe(newc, d, tol) if pipe is None: stat[m + "_pipe_fail"] += 1 continue pipe_how[how] += 1 cid, bid = o.Id, nb[0].Id if not (doc.Objects.Replace(cid, newc) and doc.Objects.Replace(bid, pipe)): stat[m + "_replace_failed"] += 1 continue for oid in (cid, bid): ro = doc.Objects.FindId(oid) at = ro.Attributes.Duplicate() key_ = "footing_fix" if isb2 else "bend_r_fix" old = at.GetUserString(key_) or "" at.SetUserString(key_, (old + " | " if old else "") + "; ".join(did) + " (" + DATE_TXT + ")") doc.Objects.ModifyAttributes(ro, at, True) stat[("B2" if isb2 else m) + "_done"] += 1 r["stat"] = dict(stat) r["pipe_settings"] = dict(pipe_how) if drift: r["end_shift_mm"] = {"max": round(max(drift), 2), "mean": round(sum(drift) / len(drift), 2)} r["short_after"] = short_left(doc, lay, st) doc.Strings.SetString("수정일", DATE_TXT) prev = doc.Strings.GetValue("수정내용") or "" doc.Strings.SetString("수정내용", (prev + " | " if prev else "") + job["note"]) r["objects_after"] = doc.Objects.Count r["saved"] = bool(doc.WriteFile(job["dst"], Rhino.FileIO.FileWriteOptions())) r["out"] = job["dst"] doc.Dispose() return r for key, job in JOBS.items(): if os.path.exists(job["dst"]): # already produced by an earlier run: keep it continue try: res[key] = run_one(key, job) except Exception: res[key] = {"error": traceback.format_exc()} json.dump(res, open(OUT, "w", encoding="utf-8"), ensure_ascii=False, indent=1) res["done"] = True json.dump(res, open(OUT, "w", encoding="utf-8"), ensure_ascii=False, indent=1)