Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Increase attribute alphabetically

11 REPLIES 11
Reply
Message 1 of 12
knj777
881 Views, 11 Replies

Increase attribute alphabetically

Hi all,

Does anyone have a LISP routine or another trick to increase an attribute alphabetically?

 

I have a block whose tag is defaulted to "A". Typically, I will copy this block however many timesI need it in a drawing (could be 20 or so). I would like to increase the letter by one, either when I copy it or afterwards when they are all placed.

Anyone have any suggestions? I've looked around alot for a LISP to do this but had no luck.

11 REPLIES 11
Message 2 of 12
lgabriel
in reply to: knj777

Does the block have only one attribute? Will the total number of blocks be inserted during one drawing session?

 

There are two approaches.

 

One is to always copy the last block inserted so the attribute can be extracted and incremented to the next letter.

 

Second is to store the attribute (letter "A") in one of the user defined system variable USERS1 (for strings). When you start to insert the block, if USERS1 is nil, then the attribute will be "A". Once inserted, USERS1 will be changed to "A", so when you enter the block again, the program will read the current value of USERS1 ("A"), increment to "B", then use the "B" as the attribute for the next block. Only problem is that the value in USERS1 cannot be saved. So if you close and then open the drawing at a later time and start to insert the block, it will start with "A".

 

Let me know which would work better for you and I will try to write something

Message 3 of 12
knj777
in reply to: lgabriel

Interesting, I attached a drawing to (hopefully) better explain.

I am mistaken though. I originally stated these are attributes. Well, one block has an attribute and another block has text. Either way, I would like the attribute, or text, to be increased by one.

 

I think youre ideas sound great. Normally, we insert all of these blocks necessary in one drawing session.

But to play it on the safer side, maybe the first approach would be better.

 

Message 4 of 12
stevor
in reply to: knj777

Regular Copy commands would increment the ATT only with some Reactor process, my guess.

 

What is more common, and which parallels the 2 from lgabriel, is either:

 

-  a routine that inserts the BLOCK and provides an incremented string for the ATT. or,

 

 - a routine that gets all the INSERTs of that block name, and increments the ATTs  by their order in the entity data base, which is the order created.

 

Examples of these have been previously shown  here, and at other web sites, more or less.

And after A thru Z, then what, AA, AB, AC, or BB, CC, DD, or what?

S
Message 5 of 12
knj777
in reply to: stevor

After Z, would be ZA, ZB, ZC, ZD, etc.

 

Message 6 of 12
3wood
in reply to: knj777

Please try attached INNB.vlx, it does exactly what you need.

Message 7 of 12
pbejse
in reply to: knj777

And after "ZZ" ? whats next? shouldnt that be "AA" instead?" then "AB" ...... "BA" "BB" ?

 

Message 8 of 12
pbejse
in reply to: pbejse

Anyhoo. try this

 

