#! python3 # capture one Ramp-A P3-2 bar (pipe solid) in the plane of the bar, plus projected pixel positions of key points import os, json, math, traceback 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, "p32_cfg.json"), encoding="utf-8")) res = {"key": cfg["key"]} 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) target = None cen = None for o in doc.Objects.GetObjectList(st): if o.Attributes.Name == cfg["pipe"]: target = o if o.Attributes.Name == cfg["curve"]: cen = o for o in doc.Objects.GetObjectList(st): if o.Id == target.Id: doc.Objects.Show(o, True) else: doc.Objects.Hide(o, True) c = cen.Geometry pts = [c.SegmentCurve(i).PointAtStart for i in range(c.SegmentCount)] + [c.SegmentCurve(c.SegmentCount - 1).PointAtEnd] T1, T2 = pts[0], pts[-1] zb = c.GetBoundingBox(True).Min.Z u = rg.Vector3d(T2.X - T1.X, T2.Y - T1.Y, 0); u.Unitize() n = rg.Vector3d(u.Y, -u.X, 0) bb = target.Geometry.GetBoundingBox(True) mid = bb.Center view = doc.Views.ActiveView vp = view.ActiveViewport vp.ChangeToParallelProjection(True) vp.SetCameraLocations(mid, mid + n * 6000.0) vp.CameraUp = rg.Vector3d(0, 0, 1) bb2 = rg.BoundingBox(bb.Min, bb.Max) bb2.Inflate(180.0) vp.ZoomBoundingBox(bb2) vp.DisplayMode = Rhino.Display.DisplayModeDescription.FindByName("Shaded") or vp.DisplayMode 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.png" % cfg["key"]) bmp.Save(png, System.Drawing.Imaging.ImageFormat.Png) def px(p): q = vp.WorldToClient(p) return [q.X * sc, q.Y * sc] C1 = rg.Point3d(T1.X, T1.Y, zb) C2 = rg.Point3d(T2.X, T2.Y, zb) res.update({"png": png, "size": [bmp.Width, bmp.Height], "T1": px(T1), "T2": px(T2), "C1": px(C1), "C2": px(C2), "T1_world": [T1.X, T1.Y, T1.Z], "T2_world": [T2.X, T2.Y, T2.Z], "zb": zb, "zt": max(T1.Z, T2.Z)}) except Exception: res["error"] = traceback.format_exc() res["done"] = True json.dump(res, open(os.path.join(cfg["out"], "%s_capture.json" % cfg["key"]), "w", encoding="utf-8"), ensure_ascii=False, indent=1) try: Rhino.RhinoDoc.ActiveDoc.Modified = False except Exception: pass