Danielm103 Posted 50 minutes ago Posted 50 minutes ago Testing a new version of PyRx’s KDTree. Maybe a useful example import traceback import pandas as pd from pyrx import Ap, Db, Ge, Ed @Ap.Command() def doit(): try: # 1. Select the text and block objects in AutoCAD ps, ss = Ed.Editor.select([(0, "MTEXT,INSERT")]) if ps != Ed.PromptStatus.eOk: return # 2. Extract block positions for spatial matching pnts = Ge.Point3dArray() for blkid in ss.objectIds(Db.BlockReference.desc()): blkref = Db.BlockReference(blkid) pnts.append(blkref.position()) # Build a spatial K-d tree to find the nearest block for each MText tree = Ge.Point3dTree(pnts) # 3. Process matched data into a structured list rows = [] for tid in ss.objectIds(Db.MText.desc()): mt = Db.MText(tid) indexes, _distances = tree.knnSearch(mt.location(), 1) if indexes: row_data = {} nearest_pt = pnts[indexes[0]] # split TODO: /P text_lines = mt.text().splitlines() for idx, line in enumerate(text_lines): row_data[f"Line_{idx + 1}"] = line row_data["Block_X"] = round(nearest_pt.x, 2) row_data["Block_Y"] = round(nearest_pt.y, 2) row_data["Block_Z"] = round(nearest_pt.z, 2) rows.append(row_data) df = pd.DataFrame(rows) print(df) output_file = "e:/temp/output_cad_data.xlsx" df.to_excel(output_file, index=False, sheet_name="CAD Extract") print(f"Successfully exported {len(df)} rows to {output_file}") except Exception as e: traceback.print_exc() Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.