Last active
December 29, 2025 07:27
-
-
Save CEXT-Dan/1c29470e327fc27f88b5e0e2fca44d37 to your computer and use it in GitHub Desktop.
Bricscad, sample data extraction with python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import traceback | |
| import pandas as pd | |
| from collections import defaultdict | |
| from pyrx import Ap, Db, Brx | |
| def getBlockParameterValue(id: Db.ObjectId, name: str): | |
| val = Db.AcValue(Brx.Core.getBlockParameterValue(id, name)) | |
| val.setFormat("%lu4%pr4%zs5") #djust this | |
| return val.format() | |
| def create_dataframe(db: Db.Database, blockname: str): | |
| items = defaultdict(list) | |
| mspace = db.modelSpace() | |
| for ref in [Db.BlockReference(id) for id in mspace.objectIds(Db.BlockReference.desc())]: | |
| if ref.getBlockName().casefold() != blockname.casefold(): | |
| continue | |
| for attid in ref.attributeIds(): | |
| att = Db.AttributeReference(attid) | |
| if att.tag() == "NOMINAL": | |
| items["NOMINAL"].append(att.textString()) | |
| if att.tag() == "NOTES": | |
| items["NOTES"].append(att.textString()) | |
| items["L"].append(getBlockParameterValue(ref.objectId(), "L")) | |
| items["T"].append(getBlockParameterValue(ref.objectId(), "T")) | |
| items["W"].append(getBlockParameterValue(ref.objectId(), "W")) | |
| items["QTY"].append(1) | |
| dfgr = pd.DataFrame(items) | |
| dfgr = dfgr.groupby(["NOMINAL", "NOTES", "L", "T", "W"], sort=False, as_index=False).agg( | |
| {"QTY": "sum"} | |
| ) | |
| return dfgr[["QTY", "NOMINAL", "NOTES", "L", "T", "W"]] | |
| @Ap.Command() | |
| def doit(): | |
| try: | |
| db = Db.curDb() | |
| dfgr = create_dataframe(db, "!_DimLum3") | |
| #requires openpyxl or xlsxwriter | |
| with pd.ExcelWriter("e:\\pandas_to_excel.xlsx") as writer: | |
| dfgr.to_excel(writer, sheet_name="sheet1", index=False) | |
| #generate a table | |
| table = Db.Table() | |
| table.setDatabaseDefaults(db) | |
| table.setSize(dfgr.shape[0] + 2, dfgr.shape[1]) | |
| table.setTextString(0, 0, "I Like to Move It, Move It") | |
| headers = dfgr.columns.values.tolist() | |
| datas = dfgr.values.tolist() | |
| for col, value in enumerate(headers): | |
| table.setTextString(1, col, "{}".format(value)) | |
| for row, data in enumerate(datas): | |
| for col, value in enumerate(data): | |
| table.setTextString(row + 2, col, "{}".format(value)) | |
| db.addToCurrentspace(table) | |
| except Exception as err: | |
| traceback.print_exception(err) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment