Fun... or at least I had fun writing this....
I include two functions that will increment attributes.
The first AtInc will increment one attribute by selecting the attribute.
It will ignore text characters and increment numbers using a 1 integer (can be changed to prompt for other values)
The second AtsInc will increment the first attributes in multiple selected blocks (ignoring letters so you can have prefixes etc...)
P=
Code:
;___________________________________________________________________________________________________________|
;
; Written By: Peter Jamtgaard C.E., P.E., S.E. copyright 2018 All Rights Reserved
;___________________________________________________________________________________________________________|
;
; Any use by unauthorized person or business is strictly prohibited.
; Include Shorthand.lsp
;___________________________________________________________________________________________________________|
;___________________________________________________________________________________________________________|
;
; Command Line Function Header List
;___________________________________________________________________________________________________________|
;* C:AtInc
;* Command line function to increment a selected attribute
;* C:AttributeIncrement
;* Command line function to increment a selected attribute
;* C:AtsInc
;* Command line function to increment attributes in selected blocks
;* C:AttributesIncrement
;* Command line function to increment attributes in selected blocks
;___________________________________________________________________________________________________________|
;
; General Function Header List
;___________________________________________________________________________________________________________|
; Function List Argument1 Argument2 Arguement3
;* (AttributeFromBlock objAttribute)
;* Function to check if attribute or block with attribute and return attribute
;* (AttributeIncrement objAttribute intIncrement)
;* Function to increment an attribute with a value intIncrement
;* (IsAsciiNumeral intAscii)
;* Function to text an Ascii value to see if it a numeral or decimal place
;* (SelectionSetToList ssSelections)
;* Function to convert a entity based selection set to a list
;* (TextIncrement strTextString intIncrement)
;* Function to increment a textstring with a value intIncrement
;* (TextNumeralParse strTextString)
;* Function to parse a string and separate letters and numerals in groups and return a list of strings.
;$ End Header
;___________________________________________________________________________________________________________|
;
; Command line function to increment an attribute
;___________________________________________________________________________________________________________|
(defun C:AtInc ()(C:AttributeIncrement))
(defun C:AttributeIncrement (/ entSelection intIncrement lstSelection objAttribute)
(while (and ;(setq intIncrement (getint "\nEnter Increment Value: "))
(setq intIncrement 1)
(setq lstSelection (nentsel "\nSelect Attribute: "))
(setq entSelection (car lstSelection))
(setq objAttribute (vlax-ename->vla-object entSelection))
(wcmatch (vla-get-objectname objAttribute) "AcDbAttribute")
)
(AttributeIncrement objAttribute 1)
)
(prin1)
)
;___________________________________________________________________________________________________________|
;
; Command line function to increment attributes in selected blocks
;___________________________________________________________________________________________________________|
(defun C:AtsInc ()(C:AttributesIncrement))
(defun C:AttributesIncrement (/ intIncrement lstSelections ssSelections)
(if (and ;(setq intIncrement (getint "\nEnter Increment Value: "))
(setq intIncrement 1)
(princ "\nSelect Blocks: ")
(setq ssSelections (ssget (list (cons 0 "insert"))))
(setq lstSelections (SelectionSetToList ssSelections))
)
(mapcar '(lambda (X)(AttributeIncrement X intIncrement)) lstSelections)
)
(prin1)
)
;___________________________________________________________________________________________________________|
;
; Function to check if attribute or block with attribute and return attribute
;___________________________________________________________________________________________________________|
(defun AttributeFromBlock (objAttribute / lstAttributes)
(if (or (wcmatch (vla-get-objectname objAttribute) "AcDbAttribute")
(and (wcmatch (vla-get-objectname objAttribute) "AcDbMInsertBlock,AcDbBlockReference")
(= (vlax-get objAttribute "HasAttributes") -1)
(setq lstAttributes (vlax-invoke objAttribute "getattributes"))
(setq objAttribute (car lstAttributes))
)
)
objAttribute
)
)
;___________________________________________________________________________________________________________|
;
; Function to increment an attribute with a value intIncrement
;___________________________________________________________________________________________________________|
(defun AttributeIncrement (objAttribute intIncrement / sngTextString strTextString)
(if (and (setq objAttribute (AttributeFromBlock objAttribute))
(setq strTextString (vla-get-textstring objAttribute))
(setq lstTextStrings (TextNumeralParse strTextString))
(setq lstTextStrings (mapcar '(lambda (X) (TextIncrement X 1)) lstTextStrings))
(setq lstTextStrings (vl-remove nil lstTextStrings))
(setq lstTextStrings (vl-remove "" lstTextStrings))
(setq strTextString (apply 'strcat lstTextStrings))
)
(progn (vla-put-textstring objAttribute strTextString) T)
)
)
;___________________________________________________________________________________________________________|
;
; Function to text an Ascii value to see if it a numeral or decimal place.
;___________________________________________________________________________________________________________|
(defun IsAsciiNumeral (intAscii)
(and (member intAscii (vl-string->list ".0123456789")))
)
;___________________________________________________________________________________________________________
;
; Function to convert a entity based selection set to a list.
;___________________________________________________________________________________________________________
(defun SelectionSetToList (ssSelections / entSelection intCount lstObjects objSelection )
(repeat (setq intCount (sslength ssSelections))
(setq intCount (1- intCount))
(setq entSelection (ssname ssSelections intCount))
(setq objSelection (vlax-ename->vla-object entSelection))
(setq lstObjects (cons objSelection lstObjects))
)
lstObjects
)
;___________________________________________________________________________________________________________|
;
; Function to increment a textstring with a value intIncrement
;___________________________________________________________________________________________________________|
(defun TextIncrement (strTextString intIncrement / sngTextString)
(if (and (apply 'and (mapcar 'IsAsciiNumeral (vl-string->list strTextString)))
(or (and (vl-string-search "." strTextString)
(setq sngTextString (atof strTextString))
)
(setq sngTextString (atoi strTextString))
)
(setq sngTextString (+ sngTextString intIncrement))
)
(setq strtextString (vl-princ-to-string sngTextString))
strTextString
)
)
;___________________________________________________________________________________________________________|
;
; Function to parse a string and separate letters and numerals in groups and return a list of strings.
;___________________________________________________________________________________________________________|
(defun TextNumeralParse (strTextString / intAsciiOld intAsciiOld lstOfAsciiValues lstSublist)
(if (= (type strTextString) 'LIST)
(setq strTextString (car strTextString))
)
(foreach intAscii (vl-string->list strTextString)
(or (and (or (= (IsAsciiNumeral intAscii)
(IsAsciiNumeral intAsciiOld)
)
(not intAsciiOld)
)
(setq lstSublist (cons intAscii lstSublist))
)
(and
(setq lstOfAsciiValues (cons (reverse lstSublist) lstOfAsciiValues))
(setq lstSublist (list intAscii))
)
)
(setq intAsciiOld intAscii)
)
(setq lstOfAsciiValues (cons (reverse lstSublist) lstOfAsciiValues))
(mapcar 'vl-list->string (reverse lstOfAsciiValues))
)
(vl-load-com)