LISP: Increase consecutive number

LISP: Increase consecutive number

BriamR
Advocate Advocate
1,248 Views
14 Replies
Message 1 of 15

LISP: Increase consecutive number

BriamR
Advocate
Advocate

Hi! I have this lisp that increments consecutive numbers in blocks with OD in a specific field (INTERNO_SENAL), what I am looking for with your help is that the number with which it begins to enumerate can be entered and not start from 1, also if it is possible that in the OD in the TIPO_SENAL field enter the block name, I attach an example file,

 

*Note: the attached lisp only works if the data type of the INTERNAL_SENAL field of the OD is in character, if it could work with an integer data type it would be great.

 

Thanks community.

0 Likes
Accepted solutions (2)
1,249 Views
14 Replies
Replies (14)
Message 2 of 15

Kent1Cooper
Consultant
Consultant

@BriamR wrote:

... the number with which it begins to enumerate can be entered and not start from 1....


For that part, one way would be to replace this much:

....

(setq inc 0)
(repeat len
(setq enam (ssname ss inc))
(setq INTERNO_SENAL (strcat (itao (+ 1 inc))))

....

with this:

....

(setq inc 0 num (getint "\nStarting number: "))
(repeat len
(setq enam (ssname ss inc))
(setq INTERNO_SENAL (itao num)))

....

and replacing this line:

(setq inc (+ 1 inc))

with this:
(setq inc (+ 1 inc) num (1+ num))

Kent Cooper, AIA
Message 3 of 15

Sea-Haven
Mentor
Mentor

Kent has provided excellent advice, when I do say block increment I check the blocks 1st and get the highest number so a choice can be made, its hard to remember what was last number across multiple projects.

 

Starting number:  <12>

 

Is itao correct ? ITOA

Message 4 of 15

BriamR
Advocate
Advocate

Thanks for the help @Kent1Cooper , I followed your instructions but it gives me an error

 

 

 

Starting number: 12
; error: no function definition: ITAO

 

 

 

This line should be like this according to the suggestion of @Sea-Haven, thanks

...

(setq INTERNO_SENAL (itoa num)))

...

the drawback is that now numbers are not assigned consecutively to all the blocks, but to just one

 

0 Likes
Message 5 of 15

ВeekeeCZ
Consultant
Consultant
Accepted solution

As it seems, (itoa) is not even necessary, it will accept an integer.

Tip: When selection, you can type F for Fence selection that honers the order.

 

(defun c:addid ( / s n e)

  (or *ad-t* (setq *ad-t* ""))
  (or *ad-i* (setq *ad-i* 1))
  
  (if (and (not (initget 128))
	   (setq *ad-t* (cond ((getkword (strcat "\nIntroducir nombre de la tabla <" *ad-t* ">: ")))
			      (*ad-t*)))
	   (setq *ad-i* (1- (cond ((getint (strcat "\nIniciar Interno <" (itoa *ad-i*) ">: ")))
				  (*ad-i*))))
	   (princ "\nSeleccionar señales verticales, ")
	   (setq s (ssget))
	   (setq n -1)
	   )
    (repeat (sslength s)
      (setq e (ssname s (setq n (1+ n))))
      (ade_odaddrecord e *ad-t*)
      (ade_odsetfield e *ad-t* "INTERNO_SENAL" 0 (setq *ad-i* (1+ *ad-i*)))
      (command "_.chprop" e "" "_c" 1 "")))
  (setq *ad-i* (1+ *ad-i*))
  (princ)
  )

(alert "Se actualizaron los bloques seleccionados con INTERNO_SENAL")
(princ)

 

Message 6 of 15

calderg1000
Mentor
Mentor

Regards @BriamR 

I made some edits to the reached code and added the Start value request

(defun c:addid1 (/ n inc enam idd)
  (setq tn "SDM_SEN_SENALIZACION")                ;nombre de la tabla fijo.
  (prompt "Seleccionar señales")
  (princ)
  (setq n  (getint "\n Ingrese valor de Inicio: ")
        ss (ssget '((0 . "insert")))
  )
  (setq inc 0)
  (repeat (setq len (sslength ss))
    (setq enam (ssname ss inc))
    (setq idd (itoa n))
    (ade_odaddrecord enam tn)
    (ade_odsetfield enam tn "INTERNO_SENAL" 0 idd)
    (command "chprop" enam "" "c" "red" "")       ;.... talvez no sea necesario...???
    (setq inc (1+ inc)
          n   (1+ n)
    )
  )
  (alert "Se actualizaron las entidades seleccionadas con INTERNO_SENAL")
  (princ)
)

Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

Message 7 of 15

BriamR
Advocate
Advocate

@ВeekeeCZthanks for your solution is very successful to that part of the requirement.

---

@calderg1000 Thank you for your interest in the subject, your solution has been very useful to me.

---

 

I would only need to solve this part of the publication, thanks for the help provided
...

also if it is possible that in the OD in the TIPO_SENAL field enter the block name

...

0 Likes
Message 8 of 15

john.kaulB9QW2
Advocate
Advocate
Z9E3zK5E,
You have a very clean style to your coding. Very nice.

I am just curious.
To be brief, the global variables decl's stick out to me because of the different style. why did you choose this style?

(or *ad-i* (setq *ad-i* 1))
statement return: T

instead of?

(setq *ad-i* (cond (*ad-i*) (1)) )
statement return: 1
another swamper
Message 9 of 15

calderg1000
Mentor
Mentor
Accepted solution

Regards @BriamR 

Here I add the type of signal

 

 

(defun c:addid1 (/ n inc enam idd)
  (setq tn "SDM_SEN_SENALIZACION")                ;nombre de la tabla fijo.
  (prompt "Seleccionar señales")
  (princ)
  (setq n  (getint "\n Ingrese valor de Inicio: ")
        ss (ssget '((0 . "insert")))
  )
  (setq inc 0)
  (repeat (setq len (sslength ss))
    (setq enam (ssname ss inc))
    (setq idd (itoa n))
    (setq efn(vla-get-effectivename(vlax-ename->vla-object enam)))
    (ade_odaddrecord enam tn)
    (ade_odsetfield enam tn "INTERNO_SENAL" 0 idd)
    (ade_odsetfield enam tn "TIPO_SENAL" 0 efn)
    (command "chprop" enam "" "c" "red" "")       ;.... talvez no sea necesario...???
    (setq inc (1+ inc)
          n   (1+ n)
    )
  )
  (alert "Se actualizaron las entidades seleccionadas con INTERNO_SENAL")
  (princ)
)

 

 

 

 

 


Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

Message 10 of 15

BriamR
Advocate
Advocate
Apparently the lines of the code are in Spanish and that's why it gave me an error, I also told you that when the INTERNO_SENAL field is in data type as integer, the consecutive assignment of internals does not work. Thank you!
0 Likes
Message 11 of 15

calderg1000
Mentor
Mentor
Hello, I already corrected it; sorry the translator sometimes plays us wrong

Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

Message 12 of 15

ВeekeeCZ
Consultant
Consultant

@john.kaulB9QW2 wrote:
Z9E3zK5E,
You have a very clean style to your coding. Very nice.

I am just curious.
To be brief, the global variables decl's stick out to me because of the different style. why did you choose this style?

(or *ad-i* (setq *ad-i* 1))
statement return: T

instead of?

(setq *ad-i* (cond (*ad-i*) (1)) )
statement return: 1

 

@john.kaulB9QW2,

thanks. 

 

You made me think about it a little. Not sure whether it is a different style or not, I just consider that as the right tool for the job.

It does what's needed, it's simple, it's clear - it still says (set something default) next to each other, it's clear even for the no-experienced user that he should try this value change to his preference. Also it's not that crowded with parentheses as your cond.

And what's more, it's smart. Someone less experienced might start to wonder... that's weird OR usage...hmm. That happened before.

I simply like it, it's convenient. Good for defaults.

 

BTW Welcome here to forums! Let's see if you stick for a while with us... to share some clever thoughts 🙂

Message 13 of 15

john.uhden
Mentor
Mentor

@ВeekeeCZ 

I shouldn't need to remind you of the Kosterian approach.

No need for an (if (and...

A simple (and will do.

For the lurkers, operations within an (and ...) will continue until an operation/evaluation within it returns nil.

John F. Uhden

0 Likes
Message 14 of 15

john.uhden
Mentor
Mentor

Actually, one should be more cautious than that.

If N is supposed to be an integer, then..

(or (= (type N) 'INT)(setq N 1))

John F. Uhden

Message 15 of 15

john.kaulB9QW2
Advocate
Advocate

I'm glad I made you think about it at least. I consider (or *ad-i* (setq ... different then (setq *ad-i* (cond ... because of what the two statements return. Yes the boole style is less parentheses but I still preferred the COND style because I spent too much time debugging programs where the return types were not carefully accounted for. I'm not saying one is better then the other in every case, just better to acknowledge one style vs the other and their proper uses.

 

I've seen problem this too much (simplified).

 

(if (or *ad-i* (setq...

(if (setq *ad-i* (cond...

 

Thank you for the welcome. Ah, you guys don't need me and I don't have clever thoughts; when I first started in my career I took the SICP course and enjoyed lisp for a few years but after a while I got tired of programming only AutoCAD so I switched to C/C++. I'm just getting back into Lisp again. So I'm very, very rusty.

***
I've not seen the Kosterian approach (link/writeup?)

 

> (or (= (type N) 'INT)(setq N 1))

 

This is the type safety side of the coin I'm referring to with the boole type statements (and why I tend to avoid/limit their use). I find the (setq N (cond ... style more readable and dependable and far easier to maintain in the long run.

 

The above becomes

(setq N (cond
	  (N (eq (type N) 'INT) N) ; -If the value of `N` is set and is 
                                   ;  type INT, use it.
	  (2))                     ; -Otherwise set the default value of 2.
 )

which is easier to maintain and easier to read. Again, this type of type safety is a bit overkill but this demonstrates the point I was trying to make about not being diligent with return types nicely.

another swamper