Updating Revision number of multiple block placed in model space

Updating Revision number of multiple block placed in model space

PJha2
Observer Observer
613 Views
6 Replies
Message 1 of 7

Updating Revision number of multiple block placed in model space

PJha2
Observer
Observer

Hello Everyone,

 

I am trying to write a code to update the title block revision number for rev up. Since I have multiple title block to do the rev up. I am writing a lisp code, but its not working. I placed the codes in local folder name UpdateRev.lsp and used Appload to call. but call is not working for some reason. 

 

any suggestion. Attached is the code:

 

(defun c:UpdateRev ()
(setq blkName "TB000000") ; The block name containing the revision attribute
(setq attrName "AB_REV") ; The attribute tag you want to update

;; Get all instances of the block in model space
(setq ss (ssget "_X" (list (cons 0 "INSERT") (cons 2 blkName) (cons 410 "Model"))))

;; Check if any blocks were found
(if ss
(progn
(princ (strcat "\nFound " (itoa (sslength ss)) " instances of block " blkName))
;; Iterate over each block instance
(setq i 0)
(while (< i (sslength ss))
(setq ent (ssname ss i))
(setq entData (entget ent))
(princ (strcat "\nProcessing block instance #" (itoa i)))

;; Find all attributes of the block
(setq attList (vl-remove-if-not '(lambda (x) (eq (car x) 100)) (entget ent)))
(foreach att attList
(setq attEnt (entget (cdr (assoc -1 att))))
(if (and (eq (cdr (assoc 0 attEnt)) "ATTRIB") (eq (cdr (assoc 2 attEnt)) attrName))
(progn
(setq currentVal (cdr (assoc 1 attEnt)))
(princ (strcat "\nFound attribute " attrName " with value " currentVal))
(setq newVal (format "%02d" (1+ (atoi currentVal))))
(setq attEnt (subst (cons 1 newVal) (assoc 1 attEnt) attEnt))
(entmod attEnt)
(princ (strcat "\nUpdated revision from " currentVal " to " newVal))
)
(princ (strcat "\nAttribute " attrName " not found in this instance"))
)
)
(setq i (1+ i))
)
)
(princ "\nNo instances of block found.")
)
(princ)
)

(princ "\nType 'UpdateRev' to increment the revision number.")
(princ)

 

 

0 Likes
614 Views
6 Replies
Replies (6)
Message 2 of 7

pbejse
Mentor
Mentor

@PJha2 wrote:

Hello Everyone,

 

I am trying to write a code to update the title block revision number for rev up.


What does this do? 

 

(setq newVal (format "%02d" (1+ (atoi currentVal))))

 

This format subfunction is missing from the code you posted @PJha2 

 

(defun c:UpdateRev ( / blkName attrName attEnt entData currentVal newVal)
  (setq blkName "TB000000")		; The block name containing the revision attribute
  (setq attrName "AB_REV")		; The attribute tag you want to update

  ;; Get all instances of the block in model space
	(if (setq ss (ssget "_X"
			    (list (cons 0 "INSERT")
				  (cons 2 blkName)
				  (cons 410 "Model")
			    )
		     )
	    )
      ;; Check if any blocks were found
    (progn
      (princ (strcat "\nFound "
		     (itoa (sslength ss))
		     " instances of block "
		     blkName
	     )
      )
      ;; Iterate over each block instance
      (setq i 0)
      (while (< i (sslength ss))
	(setq attEnt nil ent (ssname ss i))
	(princ (strcat "\nProcessing block instance #" (itoa i)))
	;; Find the target TAG attribute of the block
	(setq enx (entnext (ssname ss i)))
	(while(and (null attEnt) (not (eq (cdr (assoc 0 (setq ent (entget enx)))) "SEQEND")))
	  (if (eq attrName (cdr (assoc 2 ent)))(setq attEnt ent))
	  (setq enx (entnext enx))
	)
	(if attEnt
	    (progn
	      (setq currentVal (cdr (assoc 1 attEnt)))
	      (princ (strcat "\nFound attribute "
			     attrName
			     " with value "
			     currentVal
		     )
	      )
	      ;(setq newVal (format "%02d" (1+ (atoi currentVal))))
	      (setq newVal (itoa (1+ (atoi currentVal))))	      
	      (setq attEnt (subst (cons 1 newVal) (assoc 1 attEnt) attEnt) )
	      (entmod attEnt)
	      (princ (strcat "\nUpdated revision from " currentVal " to "  newVal ))
	    	)	
	     )
	    (princ (strcat "\nAttribute "
			   attrName
			   " not found in this instance"
		   )
	    )
	(setq i (1+ i))
	)
      )
    (princ "\nNo instances of block found.")
  )
  (princ)
)
(princ
  "\nType 'UpdateRev' to increment the revision number."
)
(princ)

 

0 Likes
Message 3 of 7

Moshe-A
Mentor
Mentor

@PJha2  hi,

 

check this fix

to prevent variables confusion keep consistency...if the attribute value is a number, set it to variable convert the string to number so you can increment it and set newVal. for printing convert the number to string. 

 

Moshe

 

 

 

(defun c:UpdateRev (/ blkName attrName ss  i ent ename att attList attEnt currentVal newVal)
 (setq blkName "TB000000") ; The block name containing the revision attribute
 (setq attrName "AB_REV") ; The attribute tag you want to update

 ;; Get all instances of the block in model space
 (setq ss (ssget "_X" (list (cons 0 "INSERT") (cons 2 blkName) (cons 410 "Model"))))

 ;; Check if any blocks were found
 (if ss
  (progn
   (princ (strcat "\nFound " (itoa (sslength ss)) " instances of block " blkName))
   ;; Iterate over each block instance
   (setq i 0)
   (while (< i (sslength ss))
    (setq ent (ssname ss i))

    ; collecting enames of attributes
    (setq ename (entnext ent))
    (while (/= (cdr (assoc '0 (entget ename))) "SEQEND")
     (setq attList (cons ename attList))
     (setq ename (entnext ename))
    ); while
     
    ; (setq entData (entget ent))
    (princ (strcat "\nProcessing block instance #" (itoa i)))

    ;; Find all attributes of the block
   ; (setq attList (vl-remove-if-not '(lambda (x) (eq (car x) -1)) (entget ent)))
    (foreach att attList
    ; (getstring "\n***")
     ; (setq attEnt (entget (cdr (assoc -1 att))))
     (setq attEnt (entget att))
     ; (if (and (eq (cdr (assoc 0 attEnt)) "ATTRIB") (eq (cdr (assoc 2 attEnt)) attrName))
     (if (eq (strcase (cdr (assoc 2 attEnt))) (strcase attrName))
      (progn
       (setq currentVal (atoi (cdr (assoc 1 attEnt))))
       (princ (strcat "\nFound attribute " attrName " with value " (itoa currentVal)))
       ; (setq newVal (format "%02d" (1+ (atoi currentVal))))
       (setq newVal (1+ currentVal))
       (setq attEnt (subst (cons 1 (itoa newVal)) (assoc 1 attEnt) attEnt))
       (entmod attEnt)
       (princ (strcat "\nUpdated revision from " (itoa currentVal) " to " (itoa newVal)))
      ); progn
      (princ (strcat "\nAttribute " attrName " not found in this instance"))
     ); if
    ); foreach
    (setq i (1+ i))
   ); while
  ); progn
  (princ "\nNo instances of block found.")
 ); if
 (princ)
)

(princ "\nType 'UpdateRev' to increment the revision number.")
(princ)

 

 

Message 4 of 7

PJha2
Observer
Observer

Thanks it worked

0 Likes
Message 5 of 7

pbejse
Mentor
Mentor

@PJha2  
What about this line?

(setq newVal (format "%02d" (1+ (atoi currentVal))))

 

I left that as it is assuming its doing some special formatting? or you don't need this? do you even have that subfunction?

 

0 Likes
Message 6 of 7

PJha2
Observer
Observer

Hi,

(setq newVal (format "%02d" (1+ (atoi currentVal))))

atoi, converts the current attribute from str to integer. 1+ provide increment for current value."%02d" convert the incremented integer back to string, ensuring it to be 2 digit long.

 

Thanks

 

0 Likes
Message 7 of 7

pbejse
Mentor
Mentor

@PJha2 wrote:

...atoi, converts the current attribute from str to integer. 1+ provide increment for current value."%02d" convert the incremented integer back to string, ensuring it to be 2 digit long.

 


Rigght.png