Stack Overflow in simple routine

Hello.
I am migrating applications from Autocad to Bricscad. I have a simple routine that in Bricscad shows me the error "stack overflow" and in Autocad works without problems. I apply the function in a drawing with about 800 texts, but it fails.

Any suggestions? Thanks

Function:
(SETQ NS "%%c")
(SETQ OS "ø")
(setq p (ssget "X" (LIST (CONS 0 "TEXT"))))

(defun VCHGTEXT2 (ns os p / CONT ENT ENT-LST TXT0 TXT)
(SETQ CONT 0)
(REPEAT (sslength P)
(SETQ ENT (SSNAME P CONT))
(IF (= "TEXT" (CDR (ASSOC 0 (ENTGET ENT))))
(PROGN
(SETQ ENT-LST (ENTGET ENT))
(SETQ TXT0 (CDR (ASSOC 1 ENT-LST)))
(SETQ TXT TXT0)
(WHILE (vl-string-search OS TXT)
(SETQ TXT (vl-string-subst NS OS TXT))
)
(ENTMOD (SUBST (CONS 1 TXT) (CONS 1 TXT0) ENT-LST))
) ;PROGN
) ;IF
(SETQ CONT (+ 1 CONT))
) ;REPEAT
) ;DEFUN

Comments

  • pretty sure its due to the ssget being part of the initialization.

    Move the

    (setq p (ssget "X" (LIST (CONS 0 "TEXT"))))

    into the defun
  • Thanks, but in this example, the function "VCHGTEXT2" is called from another function and I need to pass it as an argument a selection set
  • Anthony Apostolaros
    edited January 2023
    Vilacad, I don't understand your reply to Derek, but I find that if I move the first three (SETQ) functions to the second line of the (DEFUN) function as he suggested, and remove those three symbols from the arguments list, I no longer get an error message. The thus-modified (VCHGTEXT2) function gives me an accurate count of the number of text entities in the selection set, without de-selecting the other entities. I can't tell from the code what else it's supposed to do.

    The error message I got when running your original version (at the command line) was not "stack overflow," but rather "too few / too many arguments." When you call your (VCHGTEXT2) function in another function, do you first include those three (SETQ) functions in that other function? That might be necessary, in order to make their returns available as arguments to be passed to the (VCHGTEXT2) function.
  • Are the NS and OS values mentioned in the OP the actual values when the overflow occurs? If NS (new string) contains OS (old string) the function will enter an endless while loop causing a stack overflow.
  • ALANH
    edited January 2023
    Just a maybe need to set (SETQ OS "ø") the dia use the alt+keycode the unicode to set the text. It varys depending on the font. Example (setq os "\\U+00D8") Ø.
  • Anthony Apostolaros
    edited January 2023

    .... If NS (new string) contains OS (old string) the function will enter an endless while loop causing a stack overflow.

    ... or if TXT contains OS but the substitution fails for some reason?

    For me, in v17, the modified function fails to change unicode phi to %%c phi, or to any other string. If I change OS to a string without unicode characters, it makes the substitution. In either case, it doesn't give me the stack overflow error.

    I don't understand why it fails. The Find command doesn't mind making that substitution.
    Entering (vl-string-subst "%%c" "Ø" "Ø") on the command line returns %%c.

    I also don't understand why it doesn't go into an endless while loop after failing to make the substitution.
  • I have already solved it. Thanks for the input.
    Regards