|
| 1 | +#! python3 |
| 2 | + |
| 3 | +from ghpythonlib.componentbase import executingcomponent as component |
| 4 | +import os |
| 5 | +import tempfile |
| 6 | +import requests |
| 7 | +import threading |
| 8 | +import Rhino |
| 9 | +import Rhino.Geometry as rg |
| 10 | +import scriptcontext as sc |
| 11 | + |
| 12 | + |
| 13 | +class DFHTTPListener(component): |
| 14 | + |
| 15 | + def RunScript(self, |
| 16 | + i_load: bool, |
| 17 | + i_ply_url: str): |
| 18 | + |
| 19 | + sc.sticky.setdefault('ply_url', None) |
| 20 | + sc.sticky.setdefault('imported_geom', None) |
| 21 | + sc.sticky.setdefault('status_message','Idle') |
| 22 | + sc.sticky.setdefault('prev_load', False) |
| 23 | + sc.sticky.setdefault('thread_running', False) |
| 24 | + |
| 25 | + def _import_job(url): |
| 26 | + try: |
| 27 | + if not url.lower().endswith('.ply'): |
| 28 | + raise ValueError("URL must end in .ply") |
| 29 | + |
| 30 | + resp = requests.get(url, timeout=30); resp.raise_for_status() |
| 31 | + fn = os.path.basename(url) |
| 32 | + tmp = os.path.join(tempfile.gettempdir(), fn) |
| 33 | + with open(tmp, 'wb') as f: |
| 34 | + f.write(resp.content) |
| 35 | + |
| 36 | + doc = Rhino.RhinoDoc.ActiveDoc |
| 37 | + before_ids = {o.Id for o in doc.Objects} |
| 38 | + |
| 39 | + opts = Rhino.FileIO.FilePlyReadOptions() |
| 40 | + ok = Rhino.FileIO.FilePly.Read(tmp, doc, opts) |
| 41 | + if not ok: |
| 42 | + raise RuntimeError("Rhino.FilePly.Read failed") |
| 43 | + |
| 44 | + after_ids = {o.Id for o in doc.Objects} |
| 45 | + new_ids = after_ids - before_ids |
| 46 | + |
| 47 | + geom = None |
| 48 | + for guid in new_ids: |
| 49 | + g = doc.Objects.FindId(guid).Geometry |
| 50 | + if isinstance(g, rg.PointCloud): |
| 51 | + geom = g.Duplicate() |
| 52 | + break |
| 53 | + elif isinstance(g, rg.Mesh): |
| 54 | + geom = g.DuplicateMesh() |
| 55 | + break |
| 56 | + |
| 57 | + for guid in new_ids: |
| 58 | + doc.Objects.Delete(guid, True) |
| 59 | + doc.Views.Redraw() |
| 60 | + |
| 61 | + sc.sticky['imported_geom'] = geom |
| 62 | + count = geom.Count if isinstance(geom, rg.PointCloud) else geom.Vertices.Count |
| 63 | + if isinstance(geom, rg.PointCloud): |
| 64 | + sc.sticky['status_message'] = f"Done: {count} points" |
| 65 | + else: sc.sticky['status_message'] = f"Done: {count} vertices" |
| 66 | + ghenv.Component.Message = sc.sticky.get('status_message') |
| 67 | + |
| 68 | + except Exception as e: |
| 69 | + sc.sticky['imported_geom'] = None |
| 70 | + sc.sticky['status_message'] = f"Error: {e}" |
| 71 | + finally: |
| 72 | + try: os.remove(tmp) |
| 73 | + except: pass |
| 74 | + sc.sticky['thread_running'] = False |
| 75 | + ghenv.Component.ExpireSolution(True) |
| 76 | + |
| 77 | + if sc.sticky['ply_url'] != i_ply_url: |
| 78 | + sc.sticky['ply_url'] = i_ply_url |
| 79 | + sc.sticky['status_message'] = "URL changed. Press Load" |
| 80 | + sc.sticky['thread_running'] = False |
| 81 | + sc.sticky['prev_load'] = False |
| 82 | + |
| 83 | + if i_load and not sc.sticky['prev_load'] and not sc.sticky['thread_running']: |
| 84 | + sc.sticky['status_message'] = "Loading..." |
| 85 | + sc.sticky['thread_running'] = True |
| 86 | + threading.Thread(target=_import_job, args=(i_ply_url,), daemon=True).start() |
| 87 | + |
| 88 | + sc.sticky['prev_load'] = i_load |
| 89 | + ghenv.Component.Message = sc.sticky.get('status_message', "") |
| 90 | + |
| 91 | + # output |
| 92 | + o_geometry = sc.sticky.get('imported_geom') |
| 93 | + |
| 94 | + return [o_geometry] |
0 commit comments