Leaderboard
Popular Content
Showing content with the highest reputation on 04/17/2025 in all areas
-
1 point
-
O.K., Got it.... here is the final code with the "getEffectiveNameFromBtrId" function. @Danielm103 -MANY MANY THANKS FOR YOUR HELP. #https://www.cadtutor.net/forum/topic/97450-export-table-with-blocks-to-excel-with-python/ from pyrx import Rx, Ge, Gi, Gs, Db, Ap, Ed, Ax import traceback from openpyxl.drawing.image import Image as xlImage from openpyxl.utils.cell import get_column_letter from openpyxl.styles import Alignment import openpyxl as xl import pathlib import ctypes import wx @Ap.Command() def Py_tablewithimagetoexcel(): try: db = Db.curDb() ps, id, _ = Ed.Editor.entSel("\nSelect a table: ", Db.Table.desc()) if ps != Ed.PromptStatus.eOk: raise RuntimeError("Selection Error! {}: ".format(ps)) # paths xlpath = pathlib.Path(db.getFilename()).parent xlname = pathlib.Path(db.getFilename()).stem #fpt = "{}/{}.png".format(xlpath, xlname) fpt = "{}\{}.xlsx".format(xlpath, xlname) wb = xl.Workbook() ws = wb.active table = Db.Table(id) opts = Db.TableIteratorOption.kTableIteratorSkipMerged for cell in table.cells(opts): # format all cells currentCell = ws.cell(cell.row + 1, cell.column + 1) currentCell.alignment = Alignment(horizontal="center", vertical="center") if table.cellType(cell.row, cell.column) == Db.CellType.kBlockCell: blk = table.blockTableRecordId(cell.row, cell.column) bname = getEffectiveNameFromBtrId(blk) print (bname) print (blk) img: wx.Image = Gs.Core.getBlockImage(blk, 64, 64, 1.0, [0, 0, 0]) img.SetMaskColour(0, 0, 0) img.SetMask(True) imgpath = "{}/{}.png".format(xlpath, bname)#blk.handle() img.SaveFile(imgpath, wx.BITMAP_TYPE_PNG) xlimg = xlImage(imgpath) xlimg.width = 64 xlimg.height = 64 cellref = "{}{}".format(get_column_letter(cell.column + 1), cell.row + 1) ws.add_image(xlimg, cellref) ws.row_dimensions[cell.row + 1].height = 64 else: ws.cell( row=cell.row + 1, column=cell.column + 1, value=table.textString(cell.row, cell.column), ) #nested function def as_text(value): if value is None: return "" return str(value) # make another pass to set the widths + a little extra for column_cells in ws.columns: length = max(len(as_text(cell.value)) for cell in column_cells) ws.column_dimensions[column_cells[0].column_letter].width = length * 1.25 wb.save("{}/{}.xlsx".format(xlpath, xlname)) ctypes.windll.user32.MessageBoxW(0, fpt , "Files Saved", 1) except Exception as err: traceback.print_exception(err) def getEffectiveNameFromBtrId(btrid: Db.ObjectId): rec = Db.BlockTableRecord(btrid) if rec.isAnonymous() and rec.hasXData("AcDbBlockRepBTag"): for dfx, val in rec.xData("AcDbBlockRepBTag"): if dfx == 1005: hnd = Db.Handle(val) dynid = rec.database().tryGetObjectId(False, hnd) dynrec = Db.BlockTableRecord(dynid) return dynrec.getName() return rec.getName() aridzv.1 point
-
new code that add the name, wait for the next release of you need effectiveName from pyrx import Rx, Ge, Gi, Gs, Db, Ap, Ed, Ax import traceback from openpyxl.drawing.image import Image as xlImage from openpyxl.utils.cell import get_column_letter from openpyxl.styles import Alignment import openpyxl as xl import pathlib import wx @Ap.Command() def doit(): try: db = Db.curDb() ps, id, _ = Ed.Editor.entSel("\nSelect a table: ", Db.Table.desc()) if ps != Ed.PromptStatus.eOk: raise RuntimeError("Selection Error! {}: ".format(ps)) # paths xlpath = pathlib.Path(db.getFilename()).parent xlname = pathlib.Path(db.getFilename()).stem wb = xl.Workbook() ws = wb.active table = Db.Table(id) opts = Db.TableIteratorOption.kTableIteratorSkipMerged for cell in table.cells(opts): # format all cells currentCell = ws.cell(cell.row + 1, cell.column + 1) currentCell.alignment = Alignment(horizontal="center", vertical="center") if table.cellType(cell.row, cell.column) == Db.CellType.kBlockCell: blkid = table.blockTableRecordId(cell.row, cell.column) blkrec = Db.BlockTableRecord(blkid) #open the block img: wx.Image = Gs.Core.getBlockImage(blkid, 64, 64, 1.0, [0, 0, 0]) img.SetMaskColour(0, 0, 0) img.SetMask(True) #change to effectiveName next release imgpath = "{}/{}.png".format(xlpath, blkrec.name()) img.SaveFile(imgpath, wx.BITMAP_TYPE_PNG) xlimg = xlImage(imgpath) xlimg.width = 64 xlimg.height = 64 cellref = "{}{}".format(get_column_letter(cell.column + 1), cell.row + 1) ws.add_image(xlimg, cellref) ws.row_dimensions[cell.row + 1].height = 64 else: ws.cell( row=cell.row + 1, column=cell.column + 1, value=table.textString(cell.row, cell.column), ) #nested function def as_text(value): if value is None: return "" return str(value) # make another pass to set the widths + a little extra for column_cells in ws.columns: length = max(len(as_text(cell.value)) for cell in column_cells) ws.column_dimensions[column_cells[0].column_letter].width = length * 1.25 wb.save("{}/{}.xlsx".format(xlpath, xlname)) except Exception as err: traceback.print_exception(err)1 point
-
I’m going to add this to the API https://github.com/CEXT-Dan/PyRx/issues/2781 point
-
If you hurry, you'll make people laugh... I am replacing the formula string with (setq volume (/ (* height (+ area1 area2 (sqrt (* area1 area2)))) 3)) The code now works like a Swiss watch. ;; Thanks GLAVCVS 17.04.2025 ;; https://www.cadtutor.net/forum/topic/97474-select-a-dimension-value-as-the-height-of-the-pyramid/ ;; Calculating the volume of a truncated pyramid or a truncated cone /_\ \_/ (defun c:PyramidConeVolumeTDIM (/ ent obj txt radius area1 area2 volume insPoint to) (vl-load-com) (defun getHeightFromText () (setq ent (entsel "\nSelect the text object containing the height: ")) (setq obj (vlax-ename->vla-object (car ent))) (if (= (setq to (CDR (ASSOC 0 (entget (car ent))))) "TEXT") (setq txt (vla-get-TextString obj)) (if (= to "DIMENSION") (if (= (setq txt (vla-get-textOverride obj)) "") (setq txt (rtos (vla-get-measurement obj) 2 2)) txt ) (progn (alert "No valid object") nil) ) ) (setq height (distof txt 2)) ) (defun getCircleArea (circle) (setq radius (vla-get-Radius circle)) (* pi radius radius) ) (defun selectBase () (setq ent (entsel "\nSelect a polyline or circle as the base: ")) (setq obj (vlax-ename->vla-object (car ent))) (if (eq (vla-get-ObjectName obj) "AcDbCircle") (getCircleArea obj) (vlax-curve-getArea obj) ) ) (setq area1 (selectBase)) (princ (strcat "\nArea A1: " (rtos area1 2 3))) (setq area2 (selectBase)) (princ (strcat "\nArea A2: " (rtos area2 2 3))) (getHeightFromText) (princ (strcat "\nHeight H: " (rtos height 2 3))) ;; Calculating the volume in mm3 (setq volume (/ (* height (+ area1 area2 (sqrt (* area1 area2)))) 3)) (setq volume_m3 (/ volume 1e9)) (princ (strcat "\nThe volume of a truncated pyramid or cone (m3): " (rtos volume_m3 2 3))) (setq insPoint (getpoint "\nSpecify the insertion point of the result: ")) (command "_.TEXT" insPoint "2.5" "0" (strcat "Volume (m3): " (rtos volume_m3 2 3))) (princ) )1 point
-
Here: your code modified to make it work. But there's one problem: you haven't specified the volume calculations correctly. I think I should leave this to you. (defun c:PyramidConeVolumeT (/ ent obj txt radius area1 area2 volume insPoint to) (vl-load-com) (defun getHeightFromText () (setq ent (entsel "\nSelect the text object containing the height: ")) (setq obj (vlax-ename->vla-object (car ent))) (if (= (setq to (CDR (ASSOC 0 (entget (car ent))))) "TEXT") (setq txt (vla-get-TextString obj)) (if (= to "DIMENSION") (if (= (setq txt (vla-get-textOverride obj)) "") (setq txt (rtos (vla-get-measurement obj) 2 2)) txt ) (progn (alert "No valid object") nil) ) ) (setq height (distof txt 2)) ) (defun getCircleArea (circle) (setq radius (vla-get-Radius circle)) (* pi radius radius) ) (defun selectBase () (setq ent (entsel "\nSelect a polyline or circle as the base: ")) (setq obj (vlax-ename->vla-object (car ent))) (if (eq (vla-get-ObjectName obj) "AcDbCircle") (getCircleArea obj) (vlax-curve-getArea obj) ) ) (setq area1 (selectBase)) (princ (strcat "\nArea A1: " (rtos area1 2 3))) (setq area2 (selectBase)) (princ (strcat "\nArea A2: " (rtos area2 2 3))) (getHeightFromText) (princ (strcat "\nHeight H: " (rtos height 2 3))) ;; Calculating the volume in mm3 (setq volume (* (/ height 3) (+ area1 (sqrt (* area1 area2)) area2))) ;; Converting volume to m3 (setq volume_m3 (/ volume 1e9)) (princ (strcat "\nThe volume of a truncated pyramid or cone (m3): " (rtos volume_m3 2 3))) (setq insPoint (getpoint "\nSpecify the insertion point of the result: ")) (command "_.TEXT" insPoint "2.5" "0" (strcat "Volume (m3): " (rtos volume_m3 2 3))) (princ) )1 point
-
You've moved a lot of things around in this code compared to the original you posted. So it's hard to get it to work.1 point
-
1 point
-
You may need to copy the code again: I changed something.1 point
-
Hi Replacing your function with this one... is that enough? (defun getHeightFromText () (setq ent (entsel "\nSelect the text object containing the height: ")) (setq obj (vlax-ename->vla-object (car ent))) (if (= (setq to (CDR (ASSOC 0 (entget (car ent))))) "TEXT") (setq txt (vla-get-TextString obj)) (if (= to "DIMENSION") (if (= (setq txt (vla-get-textOverride obj)) "") (setq txt (rtos (vla-get-measurement obj) 2 2)) txt ) (progn (alert "No valid object") nil) ) ) (setq height (distof txt 2)) )1 point
-
Something I noticed, one of your objects is black, so you might want to set the bk color and mask to the color of BricsCAD’s background color so it’s not xor'ed out... it seems okay though img: wx.Image = Gs.Core.getBlockImage(id, 256, 256, 1.0, [33, 40, 48]) img.SetMaskColour(33, 40, 48)1 point
-
Just a comment, using ssget may end up with A1 A3 A2 as order as ssget gets in creation order, may still be better to do pick pick. Does the number go above 26 ? If so this is helpful based on Excel columns. ; Number2Alpha - Converts Number into Alpha string ; Function By: Gilles Chanteau from Marseille, France ; Arguments: 1 ; Num# = Number to convert ; Syntax example: (Number2Alpha 731) = "ABC" (NUMBER2ALPHA 1) "A" (NUMBER2ALPHA 27) "AA"1 point
-
My $0.05, I would add a width to the edit boxes as they are all over the place. mc_dialog : dialog { label = "NHAP THONG SO MAT CAT DOC"; : column { : edit_box { label = "Ty le ngang 1/?"; key = "hf"; } : edit_box { label = "Ty le dung 1/?"; key = "vf"; } : edit_box { label = "Cao do so sanh (m)"; key = "level"; } : edit_box { label = "Ly trinh diem dau (m)"; key = "st"; } : edit_box { label = "Chieu cao Text Title"; key = "th"; } : edit_box { label = "Chieu cao Text cao do"; key = "th1"; } : edit_box { label = "File du lieu TXT"; key = "file"; } : button { label = "Chon File..."; key = "chonfile"; } } : row { : button { label = "Ve"; is_default = true; key = "accept"; } : button { label = "Thoat"; key = "cancel"; } } } Have a look at writing the dcl as part of the lisp code, you use write line and the (setq fo (open (setq fname (vl-filename-mktemp "" "" ".dcl")) "w")) to write a temporary file just delete the file at end of dcl use. Have a look at Multi getvals.lsp will write a dcl for you based on a list of edit boxes. Around 3 lines of code to make a dcl. You can set file name so can see the dcl code. Then use the rlx code to make lisp code. Convert dcl 2 lisp rlx.lspMulti GETVALS.lsp1 point
-
1 point