Jump to content

Increase the last digit of an attribute


itacad

Recommended Posts

Hello...I have been looking for a long time, but since I have not found anything yet, I ask you the following problem.

There is a lot of lisp to realize a numerical sequence with prefixes, suffixes and variable part and that allow to increase the texts or the attributes...some are well made and I use them too...but...

Is it possible simplify everything by increasing a single digit of a selected attribute? (for example the last one)

For example I select an attribute with value 15.QF5 and I wanto to change in 15.QF6

greetings in the meantime

Link to comment
Share on other sites

Hi, heres a subfunction to increment the last numerical character(digit) in the string:

;|
(setq tmp "15.QF5")
(repeat 12 (print (setq tmp (lastchr++ tmp))))
>>
"15.QF6" 
"15.QF7" 
"15.QF8" 
"15.QF9" 
"15.QF0" 
"15.QF1" 
"15.QF2" 
"15.QF3" 
"15.QF4" 
"15.QF5" 
"15.QF6" 
"15.QF7"
|;
(setq lastchr++
  (lambda (s)
    (
      (lambda (L)
        (vl-list->string
          (reverse 
            (cons
              (cond ( (cadr (member (car L) (append '(48 49 50 51 52 53 54 55 56 57 48)))) ) ( (car L) ) )
              (cdr L)
            )
          )
        )
      )
      (reverse (vl-string->list s))
    )
  )
)

 

Link to comment
Share on other sites

Something I was working on solves the problem of when 15.gh10 should go to to 15.gh11, you look at the last character if a number do ok look at next working right to left is it a number and so on so 1 12 123 will work so convert the end numbers to be just that add 1 then update string. 

 

Its at work and I am not there, Lee-Mac has a parse number it may be possible to reverse engineer it. The question lends itself to 2 library routines get number suffix or prefix. May not get a chance today it’s a lambda function needed. Is it between 1-0.

Link to comment
Share on other sites

I'm not sure if you have something like this, 

(defun c:uprev( / anumber ones tens hundreds thousands revisionprefix leadingzero)
;;increases a revision code / text string to the next value

(princ "\n")
;;select revision code
  (setq ent (car (nentsel "\nSelect Revision Letter:")))
  (setq entlst (entget ent))
  (setq currentrevision (cdr (assoc 1 entlst)))

  (setq revlength (strlen currentrevision)) ;;length of selected revision
  (setq revisionprefix "")
  (setq anumber 0)

  (if (< 0 revlength)(progn
      (setq ones (substr currentrevision revlength))
      (if (numberp (read ones))(setq anumber 1))
  ))
  (if (< 1 revlength)(progn
      (setq tens (substr (substr currentrevision (- revlength 1) 2 ) 1 1))
      (if (and (= 1 anumber) (numberp (read tens))) (setq anumber 2))
  ))
  (if (< 2 revlength)(progn
      (setq hundreds (substr (substr currentrevision (- revlength 2) 2 ) 1 1))
      (if (and (= 2 anumber) (numberp (read hundreds))) (setq anumber 3))
  ))
  (if (< 3 revlength)(progn
      (setq thousands (substr (substr currentrevision (- revlength 3) 3 ) 1 1))
      (if (and (= 3 anumber) (numberp (read thousands))) (setq anumber 4))
  ))
  
;;work out numerical revision.
  (if (> anumber 0)
    (progn
      (setq revnumber (substr currentrevision (- revlength (- anumber 1)) anumber))
      (setq revnumber (itoa (+ 1 (read revnumber))))
      (if (and (> revlength anumber)(/= revlength anumber))
        (setq revisionprefix (substr currentrevision 1 (- revlength anumber))) ;;first characters of revision
      )

      ;;fix leading zeros
      (setq leadingzeros (- anumber (strlen revnumber)))
      (if (= 3 leadingzeros)(setq leadingzero "000"))
      (if (= 2 leadingzeros)(setq leadingzero "00"))
      (if (= 1 leadingzeros)(setq leadingzero "0"))
      (if (> 1 leadingzeros)(setq leadingzero ""))

      (setq revletter (strcat revisionprefix leadingzero revnumber))
    )
  )

;;Work out letters revisions
  (if (= anumber 0)
    (progn
      (setq revcode (+ 1 (ascii ones)))

      ;;set exceptions here
      (if (= 73 revcode)(setq revcode 74)) ;;I
      (if (= 79 revcode)(setq revcode 80)) ;;O
      (if (= 105 revcode)(setq revcode 106)) ;;i
      (if (= 111 revcode)(setq revcode 112)) ;;o.. its of to work we go.
      (if (= 91 revcode)(setq revcode 65)) ;;Z -> A. Won't increment 'tens' value
      (if (= 123 revcode)(setq revcode 97)) ;;z -> a Won't increment 'tens' value
      (setq revisionprefix (substr currentrevision 1 (- revlength 1))) ;;first characters of revision
      (setq revletter (strcat revisionprefix (chr revcode)))
    )
  )

  (setq entlst (subst (cons 1 revletter) (assoc 1 entlst) entlst))
  (entmod entlst)
  (entupd ent)
  (setvar "CMDECHO" 0)
  (command "regen") ;;in case of nested blocks
  (setvar "CMDECHO" 1)
  (princ ent)
)

I use it to increase revision codes mostly (hence the name 'uprev')

 

Its not perfect and since I wrote it for me, not pretty. The last character will increase by 1, so 15.QF5 -> 15.QF6. It should work with blocks, mtext, text (just checked, dimensions too). Also it will increment letters to the next highest (A ->B or d ->e retaining the letters case (upper or lower)). There are a couple of lines to ignore I and O (we don't use them as revision codes generally), I've left that in there for your information. It it limited in that numbers will increase by 1 even to the next magnitude (so 7, 8, 9, then 10), letters won't - it won't go X, Y, Z, AA, AB mostly by design that with letters it isn't always that clear what the sequence should after Z

 

Feel free to use it, modify or whatever

 

 

 

Hope I am answering the right thing here!

Link to comment
Share on other sites

Another one for fun :)

(defun _foo (s n / a b)
  (cond	((/= s (setq a (vl-string-right-trim "1234567890." s)))
	 (setq b (vl-princ-to-string (+ n (read (substr s (1+ (strlen a)))))))
	 (strcat (substr s 1 (strlen a)) b)
	)
  )
)
(setq i "15.QF5")
(repeat 10 (print (setq i (_foo i 1))))

;;;"15.QF6" 
;;;"15.QF7" 
;;;"15.QF8" 
;;;"15.QF9" 
;;;"15.QF10" 
;;;"15.QF11" 
;;;"15.QF12" 
;;;"15.QF13" 
;;;"15.QF14" 
;;;"15.QF15" 

 

Link to comment
Share on other sites

Hello and thank you all for the answers!...The change I had indicated from 15.QF5 to 15.QF6 was an example, in fact I was looking for a universal solution to increase a figure quickly and...WOW! The UPREV lisp it's great! Thank you Steven P!

In the next days, after having tried it every day, I will write to you again, in the meantime, thank you again

 

Link to comment
Share on other sites

2 hours ago, Steven P said:

though if Lee Mac has a solution they are generally more elegant and bug free than anything I would write

 

Lee asked a question, an uncomplete-structured demand may lead to uncomplete or undesired solution. so...

Link to comment
Share on other sites

Quote
23 hours ago, Lee Mac said:

15.QF9 15.QF10

15.QF9 15.QG0

15.QF9 15.QG1

 

Which is correct?

 

 

All three are correct ... it depends on the need of everyone ... mine is a silly answer, but I'm preparing an example to represent some typical situations like ... once shown maybe I'll be more clearly!

Link to comment
Share on other sites

On 10/31/2018 at 10:21 PM, Lee Mac said:

15.QF9 15.QF10

15.QF9 15.QG0

15.QF9 15.QG1

 

Which is correct?

 

If the second example is desired, you could use my numinc:incrementalpha function which appears on lines 4,375-4,427 of my Incremental Numbering Suite application.

 

For example:

_$ (numinc:incrementalpha "15.QF9" 1)
"15.QG0"
_$ (numinc:incrementalpha "15.QF9" 10)
"15.QG9"
_$ (numinc:incrementalpha "15.QF9" 100)
"15.QP9"
_$ (numinc:incrementalpha "15.QF9" 1000)
"15.UB9"

 

6 hours ago, Steven P said:

Not a problem.. though if Lee Mac has a solution they are generally more elegant and bug free than anything I would write

 

That's very flattering of you to say Steven - though, we all stand on the shoulders of giants and were all a beginner once 😉

  • Like 1
Link to comment
Share on other sites

Hi, I allow myself to make two considerations on the very useful lisp, but as a non-programmer I hope not to say too risky things...

The correction of an alphanumeric code is unpredictable, I can not know which example to give what is happening in this discussion...but it is possible to read, at the time of the change, which digit should be corrected...

I think this lisp should have the choice of:

1) increase / decrease (by default increase)

2) the specification of the position of the digit to be changed (by default the last one)

 

Obviously I have no idea how difficult or feasible it can be to put the second point into practice.

For the first point instead would it be possible? choose whether to increase or decrease?

 

Many greetings

 

Link to comment
Share on other sites

To decrease the number / letter

 

Copy the whole lot and change this to create a new LISP, DownRev - simplest way to do that so there is no need to choose what to do in a single LISP.

defun c:uprev

to

defun c:downrev

 

(setq revnumber (itoa (+ 1 (read revnumber))))

to

(setq revnumber (itoa (- 1 (read revnumber)))))

 

(setq revcode (+ 1 (ascii ones)))

to

(setq revcode (- 1 (ascii ones)))

 

However it will probably not work right for number 1 or 0, and for letter A or a - but you can look at changing that about

try changing

(if (= 91 revcode)(setq revcode 65))

to

(if (= 65 revcode)(setq revcode 91))

for example.

 

For altering a character that isn't the last one, its possible but will need a bit more thinking about for me

 

Link to comment
Share on other sites

1 hour ago, Steven P said:

(setq revnumber (itoa (+ 1 (read revnumber))))

You might want to use the function 1-

(1- 6.)
;;5.0 
(- 1 6.)
;;-5.0

 

Link to comment
Share on other sites

3 minutes ago, Steven P said:

Thanks RonJon - like I said, mine isn't pretty and there are better ways, I'll change it round when I get a chance

No worries .. just an observation :)

Link to comment
Share on other sites

  • 3 weeks later...

Hello...I could not properly modify the lisp to perform the correct decrement of the digits, patience, I will try again...

A further change that I ask you to explain is the following: is it possible to make the command reload to be used longer until the command is closed?

I was watching this discussion:

the first lines of the lisp (which works as I would like) have the following instruction:

(while (or (setq nent (nentsel "\nPick attribute. Right click to exit"))

But i don't know how insert in the uprev lisp.

 

Regards

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...