Jump to content

Python, Export manhole data to excel.


Recommended Posts

Posted

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()

export.thumb.png.8f2ec9eb0e298d0ff7fdc020badd3ec7.png

 

 

export2.thumb.png.b873228ba599632cf0ee5fc8a4a4be57.png

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...