Edit and replace dynamic block attributes

Edit and replace dynamic block attributes

orourkestuart
Contributor Contributor
712 Views
4 Replies
Message 1 of 5

Edit and replace dynamic block attributes

orourkestuart
Contributor
Contributor

I'm looking for a simple LISP to change the attribute entries in an existing dynamic block.

 

I have a block "DIAG_CROSS" that has up to 14 attributes. I need to change 2 of the attributes in specific ways.

 

1: The attribute "SIZE" needs to be changed from a real number to an integer and then rewritten to the block attribute as e.g. 50.000 becomes 50. I understand the rtoi operator, but I don't know how to write the result back to the block.

 

2: The attribute "CODE" needs to have any numerical characters removed from the end and then rewritten back to the block attribute as e.g. WUG13 becomes WUG. I understand the VL-STRING-RIGHT-TRIM "0123456789" operation but don't know how to write the result back to the block.

 

 

thanks in advance

 

 

 

0 Likes
Accepted solutions (1)
713 Views
4 Replies
Replies (4)
Message 2 of 5

ВeekeeCZ
Consultant
Consultant
Accepted solution

Possibly like this.

Untested. Since you did not bother with posting a sample block, the test and fix is up to you.

 

(vl-load-com)

(defun c:DiagCrossTrim ( / s i e n z c)
  
  (if (setq s (ssget '((0 . "INSERT"))))
    (repeat (setq i (sslength s))
      (setq e (ssname s (setq i (1- i))))
      (if (and (not (vl-catch-all-error-p (setq n (vl-catch-all-apply 'getpropertyvalue (list e "BlockTableRecord/Name")))))
	       (= n "DIAG_CROSS")
	       (not (vl-catch-all-error-p (setq z (vl-catch-all-apply 'getpropertyvalue (list e "SIZE")))))
	       (not (vl-catch-all-error-p (setq c (vl-catch-all-apply 'getpropertyvalue (list e "CODE")))))
	       )
	(progn
	  (setpropertyvalue e "SIZE" (itoa (fix (atof z))))
	  (setpropertyvalue e "CODE" (vl-string-right-trim "0123456789" c))))))
  (princ)
  )

 

0 Likes
Message 3 of 5

orourkestuart
Contributor
Contributor

thanks for that. will test it now

0 Likes
Message 4 of 5

orourkestuart
Contributor
Contributor

On first testing it mostly works, it does prompt me to "select objects" but I can fix that. I actually didn't realise that rtoi isn't a thing in LISP until I started trying to resolve it myself. I also hadn't encountered getpropertyvalue and setpropertyvalue before. cheers for this BeeKeeCZ 🙂

0 Likes
Message 5 of 5

ВeekeeCZ
Consultant
Consultant

I guess it could be shortened by a bit using just

(itoa (atoi z))

0 Likes