#! python3 # capture one representative bar (median of the mark) of Sanoe coping shear marks, before (수정2) or after (수정4) the lap rebuild import os, json, math, traceback, re import Rhino import Rhino.Geometry as rg import System import System.Drawing HERE = os.path.dirname(os.path.abspath(__file__)) cfg = json.load(open(os.path.join(HERE, "lap_cfg.json"), encoding="utf-8")) res = {"tag": cfg["tag"], "items": {}} try: doc = Rhino.RhinoDoc.ActiveDoc st = Rhino.DocObjects.ObjectEnumeratorSettings() st.HiddenObjects = True; st.LockedObjects = True; st.NormalObjects = True for l in doc.Layers: if not l.IsDeleted: doc.Layers.ForceLayerVisible(l.Index) allobjs = list(doc.Objects.GetObjectList(st)) strip = re.compile(r"-(뚜껑|바닥|좌|우)$") view = doc.Views.ActiveView vp = view.ActiveViewport for job in cfg["jobs"]: mark, vname = job["mark"], job["view"] bases = {} for o in allobjs: if isinstance(o.Geometry, rg.Brep) and o.Attributes.GetUserString("mark") == mark and o.Attributes.GetUserString("host") == "P1-FT": bases.setdefault(strip.sub("", o.Attributes.Name), []).append(o) names = sorted(bases.keys()) base = names[len(names) // 2] targets = bases[base] ids = set(o.Id for o in targets) for o in allobjs: if o.Id in ids: doc.Objects.Show(o, True) else: doc.Objects.Hide(o, True) # colour the pieces differently (display only, file is not saved) cols = [System.Drawing.Color.FromArgb(214, 39, 40), System.Drawing.Color.FromArgb(31, 119, 180)] for i, o in enumerate(sorted(targets, key=lambda q: q.Attributes.Name)): at = o.Attributes.Duplicate() at.ColorSource = Rhino.DocObjects.ObjectColorSource.ColorFromObject at.ObjectColor = cols[i % 2] if len(targets) > 1 else System.Drawing.Color.FromArgb(31, 119, 180) doc.Objects.ModifyAttributes(o, at, True) bb = rg.BoundingBox.Empty for o in targets: bb.Union(o.Geometry.GetBoundingBox(True)) if vname == "auto": dx = bb.Max.X - bb.Min.X dy = bb.Max.Y - bb.Min.Y vname = "Right" if dx < 0.05 * dy else ("Front" if dy < 0.05 * dx else "Top") proj = {"Top": Rhino.Display.DefinedViewportProjection.Top, "Front": Rhino.Display.DefinedViewportProjection.Front, "Right": Rhino.Display.DefinedViewportProjection.Right}[vname] vp.SetProjection(proj, vname, False) vp.DisplayMode = Rhino.Display.DisplayModeDescription.FindByName("Shaded") or vp.DisplayMode bb2 = rg.BoundingBox(bb.Min, bb.Max) bb2.Inflate(max(150.0, 0.06 * bb.Diagonal.Length)) vp.ZoomBoundingBox(bb2) view.Redraw() Rhino.RhinoApp.Wait() cw, ch = vp.Size.Width, vp.Size.Height W = 1500 H = int(round(W * ch / float(cw))) sc = W / float(cw) bmp = view.CaptureToBitmap(System.Drawing.Size(W, H)) png = os.path.join(cfg["out"], "%s_%s.png" % (cfg["tag"], mark)) bmp.Save(png, System.Drawing.Imaging.ImageFormat.Png) res["items"][mark] = {"png": png, "base": base, "pieces": len(targets), "size": [bmp.Width, bmp.Height], "view": vname, "bbox": [bb.Min.X, bb.Min.Y, bb.Min.Z, bb.Max.X, bb.Max.Y, bb.Max.Z]} except Exception: res["error"] = traceback.format_exc() res["done"] = True json.dump(res, open(os.path.join(cfg["out"], "%s_capture.json" % cfg["tag"]), "w", encoding="utf-8"), ensure_ascii=False, indent=1) try: Rhino.RhinoDoc.ActiveDoc.Modified = False except Exception: pass