AutoLISP 如何得到動態圖塊名稱

一般來說
使用entget獲取圖元資料
(cdr (assoc 2 (entget (car (entsel "\n 選取一個圖塊:")))))
會得到圖塊名稱
但因為動態圖塊的特性,
也就是匿名圖塊
名稱大多是*U開頭

提供簡單得到動態圖塊名稱的語法:
(vl-load-com)
(vla-get-Effectivename (vlax-ename->vla-object (car (entsel "\n 選取一個圖塊:"))))

其他方法

下面LISP源自於作者Lee Mac
原網址-Effective Block Name

Visual LISP(從圖塊的VLA物件得到有效圖塊名稱)
例如:(LM:effectivename (vlax-ename->vla-object (car(entsel "\n 選取一個圖塊:"))))
;; Effective Block Name (有效圖塊名稱) -  Lee Mac
;; obj - [vla] VLA Block Reference object (圖塊的VLA物件)
(defun LM:effectivename ( obj )
    (vlax-get-property obj
        (if (vlax-property-available-p obj 'effectivename)
            'effectivename
            'name
        )
    )
)

Vanilla LISP(從圖塊的圖元名稱得到有效圖塊名稱)
例如:(LM:al-effectivename (car(entsel "\n 選取一個圖塊:")))
;; Effective Block Name (有效圖塊名稱) -  Lee Mac
;; ent - [ent] Block Reference entity (圖塊的圖元名稱)

(defun LM:al-effectivename ( ent / blk rep )
    (if (wcmatch (setq blk (cdr (assoc 2 (entget ent)))) "`**")
        (if
            (and
                (setq rep
                    (cdadr
                        (assoc -3
                            (entget
                                (cdr
                                    (assoc 330
                                        (entget
                                            (tblobjname "block" blk)
                                        )
                                    )
                                )
                               '("AcDbBlockRepBTag")
                            )
                        )
                    )
                )
                (setq rep (handent (cdr (assoc 1005 rep))))
            )
            (setq blk (cdr (assoc 2 (entget rep))))
        )
    )
    blk
)

Block Name to Effective Block Name (圖塊名轉成有效圖塊名稱)
例如:(LM:name->effectivename "*U151")
;; Block Name -> Effective Block Name (圖塊名轉成有效圖塊名稱) -  Lee Mac
;; blk - [str] Block name (圖塊名)

(defun LM:name->effectivename ( blk / rep )
    (if
        (and (wcmatch blk "`**")
            (setq rep
                (cdadr
                    (assoc -3
                        (entget
                            (cdr (assoc 330 (entget (tblobjname "block" blk))))
                           '("AcDbBlockRepBTag")
                        )
                    )
                )
            )
            (setq rep (handent (cdr (assoc 1005 rep))))
        )
        (cdr (assoc 2 (entget rep)))
        blk
    )
)


相關連結



留言

這個網誌中的熱門文章

AutoCAD 填充線(剖面線)樣式簡易製作產生器

AutoCAD 如何自訂線型教學

AutoCAD 動態圖塊-如何在所有可見性顯示或隱藏