(defun c:CPI (/ str+ Block ent Numtext  Blk  pt1 pt2 NewObj)
;;;	Owen Wengerd http://www.manusoft.com/		;;;
;;;	Additional cond for INTEGER pBe			;;;
(defun Str+  (source / prefix rchar)
      (If (eq (type (read source)) 'INT)
          	(itoa(1+ (atoi source)))
      (cond
            ((= source "") "A")
            ((= (setq prefix (substr source
                                     1
                                     (1- (strlen source)))
                      rchar  (substr source (strlen source))
                      )
                "Z"
                )
             (strcat (Str+ prefix) "A")
             )
            ((strcat prefix (chr (1+ (ascii rchar)))))
            )
      )
      )
;;;	pBe old routine circa 2009	;;;
      (vl-load-com)
        (prompt "\rSelect Block/Text:")
        (cond
              ((and
      	(setq Block (ssget "_+.:S:L" '((0 . "INSERT,*TEXT"))))
        (setq l "" Block (ssname Block 0))
      	(setq NumText
                   (if (and (eq (cdr (assoc 0
                                            (entget Block)))
                                "INSERT")
                            (setq Blk  (member '(66 . 1)
                                               (entget Block))))
                         (cdr (assoc 1 (entget (entnext Block))))
                         (cdr (assoc 1 (entget Block)))))
	(setq ent (vlax-ename->vla-object Block))
	(setq pt1 (getpoint "\nPick Base Point:"))
	(while 
      	(setq pt2 (getpoint pt1 "\nNext Point"))
          	(vlax-invoke (setq NewObj (vla-copy ent)) 'Move pt1 pt2)
		(vla-put-textstring
		      (if (not Blk)
		            NewObj
		            (car (vlax-invoke NewObj 'GetAttributes))
		            )
			(setq NumText (str+ numtext))	
                      )
        (setq pt1 pt2 ent NewObj)
              				)
        			)
               		)
              )
	(princ)
        )

 

Message 9 of 12
marko_ribar
in reply to: pbejse

After "ZZ" comes "ZZA" and don't forget for lowercase letters... Nice to see that you format your code pBe...

Cheers...

 

M.R.

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 10 of 12
marko_ribar
in reply to: marko_ribar

What I meant to say after "zz" comes "zzA" if incrementing - if decrementing it's just opposite :

 

Check these 2 examples for string incrementation :

 

- First one - string contains number inside it :

(defun prestr ( str char )
  (substr str 1 (vl-string-position (ascii char) str))
)

(defun sufstr ( str char )
  (substr str (+ (vl-string-position (ascii char) str nil t) 2) (- (strlen str) (vl-string-position (ascii char) str nil t) 1))
)

(defun incrstr ( str / k n nstr pstr sstr )
  (setq k 0)
  (while (and (<= (setq k (1+ k)) (strlen str)) (= 0 (setq n (atoi (substr str k (+ (- (strlen str) k) 1)))))))
  (setq nstr (itoa n))
  (setq pstr (prestr str (substr nstr 1 1)))
  (setq sstr (sufstr str (substr nstr (strlen nstr) 1)))
  (strcat pstr (itoa (+ n 1)) sstr)
)
  
(defun decrstr ( str / k n nstr pstr sstr )
  (setq k 0)
  (while (and (<= (setq k (1+ k)) (strlen str)) (= 0 (setq n (atoi (substr str k (+ (- (strlen str) k) 1)))))))
  (setq nstr (itoa n))
  (setq pstr (prestr str (substr nstr 1 1)))
  (setq sstr (sufstr str (substr nstr (strlen nstr) 1)))
  (strcat pstr (itoa (- n 1)) sstr)
)

(defun c:strincr ( / s )
  (if (null ch)
    (progn
      (initget 1 "Increment + Decrement -")
      (setq ch (getkword "\nIncrement / Decrement [ + / - ] : "))
    )
  )
  (if (null str) (setq str (getstring "\nString : ")))
  (prompt "\nENTER to continue, or any other key + ENTER to restart, or SPACE + ENTER to change choice")
  (while (eq "" (setq s (getstring t)))
    (progn
      (cond
             ( (or (eq ch "Increment") (eq ch "+"))
               (setq str (incrstr str))
             )
             ( (or (eq ch "Decrement") (eq ch "-"))
               (setq str (decrstr str))
             )
      )
      (princ str)
      (terpri)
    )
  )
  (if (and (eq s " ") (or (eq ch "Increment") (eq ch "+"))) (progn (setq ch "-") (c:strincr)))
  (if (and (eq s " ") (or (eq ch "Decrement") (eq ch "-"))) (progn (setq ch "+") (c:strincr)))
  (setq ch nil str nil)
  (c:strincr)
  (princ)
)

 - Second one - string contains only letters :

(defun incrstr ( str / lchr pref chrlst char )
  (setq lchr (substr str (strlen str) 1))
  (if (/= 1 (strlen str)) (setq pref (substr str 1 (- (strlen str) 1))) (setq pref ""))
  (setq chrlst '(65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122))
  (if (eq (ascii lchr) 122) (setq pref (strcat pref lchr) char (chr 65)) (setq char (chr (cadr (member (ascii lchr) chrlst)))))
  (strcat pref char)
)
  
(defun decrstr ( str / lchr pref chrlst char )
  (setq lchr (substr str (strlen str) 1))
  (if (/= 1 (strlen str)) (setq pref (substr str 1 (- (strlen str) 1))) (setq pref ""))
  (setq chrlst '(65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122))
  (if (eq (ascii lchr) 65) (progn (if (/= pref "") (setq pref (substr pref 1 (- (strlen pref) 1)))) (setq char (chr 122))) (setq char (chr (cadr (member (ascii lchr) (reverse chrlst))))))
  (strcat pref char)
)

(defun c:strincr-abc ( / s )
  (if (null ch)
    (progn
      (initget 1 "Increment + Decrement -")
      (setq ch (getkword "\nIncrement / Decrement [ + / - ] : "))
    )
  )
  (if (null str) (setq str (getstring "\nString : ")))
  (prompt "\nENTER to continue, or any other key + ENTER to restart, or SPACE + ENTER to change choice")
  (while (eq "" (setq s (getstring t)))
    (progn
      (cond
             ( (or (eq ch "Increment") (eq ch "+"))
               (setq str (incrstr str))
             )
             ( (or (eq ch "Decrement") (eq ch "-"))
               (setq str (decrstr str))
             )
      )
      (princ str)
      (terpri)
    )
  )
  (if (and (eq s " ") (or (eq ch "Increment") (eq ch "+"))) (progn (setq ch "-") (c:strincr-abc)))
  (if (and (eq s " ") (or (eq ch "Decrement") (eq ch "-"))) (progn (setq ch "+") (c:strincr-abc)))
  (setq ch nil str nil)
  (c:strincr-abc)
  (princ)
)

 To see what I meant, use second one c:strincr-abc...

 

Unfortunatelly, it's impossible to combine these 2 into single lisp, as numbers may increase - decrease from - infinity to + infinity...

 

Hope I explained now what I should...

Regards, M.R.

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 11 of 12
knj777
in reply to: knj777

Hey thanks alot for all your help. I'll give these a shot!

Message 12 of 12
marko_ribar
in reply to: knj777

It's just string incrementation, you should implement it into pBe' code with attributes...

 

You're welcome, any time...

M.R.

Marko Ribar, d.i.a. (graduated engineer of architecture)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost