这就是你需要的。
命令:RB(如在读取块中);可以随意重命名底部函数
在该函数中,您可以设置模式(现在是“*”)新的价值观(现在是“你好,世界!”)
(当我将模式设置为带小大写字母的“*”,这给我带来了问题;
使用大写字母,它可以工作(对于我的dwg)
(顺便说一句,标签应该是大写字母,最好没有特殊字符,除了“_”,但那只是我,我在这类事情上有点过时了)
如果它不完全符合你的期望,请告诉我。
现在,星号需要在在中间的某个地方。它需要前面有东西,后面有东西。(我应该解决这个问题)
- ;; @file : Kind of a QSELECT, where we select blocks with attributes with a certain pattern with wildcard
- ;; @author: Emmanuel Delay - emmanueldelay@gmail.com - september 2014
- ;; - First thing, we want a system for wild cards. The pattern is ""*"", where the * is the wildcard.
- ;; so we want to find values like ""Att_11""
- ;; - We want to find all blocks in the dwg with an attribute where the tag has such a pattern.
- ;; - We give all those attributes a new value
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; Read blocks with attributes, pass through wildcard, set value to attributes
- (defun readBlocks (pattern newValue / blocks block i)
- (setq
- i 0
- blocks (ssget "x" '((0 . "INSERT")(66 . 1)) )
- )
- (repeat (sslength blocks)
- (setq block (vlax-ename->vla-object (ssname blocks i)))
- (foreach att (vlax-invoke block 'GetAttributes)
- (setq tag (vla-get-tagstring att))
- (if (wildcard pattern tag)
- (LM:vl-setattributevalue block tag newValue)
- )
- )
- (setq i (+ i 1))
- )
- )
- ;; Set Attribute Value - Lee Mac
- ;; Sets the value of the first attribute with the given tag found within the block, if present.
- ;; blk - [vla] VLA Block Reference Object
- ;; tag - [str] Attribute TagString
- ;; val - [str] Attribute Value
- ;; Returns: [str] Attribute value if successful, else nil.
- ;; @see http://www.lee-mac.com/attributefunctions.html#vlgetattributevalue
- (defun LM:vl-setattributevalue ( blk tag val )
- (setq tag (strcase tag))
- (vl-some
- '(lambda ( att )
- (if (= tag (strcase (vla-get-tagstring att)))
- (progn (vla-put-textstring att val) val)
- )
- )
- (vlax-invoke blk 'getattributes)
- )
- )
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;; String operations
- ;; Wild card. Compares two strings: a pattern containing 1 "*" character; and a value to be evaluated.
- ;; Returns T if the value contains the pattern
- ;; examples:
- ;; (wildcard "AZERTYU*IOP" "AZERTYU12345IOP") ;; returns T
- ;; (wildcard "AZERTYU*IOP" "AZRTYU12345IOP") ;; returns nil
- (defun wildcard (pattern val / result startString endString asteriskPlace charsLeft)
- ;; we search for the place of the asterisk in the string. (nil = nothing found)
- (setq
- asteriskPlace (vl-string-search "*" pattern)
- charsLeft (- (- (strlen pattern) 1) asteriskPlace)
- result nil
- )
- (if (= asteriskPlace nil)
- (setq result nil) ;; no asterisk in found in the pattern
- (progn
- (setq startString (substring pattern 0 asteriskPlace))
- (setq endString (substring pattern (- 0 charsLeft) nil))
- (if (and ;; so we check if the start and end of the pattern and value are equal
- (= startString (substring val 0 asteriskPlace))
- (= endString (substring val (- 0 charsLeft) nil))
- )
- (setq result T)
- (setq result nil)
- )
- )
- )
- result
- )
- ;; I want substr to work like it does in PHP (and most languages I know). the lisp subst is too limited
- ;; So I made my own substring
- ;; [complaint]unlike civilized languages, for some reason substr is "1-based" (first character of start is 1); who ever invented this ...[/complaint]
- ;; also I add negative numbers to the start parameter.
- ;; A start of -2 means it starts looking at the second-last character
- ;; set len to nil to let the lenth parameter empty
- ;;
- ;; examples:
- ;; (substring "AZERTYUIOP" 3 4) returns "RTYU" ;; start from letter 3 (0 based), give 4 letters
- ;; (substring "AZERTYUIOP" 4 nil) returns "TYUIOP" ;; start from letter 4 (0 based), give all the rest of the letters
- ;; (substring "AZERTYUIOP" -3 nil) returns "IOP" ;; start from the last 3 letters, give all the rest of the letters
- ;; (substring "AZERTYUIOP" -5 2) returns "YU" ;; start from the last 5 letters, give 2 letters
- (defun substring (string start len / st ln)
- (if (< start 0)
- (setq st (+ (strlen string) (+ start 1)))
- (setq st (+ start 1))
- )
- (if (= len nil)
- (setq ln (strlen string))
- (setq ln len)
- )
- (substr string st len)
- )
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- (defun c:rb ()
- (readBlocks ""*"" "Hello World!")
- (princ)
- )
|